KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > portal > impl > DefaultPortalComponentManager


1 /*
2  * Copyright 1999-2002,2004-2005 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.impl;
17
18 import java.util.HashMap JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.Map JavaDoc;
21
22 import org.apache.avalon.framework.CascadingRuntimeException;
23 import org.apache.avalon.framework.activity.Disposable;
24 import org.apache.avalon.framework.configuration.Configurable;
25 import org.apache.avalon.framework.configuration.Configuration;
26 import org.apache.avalon.framework.configuration.ConfigurationException;
27 import org.apache.avalon.framework.context.Context;
28 import org.apache.avalon.framework.logger.AbstractLogEnabled;
29 import org.apache.avalon.framework.service.ServiceException;
30 import org.apache.avalon.framework.service.ServiceManager;
31 import org.apache.avalon.framework.service.ServiceSelector;
32 import org.apache.avalon.framework.service.Serviceable;
33 import org.apache.avalon.framework.thread.ThreadSafe;
34 import org.apache.cocoon.portal.LinkService;
35 import org.apache.cocoon.portal.PortalComponentManager;
36 import org.apache.cocoon.portal.PortalManager;
37 import org.apache.cocoon.portal.PortalService;
38 import org.apache.cocoon.portal.coplet.CopletFactory;
39 import org.apache.cocoon.portal.event.EventManager;
40 import org.apache.cocoon.portal.layout.LayoutFactory;
41 import org.apache.cocoon.portal.layout.renderer.Renderer;
42 import org.apache.cocoon.portal.profile.ProfileManager;
43
44 /**
45  * Default {@link PortalComponentManager} implementation
46  *
47  * @see org.apache.cocoon.portal.PortalComponentManager
48  *
49  * TODO Handle non ThreadSafe components
50  *
51  * @author <a HREF="mailto:cziegeler@s-und-n.de">Carsten Ziegeler</a>
52  *
53  * @version CVS $Id: DefaultPortalComponentManager.java 230796 2005-08-08 12:54:55Z cziegeler $
54  */

55 public class DefaultPortalComponentManager
56     extends AbstractLogEnabled
57     implements PortalComponentManager, Serviceable, Disposable, ThreadSafe, Configurable {
58
59     /** The avalon component manager */
60     protected ServiceManager manager;
61
62     /** The portal service */
63     protected final PortalService portalService;
64
65     protected String JavaDoc profileManagerRole;
66     protected ProfileManager profileManager;
67
68     protected String JavaDoc linkServiceRole;
69     protected LinkService linkService;
70
71     protected String JavaDoc rendererSelectorRole;
72     protected ServiceSelector rendererSelector;
73
74     protected Map JavaDoc renderers;
75
76     protected String JavaDoc copletFactoryRole;
77     protected CopletFactory copletFactory;
78
79     protected String JavaDoc layoutFactoryRole;
80     protected LayoutFactory layoutFactory;
81
82     protected String JavaDoc eventManagerRole;
83     protected EventManager eventManager;
84
85     protected String JavaDoc portalManagerRole;
86     protected PortalManager portalManager;
87
88     protected final Context context;
89
90     /**
91      * Create a new portal component manager. Each portal has a own
92      * component manager that manages all central components for this
93      * portal.
94      * This implementation stores the portal service (a global singleton)
95      * to pass it to the other components (TODO).
96      * @param service The portal service.
97      */

98     public DefaultPortalComponentManager(final PortalService service, Context context) {
99         this.portalService = service;
100         this.context = context;
101     }
102
103     /* (non-Javadoc)
104      * @see org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager)
105      */

106     public void service(ServiceManager manager) throws ServiceException {
107         this.manager = manager;
108     }
109
110     /* (non-Javadoc)
111      * @see org.apache.cocoon.portal.PortalComponentManager#getLinkService()
112      */

113     public LinkService getLinkService() {
114         if ( null == this.linkService ) {
115             try {
116                 this.linkService = (LinkService)this.manager.lookup( this.linkServiceRole );
117             } catch (ServiceException e) {
118                 throw new CascadingRuntimeException("Unable to lookup link service with role " + this.linkServiceRole, e);
119             }
120         }
121         return this.linkService;
122     }
123
124     /* (non-Javadoc)
125      * @see org.apache.cocoon.portal.PortalComponentManager#getProfileManager()
126      */

127     public ProfileManager getProfileManager() {
128         if ( null == this.profileManager ) {
129             try {
130                 this.profileManager = (ProfileManager)this.manager.lookup( this.profileManagerRole );
131             } catch (ServiceException e) {
132                 throw new CascadingRuntimeException("Unable to lookup profile manager with role " + this.profileManagerRole, e);
133             }
134         }
135         return this.profileManager;
136     }
137
138     /* (non-Javadoc)
139      * @see org.apache.cocoon.portal.PortalComponentManager#getEventManager()
140      */

141     public EventManager getEventManager() {
142         if ( null == this.eventManager ) {
143             try {
144                 this.eventManager = (EventManager)this.manager.lookup( this.eventManagerRole );
145             } catch (ServiceException e) {
146                 throw new CascadingRuntimeException("Unable to lookup event manager with role " + this.eventManagerRole, e);
147             }
148         }
149         return this.eventManager;
150     }
151
152     /* (non-Javadoc)
153      * @see org.apache.avalon.framework.activity.Disposable#dispose()
154      */

155     public void dispose() {
156         if (this.manager != null) {
157             if (this.rendererSelector != null) {
158                 Iterator JavaDoc i = this.renderers.values().iterator();
159                 while (i.hasNext()) {
160                     this.rendererSelector.release(i.next());
161                 }
162                 this.manager.release(this.rendererSelector);
163                 this.rendererSelector = null;
164                 this.renderers = null;
165             }
166             this.manager.release(this.profileManager);
167             this.profileManager = null;
168             this.manager.release(this.linkService);
169             this.linkService = null;
170             this.manager.release(this.copletFactory);
171             this.copletFactory = null;
172             this.manager.release(this.layoutFactory);
173             this.layoutFactory = null;
174             this.manager.release(this.eventManager);
175             this.eventManager = null;
176             this.manager.release(this.portalManager);
177             this.portalManager = null;
178             this.manager = null;
179         }
180     }
181
182     /* (non-Javadoc)
183      * @see org.apache.avalon.framework.configuration.Configurable#configure(org.apache.avalon.framework.configuration.Configuration)
184      */

185     public void configure(Configuration config) throws ConfigurationException {
186         this.profileManagerRole = config.getChild("profile-manager").getValue(ProfileManager.ROLE);
187         this.linkServiceRole = config.getChild("link-service").getValue(LinkService.ROLE);
188         this.rendererSelectorRole = config.getChild("renderer-selector").getValue(Renderer.ROLE+"Selector");
189         this.copletFactoryRole = config.getChild("coplet-factory").getValue(CopletFactory.ROLE);
190         this.layoutFactoryRole = config.getChild("layout-factory").getValue(LayoutFactory.ROLE);
191         this.eventManagerRole = config.getChild("event-manager").getValue(EventManager.ROLE);
192         this.portalManagerRole = config.getChild("portal-manager").getValue(PortalManager.ROLE);
193     }
194
195     /* (non-Javadoc)
196      * @see org.apache.cocoon.portal.PortalComponentManager#getRenderer(java.lang.String)
197      */

198     public Renderer getRenderer(String JavaDoc hint) {
199         if ( rendererSelector == null ) {
200             try {
201                 this.rendererSelector = (ServiceSelector)this.manager.lookup( this.rendererSelectorRole );
202             } catch (ServiceException e) {
203                 throw new CascadingRuntimeException("Unable to lookup renderer selector with role " + this.rendererSelectorRole, e);
204             }
205             this.renderers = new HashMap JavaDoc();
206         }
207         Renderer o = (Renderer) this.renderers.get( hint );
208         if ( o == null ) {
209             try {
210                 o = (Renderer) this.rendererSelector.select( hint );
211                 this.renderers.put( hint, o );
212             } catch (ServiceException e) {
213                 throw new CascadingRuntimeException("Unable to lookup renderer with hint " + hint, e);
214             }
215         }
216         return o;
217     }
218
219     /* (non-Javadoc)
220      * @see org.apache.cocoon.portal.PortalComponentManager#getCopletFactory()
221      */

222     public CopletFactory getCopletFactory() {
223         if ( null == this.copletFactory ) {
224             try {
225                 this.copletFactory = (CopletFactory)this.manager.lookup( this.copletFactoryRole);
226             } catch (ServiceException e) {
227                 throw new CascadingRuntimeException("Unable to lookup coplet factory with role " + this.copletFactoryRole, e);
228             }
229         }
230         return this.copletFactory;
231     }
232
233     /* (non-Javadoc)
234      * @see org.apache.cocoon.portal.PortalComponentManager#getLayoutFactory()
235      */

236     public LayoutFactory getLayoutFactory() {
237         if ( null == this.layoutFactory ) {
238             try {
239                 this.layoutFactory = (LayoutFactory)this.manager.lookup( this.layoutFactoryRole);
240             } catch (ServiceException e) {
241                 throw new CascadingRuntimeException("Unable to lookup layout factory with role " + this.layoutFactoryRole, e);
242             }
243         }
244         return this.layoutFactory;
245     }
246
247
248     /**
249      * @see org.apache.cocoon.portal.PortalComponentManager#getPortalManager()
250      */

251     public PortalManager getPortalManager() {
252         if ( null == this.portalManager ) {
253             try {
254                 this.portalManager = (PortalManager)this.manager.lookup( this.portalManagerRole);
255             } catch (ServiceException e) {
256                 throw new CascadingRuntimeException("Unable to lookup portal manager with role " + this.portalManagerRole, e);
257             }
258         }
259         return this.portalManager;
260     }
261
262     /**
263      * @see org.apache.cocoon.portal.PortalComponentManager#getComponentContext()
264      */

265     public Context getComponentContext() {
266         return this.context;
267     }
268 }
269
Popular Tags