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 |
|
|
47 |
|
|
48 |
|
|
49 |
|
|
50 |
|
|
51 |
|
public class PortletWindowAccessorImpl implements PortletWindowAccessor, RegistryEventListener |
52 |
|
{ |
53 |
0 |
protected final static Log log = LogFactory.getLog(PortletWindowAccessorImpl.class); |
54 |
|
|
55 |
|
|
56 |
|
private PortletEntityAccessComponent entityAccessor; |
57 |
|
private PortletFactory portletFactory; |
58 |
0 |
private boolean validateWindows = false; |
59 |
|
private PortletWindowCache portletWindowCache; |
60 |
|
|
61 |
|
|
62 |
|
|
63 |
|
public PortletWindowAccessorImpl(PortletEntityAccessComponent entityAccessor, PortletFactory portletFactory, PortletWindowCache portletWindowCache, boolean validateWindows) |
64 |
0 |
{ |
65 |
0 |
this.entityAccessor = entityAccessor; |
66 |
0 |
this.portletFactory = portletFactory; |
67 |
0 |
this.validateWindows = validateWindows; |
68 |
0 |
this.portletWindowCache = portletWindowCache; |
69 |
|
|
70 |
0 |
} |
71 |
|
|
72 |
|
public PortletWindowAccessorImpl(PortletEntityAccessComponent entityAccessor, |
73 |
|
PortletFactory portletFactory, |
74 |
|
PortletRegistry registry, |
75 |
|
PortletWindowCache portletWindowCache, |
76 |
|
boolean validateWindows) |
77 |
0 |
{ |
78 |
0 |
this.entityAccessor = entityAccessor; |
79 |
0 |
this.portletFactory = portletFactory; |
80 |
0 |
this.validateWindows = validateWindows; |
81 |
0 |
this.portletWindowCache = portletWindowCache; |
82 |
0 |
registry.addRegistryListener(this); |
83 |
0 |
} |
84 |
|
|
85 |
|
public PortletWindow createPortletWindow(PortletEntity entity, String windowId) |
86 |
|
{ |
87 |
0 |
if(entity == null) |
88 |
|
{ |
89 |
0 |
throw new IllegalArgumentException("cratePortletWindow requires a non-null PortletEntity"); |
90 |
|
} |
91 |
|
|
92 |
0 |
PortletWindow found = getWindowFromCache(windowId); |
93 |
0 |
if (found != null) |
94 |
|
{ |
95 |
|
|
96 |
0 |
checkPortletWindowEntity(found); |
97 |
0 |
((PortletWindowCtrl)found).setPortletEntity(entity); |
98 |
0 |
return found; |
99 |
|
} |
100 |
|
|
101 |
0 |
PortletWindowImpl window = new PortletWindowImpl(windowId); |
102 |
0 |
window.setPortletEntity(entity); |
103 |
0 |
if ( isValidPortletEntity(entity)) |
104 |
|
{ |
105 |
|
|
106 |
0 |
portletWindowCache.putPortletWindow(window); |
107 |
|
} |
108 |
0 |
return window; |
109 |
|
} |
110 |
|
|
111 |
|
public PortletWindow createPortletWindow(String windowId) |
112 |
|
{ |
113 |
0 |
PortletWindow found = getWindowFromCache(windowId); |
114 |
0 |
if (found != null) |
115 |
|
{ |
116 |
|
|
117 |
0 |
checkPortletWindowEntity(found); |
118 |
0 |
return found; |
119 |
|
} |
120 |
0 |
PortletWindowImpl window = new PortletWindowImpl(windowId); |
121 |
0 |
return window; |
122 |
|
} |
123 |
|
|
124 |
|
public PortletWindow getPortletWindow(String windowId) |
125 |
|
{ |
126 |
0 |
PortletWindow window = getWindowFromCache(windowId); |
127 |
0 |
if (window != null) |
128 |
|
{ |
129 |
|
|
130 |
0 |
checkPortletWindowEntity(window); |
131 |
|
} |
132 |
0 |
return window; |
133 |
|
} |
134 |
|
|
135 |
|
public PortletWindow getPortletWindow(ContentFragment fragment) throws FailedToRetrievePortletWindow, PortletEntityNotStoredException |
136 |
|
{ |
137 |
0 |
ArgUtil.assertNotNull(ContentFragment.class, fragment, this, "getPortletWindow(Fragment fragment)"); |
138 |
0 |
PortletWindow portletWindow = getWindowFromCache(fragment); |
139 |
0 |
if (portletWindow == null || !checkPortletWindowEntity(portletWindow)) |
140 |
|
{ |
141 |
|
try |
142 |
|
{ |
143 |
0 |
return createPortletWindow(fragment); |
144 |
|
} |
145 |
0 |
catch (FailedToCreateWindowException e) |
146 |
|
{ |
147 |
0 |
throw new FailedToRetrievePortletWindow(e.toString(), e); |
148 |
|
} |
149 |
|
} |
150 |
|
else |
151 |
|
{ |
152 |
0 |
if (validateWindows) |
153 |
|
{ |
154 |
0 |
validateWindow(fragment, portletWindow); |
155 |
|
} |
156 |
|
} |
157 |
|
|
158 |
0 |
return portletWindow; |
159 |
|
} |
160 |
|
|
161 |
|
|
162 |
|
|
163 |
|
|
164 |
|
|
165 |
|
|
166 |
|
|
167 |
|
|
168 |
|
|
169 |
|
|
170 |
|
|
171 |
|
protected void validateWindow( ContentFragment fragment, PortletWindow portletWindow ) throws FailedToRetrievePortletWindow, PortletEntityNotStoredException |
172 |
|
{ |
173 |
|
|
174 |
0 |
PortletEntity portletEntity = entityAccessor.getPortletEntityForFragment(fragment); |
175 |
0 |
if(portletEntity != null) |
176 |
|
{ |
177 |
0 |
((PortletWindowCtrl) portletWindow).setPortletEntity(portletEntity); |
178 |
|
|
179 |
0 |
checkPortletWindowEntity(portletWindow); |
180 |
|
} |
181 |
|
else |
182 |
|
{ |
183 |
0 |
removeWindow(portletWindow); |
184 |
0 |
throw new FailedToRetrievePortletWindow("No PortletEntity exists for for id "+fragment.getId()+" removing window from cache."); |
185 |
|
} |
186 |
0 |
} |
187 |
|
|
188 |
|
public PortletWindow getPortletWindow(ContentFragment fragment, String principal) throws FailedToRetrievePortletWindow, FailedToCreateWindowException, PortletEntityNotStoredException |
189 |
|
{ |
190 |
0 |
ArgUtil.assertNotNull(ContentFragment.class, fragment, this, "getPortletWindow(Fragment fragment, String principal)"); |
191 |
0 |
ArgUtil.assertNotNull(String.class, principal, this, "getPortletWindow(Fragment fragment, String principal)"); |
192 |
0 |
PortletWindow portletWindow = getWindowFromCache(fragment); |
193 |
0 |
if (portletWindow == null) |
194 |
|
{ |
195 |
0 |
return createPortletWindow(fragment, principal); |
196 |
|
} |
197 |
|
else |
198 |
|
{ |
199 |
|
|
200 |
0 |
validateWindow(fragment, portletWindow); |
201 |
|
} |
202 |
0 |
return portletWindow; |
203 |
|
} |
204 |
|
|
205 |
|
private PortletWindow createPortletWindow(ContentFragment fragment) throws FailedToCreateWindowException, PortletEntityNotStoredException |
206 |
|
{ |
207 |
0 |
return createPortletWindow(fragment, null); |
208 |
|
} |
209 |
|
|
210 |
|
private PortletWindow createPortletWindow(ContentFragment fragment, String principal) throws FailedToCreateWindowException, PortletEntityNotStoredException |
211 |
|
{ |
212 |
0 |
PortletWindow portletWindow = new PortletWindowImpl(fragment.getId()); |
213 |
0 |
boolean temporaryWindow = false; |
214 |
|
|
215 |
0 |
MutablePortletEntity portletEntity = entityAccessor.getPortletEntityForFragment(fragment, principal); |
216 |
0 |
if (portletEntity == null) |
217 |
|
{ |
218 |
0 |
log.info("No portlet entity defined for fragment ID "+fragment.getId()+" attempting to auto-generate..."); |
219 |
|
try |
220 |
|
{ |
221 |
0 |
portletEntity = entityAccessor.generateEntityFromFragment(fragment, principal); |
222 |
|
|
223 |
0 |
if(isValidPortletEntity(portletEntity)) |
224 |
|
{ |
225 |
0 |
entityAccessor.storePortletEntity(portletEntity); |
226 |
|
} |
227 |
|
else |
228 |
|
{ |
229 |
|
|
230 |
0 |
temporaryWindow = true; |
231 |
|
} |
232 |
|
} |
233 |
0 |
catch (PortletEntityNotGeneratedException e) |
234 |
|
{ |
235 |
0 |
throw new FailedToCreateWindowException("Error generating new PortletEntity: "+e.toString(), e); |
236 |
|
} |
237 |
0 |
catch (PortletEntityNotStoredException e) |
238 |
|
{ |
239 |
0 |
throw new FailedToCreateWindowException("Error storing new PortletEntity: "+e.toString(), e); |
240 |
0 |
} |
241 |
|
|
242 |
0 |
if(portletEntity == null) |
243 |
|
{ |
244 |
0 |
throw new FailedToCreateWindowException("Unable to generate portlet entity."); |
245 |
|
} |
246 |
|
|
247 |
|
} |
248 |
0 |
((PortletWindowCtrl) portletWindow).setPortletEntity(portletEntity); |
249 |
|
|
250 |
0 |
if ( !temporaryWindow ) |
251 |
|
{ |
252 |
0 |
portletWindowCache.putPortletWindow(portletWindow); |
253 |
|
} |
254 |
|
|
255 |
0 |
return portletWindow; |
256 |
|
} |
257 |
|
|
258 |
|
|
259 |
|
public void removeWindows(PortletEntity portletEntity) |
260 |
|
{ |
261 |
|
|
262 |
|
|
263 |
|
|
264 |
|
|
265 |
|
|
266 |
|
|
267 |
|
|
268 |
|
|
269 |
|
|
270 |
|
|
271 |
|
|
272 |
0 |
portletWindowCache.removePortletWindowByPortletEntityId(portletEntity.getId().toString()); |
273 |
|
|
274 |
0 |
} |
275 |
|
|
276 |
|
public void removeWindow(PortletWindow window) |
277 |
|
{ |
278 |
|
|
279 |
0 |
portletWindowCache.removePortletWindow(window.getId().toString()); |
280 |
0 |
} |
281 |
|
|
282 |
|
private PortletWindow getWindowFromCache(ContentFragment fragment) |
283 |
|
{ |
284 |
0 |
return portletWindowCache.getPortletWindow(fragment.getId()); |
285 |
|
} |
286 |
|
|
287 |
|
private PortletWindow getWindowFromCache(String id) |
288 |
|
{ |
289 |
0 |
return portletWindowCache.getPortletWindow(id); |
290 |
|
} |
291 |
|
|
292 |
|
private boolean checkPortletWindowEntity(PortletWindow window) |
293 |
|
{ |
294 |
0 |
if (!isValidPortletEntity(window.getPortletEntity())) |
295 |
|
{ |
296 |
0 |
removeWindow(window); |
297 |
0 |
return false; |
298 |
|
} |
299 |
0 |
return true; |
300 |
|
} |
301 |
|
|
302 |
|
private boolean isValidPortletEntity(PortletEntity pe) |
303 |
|
{ |
304 |
0 |
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 |
0 |
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 |
0 |
Iterator windows = getPortletWindows().iterator(); |
334 |
0 |
while(windows.hasNext()) |
335 |
|
{ |
336 |
0 |
PortletWindow window = (PortletWindow) windows.next(); |
337 |
0 |
PortletDefinitionComposite windowDef = (PortletDefinitionComposite)window.getPortletEntity().getPortletDefinition(); |
338 |
0 |
if(def != null && windowDef != class="keyword">null && def.getUniqueName() != class="keyword">null && def.getUniqueName().equals(windowDef.getUniqueName())) |
339 |
|
{ |
340 |
0 |
removeWindow(window); |
341 |
|
} |
342 |
0 |
} |
343 |
|
|
344 |
0 |
if (def != null) |
345 |
0 |
portletFactory.updatePortletConfig(def); |
346 |
0 |
} |
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 |
0 |
Iterator windows = getPortletWindows().iterator(); |
367 |
0 |
while(windows.hasNext()) |
368 |
|
{ |
369 |
0 |
PortletWindow window = (PortletWindow) windows.next(); |
370 |
0 |
PortletDefinitionComposite pd = (PortletDefinitionComposite)window.getPortletEntity().getPortletDefinition(); |
371 |
0 |
if (pd != null) |
372 |
|
{ |
373 |
0 |
MutablePortletApplication windowApp = (MutablePortletApplication)pd.getPortletApplicationDefinition(); |
374 |
0 |
if (app.getName().equals(windowApp.getName())) |
375 |
|
{ |
376 |
0 |
removeWindow(window); |
377 |
|
} |
378 |
|
} |
379 |
0 |
} |
380 |
|
|
381 |
|
|
382 |
|
|
383 |
0 |
} |
384 |
|
|
385 |
|
public void applicationRemoved(MutablePortletApplication app) |
386 |
|
{ |
387 |
0 |
if (app == null) |
388 |
|
{ |
389 |
|
|
390 |
0 |
return; |
391 |
|
} |
392 |
|
|
393 |
0 |
removeForPortletApplication(app); |
394 |
0 |
} |
395 |
|
|
396 |
|
|
397 |
|
public void applicationUpdated(MutablePortletApplication app) |
398 |
|
{ |
399 |
0 |
if (app == null) |
400 |
|
{ |
401 |
|
|
402 |
0 |
return; |
403 |
|
} |
404 |
|
|
405 |
0 |
removeForPortletApplication(app); |
406 |
0 |
} |
407 |
|
|
408 |
|
public void portletRemoved(PortletDefinitionComposite def) |
409 |
|
{ |
410 |
0 |
if (def == null) |
411 |
|
{ |
412 |
|
|
413 |
0 |
return; |
414 |
|
} |
415 |
|
|
416 |
0 |
removeForPortletDefinition(def); |
417 |
0 |
} |
418 |
|
|
419 |
|
public void portletUpdated(PortletDefinitionComposite def) |
420 |
|
{ |
421 |
0 |
if (def == null) |
422 |
|
{ |
423 |
|
|
424 |
0 |
return; |
425 |
|
} |
426 |
|
|
427 |
0 |
removeForPortletDefinition(def); |
428 |
0 |
} |
429 |
|
} |