KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.ArrayList JavaDoc;
19 import java.util.Collection JavaDoc;
20 import java.util.HashMap JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.util.List JavaDoc;
23 import java.util.Map JavaDoc;
24
25 import org.apache.avalon.framework.CascadingRuntimeException;
26 import org.apache.avalon.framework.service.ServiceException;
27 import org.apache.avalon.framework.service.ServiceSelector;
28 import org.apache.cocoon.ProcessingException;
29 import org.apache.cocoon.portal.PortalService;
30 import org.apache.cocoon.portal.coplet.CopletData;
31 import org.apache.cocoon.portal.coplet.CopletFactory;
32 import org.apache.cocoon.portal.coplet.CopletInstanceData;
33 import org.apache.cocoon.portal.coplet.adapter.CopletAdapter;
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
39 /**
40  * The profile manager using the authentication framework
41  *
42  * @author <a HREF="mailto:cziegeler@s-und-n.de">Carsten Ziegeler</a>
43  * @author <a HREF="mailto:bluetkemeier@s-und-n.de">Bj&ouml;rn L&uuml;tkemeier</a>
44  * @deprecated Use the {@link org.apache.cocoon.portal.profile.impl.GroupBasedProfileManager}
45  * @version CVS $Id: AbstractUserProfileManager.java 326434 2005-10-19 07:50:22Z cziegeler $
46  */

47 public abstract class AbstractUserProfileManager
48     extends AbstractProfileManager {
49
50     /* (non-Javadoc)
51      * @see org.apache.cocoon.portal.profile.ProfileManager#login()
52      */

53     public void login() {
54         super.login();
55         // TODO - we should move most of the stuff from getPortalLayout to here
56
// for now we use a hack :)
57
this.getPortalLayout(null, null);
58     }
59     
60     /* (non-Javadoc)
61      * @see org.apache.cocoon.portal.profile.ProfileManager#logout()
62      */

63     public void logout() {
64         PortalService service = null;
65         ServiceSelector adapterSelector = null;
66         try {
67             adapterSelector = (ServiceSelector)this.manager.lookup(CopletAdapter.ROLE+"Selector");
68             service = (PortalService)this.manager.lookup(PortalService.ROLE);
69             final String JavaDoc layoutKey = service.getDefaultLayoutKey();
70
71             CopletInstanceDataManager copletInstanceDataManager = (CopletInstanceDataManager)service.getAttribute("CopletInstanceData:"+layoutKey);
72             if ( copletInstanceDataManager != null ) {
73                 Iterator JavaDoc iter = copletInstanceDataManager.getCopletInstanceData().values().iterator();
74                 while ( iter.hasNext() ) {
75                     CopletInstanceData cid = (CopletInstanceData) iter.next();
76                     CopletAdapter adapter = null;
77                     try {
78                         adapter = (CopletAdapter)adapterSelector.select(cid.getCopletData().getCopletBaseData().getCopletAdapterName());
79                         adapter.logout( cid );
80                     } finally {
81                         adapterSelector.release( adapter );
82                     }
83                 }
84             }
85             
86             service.removeAttribute("CopletData:"+layoutKey);
87             service.removeAttribute("CopletInstanceData:"+layoutKey);
88             service.removeAttribute("Layout:"+layoutKey);
89         } catch (ServiceException e) {
90             throw new CascadingRuntimeException("Unable to lookup portal service.", e);
91         } finally {
92             this.manager.release(service);
93             this.manager.release(adapterSelector);
94         }
95         super.logout();
96     }
97        
98     /**
99      * @param layoutMap
100      * @param layout
101      */

102     protected void cacheLayouts(Map JavaDoc layoutMap, Layout layout) {
103         if ( layout != null ) {
104             if ( layout.getId() != null ) {
105                 layoutMap.put( layout.getId(), layout );
106             }
107             if ( layout instanceof CompositeLayout ) {
108                 CompositeLayout cl = (CompositeLayout)layout;
109                 Iterator JavaDoc i = cl.getItems().iterator();
110                 while ( i.hasNext() ) {
111                     Item current = (Item)i.next();
112                     this.cacheLayouts( layoutMap, current.getLayout() );
113                 }
114             }
115         }
116         
117     }
118
119     /**
120      * Prepares the object by using the specified factory.
121      */

122     protected void prepareObject(Object JavaDoc object, Object JavaDoc factory)
123     throws ProcessingException {
124         if (factory != null && object != null) {
125             if (object instanceof Layout) {
126                 ((LayoutFactory)factory).prepareLayout((Layout)object);
127             } else if (object instanceof CopletDataManager) {
128                 CopletFactory copletFactory = (CopletFactory)factory;
129                 Iterator JavaDoc iterator = ((CopletDataManager)object).getCopletData().values().iterator();
130                 while (iterator.hasNext()) {
131                     CopletData cd = (CopletData)iterator.next();
132                     copletFactory.prepare(cd);
133                 }
134             } else if (object instanceof CopletInstanceDataManager) {
135                 CopletFactory copletFactory = (CopletFactory)factory;
136                 Iterator JavaDoc iterator = ((CopletInstanceDataManager)object).getCopletInstanceData().values().iterator();
137                 while (iterator.hasNext()) {
138                     CopletInstanceData cid = (CopletInstanceData)iterator.next();
139                     copletFactory.prepare(cid);
140                 }
141             }
142         }
143     }
144     
145     /* (non-Javadoc)
146      * @see org.apache.cocoon.portal.profile.ProfileManager#getCopletInstanceData(java.lang.String)
147      */

148     public CopletInstanceData getCopletInstanceData(String JavaDoc copletID) {
149         PortalService service = null;
150         String JavaDoc attribute = null;
151         try {
152             service = (PortalService) this.manager.lookup(PortalService.ROLE);
153             final String JavaDoc layoutKey = service.getDefaultLayoutKey();
154
155             attribute = "CopletInstanceData:"+layoutKey;
156             CopletInstanceDataManager copletInstanceDataManager = (CopletInstanceDataManager)service.getAttribute(attribute);
157
158             return copletInstanceDataManager.getCopletInstanceData(copletID);
159         } catch (ServiceException e) {
160             throw new CascadingRuntimeException("Unable to lookup portal service.", e);
161         } finally {
162             this.manager.release(service);
163         }
164     }
165
166     /* (non-Javadoc)
167      * @see org.apache.cocoon.portal.profile.ProfileManager#getCopletData(java.lang.String)
168      */

169     public CopletData getCopletData(String JavaDoc copletDataId) {
170         PortalService service = null;
171         String JavaDoc attribute = null;
172         try {
173             service = (PortalService) this.manager.lookup(PortalService.ROLE);
174             final String JavaDoc layoutKey = service.getDefaultLayoutKey();
175
176             attribute = "CopletInstanceData:"+layoutKey;
177             CopletInstanceDataManager copletInstanceDataManager = (CopletInstanceDataManager)service.getAttribute(attribute);
178
179             Iterator JavaDoc i = copletInstanceDataManager.getCopletInstanceData().values().iterator();
180             boolean found = false;
181             CopletInstanceData current = null;
182             while ( !found && i.hasNext() ) {
183                 current = (CopletInstanceData)i.next();
184                 found = current.getCopletData().getId().equals(copletDataId);
185             }
186             if ( found ) {
187                 return current.getCopletData();
188             }
189             return null;
190         } catch (ServiceException e) {
191             throw new CascadingRuntimeException("Unable to lookup portal service.", e);
192         } finally {
193             this.manager.release(service);
194         }
195     }
196
197     /* (non-Javadoc)
198      * @see org.apache.cocoon.portal.profile.ProfileManager#getCopletInstanceData(org.apache.cocoon.portal.coplet.CopletData)
199      */

200     public List JavaDoc getCopletInstanceData(CopletData data) {
201         List JavaDoc coplets = new ArrayList JavaDoc();
202         PortalService service = null;
203         String JavaDoc attribute = null;
204         try {
205             service = (PortalService) this.manager.lookup(PortalService.ROLE);
206             final String JavaDoc layoutKey = service.getDefaultLayoutKey();
207
208             attribute = "CopletInstanceData:" + layoutKey;
209             CopletInstanceDataManager copletInstanceDataManager = (CopletInstanceDataManager)service.getAttribute(attribute);
210
211             Iterator JavaDoc iter = copletInstanceDataManager.getCopletInstanceData().values().iterator();
212             while ( iter.hasNext() ) {
213                 CopletInstanceData current = (CopletInstanceData)iter.next();
214                 if ( current.getCopletData().equals(data) ) {
215                     coplets.add( current );
216                 }
217             }
218             return coplets;
219         } catch (ServiceException e) {
220             throw new CascadingRuntimeException("Unable to lookup portal service.", e);
221         } finally {
222             this.manager.release(service);
223         }
224     }
225
226     /* (non-Javadoc)
227      * @see org.apache.cocoon.portal.profile.ProfileManager#register(org.apache.cocoon.portal.coplet.CopletInstanceData)
228      */

229     public void register(CopletInstanceData coplet) {
230         PortalService service = null;
231         String JavaDoc attribute = null;
232         try {
233             service = (PortalService) this.manager.lookup(PortalService.ROLE);
234             final String JavaDoc layoutKey = service.getDefaultLayoutKey();
235
236             attribute = "CopletInstanceData:" + layoutKey;
237             CopletInstanceDataManager copletInstanceDataManager = (CopletInstanceDataManager)service.getAttribute(attribute);
238             
239             copletInstanceDataManager.putCopletInstanceData( coplet );
240             
241         } catch (ServiceException e) {
242             throw new CascadingRuntimeException("Unable to lookup portal service.", e);
243         } finally {
244             this.manager.release(service);
245         }
246     }
247     
248     /* (non-Javadoc)
249      * @see org.apache.cocoon.portal.profile.ProfileManager#unregister(org.apache.cocoon.portal.coplet.CopletInstanceData)
250      */

251     public void unregister(CopletInstanceData coplet) {
252         PortalService service = null;
253         String JavaDoc attribute = null;
254         try {
255             service = (PortalService) this.manager.lookup(PortalService.ROLE);
256             final String JavaDoc layoutKey = service.getDefaultLayoutKey();
257
258             attribute = "CopletInstanceData:" + layoutKey;
259             CopletInstanceDataManager copletInstanceDataManager = (CopletInstanceDataManager)service.getAttribute(attribute);
260             
261             copletInstanceDataManager.getCopletInstanceData().remove(coplet.getId());
262             
263         } catch (ServiceException e) {
264             throw new CascadingRuntimeException("Unable to lookup portal service.", e);
265         } finally {
266             this.manager.release(service);
267         }
268     }
269
270     /* (non-Javadoc)
271      * @see org.apache.cocoon.portal.profile.ProfileManager#register(org.apache.cocoon.portal.layout.Layout)
272      */

273     public void register(Layout layout) {
274         PortalService service = null;
275         try {
276             service = (PortalService) this.manager.lookup(PortalService.ROLE);
277             final String JavaDoc layoutKey = service.getDefaultLayoutKey();
278
279             Map JavaDoc layoutMap = (Map JavaDoc)service.getAttribute("Layout-Map:" + layoutKey);
280             if ( layoutMap == null ) {
281                 layout = (Layout)service.getAttribute("Layout:" + layoutKey);
282                 if (layout != null) {
283                     layoutMap = new HashMap JavaDoc();
284                     this.cacheLayouts(layoutMap, layout);
285                     service.setAttribute("Layout-Map:" + layoutKey, layoutMap);
286                 }
287             }
288             
289             if ( layoutMap != null) {
290                 layoutMap.put(layout.getId(), layout);
291             }
292             
293         } catch (ServiceException e) {
294             throw new CascadingRuntimeException("Unable to lookup portal service.", e);
295         } finally {
296             this.manager.release(service);
297         }
298     }
299     
300     /* (non-Javadoc)
301      * @see org.apache.cocoon.portal.profile.ProfileManager#unregister(org.apache.cocoon.portal.layout.Layout)
302      */

303     public void unregister(Layout layout) {
304         PortalService service = null;
305         try {
306             service = (PortalService) this.manager.lookup(PortalService.ROLE);
307             final String JavaDoc layoutKey = service.getDefaultLayoutKey();
308
309             Map JavaDoc layoutMap = (Map JavaDoc)service.getAttribute("Layout-Map:" + layoutKey);
310             
311             if ( layoutMap != null) {
312                 layoutMap.remove(layout.getId());
313             }
314             
315         } catch (ServiceException e) {
316             throw new CascadingRuntimeException("Unable to lookup portal service.", e);
317         } finally {
318             this.manager.release(service);
319         }
320     }
321
322     /* (non-Javadoc)
323      * @see org.apache.cocoon.portal.profile.ProfileManager#getPortalLayout(java.lang.String, java.lang.String)
324      */

325     public Layout getPortalLayout(String JavaDoc layoutKey, String JavaDoc layoutID) {
326         PortalService service = null;
327         ServiceSelector adapterSelector = null;
328         
329         try {
330             service = (PortalService) this.manager.lookup(PortalService.ROLE);
331             LayoutFactory factory = service.getComponentManager().getLayoutFactory();
332             CopletFactory copletFactory = service.getComponentManager().getCopletFactory();
333             
334             adapterSelector = (ServiceSelector)this.manager.lookup(CopletAdapter.ROLE+"Selector");
335             
336             if ( null == layoutKey ) {
337                 layoutKey = service.getDefaultLayoutKey();
338             }
339             
340             final String JavaDoc layoutAttributeKey = "Layout:" + layoutKey;
341             final String JavaDoc layoutObjectsAttributeKey = "Layout-Map:" + layoutKey;
342             
343             Layout layout = (Layout)service.getAttribute(layoutAttributeKey);
344             if (layout == null) {
345                 layout = this.loadProfile(layoutKey, service, copletFactory, factory, adapterSelector);
346             }
347             
348             if ( layoutID != null ) {
349                 // now search for a layout
350
Map JavaDoc layoutMap = (Map JavaDoc)service.getAttribute(layoutObjectsAttributeKey);
351                 if ( layoutMap == null ) {
352                     layoutMap = new HashMap JavaDoc();
353                     this.cacheLayouts(layoutMap, layout);
354                     service.setAttribute(layoutObjectsAttributeKey, layoutMap);
355                 }
356                 if ( layoutMap != null) {
357                     return (Layout) layoutMap.get( layoutID );
358                 }
359             }
360             
361             return layout;
362         } catch (Exception JavaDoc ce) {
363             throw new CascadingRuntimeException("Exception during loading of profile.", ce);
364         } finally {
365             this.manager.release(service);
366             this.manager.release(adapterSelector);
367         }
368     }
369     
370     /**
371      * This loads a new profile
372      */

373     protected abstract Layout loadProfile(String JavaDoc layoutKey,
374                                             PortalService service,
375                                             CopletFactory copletFactory,
376                                             LayoutFactory layoutFactory,
377                                             ServiceSelector adapterSelector)
378     throws Exception JavaDoc;
379
380     /* (non-Javadoc)
381      * @see org.apache.cocoon.portal.profile.ProfileManager#getCopletDatas()
382      */

383     public Collection JavaDoc getCopletDatas() {
384         PortalService service = null;
385         try {
386             service = (PortalService) this.manager.lookup(PortalService.ROLE);
387             final String JavaDoc layoutKey = service.getDefaultLayoutKey();
388             CopletDataManager manager = (CopletDataManager)service.getAttribute("CopletData:" + layoutKey);
389             return manager.getCopletData().values();
390         } catch (Exception JavaDoc e) {
391             throw new CascadingRuntimeException("Error in getCopletDatas.", e);
392         } finally {
393             this.manager.release(service);
394         }
395     }
396
397     /* (non-Javadoc)
398      * @see org.apache.cocoon.portal.profile.ProfileManager#getCopletInstanceDatas()
399      */

400     public Collection JavaDoc getCopletInstanceDatas() {
401         PortalService service = null;
402         try {
403             service = (PortalService) this.manager.lookup(PortalService.ROLE);
404             final String JavaDoc layoutKey = service.getDefaultLayoutKey();
405             CopletInstanceDataManager manager = (CopletInstanceDataManager)service.getAttribute("CopletInstanceData:" + layoutKey);
406             return manager.getCopletInstanceData().values();
407         } catch (Exception JavaDoc e) {
408             throw new CascadingRuntimeException("Error in getCopletInstanceDatas.", e);
409         } finally {
410             this.manager.release(service);
411         }
412     }
413     
414     /* (non-Javadoc)
415      * @see org.apache.cocoon.portal.profile.ProfileManager#storeProfile(org.apache.cocoon.portal.layout.Layout, java.lang.String)
416      */

417     public void storeProfile(Layout rootLayout, String JavaDoc layoutKey) {
418         PortalService service = null;
419         
420         try {
421             service = (PortalService) this.manager.lookup(PortalService.ROLE);
422             if ( null == layoutKey ) {
423                 layoutKey = service.getDefaultLayoutKey();
424             }
425
426             final String JavaDoc layoutAttributeKey = "Layout:" + layoutKey;
427             
428             service.setAttribute(layoutAttributeKey, rootLayout);
429         } catch (Exception JavaDoc ce) {
430             throw new CascadingRuntimeException("Exception during loading of profile.", ce);
431         } finally {
432             this.manager.release(service);
433         }
434     }
435 }
436
Popular Tags