001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.jetspeed.om.page;
018:
019: import java.io.Serializable;
020: import java.util.Collection;
021: import java.util.Iterator;
022: import java.util.List;
023: import java.util.ListIterator;
024: import java.util.Map;
025:
026: import org.apache.commons.logging.Log;
027: import org.apache.commons.logging.LogFactory;
028: import org.apache.jetspeed.aggregator.PortletContent;
029: import org.apache.jetspeed.decoration.Decoration;
030: import org.apache.jetspeed.om.common.SecurityConstraint;
031: import org.apache.jetspeed.om.common.SecurityConstraints;
032: import org.apache.jetspeed.page.impl.DatabasePageManagerUtils;
033:
034: public class ContentFragmentImpl implements ContentFragment {
035:
036: private final Fragment fragment;
037: private StringBuffer overridenContent;
038: private PortletContent portletContent;
039: private List contentFragments;
040: private static final Log log = LogFactory
041: .getLog(ContentFragmentImpl.class);
042: private final Map cachedFragments;
043: private Decoration decoration;
044: private boolean instantlyRendered;
045:
046: public ContentFragmentImpl(Fragment fragment, Map cachedFagments) {
047: this (fragment, cachedFagments, false);
048: }
049:
050: public ContentFragmentImpl(Fragment fragment, Map cachedFagments,
051: boolean instantlyRendered) {
052: this .fragment = fragment;
053: this .cachedFragments = cachedFagments;
054: this .instantlyRendered = instantlyRendered;
055: }
056:
057: /* (non-Javadoc)
058: * @see org.apache.jetspeed.om.page.ContentFragment#getContentFragments()
059: */
060: public List getContentFragments() {
061: if (contentFragments == null) {
062: contentFragments = new ContentFragmentList();
063: }
064: return contentFragments;
065: }
066:
067: /* (non-Javadoc)
068: * @see org.apache.jetspeed.om.page.ContentFragment#getFragments()
069: */
070: public List getFragments() {
071: return getContentFragments();
072: }
073:
074: /* (non-Javadoc)
075: * @see org.apache.jetspeed.om.page.ContentFragment#getOverriddenContent()
076: */
077: public String getOverriddenContent() {
078: return overridenContent != null ? overridenContent.toString()
079: : null;
080: }
081:
082: /* (non-Javadoc)
083: * @see org.apache.jetspeed.om.page.ContentFragment#getRenderedContent()
084: */
085: public String getRenderedContent() throws IllegalStateException {
086: if (overridenContent != null) {
087: return overridenContent.toString();
088: }
089:
090: if (portletContent != null) {
091: //TODO are you sure? Intellij warns, synchronization on a non-final field is
092: //unlikely to have useful semantics.
093: synchronized (portletContent) {
094: if (portletContent.isComplete()) {
095: return portletContent.getContent();
096: } else {
097: try {
098: log.debug("Waiting on content for Fragment "
099: + getId());
100: portletContent.wait();
101: return portletContent.getContent();
102: } catch (InterruptedException e) {
103: return e.getMessage();
104: } finally {
105: log.debug("Been notified that Faragment "
106: + getId() + " is complete");
107: }
108: }
109: }
110: } else {
111: throw new IllegalStateException(
112: "You cannot invoke getRenderedContent() until the content has been set.");
113: }
114: }
115:
116: /* (non-Javadoc)
117: * @see org.apache.jetspeed.om.page.ContentFragment#overrideRenderedContent(java.lang.String)
118: */
119: public void overrideRenderedContent(String contnent) {
120: if (contnent != null) {
121: if (overridenContent == null) {
122: overridenContent = new StringBuffer();
123: }
124: // prevent repeated storing of the same error message
125: if (!contnent.equals(overridenContent.toString())) {
126: overridenContent.append(contnent);
127: }
128: }
129:
130: }
131:
132: /* (non-Javadoc)
133: * @see org.apache.jetspeed.om.page.ContentFragment#setPortletContent(org.apache.jetspeed.aggregator.PortletContent)
134: */
135: public void setPortletContent(PortletContent portletContent) {
136: this .portletContent = portletContent;
137: }
138:
139: /* (non-Javadoc)
140: * @see org.apache.jetspeed.om.page.Fragment#getDecorator()
141: */
142: public String getDecorator() {
143:
144: return fragment.getDecorator();
145: }
146:
147: /* (non-Javadoc)
148: * @see org.apache.jetspeed.om.page.Fragment#getName()
149: */
150: public String getName() {
151:
152: return fragment.getName();
153: }
154:
155: /* (non-Javadoc)
156: * @see org.apache.jetspeed.om.page.Fragment#getProperties()
157: */
158: public Map getProperties() {
159:
160: return fragment.getProperties();
161: }
162:
163: /* (non-Javadoc)
164: * @see org.apache.jetspeed.om.page.Fragment#getProperty(java.lang.String)
165: */
166: public String getProperty(String propName) {
167:
168: return fragment.getProperty(propName);
169: }
170:
171: /* (non-Javadoc)
172: * @see org.apache.jetspeed.om.page.Fragment#getIntProperty(java.lang.String)
173: */
174: public int getIntProperty(String propName) {
175:
176: return fragment.getIntProperty(propName);
177: }
178:
179: /* (non-Javadoc)
180: * @see org.apache.jetspeed.om.page.Fragment#getFloatProperty(java.lang.String)
181: */
182: public float getFloatProperty(String propName) {
183:
184: return fragment.getFloatProperty(propName);
185: }
186:
187: /* (non-Javadoc)
188: * @see org.apache.jetspeed.om.page.Fragment#getSkin()
189: */
190: public String getSkin() {
191:
192: return fragment.getSkin();
193: }
194:
195: /* (non-Javadoc)
196: * @see org.apache.jetspeed.om.page.Fragment#getState()
197: */
198: public String getState() {
199:
200: return fragment.getState();
201: }
202:
203: /* (non-Javadoc)
204: * @see org.apache.jetspeed.om.page.Fragment#getMode()
205: */
206: public String getMode() {
207:
208: return fragment.getMode();
209: }
210:
211: /* (non-Javadoc)
212: * @see org.apache.jetspeed.om.page.Fragment#getType()
213: */
214: public String getType() {
215:
216: return fragment.getType();
217: }
218:
219: /* (non-Javadoc)
220: * @see org.apache.jetspeed.om.page.Fragment#isReference()
221: */
222: public boolean isReference() {
223:
224: return fragment.isReference();
225: }
226:
227: /* (non-Javadoc)
228: * @see org.apache.jetspeed.om.page.Fragment#setDecorator(java.lang.String)
229: */
230: public void setDecorator(String decoratorName) {
231:
232: fragment.setDecorator(decoratorName);
233: }
234:
235: /* (non-Javadoc)
236: * @see org.apache.jetspeed.om.page.Fragment#setName(java.lang.String)
237: */
238: public void setName(String name) {
239:
240: fragment.setName(name);
241: }
242:
243: /* (non-Javadoc)
244: * @see org.apache.jetspeed.om.page.Fragment#getLayoutRow()
245: */
246: public int getLayoutRow() {
247: return fragment.getLayoutRow();
248: }
249:
250: /* (non-Javadoc)
251: * @see org.apache.jetspeed.om.page.Fragment#getLayoutColumn()
252: */
253: public int getLayoutColumn() {
254: return fragment.getLayoutColumn();
255: }
256:
257: /* (non-Javadoc)
258: * @see org.apache.jetspeed.om.page.Fragment#getLayoutSizes()
259: */
260: public String getLayoutSizes() {
261: return fragment.getLayoutSizes();
262: }
263:
264: /* (non-Javadoc)
265: * @see org.apache.jetspeed.om.page.Fragment#getLayoutX()
266: */
267: public float getLayoutX() {
268: return fragment.getLayoutX();
269: }
270:
271: /* (non-Javadoc)
272: * @see org.apache.jetspeed.om.page.Fragment#getLayoutY()
273: */
274: public float getLayoutY() {
275: return fragment.getLayoutY();
276: }
277:
278: /* (non-Javadoc)
279: * @see org.apache.jetspeed.om.page.Fragment#getLayoutZ()
280: */
281: public float getLayoutZ() {
282: return fragment.getLayoutZ();
283: }
284:
285: /* (non-Javadoc)
286: * @see org.apache.jetspeed.om.page.Fragment#getLayoutWidth()
287: */
288: public float getLayoutWidth() {
289: return fragment.getLayoutWidth();
290: }
291:
292: /* (non-Javadoc)
293: * @see org.apache.jetspeed.om.page.Fragment#getLayoutHeight()
294: */
295: public float getLayoutHeight() {
296: return fragment.getLayoutHeight();
297: }
298:
299: /* (non-Javadoc)
300: * @see org.apache.jetspeed.om.page.Fragment#setLayoutRow(int)
301: */
302: public void setLayoutRow(int row) {
303: fragment.setLayoutRow(row);
304: }
305:
306: /* (non-Javadoc)
307: * @see org.apache.jetspeed.om.page.Fragment#setLayoutColumn(int)
308: */
309: public void setLayoutColumn(int column) {
310: fragment.setLayoutColumn(column);
311: }
312:
313: /* (non-Javadoc)
314: * @see org.apache.jetspeed.om.page.Fragment#setLayoutSizes(java.lang.String)
315: */
316: public void setLayoutSizes(String sizes) {
317: fragment.setLayoutSizes(sizes);
318: }
319:
320: /* (non-Javadoc)
321: * @see org.apache.jetspeed.om.page.Fragment#setLayoutX(float)
322: */
323: public void setLayoutX(float x) {
324: fragment.setLayoutX(x);
325: }
326:
327: /* (non-Javadoc)
328: * @see org.apache.jetspeed.om.page.Fragment#setLayoutY(float)
329: */
330: public void setLayoutY(float y) {
331: fragment.setLayoutY(y);
332: }
333:
334: /* (non-Javadoc)
335: * @see org.apache.jetspeed.om.page.Fragment#setLayoutZ(float)
336: */
337: public void setLayoutZ(float z) {
338: fragment.setLayoutZ(z);
339: }
340:
341: /* (non-Javadoc)
342: * @see org.apache.jetspeed.om.page.Fragment#setLayoutWidth(float)
343: */
344: public void setLayoutWidth(float width) {
345: fragment.setLayoutWidth(width);
346: }
347:
348: /* (non-Javadoc)
349: * @see org.apache.jetspeed.om.page.Fragment#setLayoutHeight(float)
350: */
351: public void setLayoutHeight(float height) {
352: fragment.setLayoutHeight(height);
353: }
354:
355: /* (non-Javadoc)
356: * @see org.apache.jetspeed.om.page.Fragment#setSkin(java.lang.String)
357: */
358: public void setSkin(String skinName) {
359:
360: fragment.setSkin(skinName);
361: }
362:
363: /* (non-Javadoc)
364: * @see org.apache.jetspeed.om.page.Fragment#setState(java.lang.String)
365: */
366: public void setState(String state) {
367:
368: fragment.setState(state);
369: }
370:
371: /* (non-Javadoc)
372: * @see org.apache.jetspeed.om.page.Fragment#setMode(java.lang.String)
373: */
374: public void setMode(String mode) {
375:
376: fragment.setMode(mode);
377: }
378:
379: /* (non-Javadoc)
380: * @see org.apache.jetspeed.om.page.Fragment#setType(java.lang.String)
381: */
382: public void setType(String type) {
383:
384: fragment.setType(type);
385: }
386:
387: /* (non-Javadoc)
388: * @see org.apache.jetspeed.om.page.BaseElement#getId()
389: */
390: public String getId() {
391:
392: return fragment.getId();
393: }
394:
395: /* (non-Javadoc)
396: * @see org.apache.jetspeed.om.page.BaseElement#getShortTitle()
397: */
398: public String getShortTitle() {
399:
400: return fragment.getShortTitle();
401: }
402:
403: /* (non-Javadoc)
404: * @see org.apache.jetspeed.om.page.BaseElement#getTitle()
405: */
406: public String getTitle() {
407:
408: return fragment.getTitle();
409: }
410:
411: /* (non-Javadoc)
412: * @see org.apache.jetspeed.om.page.BaseElement#setShortTitle(java.lang.String)
413: */
414: public void setShortTitle(String title) {
415:
416: fragment.setShortTitle(title);
417: }
418:
419: /* (non-Javadoc)
420: * @see org.apache.jetspeed.om.page.BaseElement#setTitle(java.lang.String)
421: */
422: public void setTitle(String title) {
423:
424: fragment.setTitle(title);
425: }
426:
427: /* (non-Javadoc)
428: * @see org.apache.jetspeed.om.common.SecuredResource#checkAccess(java.lang.String)
429: */
430: public void checkAccess(String actions) throws SecurityException {
431:
432: fragment.checkAccess(actions);
433: }
434:
435: /* (non-Javadoc)
436: * @see org.apache.jetspeed.om.common.SecuredResource#checkConstraints(java.lang.String)
437: */
438: public void checkConstraints(String actions)
439: throws SecurityException {
440:
441: fragment.checkConstraints(actions);
442: }
443:
444: /* (non-Javadoc)
445: * @see org.apache.jetspeed.om.common.SecuredResource#checkPermissions(int)
446: */
447: public void checkPermissions(int mask) throws SecurityException {
448:
449: fragment.checkPermissions(mask);
450: }
451:
452: /* (non-Javadoc)
453: * @see org.apache.jetspeed.om.common.SecuredResource#getConstraintsEnabled()
454: */
455: public boolean getConstraintsEnabled() {
456:
457: return fragment.getConstraintsEnabled();
458: }
459:
460: /* (non-Javadoc)
461: * @see org.apache.jetspeed.om.common.SecuredResource#getPermissionsEnabled()
462: */
463: public boolean getPermissionsEnabled() {
464:
465: return fragment.getPermissionsEnabled();
466: }
467:
468: /* (non-Javadoc)
469: * @see org.apache.jetspeed.om.common.SecuredResource#getSecurityConstraints()
470: */
471: public SecurityConstraints getSecurityConstraints() {
472:
473: return fragment.getSecurityConstraints();
474: }
475:
476: /* (non-Javadoc)
477: * @see org.apache.jetspeed.om.common.SecuredResource#newSecurityConstraints()
478: */
479: public SecurityConstraints newSecurityConstraints() {
480:
481: return fragment.newSecurityConstraints();
482: }
483:
484: /* (non-Javadoc)
485: * @see org.apache.jetspeed.om.common.SecuredResource#newSecurityConstraint()
486: */
487: public SecurityConstraint newSecurityConstraint() {
488:
489: return fragment.newSecurityConstraint();
490: }
491:
492: /* (non-Javadoc)
493: * @see org.apache.jetspeed.om.common.SecuredResource#setSecurityConstraints(org.apache.jetspeed.om.common.SecurityConstraints)
494: */
495: public void setSecurityConstraints(SecurityConstraints constraints) {
496: fragment.setSecurityConstraints(constraints);
497: }
498:
499: /**
500: * Checks the ContentFragment cache for a ContentFragment
501: * that matches the <code>Id</code> of this fragment. If
502: * one is found, it returned. If no matches are found, a new
503: * <code>ContentFragment</code> represnentive of the {@link Fragment}
504: * argument is subsequently created, stored into the cahce and returned.
505: *
506: * @param f
507: * @return ContentFrament
508: */
509: protected ContentFragment getContentFragment(Fragment f) {
510: ContentFragment cf;
511: if (cachedFragments.containsKey(f.getId())) {
512: cf = (ContentFragment) cachedFragments.get(f.getId());
513: } else {
514: cf = new ContentFragmentImpl(f, cachedFragments);
515: cachedFragments.put(f.getId(), cf);
516: }
517: return cf;
518: }
519:
520: protected final class ContentFragmentList implements List,
521: Serializable {
522: private List baseList = fragment.getFragments();
523:
524: /* (non-Javadoc)
525: * @see java.util.List#add(int, java.lang.Object)
526: */
527: public void add(int index, Object element) {
528: if (element instanceof ContentFragmentImpl)
529: element = ((ContentFragmentImpl) element).fragment;
530: baseList.add(index, element);
531: }
532:
533: /* (non-Javadoc)
534: * @see java.util.List#add(java.lang.Object)
535: */
536: public boolean add(Object o) {
537: if (o instanceof ContentFragmentImpl)
538: o = ((ContentFragmentImpl) o).fragment;
539: return baseList.add(o);
540: }
541:
542: /* (non-Javadoc)
543: * @see java.util.List#addAll(int, java.util.Collection)
544: */
545: public boolean addAll(int index, Collection c) {
546:
547: return baseList.addAll(index, c);
548: }
549:
550: /* (non-Javadoc)
551: * @see java.util.List#addAll(java.util.Collection)
552: */
553: public boolean addAll(Collection c) {
554:
555: return baseList.addAll(c);
556: }
557:
558: /* (non-Javadoc)
559: * @see java.util.List#clear()
560: */
561: public void clear() {
562:
563: baseList.clear();
564: }
565:
566: /* (non-Javadoc)
567: * @see java.util.List#contains(java.lang.Object)
568: */
569: public boolean contains(Object o) {
570:
571: return baseList.contains(o);
572: }
573:
574: /* (non-Javadoc)
575: * @see java.util.List#containsAll(java.util.Collection)
576: */
577: public boolean containsAll(Collection c) {
578:
579: return baseList.containsAll(c);
580: }
581:
582: /* (non-Javadoc)
583: * @see java.util.List#equals(java.lang.Object)
584: */
585: public boolean equals(Object o) {
586:
587: return baseList.equals(o);
588: }
589:
590: /* (non-Javadoc)
591: * @see java.util.List#get(int)
592: */
593: public Object get(int index) {
594: Fragment f = (Fragment) baseList.get(index);
595: return getContentFragment(f);
596: }
597:
598: /* (non-Javadoc)
599: * @see java.util.List#hashCode()
600: */
601: public int hashCode() {
602:
603: return baseList.hashCode();
604: }
605:
606: /* (non-Javadoc)
607: * @see java.util.List#indexOf(java.lang.Object)
608: */
609: public int indexOf(Object o) {
610:
611: return baseList.indexOf(o);
612: }
613:
614: /* (non-Javadoc)
615: * @see java.util.List#isEmpty()
616: */
617: public boolean isEmpty() {
618:
619: return baseList.isEmpty();
620: }
621:
622: /* (non-Javadoc)
623: * @see java.util.List#iterator()
624: */
625: public Iterator iterator() {
626: return duplicateList().iterator();
627: }
628:
629: /* (non-Javadoc)
630: * @see java.util.List#lastIndexOf(java.lang.Object)
631: */
632: public int lastIndexOf(Object o) {
633:
634: return baseList.lastIndexOf(o);
635: }
636:
637: /* (non-Javadoc)
638: * @see java.util.List#listIterator()
639: */
640: public ListIterator listIterator() {
641: return duplicateList().listIterator();
642: }
643:
644: /* (non-Javadoc)
645: * @see java.util.List#listIterator(int)
646: */
647: public ListIterator listIterator(int index) {
648: return duplicateList().listIterator(index);
649: }
650:
651: /* (non-Javadoc)
652: * @see java.util.List#remove(int)
653: */
654: public Object remove(int index) {
655:
656: return baseList.remove(index);
657: }
658:
659: /* (non-Javadoc)
660: * @see java.util.List#remove(java.lang.Object)
661: */
662: public boolean remove(Object o) {
663:
664: return baseList.remove(o);
665: }
666:
667: /* (non-Javadoc)
668: * @see java.util.List#removeAll(java.util.Collection)
669: */
670: public boolean removeAll(Collection c) {
671:
672: return baseList.removeAll(c);
673: }
674:
675: /* (non-Javadoc)
676: * @see java.util.List#retainAll(java.util.Collection)
677: */
678: public boolean retainAll(Collection c) {
679:
680: return baseList.retainAll(c);
681: }
682:
683: /* (non-Javadoc)
684: * @see java.util.List#set(int, java.lang.Object)
685: */
686: public Object set(int index, Object element) {
687:
688: return baseList.set(index, element);
689: }
690:
691: /* (non-Javadoc)
692: * @see java.util.List#size()
693: */
694: public int size() {
695:
696: return baseList.size();
697: }
698:
699: /* (non-Javadoc)
700: * @see java.util.List#subList(int, int)
701: */
702: public List subList(int fromIndex, int toIndex) {
703: return duplicateList().subList(fromIndex, toIndex);
704: }
705:
706: /* (non-Javadoc)
707: * @see java.util.List#toArray()
708: */
709: public Object[] toArray() {
710: return duplicateList().toArray();
711: }
712:
713: /* (non-Javadoc)
714: * @see java.util.List#toArray(java.lang.Object[])
715: */
716: public Object[] toArray(Object[] a) {
717: return duplicateList().toArray(a);
718: }
719:
720: private List duplicateList() {
721: List rFragList = DatabasePageManagerUtils.createList();
722: for (int i = 0; i < baseList.size(); i++) {
723: Fragment f = (Fragment) baseList.get(i);
724: ContentFragment cf = getContentFragment(f);
725: rFragList.add(cf);
726: }
727: return rFragList;
728: }
729:
730: }
731:
732: /* (non-Javadoc)
733: * @see org.apache.jetspeed.om.page.Fragment#getPreferences()
734: */
735: public List getPreferences() {
736: return fragment.getPreferences();
737: }
738:
739: public Decoration getDecoration() {
740: return decoration;
741: }
742:
743: /* (non-Javadoc)
744: * @see org.apache.jetspeed.om.page.Fragment#setPreferences(java.util.List)
745: */
746: public void setPreferences(List preferences) {
747: fragment.setPreferences(preferences);
748: }
749:
750: public void setDecoration(Decoration decoration) {
751: this .decoration = decoration;
752:
753: }
754:
755: /* (non-Javadoc)
756: * @see org.apache.jetspeed.om.page.ContentFragment#isInstantlyRendered()
757: */
758: public boolean isInstantlyRendered() {
759: return this.instantlyRendered;
760: }
761:
762: }
|