KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > portal > pluto > services > factory > FactoryManagerServiceImpl


1 /*
2  * Copyright 2004,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.pluto.services.factory;
17
18 import java.util.ArrayList JavaDoc;
19 import java.util.HashMap JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.Map JavaDoc;
23
24 import javax.portlet.ActionRequest;
25 import javax.portlet.ActionResponse;
26 import javax.portlet.PortalContext;
27 import javax.portlet.PortletConfig;
28 import javax.portlet.PortletContext;
29 import javax.portlet.PortletPreferences;
30 import javax.portlet.PortletSession;
31 import javax.portlet.PortletURL;
32 import javax.portlet.RenderRequest;
33 import javax.portlet.RenderResponse;
34 import javax.servlet.ServletConfig JavaDoc;
35
36 import org.apache.avalon.framework.activity.Disposable;
37 import org.apache.avalon.framework.activity.Initializable;
38 import org.apache.avalon.framework.container.ContainerUtil;
39 import org.apache.avalon.framework.context.Context;
40 import org.apache.avalon.framework.context.ContextException;
41 import org.apache.avalon.framework.context.Contextualizable;
42 import org.apache.avalon.framework.logger.AbstractLogEnabled;
43 import org.apache.avalon.framework.service.ServiceException;
44 import org.apache.avalon.framework.service.ServiceManager;
45 import org.apache.avalon.framework.service.Serviceable;
46 import org.apache.cocoon.portal.pluto.factory.ControllerFactoryImpl;
47 import org.apache.cocoon.portal.pluto.factory.ObjectIDFactoryImpl;
48 import org.apache.cocoon.portal.pluto.factory.PortletInvokerFactoryImpl;
49 import org.apache.cocoon.portal.pluto.factory.PortletPreferencesFactoryImpl;
50 import org.apache.cocoon.servlet.CocoonServlet;
51 import org.apache.pluto.factory.Factory;
52 import org.apache.pluto.factory.ObjectIDFactory;
53 import org.apache.pluto.factory.impl.ActionRequestFactoryImpl;
54 import org.apache.pluto.factory.impl.ActionResponseFactoryImpl;
55 import org.apache.pluto.factory.impl.PortalContextFactoryImpl;
56 import org.apache.pluto.factory.impl.PortletConfigFactoryImpl;
57 import org.apache.pluto.factory.impl.PortletContextFactoryImpl;
58 import org.apache.pluto.factory.impl.PortletSessionFactoryImpl;
59 import org.apache.pluto.factory.impl.PortletURLFactoryImpl;
60 import org.apache.pluto.factory.impl.RenderRequestFactoryImpl;
61 import org.apache.pluto.factory.impl.RenderResponseFactoryImpl;
62 import org.apache.pluto.invoker.PortletInvoker;
63 import org.apache.pluto.om.ControllerFactory;
64 import org.apache.pluto.services.factory.FactoryManagerService;
65 import org.apache.pluto.util.NamespaceMapper;
66 import org.apache.pluto.util.impl.NamespaceMapperFactoryImpl;
67
68 /**
69  * Manages the life-time of factories registered during container startup.
70  * A service has to derive from {@link Factory} and implement the
71  * <CODE>init()</CODE> and <CODE>destroy()</CODE> methods as appropriate.
72  *
73  * TODO This is a very lazy implementation, we need to improve it
74  *
75  * @see Factory
76  * @author <a HREF="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
77  *
78  * @version CVS $Id: FactoryManagerServiceImpl.java 123825 2004-12-31 21:18:01Z antonio $
79  */

80 public class FactoryManagerServiceImpl
81 extends AbstractLogEnabled
82 implements FactoryManagerService, Initializable, Contextualizable, Serviceable, Disposable {
83
84     /** The servlet configuration */
85     protected ServletConfig JavaDoc servletConfig;
86     
87     /** The avalon context */
88     protected Context context;
89     
90     /** The service manager */
91     protected ServiceManager manager;
92     
93     /* (non-Javadoc)
94      * @see org.apache.avalon.framework.context.Contextualizable#contextualize(org.apache.avalon.framework.context.Context)
95      */

96     public void contextualize(Context context) throws ContextException {
97         this.context = context;
98         this.servletConfig = (ServletConfig JavaDoc) context.get(CocoonServlet.CONTEXT_SERVLET_CONFIG);
99     }
100     
101     /* (non-Javadoc)
102      * @see org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager)
103      */

104     public void service(ServiceManager manager) throws ServiceException {
105         this.manager = manager;
106     }
107
108     /* (non-Javadoc)
109      * @see org.apache.avalon.framework.activity.Initializable#initialize()
110      */

111     public void initialize()
112     throws Exception JavaDoc {
113         final Map JavaDoc factories = new HashMap JavaDoc();
114
115         factories.put(ActionRequest.class.getName(), ActionRequestFactoryImpl.class.getName());
116         factories.put(RenderRequest.class.getName(), RenderRequestFactoryImpl.class.getName());
117         factories.put(RenderResponse.class.getName(), RenderResponseFactoryImpl.class.getName());
118         factories.put(PortletSession.class.getName(), PortletSessionFactoryImpl.class.getName());
119         factories.put(PortletConfig.class.getName(), PortletConfigFactoryImpl.class.getName());
120         factories.put(PortletContext.class.getName(), PortletContextFactoryImpl.class.getName());
121         factories.put(PortletPreferences.class.getName(), PortletPreferencesFactoryImpl.class.getName());
122         factories.put(PortalContext.class.getName(), PortalContextFactoryImpl.class.getName());
123         factories.put(ActionResponse.class.getName(), ActionResponseFactoryImpl.class.getName());
124         factories.put(PortletURL.class.getName(), PortletURLFactoryImpl.class.getName());
125         factories.put(PortletPreferences.class.getName(), PortletPreferencesFactoryImpl.class.getName());
126
127         factories.put(PortletInvoker.class.getName(), PortletInvokerFactoryImpl.class.getName());
128
129         factories.put(NamespaceMapper.class.getName(), NamespaceMapperFactoryImpl.class.getName());
130
131         factories.put(ObjectIDFactory.class.getName(), ObjectIDFactoryImpl.class.getName());
132
133         factories.put(ControllerFactory.class.getName(), ControllerFactoryImpl.class.getName());
134   
135         for (Iterator JavaDoc iter = factories.entrySet().iterator(); iter.hasNext (); ) {
136             Map.Entry JavaDoc me = (Map.Entry JavaDoc)iter.next();
137             String JavaDoc factoryInterfaceName = (String JavaDoc)me.getKey();
138             // try to get hold of the factory
139
Class JavaDoc factoryInterface;
140
141             factoryInterface = Class.forName (factoryInterfaceName);
142
143             String JavaDoc factoryImplName = (String JavaDoc)me.getValue();
144             Class JavaDoc factoryImpl = Class.forName (factoryImplName);
145             Factory factory = (Factory) factoryImpl.newInstance ();
146
147             ContainerUtil.enableLogging(factory, this.getLogger());
148             ContainerUtil.contextualize(factory, this.context);
149             ContainerUtil.service(factory, this.manager);
150             ContainerUtil.initialize(factory);
151             
152             factory.init(this.servletConfig, new HashMap JavaDoc());
153
154             factoryMap.put (factoryInterface.getName(), factory);
155
156             // build up list in reverse order for later destruction
157
factoryList.add (0, factory);
158             
159         }
160     }
161
162     /* (non-Javadoc)
163      * @see org.apache.avalon.framework.activity.Disposable#dispose()
164      */

165     public void dispose() {
166
167         // destroy the services in reverse order
168
for (Iterator JavaDoc iterator = factoryList.iterator (); iterator.hasNext (); ) {
169             Factory factory = (Factory) iterator.next ();
170
171             try {
172                 factory.destroy ();
173             } catch (Exception JavaDoc exc) {
174                 // ignore it
175
}
176         }
177
178         factoryList.clear();
179         factoryMap.clear();
180
181     }
182
183     /**
184      ** Returns the service implementation for the given service class, or
185      ** <CODE>null</CODE> if no such service is registered.
186      **
187      ** @param theClass the service class
188      **
189      ** @return the service implementation
190      **/

191     public Factory getFactory (Class JavaDoc theClass) {
192         // at this state the services map is read-only,
193
// therefore we can go without synchronization
194
return ((Factory) factoryMap.get (theClass.getName()));
195     }
196
197     private Map JavaDoc factoryMap = new HashMap JavaDoc();
198     private List JavaDoc factoryList = new ArrayList JavaDoc();
199
200 }
201
Popular Tags