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.container.window.impl;
018:
019: import java.util.Iterator;
020: import java.util.Set;
021:
022: import org.apache.commons.logging.Log;
023: import org.apache.commons.logging.LogFactory;
024: import org.apache.jetspeed.cache.PortletWindowCache;
025: import org.apache.jetspeed.components.portletentity.PortletEntityAccessComponent;
026: import org.apache.jetspeed.components.portletentity.PortletEntityNotGeneratedException;
027: import org.apache.jetspeed.components.portletentity.PortletEntityNotStoredException;
028: import org.apache.jetspeed.components.portletregistry.PortletRegistry;
029: import org.apache.jetspeed.components.portletregistry.RegistryEventListener;
030: import org.apache.jetspeed.container.window.FailedToCreateWindowException;
031: import org.apache.jetspeed.container.window.FailedToRetrievePortletWindow;
032: import org.apache.jetspeed.container.window.PortletWindowAccessor;
033: import org.apache.jetspeed.factory.PortletFactory;
034: import org.apache.jetspeed.om.common.portlet.MutablePortletApplication;
035: import org.apache.jetspeed.om.common.portlet.MutablePortletEntity;
036: import org.apache.jetspeed.om.common.portlet.PortletApplication;
037: import org.apache.jetspeed.om.common.portlet.PortletDefinitionComposite;
038: import org.apache.jetspeed.om.page.ContentFragment;
039: import org.apache.jetspeed.om.window.impl.PortletWindowImpl;
040: import org.apache.jetspeed.util.ArgUtil;
041: import org.apache.pluto.om.entity.PortletEntity;
042: import org.apache.pluto.om.window.PortletWindow;
043: import org.apache.pluto.om.window.PortletWindowCtrl;
044:
045: /**
046: * Portlet Window Accessor Implementation
047: *
048: * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
049: * @version $Id: PortletWindowAccessorImpl.java,v 1.12 2005/04/29 14:01:57 weaver Exp $
050: */
051: public class PortletWindowAccessorImpl implements
052: PortletWindowAccessor, RegistryEventListener {
053: protected final static Log log = LogFactory
054: .getLog(PortletWindowAccessorImpl.class);
055:
056: //private Map windows = Collections.synchronizedMap(new HashMap());
057: private PortletEntityAccessComponent entityAccessor;
058: private PortletFactory portletFactory;
059: private boolean validateWindows = false;
060: private PortletWindowCache portletWindowCache;
061:
062: public PortletWindowAccessorImpl(
063: PortletEntityAccessComponent entityAccessor,
064: PortletFactory portletFactory,
065: PortletWindowCache portletWindowCache,
066: boolean validateWindows) {
067: this .entityAccessor = entityAccessor;
068: this .portletFactory = portletFactory;
069: this .validateWindows = validateWindows;
070: this .portletWindowCache = portletWindowCache;
071:
072: }
073:
074: public PortletWindowAccessorImpl(
075: PortletEntityAccessComponent entityAccessor,
076: PortletFactory portletFactory, PortletRegistry registry,
077: PortletWindowCache portletWindowCache,
078: boolean validateWindows) {
079: this .entityAccessor = entityAccessor;
080: this .portletFactory = portletFactory;
081: this .validateWindows = validateWindows;
082: this .portletWindowCache = portletWindowCache;
083: registry.addRegistryListener(this );
084: }
085:
086: public PortletWindow createPortletWindow(PortletEntity entity,
087: String windowId) {
088: if (entity == null) {
089: throw new IllegalArgumentException(
090: "cratePortletWindow requires a non-null PortletEntity");
091: }
092:
093: PortletWindow found = getWindowFromCache(windowId);
094: if (found != null) {
095: // remove from cache if invalid entity
096: checkPortletWindowEntity(found);
097: ((PortletWindowCtrl) found).setPortletEntity(entity);
098: return found;
099: }
100:
101: PortletWindowImpl window = new PortletWindowImpl(windowId);
102: window.setPortletEntity(entity);
103: if (isValidPortletEntity(entity)) {
104: // windows.put(windowId, window);
105: portletWindowCache.putPortletWindow(window);
106: }
107: return window;
108: }
109:
110: public PortletWindow createPortletWindow(String windowId) {
111: PortletWindow found = getWindowFromCache(windowId);
112: if (found != null) {
113: // remove from cache if invalid entity
114: checkPortletWindowEntity(found);
115: return found;
116: }
117: PortletWindowImpl window = new PortletWindowImpl(windowId);
118: return window;
119: }
120:
121: public PortletWindow getPortletWindow(String windowId) {
122: PortletWindow window = getWindowFromCache(windowId);
123: if (window != null) {
124: // remove from cache if invalid entity
125: checkPortletWindowEntity(window);
126: }
127: return window;
128: }
129:
130: public PortletWindow getPortletWindow(ContentFragment fragment)
131: throws FailedToRetrievePortletWindow,
132: PortletEntityNotStoredException {
133: ArgUtil.assertNotNull(ContentFragment.class, fragment, this ,
134: "getPortletWindow(Fragment fragment)");
135: PortletWindow portletWindow = getWindowFromCache(fragment);
136: if (portletWindow == null
137: || !checkPortletWindowEntity(portletWindow)) {
138: try {
139: return createPortletWindow(fragment);
140: } catch (FailedToCreateWindowException e) {
141: throw new FailedToRetrievePortletWindow(e.toString(), e);
142: }
143: } else {
144: if (validateWindows) {
145: validateWindow(fragment, portletWindow);
146: }
147: }
148:
149: return portletWindow;
150: }
151:
152: /**
153: * <p>
154: * validateWindow
155: * </p>
156: *
157: * @param fragment
158: * @param portletWindow
159: * @throws PortletEntityNotStoredException
160: * @throws InconsistentWindowStateException
161: */
162: protected void validateWindow(ContentFragment fragment,
163: PortletWindow portletWindow)
164: throws FailedToRetrievePortletWindow,
165: PortletEntityNotStoredException {
166: // make sure the window has the most up-to-date portlet entity
167: PortletEntity portletEntity = entityAccessor
168: .getPortletEntityForFragment(fragment);
169: if (portletEntity != null) {
170: ((PortletWindowCtrl) portletWindow)
171: .setPortletEntity(portletEntity);
172: // if not a valid entity, remove window from cache
173: checkPortletWindowEntity(portletWindow);
174: } else {
175: removeWindow(portletWindow);
176: throw new FailedToRetrievePortletWindow(
177: "No PortletEntity exists for for id "
178: + fragment.getId()
179: + " removing window from cache.");
180: }
181: }
182:
183: public PortletWindow getPortletWindow(ContentFragment fragment,
184: String principal) throws FailedToRetrievePortletWindow,
185: FailedToCreateWindowException,
186: PortletEntityNotStoredException {
187: ArgUtil
188: .assertNotNull(ContentFragment.class, fragment, this ,
189: "getPortletWindow(Fragment fragment, String principal)");
190: ArgUtil
191: .assertNotNull(String.class, principal, this ,
192: "getPortletWindow(Fragment fragment, String principal)");
193: PortletWindow portletWindow = getWindowFromCache(fragment);
194: if (portletWindow == null) {
195: return createPortletWindow(fragment, principal);
196: } else {
197: // make sure the window has the most up-to-date portlet entity
198: validateWindow(fragment, portletWindow);
199: }
200: return portletWindow;
201: }
202:
203: private PortletWindow createPortletWindow(ContentFragment fragment)
204: throws FailedToCreateWindowException,
205: PortletEntityNotStoredException {
206: return createPortletWindow(fragment, null);
207: }
208:
209: private PortletWindow createPortletWindow(ContentFragment fragment,
210: String principal) throws FailedToCreateWindowException,
211: PortletEntityNotStoredException {
212: PortletWindow portletWindow = new PortletWindowImpl(fragment
213: .getId());
214: boolean temporaryWindow = false;
215:
216: MutablePortletEntity portletEntity = entityAccessor
217: .getPortletEntityForFragment(fragment, principal);
218: if (portletEntity == null) {
219: log.info("No portlet entity defined for fragment ID "
220: + fragment.getId()
221: + " attempting to auto-generate...");
222: try {
223: portletEntity = entityAccessor
224: .generateEntityFromFragment(fragment, principal);
225: // not portlet definition most likely means that the portlet has not been deployed so dont worry about storing off the entity
226: if (isValidPortletEntity(portletEntity)) {
227: entityAccessor.storePortletEntity(portletEntity);
228: } else {
229: // don't cache the incomplete window
230: temporaryWindow = true;
231: }
232: } catch (PortletEntityNotGeneratedException e) {
233: throw new FailedToCreateWindowException(
234: "Error generating new PortletEntity: "
235: + e.toString(), e);
236: } catch (PortletEntityNotStoredException e) {
237: throw new FailedToCreateWindowException(
238: "Error storing new PortletEntity: "
239: + e.toString(), e);
240: }
241:
242: if (portletEntity == null) {
243: throw new FailedToCreateWindowException(
244: "Unable to generate portlet entity.");
245: }
246:
247: }
248: ((PortletWindowCtrl) portletWindow)
249: .setPortletEntity(portletEntity);
250:
251: if (!temporaryWindow) {
252: portletWindowCache.putPortletWindow(portletWindow);
253: }
254:
255: return portletWindow;
256: }
257:
258: public void removeWindows(PortletEntity portletEntity) {
259: // List tmpWindows = new ArrayList(windows.entrySet());
260: // for(int i = 0; i < tmpWindows.size(); i++)
261: // {
262: // PortletWindow window = (PortletWindow)((Map.Entry)tmpWindows.get(i)).getValue();
263: // if (portletEntity.getId().equals(window.getPortletEntity().getId()))
264: // {
265: // removeWindow(window);
266: // }
267: // }
268: // tmpWindows.clear();
269: //
270: portletWindowCache
271: .removePortletWindowByPortletEntityId(portletEntity
272: .getId().toString());
273:
274: }
275:
276: public void removeWindow(PortletWindow window) {
277: // windows.remove(window.getId().toString());
278: portletWindowCache.removePortletWindow(window.getId()
279: .toString());
280: }
281:
282: private PortletWindow getWindowFromCache(ContentFragment fragment) {
283: return portletWindowCache.getPortletWindow(fragment.getId());
284: }
285:
286: private PortletWindow getWindowFromCache(String id) {
287: return portletWindowCache.getPortletWindow(id);
288: }
289:
290: private boolean checkPortletWindowEntity(PortletWindow window) {
291: if (!isValidPortletEntity(window.getPortletEntity())) {
292: removeWindow(window);
293: return false;
294: }
295: return true;
296: }
297:
298: private boolean isValidPortletEntity(PortletEntity pe) {
299: return pe != null
300: && pe.getPortletDefinition() != null
301: && pe.getPortletDefinition()
302: .getPortletApplicationDefinition() != null
303: && portletFactory
304: .isPortletApplicationRegistered((PortletApplication) pe
305: .getPortletDefinition()
306: .getPortletApplicationDefinition());
307: }
308:
309: public Set getPortletWindows() {
310: return portletWindowCache.getAllPortletWindows();
311: }
312:
313: protected void removeForPortletDefinition(
314: PortletDefinitionComposite def) {
315: // List tmpWindows = new ArrayList(windows.entrySet());
316: // for (int i = 0; i < tmpWindows.size(); i++)
317: // {
318: // PortletWindow window = (PortletWindow)((Map.Entry)tmpWindows.get(i)).getValue();
319: // PortletDefinitionComposite windowDef = (PortletDefinitionComposite)window.getPortletEntity().getPortletDefinition();
320: // if(def != null && windowDef != null && def.getUniqueName() != null && def.getUniqueName().equals(windowDef.getUniqueName()))
321: // {
322: // removeWindow(window);
323: // }
324: // }
325: // tmpWindows.clear();
326: // if (def != null)
327: // portletFactory.updatePortletConfig(def);
328:
329: Iterator windows = getPortletWindows().iterator();
330: while (windows.hasNext()) {
331: PortletWindow window = (PortletWindow) windows.next();
332: PortletDefinitionComposite windowDef = (PortletDefinitionComposite) window
333: .getPortletEntity().getPortletDefinition();
334: if (def != null
335: && windowDef != null
336: && def.getUniqueName() != null
337: && def.getUniqueName().equals(
338: windowDef.getUniqueName())) {
339: removeWindow(window);
340: }
341: }
342:
343: if (def != null)
344: portletFactory.updatePortletConfig(def);
345: }
346:
347: protected void removeForPortletApplication(
348: MutablePortletApplication app) {
349: // List tmpWindows = new ArrayList(windows.entrySet());
350: // for (int i = 0; i < tmpWindows.size(); i++)
351: // {
352: // PortletWindow window = (PortletWindow)((Map.Entry)tmpWindows.get(i)).getValue();
353: // PortletDefinitionComposite pd = (PortletDefinitionComposite)window.getPortletEntity().getPortletDefinition();
354: // if (pd != null)
355: // {
356: // MutablePortletApplication windowApp = (MutablePortletApplication)pd.getPortletApplicationDefinition();
357: // if (app.getName().equals(windowApp.getName()))
358: // {
359: // removeWindow(window);
360: // }
361: // }
362: // }
363: // tmpWindows.clear();
364:
365: Iterator windows = getPortletWindows().iterator();
366: while (windows.hasNext()) {
367: PortletWindow window = (PortletWindow) windows.next();
368: PortletDefinitionComposite pd = (PortletDefinitionComposite) window
369: .getPortletEntity().getPortletDefinition();
370: if (pd != null) {
371: MutablePortletApplication windowApp = (MutablePortletApplication) pd
372: .getPortletApplicationDefinition();
373: if (app.getName().equals(windowApp.getName())) {
374: removeWindow(window);
375: }
376: }
377: }
378:
379: }
380:
381: public void applicationRemoved(MutablePortletApplication app) {
382: if (app == null) {
383: //System.out.println("@@@ receiving APP REMOVED message with NULL");
384: return;
385: }
386: //System.out.println("@@@ receiving APP REMOVED message: " + app.getName());
387: removeForPortletApplication(app);
388: }
389:
390: public void applicationUpdated(MutablePortletApplication app) {
391: if (app == null) {
392: //System.out.println("@@@ receiving APP UPDATED message with NULL");
393: return;
394: }
395: //System.out.println("@@@ receiving APP UPDATED message: " + app.getName());
396: removeForPortletApplication(app);
397: }
398:
399: public void portletRemoved(PortletDefinitionComposite def) {
400: if (def == null) {
401: //System.out.println("@@@ receiving DEF REMOVED message with NULL");
402: return;
403: }
404: //System.out.println("@@@ receiving DEF REMOVED message: " + def.getName());
405: removeForPortletDefinition(def);
406: }
407:
408: public void portletUpdated(PortletDefinitionComposite def) {
409: if (def == null) {
410: //System.out.println("@@@ receiving DEF UPDATED message with NULL");
411: return;
412: }
413: //System.out.println("@@@ receiving DEF UPDATED message: " + def.getName());
414: removeForPortletDefinition(def);
415: }
416: }
|