1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.jetspeed.container.window.impl;
18
19 import java.util.Iterator;
20 import java.util.Set;
21
22 import org.apache.commons.logging.Log;
23 import org.apache.commons.logging.LogFactory;
24 import org.apache.jetspeed.cache.PortletWindowCache;
25 import org.apache.jetspeed.components.portletentity.PortletEntityAccessComponent;
26 import org.apache.jetspeed.components.portletentity.PortletEntityNotGeneratedException;
27 import org.apache.jetspeed.components.portletentity.PortletEntityNotStoredException;
28 import org.apache.jetspeed.components.portletregistry.PortletRegistry;
29 import org.apache.jetspeed.components.portletregistry.RegistryEventListener;
30 import org.apache.jetspeed.container.window.FailedToCreateWindowException;
31 import org.apache.jetspeed.container.window.FailedToRetrievePortletWindow;
32 import org.apache.jetspeed.container.window.PortletWindowAccessor;
33 import org.apache.jetspeed.factory.PortletFactory;
34 import org.apache.jetspeed.om.common.portlet.MutablePortletApplication;
35 import org.apache.jetspeed.om.common.portlet.MutablePortletEntity;
36 import org.apache.jetspeed.om.common.portlet.PortletApplication;
37 import org.apache.jetspeed.om.common.portlet.PortletDefinitionComposite;
38 import org.apache.jetspeed.om.page.ContentFragment;
39 import org.apache.jetspeed.om.window.impl.PortletWindowImpl;
40 import org.apache.jetspeed.util.ArgUtil;
41 import org.apache.pluto.om.entity.PortletEntity;
42 import org.apache.pluto.om.window.PortletWindow;
43 import org.apache.pluto.om.window.PortletWindowCtrl;
44
45 /***
46 * Portlet Window Accessor Implementation
47 *
48 * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
49 * @version $Id: PortletWindowAccessorImpl.java,v 1.12 2005/04/29 14:01:57 weaver Exp $
50 */
51 public class PortletWindowAccessorImpl implements PortletWindowAccessor, RegistryEventListener
52 {
53 protected final static Log log = LogFactory.getLog(PortletWindowAccessorImpl.class);
54
55
56 private PortletEntityAccessComponent entityAccessor;
57 private PortletFactory portletFactory;
58 private boolean validateWindows = false;
59 private PortletWindowCache portletWindowCache;
60
61
62
63 public PortletWindowAccessorImpl(PortletEntityAccessComponent entityAccessor, PortletFactory portletFactory, PortletWindowCache portletWindowCache, boolean validateWindows)
64 {
65 this.entityAccessor = entityAccessor;
66 this.portletFactory = portletFactory;
67 this.validateWindows = validateWindows;
68 this.portletWindowCache = portletWindowCache;
69
70 }
71
72 public PortletWindowAccessorImpl(PortletEntityAccessComponent entityAccessor,
73 PortletFactory portletFactory,
74 PortletRegistry registry,
75 PortletWindowCache portletWindowCache,
76 boolean validateWindows)
77 {
78 this.entityAccessor = entityAccessor;
79 this.portletFactory = portletFactory;
80 this.validateWindows = validateWindows;
81 this.portletWindowCache = portletWindowCache;
82 registry.addRegistryListener(this);
83 }
84
85 public PortletWindow createPortletWindow(PortletEntity entity, String windowId)
86 {
87 if(entity == null)
88 {
89 throw new IllegalArgumentException("cratePortletWindow requires a non-null PortletEntity");
90 }
91
92 PortletWindow found = getWindowFromCache(windowId);
93 if (found != null)
94 {
95
96 checkPortletWindowEntity(found);
97 ((PortletWindowCtrl)found).setPortletEntity(entity);
98 return found;
99 }
100
101 PortletWindowImpl window = new PortletWindowImpl(windowId);
102 window.setPortletEntity(entity);
103 if ( isValidPortletEntity(entity))
104 {
105
106 portletWindowCache.putPortletWindow(window);
107 }
108 return window;
109 }
110
111 public PortletWindow createPortletWindow(String windowId)
112 {
113 PortletWindow found = getWindowFromCache(windowId);
114 if (found != null)
115 {
116
117 checkPortletWindowEntity(found);
118 return found;
119 }
120 PortletWindowImpl window = new PortletWindowImpl(windowId);
121 return window;
122 }
123
124 public PortletWindow getPortletWindow(String windowId)
125 {
126 PortletWindow window = getWindowFromCache(windowId);
127 if (window != null)
128 {
129
130 checkPortletWindowEntity(window);
131 }
132 return window;
133 }
134
135 public PortletWindow getPortletWindow(ContentFragment fragment) throws FailedToRetrievePortletWindow, PortletEntityNotStoredException
136 {
137 ArgUtil.assertNotNull(ContentFragment.class, fragment, this, "getPortletWindow(Fragment fragment)");
138 PortletWindow portletWindow = getWindowFromCache(fragment);
139 if (portletWindow == null || !checkPortletWindowEntity(portletWindow))
140 {
141 try
142 {
143 return createPortletWindow(fragment);
144 }
145 catch (FailedToCreateWindowException e)
146 {
147 throw new FailedToRetrievePortletWindow(e.toString(), e);
148 }
149 }
150 else
151 {
152 if (validateWindows)
153 {
154 validateWindow(fragment, portletWindow);
155 }
156 }
157
158 return portletWindow;
159 }
160
161 /***
162 * <p>
163 * validateWindow
164 * </p>
165 *
166 * @param fragment
167 * @param portletWindow
168 * @throws PortletEntityNotStoredException
169 * @throws InconsistentWindowStateException
170 */
171 protected void validateWindow( ContentFragment fragment, PortletWindow portletWindow ) throws FailedToRetrievePortletWindow, PortletEntityNotStoredException
172 {
173
174 PortletEntity portletEntity = entityAccessor.getPortletEntityForFragment(fragment);
175 if(portletEntity != null)
176 {
177 ((PortletWindowCtrl) portletWindow).setPortletEntity(portletEntity);
178
179 checkPortletWindowEntity(portletWindow);
180 }
181 else
182 {
183 removeWindow(portletWindow);
184 throw new FailedToRetrievePortletWindow("No PortletEntity exists for for id "+fragment.getId()+" removing window from cache.");
185 }
186 }
187
188 public PortletWindow getPortletWindow(ContentFragment fragment, String principal) throws FailedToRetrievePortletWindow, FailedToCreateWindowException, PortletEntityNotStoredException
189 {
190 ArgUtil.assertNotNull(ContentFragment.class, fragment, this, "getPortletWindow(Fragment fragment, String principal)");
191 ArgUtil.assertNotNull(String.class, principal, this, "getPortletWindow(Fragment fragment, String principal)");
192 PortletWindow portletWindow = getWindowFromCache(fragment);
193 if (portletWindow == null)
194 {
195 return createPortletWindow(fragment, principal);
196 }
197 else
198 {
199
200 validateWindow(fragment, portletWindow);
201 }
202 return portletWindow;
203 }
204
205 private PortletWindow createPortletWindow(ContentFragment fragment) throws FailedToCreateWindowException, PortletEntityNotStoredException
206 {
207 return createPortletWindow(fragment, null);
208 }
209
210 private PortletWindow createPortletWindow(ContentFragment fragment, String principal) throws FailedToCreateWindowException, PortletEntityNotStoredException
211 {
212 PortletWindow portletWindow = new PortletWindowImpl(fragment.getId());
213 boolean temporaryWindow = false;
214
215 MutablePortletEntity portletEntity = entityAccessor.getPortletEntityForFragment(fragment, principal);
216 if (portletEntity == null)
217 {
218 log.info("No portlet entity defined for fragment ID "+fragment.getId()+" attempting to auto-generate...");
219 try
220 {
221 portletEntity = entityAccessor.generateEntityFromFragment(fragment, principal);
222
223 if(isValidPortletEntity(portletEntity))
224 {
225 entityAccessor.storePortletEntity(portletEntity);
226 }
227 else
228 {
229
230 temporaryWindow = true;
231 }
232 }
233 catch (PortletEntityNotGeneratedException e)
234 {
235 throw new FailedToCreateWindowException("Error generating new PortletEntity: "+e.toString(), e);
236 }
237 catch (PortletEntityNotStoredException e)
238 {
239 throw new FailedToCreateWindowException("Error storing new PortletEntity: "+e.toString(), e);
240 }
241
242 if(portletEntity == null)
243 {
244 throw new FailedToCreateWindowException("Unable to generate portlet entity.");
245 }
246
247 }
248 ((PortletWindowCtrl) portletWindow).setPortletEntity(portletEntity);
249
250 if ( !temporaryWindow )
251 {
252 portletWindowCache.putPortletWindow(portletWindow);
253 }
254
255 return portletWindow;
256 }
257
258
259 public void removeWindows(PortletEntity portletEntity)
260 {
261
262
263
264
265
266
267
268
269
270
271
272 portletWindowCache.removePortletWindowByPortletEntityId(portletEntity.getId().toString());
273
274 }
275
276 public void removeWindow(PortletWindow window)
277 {
278
279 portletWindowCache.removePortletWindow(window.getId().toString());
280 }
281
282 private PortletWindow getWindowFromCache(ContentFragment fragment)
283 {
284 return portletWindowCache.getPortletWindow(fragment.getId());
285 }
286
287 private PortletWindow getWindowFromCache(String id)
288 {
289 return portletWindowCache.getPortletWindow(id);
290 }
291
292 private boolean checkPortletWindowEntity(PortletWindow window)
293 {
294 if (!isValidPortletEntity(window.getPortletEntity()))
295 {
296 removeWindow(window);
297 return false;
298 }
299 return true;
300 }
301
302 private boolean isValidPortletEntity(PortletEntity pe)
303 {
304 return pe != null
305 && pe.getPortletDefinition() != null
306 && pe.getPortletDefinition().getPortletApplicationDefinition() != null
307 && portletFactory.isPortletApplicationRegistered((PortletApplication) pe.getPortletDefinition()
308 .getPortletApplicationDefinition());
309 }
310
311 public Set getPortletWindows()
312 {
313 return portletWindowCache.getAllPortletWindows();
314 }
315
316 protected void removeForPortletDefinition(PortletDefinitionComposite def)
317 {
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333 Iterator windows = getPortletWindows().iterator();
334 while(windows.hasNext())
335 {
336 PortletWindow window = (PortletWindow) windows.next();
337 PortletDefinitionComposite windowDef = (PortletDefinitionComposite)window.getPortletEntity().getPortletDefinition();
338 if(def != null && windowDef != null && def.getUniqueName() != null && def.getUniqueName().equals(windowDef.getUniqueName()))
339 {
340 removeWindow(window);
341 }
342 }
343
344 if (def != null)
345 portletFactory.updatePortletConfig(def);
346 }
347
348 protected void removeForPortletApplication(MutablePortletApplication app)
349 {
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366 Iterator windows = getPortletWindows().iterator();
367 while(windows.hasNext())
368 {
369 PortletWindow window = (PortletWindow) windows.next();
370 PortletDefinitionComposite pd = (PortletDefinitionComposite)window.getPortletEntity().getPortletDefinition();
371 if (pd != null)
372 {
373 MutablePortletApplication windowApp = (MutablePortletApplication)pd.getPortletApplicationDefinition();
374 if (app.getName().equals(windowApp.getName()))
375 {
376 removeWindow(window);
377 }
378 }
379 }
380
381
382
383 }
384
385 public void applicationRemoved(MutablePortletApplication app)
386 {
387 if (app == null)
388 {
389
390 return;
391 }
392
393 removeForPortletApplication(app);
394 }
395
396
397 public void applicationUpdated(MutablePortletApplication app)
398 {
399 if (app == null)
400 {
401
402 return;
403 }
404
405 removeForPortletApplication(app);
406 }
407
408 public void portletRemoved(PortletDefinitionComposite def)
409 {
410 if (def == null)
411 {
412
413 return;
414 }
415
416 removeForPortletDefinition(def);
417 }
418
419 public void portletUpdated(PortletDefinitionComposite def)
420 {
421 if (def == null)
422 {
423
424 return;
425 }
426
427 removeForPortletDefinition(def);
428 }
429 }