KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > portal > profile > impl > StaticProfileManager


1 /*
2  * Copyright 1999-2002,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.portal.profile.impl;
17
18 import java.io.Serializable JavaDoc;
19 import java.util.ArrayList JavaDoc;
20 import java.util.Collection JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.Map JavaDoc;
25
26 import org.apache.avalon.framework.CascadingRuntimeException;
27 import org.apache.avalon.framework.configuration.Configurable;
28 import org.apache.avalon.framework.configuration.Configuration;
29 import org.apache.avalon.framework.configuration.ConfigurationException;
30 import org.apache.cocoon.portal.PortalService;
31 import org.apache.cocoon.portal.coplet.CopletData;
32 import org.apache.cocoon.portal.coplet.CopletFactory;
33 import org.apache.cocoon.portal.coplet.CopletInstanceData;
34 import org.apache.cocoon.portal.layout.CompositeLayout;
35 import org.apache.cocoon.portal.layout.Item;
36 import org.apache.cocoon.portal.layout.Layout;
37 import org.apache.cocoon.portal.layout.LayoutFactory;
38 import org.apache.cocoon.portal.profile.PortalUser;
39 import org.apache.cocoon.portal.profile.ProfileLS;
40 import org.apache.commons.collections.map.LinkedMap;
41 import org.apache.commons.collections.map.StaticBucketMap;
42 import org.apache.excalibur.source.SourceValidity;
43
44 /**
45  *
46  * @author <a HREF="mailto:cziegeler@s-und-n.de">Carsten Ziegeler</a>
47  * @author <a HREF="mailto:volker.schmitt@basf-it-services.com">Volker Schmitt</a>
48  * @author <a HREF="mailto:juergen.seitz@basf-it-services.com">J&uuml;rgen Seitz</a>
49  *
50  * @version CVS $Id: StaticProfileManager.java 180312 2005-06-06 14:01:18Z cziegeler $
51  */

52 public class StaticProfileManager
53 extends AbstractProfileManager
54 implements Configurable {
55     
56     protected String JavaDoc profilesPath;
57
58     protected StaticBucketMap copletInstanceDataManagers = new StaticBucketMap();
59     protected StaticBucketMap copletDataManagers = new StaticBucketMap();
60
61     protected static final String JavaDoc LAYOUTKEY_PREFIX = StaticProfileManager.class.getName() + "/Layout/";
62
63     protected PortalUser portalUser = new StaticPortalUser();
64     
65     /**
66      * @see org.apache.cocoon.portal.profile.ProfileManager#getPortalLayout(String, String)
67      */

68     public Layout getPortalLayout(String JavaDoc layoutKey, String JavaDoc layoutID) {
69         PortalService service = null;
70         ProfileLS adapter = null;
71         try {
72             service = (PortalService) this.manager.lookup(PortalService.ROLE);
73
74             if (layoutKey == null) {
75                 layoutKey = service.getDefaultLayoutKey();
76             }
77
78             String JavaDoc serviceKey = LAYOUTKEY_PREFIX + layoutKey;
79             Object JavaDoc[] objects = (Object JavaDoc[]) service.getAttribute(serviceKey);
80
81             // check if the layout is already cached and still valid
82
int valid = SourceValidity.INVALID;
83             SourceValidity sourceValidity = null;
84             if (objects != null) {
85                 sourceValidity = (SourceValidity) objects[1];
86                 valid = sourceValidity.isValid();
87                 Layout layout = null;
88                 if (valid == SourceValidity.VALID)
89                     layout = (Layout) ((Map JavaDoc) objects[0]).get(layoutID);
90                 if (layout != null)
91                     return layout;
92             }
93
94             CopletInstanceDataManager copletInstanceDataManager = getCopletInstanceDataManager(service);
95
96             Map JavaDoc parameters = new HashMap JavaDoc();
97             parameters.put("profiletype", "layout");
98             parameters.put("objectmap", copletInstanceDataManager.getCopletInstanceData());
99
100             Map JavaDoc map = new LinkedMap();
101             map.put("base", this.profilesPath);
102             map.put("portalname", service.getPortalName());
103             map.put("profile", "layout");
104             map.put("groupKey", layoutKey);
105
106             adapter = (ProfileLS) this.manager.lookup(ProfileLS.ROLE);
107             SourceValidity newValidity = adapter.getValidity(map, parameters);
108             if (valid == SourceValidity.UNKNOWN) {
109                 if (sourceValidity.isValid(newValidity) == SourceValidity.VALID) {
110                     return (Layout) ((Map JavaDoc) objects[0]).get(layoutID);
111                 }
112             }
113
114             // get Layout specified in the map
115
Layout layout = (Layout) adapter.loadProfile(map, parameters);
116             Map JavaDoc layouts = new HashMap JavaDoc();
117
118             layouts.put(null, layout); //save root with null as key
119
cacheLayouts(layouts, layout);
120
121             LayoutFactory factory = service.getComponentManager().getLayoutFactory();
122             factory.prepareLayout(layout);
123
124             // store the new values in the service
125
if (newValidity != null) {
126                 objects = new Object JavaDoc[] { layouts, newValidity };
127                 service.setAttribute(serviceKey, objects);
128             }
129
130             return (Layout) layouts.get(layoutID);
131         } catch (Exception JavaDoc ce) {
132             throw new CascadingRuntimeException("Unable to get layout.", ce);
133         } finally {
134             this.manager.release(service);
135             this.manager.release(adapter);
136         }
137     }
138
139     /**
140      * @param layoutMap
141      * @param layout
142      */

143     private void cacheLayouts(Map JavaDoc layoutMap, Layout layout) {
144         if (layout != null) {
145             if (layout.getId() != null) {
146                 String JavaDoc layoutId = layout.getId();
147                 layoutMap.put(layoutId, layout);
148             }
149             if (layout instanceof CompositeLayout) {
150                 // step through all it's child layouts and cache them too
151
CompositeLayout cl = (CompositeLayout) layout;
152                 Iterator JavaDoc i = cl.getItems().iterator();
153                 while (i.hasNext()) {
154                     Item current = (Item) i.next();
155                     this.cacheLayouts(layoutMap, current.getLayout());
156                 }
157             }
158         }
159
160     }
161
162     private CopletDataManager getCopletDataManager(PortalService service)
163     throws Exception JavaDoc {
164         final String JavaDoc portalName = service.getPortalName();
165         // ensure that profile is loaded
166
this.getCopletInstanceDataManager(service);
167         return (CopletDataManager)this.copletDataManagers.get(portalName);
168     }
169     
170     private CopletInstanceDataManager getCopletInstanceDataManager(PortalService service)
171     throws Exception JavaDoc {
172         String JavaDoc portalName = service.getPortalName();
173         CopletInstanceDataManager copletInstanceDataManager =
174             (CopletInstanceDataManager) this.copletInstanceDataManagers.get(portalName);
175         if (copletInstanceDataManager != null) {
176             return copletInstanceDataManager;
177         }
178         
179         ProfileLS adapter = null;
180         try {
181             adapter = (ProfileLS) this.manager.lookup(ProfileLS.ROLE);
182
183             Map JavaDoc parameters = new HashMap JavaDoc();
184             parameters.put("profiletype", "copletbasedata");
185             parameters.put("objectmap", null);
186
187             Map JavaDoc map = new LinkedMap();
188             map.put("base", this.profilesPath);
189             map.put("portalname", service.getPortalName());
190             map.put("profile", "coplet");
191             map.put("name", "basedata");
192             CopletBaseDataManager copletBaseDataManager = (CopletBaseDataManager) adapter.loadProfile(map, parameters);
193
194             //CopletData
195
parameters.clear();
196             parameters.put("profiletype", "copletdata");
197             parameters.put("objectmap", copletBaseDataManager.getCopletBaseData());
198
199             map.clear();
200             map.put("base", this.profilesPath);
201             map.put("portalname", service.getPortalName());
202             map.put("profile", "coplet");
203             map.put("name", "data");
204             CopletDataManager copletDataManager = (CopletDataManager) adapter.loadProfile(map, parameters);
205
206             //CopletInstanceData
207
parameters.clear();
208             parameters.put("profiletype", "copletinstancedata");
209             parameters.put("objectmap", copletDataManager.getCopletData());
210
211             map.clear();
212             map.put("base", this.profilesPath);
213             map.put("portalname", service.getPortalName());
214             map.put("profile", "coplet");
215             map.put("name", "instancedata");
216             copletInstanceDataManager = (CopletInstanceDataManager) adapter.loadProfile(map, parameters);
217
218             CopletFactory copletFactory = service.getComponentManager().getCopletFactory();
219             Iterator JavaDoc iterator = copletDataManager.getCopletData().values().iterator();
220             while (iterator.hasNext()) {
221                 CopletData cd = (CopletData) iterator.next();
222                 copletFactory.prepare(cd);
223             }
224             iterator = copletInstanceDataManager.getCopletInstanceData().values().iterator();
225             while (iterator.hasNext()){
226                 CopletInstanceData cid = (CopletInstanceData) iterator.next();
227                 copletFactory.prepare(cid);
228             }
229
230             // store managers
231
this.copletInstanceDataManagers.put(portalName, copletInstanceDataManager);
232             this.copletDataManagers.put(portalName, copletDataManager);
233             return copletInstanceDataManager;
234             
235         } finally {
236             this.manager.release(service);
237             this.manager.release(adapter);
238         }
239     }
240
241     /* (non-Javadoc)
242      * @see org.apache.cocoon.portal.profile.ProfileManager#getCopletInstanceData(java.lang.String)
243      */

244     public CopletInstanceData getCopletInstanceData(String JavaDoc copletID) {
245         PortalService service = null;
246         try {
247             service = (PortalService) this.manager.lookup(PortalService.ROLE);
248             return getCopletInstanceDataManager(service).getCopletInstanceData(copletID);
249         } catch (Exception JavaDoc e) {
250             throw new CascadingRuntimeException("Error in getCopletInstanceData", e);
251         } finally {
252             this.manager.release(service);
253         }
254     }
255
256     /* (non-Javadoc)
257      * @see org.apache.cocoon.portal.profile.ProfileManager#getCopletData(java.lang.String)
258      */

259     public CopletData getCopletData(String JavaDoc copletDataId) {
260         PortalService service = null;
261         try {
262             service = (PortalService) this.manager.lookup(PortalService.ROLE);
263
264             Iterator JavaDoc i = getCopletInstanceDataManager(service).getCopletInstanceData().values().iterator();
265             boolean found = false;
266             CopletInstanceData current = null;
267             while ( !found && i.hasNext() ) {
268                 current = (CopletInstanceData)i.next();
269                 found = current.getCopletData().getId().equals(copletDataId);
270             }
271             if ( found ) {
272                 return current.getCopletData();
273             }
274             return null;
275         } catch (Exception JavaDoc e) {
276             throw new CascadingRuntimeException("Unable to lookup portal service.", e);
277         } finally {
278             this.manager.release(service);
279         }
280     }
281
282     /* (non-Javadoc)
283      * @see org.apache.cocoon.portal.profile.ProfileManager#getCopletInstanceData(org.apache.cocoon.portal.coplet.CopletData)
284      */

285     public List JavaDoc getCopletInstanceData(CopletData data) {
286         List JavaDoc coplets = new ArrayList JavaDoc();
287         PortalService service = null;
288         try {
289             service = (PortalService) this.manager.lookup(PortalService.ROLE);
290             Iterator JavaDoc iter = getCopletInstanceDataManager(service).getCopletInstanceData().values().iterator();
291             while (iter.hasNext()){
292                 CopletInstanceData current = (CopletInstanceData) iter.next();
293                 if (current.getCopletData().equals(data)) {
294                     coplets.add(current);
295                 }
296             }
297             return coplets;
298         } catch (Exception JavaDoc e) {
299             throw new CascadingRuntimeException("Error in getCopletInstanceData", e);
300         } finally {
301             this.manager.release(service);
302         }
303     }
304
305     /* (non-Javadoc)
306      * @see org.apache.cocoon.portal.profile.ProfileManager#register(org.apache.cocoon.portal.coplet.CopletInstanceData)
307      */

308     public void register(CopletInstanceData coplet) {
309         // nothing to do
310
}
311
312     /* (non-Javadoc)
313      * @see org.apache.cocoon.portal.profile.ProfileManager#unregister(org.apache.cocoon.portal.coplet.CopletInstanceData)
314      */

315     public void unregister(CopletInstanceData coplet) {
316         // nothing to do
317
}
318
319     /* (non-Javadoc)
320      * @see org.apache.cocoon.portal.profile.ProfileManager#register(org.apache.cocoon.portal.layout.Layout)
321      */

322     public void register(Layout layout) {
323         // nothing to do
324
}
325
326     /* (non-Javadoc)
327      * @see org.apache.cocoon.portal.profile.ProfileManager#unregister(org.apache.cocoon.portal.layout.Layout)
328      */

329     public void unregister(Layout layout) {
330         // nothing to do
331
}
332
333     /* (non-Javadoc)
334      * @see org.apache.avalon.framework.configuration.Configurable#configure(org.apache.avalon.framework.configuration.Configuration)
335      */

336     public void configure(Configuration configuration)
337     throws ConfigurationException {
338         Configuration child = configuration.getChild("profiles-path");
339         this.profilesPath = child.getValue("cocoon:/profiles");
340     }
341         
342     /* (non-Javadoc)
343      * @see org.apache.cocoon.portal.profile.ProfileManager#getCopletDatas()
344      */

345     public Collection JavaDoc getCopletDatas() {
346         PortalService service = null;
347         try {
348             service = (PortalService) this.manager.lookup(PortalService.ROLE);
349             return this.getCopletDataManager(service).getCopletData().values();
350         } catch (Exception JavaDoc e) {
351             throw new CascadingRuntimeException("Error in getCopletDatas.", e);
352         } finally {
353             this.manager.release(service);
354         }
355     }
356
357     /* (non-Javadoc)
358      * @see org.apache.cocoon.portal.profile.ProfileManager#getCopletInstanceDatas()
359      */

360     public Collection JavaDoc getCopletInstanceDatas() {
361         PortalService service = null;
362         try {
363             service = (PortalService) this.manager.lookup(PortalService.ROLE);
364             return this.getCopletInstanceDataManager(service).getCopletInstanceData().values();
365         } catch (Exception JavaDoc e) {
366             throw new CascadingRuntimeException("Error in getCopletInstanceDatas.", e);
367         } finally {
368             this.manager.release(service);
369         }
370     }
371     
372     
373     /* (non-Javadoc)
374      * @see org.apache.cocoon.portal.profile.ProfileManager#storeProfile(org.apache.cocoon.portal.layout.Layout, java.lang.String)
375      */

376     public void storeProfile(Layout rootLayout, String JavaDoc layoutKey) {
377         throw new RuntimeException JavaDoc("The static profile manager does not support the storeProfile() method.");
378     }
379     
380     /* (non-Javadoc)
381      * @see org.apache.cocoon.portal.profile.ProfileManager#getUser()
382      */

383     public PortalUser getUser() {
384         return this.portalUser;
385     }
386     
387     protected static final class StaticPortalUser implements PortalUser, Serializable JavaDoc {
388
389         /* (non-Javadoc)
390          * @see org.apache.cocoon.portal.profile.PortalUser#getGroup()
391          */

392         public String JavaDoc getGroup() {
393             return null;
394         }
395
396         /* (non-Javadoc)
397          * @see org.apache.cocoon.portal.profile.PortalUser#getUserName()
398          */

399         public String JavaDoc getUserName() {
400             return "static";
401         }
402
403         /* (non-Javadoc)
404          * @see org.apache.cocoon.portal.profile.PortalUser#isUserInRole(java.lang.String)
405          */

406         public boolean isUserInRole(String JavaDoc role) {
407             return false;
408         }
409     }
410 }
411
Popular Tags