View Javadoc

1   /* 
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8   *
9   *     http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17  package org.apache.jetspeed.om.page;
18  
19  import java.io.Serializable;
20  import java.util.Collection;
21  import java.util.Iterator;
22  import java.util.List;
23  import java.util.ListIterator;
24  import java.util.Map;
25  
26  import org.apache.commons.logging.Log;
27  import org.apache.commons.logging.LogFactory;
28  import org.apache.jetspeed.aggregator.PortletContent;
29  import org.apache.jetspeed.decoration.Decoration;
30  import org.apache.jetspeed.om.common.SecurityConstraint;
31  import org.apache.jetspeed.om.common.SecurityConstraints;
32  import org.apache.jetspeed.page.impl.DatabasePageManagerUtils;
33  
34  public class ContentFragmentImpl implements ContentFragment
35  {
36      
37  
38      private final Fragment fragment;
39      private StringBuffer overridenContent;
40      private PortletContent portletContent;
41      private List contentFragments;
42      private static final Log log = LogFactory.getLog(ContentFragmentImpl.class);
43      private final Map cachedFragments;
44      private Decoration decoration;
45      private boolean instantlyRendered;
46      
47  
48      public ContentFragmentImpl(Fragment fragment, Map cachedFagments)
49      {
50          this(fragment, cachedFagments, false);
51      }
52  
53      public ContentFragmentImpl(Fragment fragment, Map cachedFagments, boolean instantlyRendered)
54      {
55          this.fragment = fragment;
56          this.cachedFragments = cachedFagments;
57          this.instantlyRendered = instantlyRendered;
58      }
59  
60      /* (non-Javadoc)
61       * @see org.apache.jetspeed.om.page.ContentFragment#getContentFragments()
62       */
63      public List getContentFragments()
64      {   
65          if(contentFragments == null)
66          {
67             contentFragments = new ContentFragmentList();
68          }
69          return contentFragments;
70      }
71  
72      /* (non-Javadoc)
73       * @see org.apache.jetspeed.om.page.ContentFragment#getFragments()
74       */
75      public List getFragments()
76      {
77          return getContentFragments();
78      }
79      
80      /* (non-Javadoc)
81       * @see org.apache.jetspeed.om.page.ContentFragment#getOverriddenContent()
82       */
83      public String getOverriddenContent()
84      {
85          return overridenContent != null ? overridenContent.toString() : null;
86      }
87  
88      /* (non-Javadoc)
89       * @see org.apache.jetspeed.om.page.ContentFragment#getRenderedContent()
90       */
91      public String getRenderedContent() throws IllegalStateException
92      {       
93          if(overridenContent != null)
94          {
95              return overridenContent.toString();
96          }
97          
98          
99          if (portletContent != null)
100         {
101             //TODO are you sure? Intellij warns, synchronization on a non-final field is
102             //unlikely to have useful semantics.
103             synchronized (portletContent)
104             {
105                 if (portletContent.isComplete())
106                 {
107                     return portletContent.getContent();
108                 }
109                 else
110                 {
111                     try
112                     {
113                         log.debug("Waiting on content for Fragment " + getId());
114                         portletContent.wait();
115                         return portletContent.getContent();
116                     }
117                     catch (InterruptedException e)
118                     {
119                         return e.getMessage();
120                     }
121                     finally
122                     {
123                         log.debug("Been notified that Faragment " + getId() + " is complete");
124                     }
125                 }
126             }
127         }
128         else
129         {
130             throw new IllegalStateException("You cannot invoke getRenderedContent() until the content has been set.");
131         }
132     }
133 
134     /* (non-Javadoc)
135      * @see org.apache.jetspeed.om.page.ContentFragment#overrideRenderedContent(java.lang.String)
136      */
137     public void overrideRenderedContent(String contnent)
138     {
139         if ( contnent != null )
140         {
141             if(overridenContent == null)
142             {
143                 overridenContent = new StringBuffer();
144             }
145             // prevent repeated storing of the same error message
146             if (!contnent.equals(overridenContent.toString()))
147             {
148                 overridenContent.append(contnent);
149             }
150         }
151         
152     }
153 
154     /* (non-Javadoc)
155      * @see org.apache.jetspeed.om.page.ContentFragment#setPortletContent(org.apache.jetspeed.aggregator.PortletContent)
156      */
157     public void setPortletContent(PortletContent portletContent)
158     {
159         this.portletContent = portletContent;        
160     }
161 
162     /* (non-Javadoc)
163      * @see org.apache.jetspeed.om.page.Fragment#getDecorator()
164      */
165     public String getDecorator()
166     {
167         
168         return fragment.getDecorator();
169     }
170 
171     /* (non-Javadoc)
172      * @see org.apache.jetspeed.om.page.Fragment#getName()
173      */
174     public String getName()
175     {
176         
177         return fragment.getName();
178     }
179 
180     /* (non-Javadoc)
181      * @see org.apache.jetspeed.om.page.Fragment#getProperties()
182      */
183     public Map getProperties()
184     {
185         
186         return fragment.getProperties();
187     }
188 
189     /* (non-Javadoc)
190      * @see org.apache.jetspeed.om.page.Fragment#getProperty(java.lang.String)
191      */
192     public String getProperty(String propName)
193     {
194         
195         return fragment.getProperty(propName);
196     }
197 
198     /* (non-Javadoc)
199      * @see org.apache.jetspeed.om.page.Fragment#getIntProperty(java.lang.String)
200      */
201     public int getIntProperty(String propName)
202     {
203         
204         return fragment.getIntProperty(propName);
205     }
206 
207     /* (non-Javadoc)
208      * @see org.apache.jetspeed.om.page.Fragment#getFloatProperty(java.lang.String)
209      */
210     public float getFloatProperty(String propName)
211     {
212         
213         return fragment.getFloatProperty(propName);
214     }
215 
216     /* (non-Javadoc)
217      * @see org.apache.jetspeed.om.page.Fragment#getSkin()
218      */
219     public String getSkin()
220     {
221         
222         return fragment.getSkin();
223     }
224 
225     /* (non-Javadoc)
226      * @see org.apache.jetspeed.om.page.Fragment#getState()
227      */
228     public String getState()
229     {
230         
231         return fragment.getState();
232     }
233 
234     /* (non-Javadoc)
235      * @see org.apache.jetspeed.om.page.Fragment#getMode()
236      */
237     public String getMode()
238     {
239         
240         return fragment.getMode();
241     }
242 
243     /* (non-Javadoc)
244      * @see org.apache.jetspeed.om.page.Fragment#getType()
245      */
246     public String getType()
247     {
248         
249         return fragment.getType();
250     }
251 
252     /* (non-Javadoc)
253      * @see org.apache.jetspeed.om.page.Fragment#isReference()
254      */
255     public boolean isReference()
256     {
257         
258         return fragment.isReference();
259     }
260 
261     /* (non-Javadoc)
262      * @see org.apache.jetspeed.om.page.Fragment#setDecorator(java.lang.String)
263      */
264     public void setDecorator(String decoratorName)
265     {
266         
267         fragment.setDecorator(decoratorName);
268     }
269 
270     /* (non-Javadoc)
271      * @see org.apache.jetspeed.om.page.Fragment#setName(java.lang.String)
272      */
273     public void setName(String name)
274     {
275         
276         fragment.setName(name);
277     }
278 
279     /* (non-Javadoc)
280      * @see org.apache.jetspeed.om.page.Fragment#getLayoutRow()
281      */
282     public int getLayoutRow()
283     {
284         return fragment.getLayoutRow();
285     }
286     
287     /* (non-Javadoc)
288      * @see org.apache.jetspeed.om.page.Fragment#getLayoutColumn()
289      */
290     public int getLayoutColumn()
291     {
292         return fragment.getLayoutColumn();
293     }
294 
295     /* (non-Javadoc)
296      * @see org.apache.jetspeed.om.page.Fragment#getLayoutSizes()
297      */
298     public String getLayoutSizes()
299     {
300         return fragment.getLayoutSizes();
301     }
302 
303     /* (non-Javadoc)
304      * @see org.apache.jetspeed.om.page.Fragment#getLayoutX()
305      */
306     public float getLayoutX()
307     {
308         return fragment.getLayoutX();
309     }
310 
311     /* (non-Javadoc)
312      * @see org.apache.jetspeed.om.page.Fragment#getLayoutY()
313      */
314     public float getLayoutY()
315     {
316         return fragment.getLayoutY();
317     }
318 
319     /* (non-Javadoc)
320      * @see org.apache.jetspeed.om.page.Fragment#getLayoutZ()
321      */
322     public float getLayoutZ()
323     {
324         return fragment.getLayoutZ();
325     }
326 
327     /* (non-Javadoc)
328      * @see org.apache.jetspeed.om.page.Fragment#getLayoutWidth()
329      */
330     public float getLayoutWidth()
331     {
332         return fragment.getLayoutWidth();
333     }
334 
335     /* (non-Javadoc)
336      * @see org.apache.jetspeed.om.page.Fragment#getLayoutHeight()
337      */
338     public float getLayoutHeight()
339     {
340         return fragment.getLayoutHeight();
341     }
342 
343     /* (non-Javadoc)
344      * @see org.apache.jetspeed.om.page.Fragment#setLayoutRow(int)
345      */
346     public void setLayoutRow(int row)
347     {
348         fragment.setLayoutRow(row);
349     }
350     
351     /* (non-Javadoc)
352      * @see org.apache.jetspeed.om.page.Fragment#setLayoutColumn(int)
353      */
354     public void setLayoutColumn(int column)
355     {
356         fragment.setLayoutColumn(column);
357     }
358     
359     /* (non-Javadoc)
360      * @see org.apache.jetspeed.om.page.Fragment#setLayoutSizes(java.lang.String)
361      */
362     public void setLayoutSizes(String sizes)
363     {
364         fragment.setLayoutSizes(sizes);
365     }
366     
367     /* (non-Javadoc)
368      * @see org.apache.jetspeed.om.page.Fragment#setLayoutX(float)
369      */
370     public void setLayoutX(float x)
371     {
372         fragment.setLayoutX(x);
373     }
374     
375     /* (non-Javadoc)
376      * @see org.apache.jetspeed.om.page.Fragment#setLayoutY(float)
377      */
378     public void setLayoutY(float y)
379     {
380         fragment.setLayoutY(y);
381     }
382 
383     /* (non-Javadoc)
384      * @see org.apache.jetspeed.om.page.Fragment#setLayoutZ(float)
385      */
386     public void setLayoutZ(float z)
387     {
388         fragment.setLayoutZ(z);
389     }
390 
391     /* (non-Javadoc)
392      * @see org.apache.jetspeed.om.page.Fragment#setLayoutWidth(float)
393      */
394     public void setLayoutWidth(float width)
395     {
396         fragment.setLayoutWidth(width);
397     }
398 
399     /* (non-Javadoc)
400      * @see org.apache.jetspeed.om.page.Fragment#setLayoutHeight(float)
401      */
402     public void setLayoutHeight(float height)
403     {
404         fragment.setLayoutHeight(height);
405     }
406 
407     /* (non-Javadoc)
408      * @see org.apache.jetspeed.om.page.Fragment#setSkin(java.lang.String)
409      */
410     public void setSkin(String skinName)
411     {
412         
413         fragment.setSkin(skinName);
414     }
415 
416     /* (non-Javadoc)
417      * @see org.apache.jetspeed.om.page.Fragment#setState(java.lang.String)
418      */
419     public void setState(String state)
420     {
421         
422         fragment.setState(state);
423     }
424 
425     /* (non-Javadoc)
426      * @see org.apache.jetspeed.om.page.Fragment#setMode(java.lang.String)
427      */
428     public void setMode(String mode)
429     {
430         
431         fragment.setMode(mode);
432     }
433 
434     /* (non-Javadoc)
435      * @see org.apache.jetspeed.om.page.Fragment#setType(java.lang.String)
436      */
437     public void setType(String type)
438     {
439         
440         fragment.setType(type);
441     }
442 
443     /* (non-Javadoc)
444      * @see org.apache.jetspeed.om.page.BaseElement#getId()
445      */
446     public String getId()
447     {
448         
449         return fragment.getId();
450     }
451 
452     /* (non-Javadoc)
453      * @see org.apache.jetspeed.om.page.BaseElement#getShortTitle()
454      */
455     public String getShortTitle()
456     {
457         
458         return fragment.getShortTitle();
459     }
460 
461     /* (non-Javadoc)
462      * @see org.apache.jetspeed.om.page.BaseElement#getTitle()
463      */
464     public String getTitle()
465     {
466         
467         return fragment.getTitle();
468     }
469 
470     /* (non-Javadoc)
471      * @see org.apache.jetspeed.om.page.BaseElement#setShortTitle(java.lang.String)
472      */
473     public void setShortTitle(String title)
474     {
475         
476         fragment.setShortTitle(title);
477     }
478 
479     /* (non-Javadoc)
480      * @see org.apache.jetspeed.om.page.BaseElement#setTitle(java.lang.String)
481      */
482     public void setTitle(String title)
483     {
484         
485         fragment.setTitle(title);
486     }
487 
488     /* (non-Javadoc)
489      * @see org.apache.jetspeed.om.common.SecuredResource#checkAccess(java.lang.String)
490      */
491     public void checkAccess(String actions) throws SecurityException
492     {
493         
494         fragment.checkAccess(actions);
495     }
496 
497     /* (non-Javadoc)
498      * @see org.apache.jetspeed.om.common.SecuredResource#checkConstraints(java.lang.String)
499      */
500     public void checkConstraints(String actions) throws SecurityException
501     {
502         
503         fragment.checkConstraints(actions);
504     }
505 
506     /* (non-Javadoc)
507      * @see org.apache.jetspeed.om.common.SecuredResource#checkPermissions(int)
508      */
509     public void checkPermissions(int mask) throws SecurityException
510     {
511         
512         fragment.checkPermissions(mask);
513     }
514 
515     /* (non-Javadoc)
516      * @see org.apache.jetspeed.om.common.SecuredResource#getConstraintsEnabled()
517      */
518     public boolean getConstraintsEnabled()
519     {
520         
521         return fragment.getConstraintsEnabled();
522     }
523 
524     /* (non-Javadoc)
525      * @see org.apache.jetspeed.om.common.SecuredResource#getPermissionsEnabled()
526      */
527     public boolean getPermissionsEnabled()
528     {
529         
530         return fragment.getPermissionsEnabled();
531     }
532 
533     /* (non-Javadoc)
534      * @see org.apache.jetspeed.om.common.SecuredResource#getSecurityConstraints()
535      */
536     public SecurityConstraints getSecurityConstraints()
537     {
538         
539         return fragment.getSecurityConstraints();
540     }
541 
542     /* (non-Javadoc)
543      * @see org.apache.jetspeed.om.common.SecuredResource#newSecurityConstraints()
544      */
545     public SecurityConstraints newSecurityConstraints()
546     {
547         
548         return fragment.newSecurityConstraints();
549     }
550 
551     /* (non-Javadoc)
552      * @see org.apache.jetspeed.om.common.SecuredResource#newSecurityConstraint()
553      */
554     public SecurityConstraint newSecurityConstraint()
555     {
556         
557         return fragment.newSecurityConstraint();
558     }
559 
560     /* (non-Javadoc)
561      * @see org.apache.jetspeed.om.common.SecuredResource#setSecurityConstraints(org.apache.jetspeed.om.common.SecurityConstraints)
562      */
563     public void setSecurityConstraints(SecurityConstraints constraints)
564     {
565         fragment.setSecurityConstraints(constraints);
566     }
567     
568     
569     /***
570      * Checks the ContentFragment cache for a ContentFragment
571      * that matches the <code>Id</code> of this fragment.  If
572      * one is found, it returned.  If no matches are found, a new
573      * <code>ContentFragment</code> represnentive of the {@link Fragment}
574      * argument is subsequently created, stored into the cahce and returned. 
575      * 
576      * @param f
577      * @return ContentFrament
578      */
579     protected ContentFragment getContentFragment(Fragment f)
580     {
581         ContentFragment cf;
582         if(cachedFragments.containsKey(f.getId()))
583         {
584             cf = (ContentFragment) cachedFragments.get(f.getId());
585         }
586         else
587         {
588             cf = new ContentFragmentImpl(f, cachedFragments);
589             cachedFragments.put(f.getId(), cf);
590         }
591         return cf;
592     }
593     
594     
595     protected final class ContentFragmentList implements List, Serializable
596     {
597         private List baseList = fragment.getFragments();
598 
599         /* (non-Javadoc)
600          * @see java.util.List#add(int, java.lang.Object)
601          */
602         public void add(int index, Object element)
603         {
604             if (element instanceof ContentFragmentImpl)
605                 element = ((ContentFragmentImpl)element).fragment;
606             baseList.add(index, element);
607         }
608 
609         /* (non-Javadoc)
610          * @see java.util.List#add(java.lang.Object)
611          */
612         public boolean add(Object o)
613         {
614             if (o instanceof ContentFragmentImpl)
615                 o = ((ContentFragmentImpl)o).fragment;            
616             return baseList.add(o);
617         }
618 
619         /* (non-Javadoc)
620          * @see java.util.List#addAll(int, java.util.Collection)
621          */
622         public boolean addAll(int index, Collection c)
623         {
624             
625             return baseList.addAll(index, c);
626         }
627 
628         /* (non-Javadoc)
629          * @see java.util.List#addAll(java.util.Collection)
630          */
631         public boolean addAll(Collection c)
632         {
633             
634             return baseList.addAll(c);
635         }
636 
637         /* (non-Javadoc)
638          * @see java.util.List#clear()
639          */
640         public void clear()
641         {
642             
643             baseList.clear();
644         }
645 
646         /* (non-Javadoc)
647          * @see java.util.List#contains(java.lang.Object)
648          */
649         public boolean contains(Object o)
650         {
651             
652             return baseList.contains(o);
653         }
654 
655         /* (non-Javadoc)
656          * @see java.util.List#containsAll(java.util.Collection)
657          */
658         public boolean containsAll(Collection c)
659         {
660             
661             return baseList.containsAll(c);
662         }
663 
664         /* (non-Javadoc)
665          * @see java.util.List#equals(java.lang.Object)
666          */
667         public boolean equals(Object o)
668         {
669             
670             return baseList.equals(o);
671         }
672 
673         /* (non-Javadoc)
674          * @see java.util.List#get(int)
675          */
676         public Object get(int index)
677         {
678             Fragment f= (Fragment) baseList.get(index);
679             return getContentFragment(f);            
680         }
681 
682         /* (non-Javadoc)
683          * @see java.util.List#hashCode()
684          */
685         public int hashCode()
686         {
687             
688             return baseList.hashCode();
689         }
690 
691         /* (non-Javadoc)
692          * @see java.util.List#indexOf(java.lang.Object)
693          */
694         public int indexOf(Object o)
695         {
696             
697             return baseList.indexOf(o);
698         }
699 
700         /* (non-Javadoc)
701          * @see java.util.List#isEmpty()
702          */
703         public boolean isEmpty()
704         {
705             
706             return baseList.isEmpty();
707         }
708 
709         /* (non-Javadoc)
710          * @see java.util.List#iterator()
711          */
712         public Iterator iterator()
713         {
714             return duplicateList().iterator();
715         }
716 
717         /* (non-Javadoc)
718          * @see java.util.List#lastIndexOf(java.lang.Object)
719          */
720         public int lastIndexOf(Object o)
721         {
722             
723             return baseList.lastIndexOf(o);
724         }
725 
726         /* (non-Javadoc)
727          * @see java.util.List#listIterator()
728          */
729         public ListIterator listIterator()
730         {
731             return duplicateList().listIterator();
732         }
733 
734         /* (non-Javadoc)
735          * @see java.util.List#listIterator(int)
736          */
737         public ListIterator listIterator(int index)
738         {
739             return duplicateList().listIterator(index);
740         }
741 
742         /* (non-Javadoc)
743          * @see java.util.List#remove(int)
744          */
745         public Object remove(int index)
746         {
747             
748             return baseList.remove(index);
749         }
750 
751         /* (non-Javadoc)
752          * @see java.util.List#remove(java.lang.Object)
753          */
754         public boolean remove(Object o)
755         {
756             
757             return baseList.remove(o);
758         }
759 
760         /* (non-Javadoc)
761          * @see java.util.List#removeAll(java.util.Collection)
762          */
763         public boolean removeAll(Collection c)
764         {
765             
766             return baseList.removeAll(c);
767         }
768 
769         /* (non-Javadoc)
770          * @see java.util.List#retainAll(java.util.Collection)
771          */
772         public boolean retainAll(Collection c)
773         {
774             
775             return baseList.retainAll(c);
776         }
777 
778         /* (non-Javadoc)
779          * @see java.util.List#set(int, java.lang.Object)
780          */
781         public Object set(int index, Object element)
782         {
783             
784             return baseList.set(index, element);
785         }
786 
787         /* (non-Javadoc)
788          * @see java.util.List#size()
789          */
790         public int size()
791         {
792             
793             return baseList.size();
794         }
795 
796         /* (non-Javadoc)
797          * @see java.util.List#subList(int, int)
798          */
799         public List subList(int fromIndex, int toIndex)
800         {
801             return duplicateList().subList(fromIndex, toIndex);
802         }
803 
804 
805 
806         /* (non-Javadoc)
807          * @see java.util.List#toArray()
808          */
809         public Object[] toArray()
810         {
811             return duplicateList().toArray();
812         }
813 
814         /* (non-Javadoc)
815          * @see java.util.List#toArray(java.lang.Object[])
816          */
817         public Object[] toArray(Object[] a)
818         {
819               return duplicateList().toArray(a);
820         }
821         
822         private List duplicateList()
823         {            
824             List rFragList = DatabasePageManagerUtils.createList();
825             for(int i=0; i < baseList.size(); i++)
826             {                
827                 Fragment f = (Fragment)baseList.get(i);
828                 ContentFragment cf = getContentFragment(f);
829                 rFragList.add(cf);
830             }
831             return rFragList;
832         }
833         
834         
835 
836     }
837 
838     /* (non-Javadoc)
839      * @see org.apache.jetspeed.om.page.Fragment#getPreferences()
840      */
841     public List getPreferences()
842     {
843         return fragment.getPreferences();
844     }
845 
846     public Decoration getDecoration()
847     {
848         return decoration;
849     }
850     
851     /* (non-Javadoc)
852      * @see org.apache.jetspeed.om.page.Fragment#setPreferences(java.util.List)
853      */
854     public void setPreferences(List preferences)
855     {
856         fragment.setPreferences(preferences);
857     }
858 
859 
860     public void setDecoration(Decoration decoration)
861     {
862         this.decoration = decoration;
863         
864     }
865     
866     /* (non-Javadoc)
867      * @see org.apache.jetspeed.om.page.ContentFragment#isInstantlyRendered()
868      */
869     public boolean isInstantlyRendered()
870     {
871         return this.instantlyRendered;
872     }
873     
874 }