001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.portal.pluto.services.factory;
018:
019: import java.util.ArrayList;
020: import java.util.HashMap;
021: import java.util.Iterator;
022: import java.util.List;
023: import java.util.Map;
024:
025: import javax.portlet.ActionRequest;
026: import javax.portlet.ActionResponse;
027: import javax.portlet.PortalContext;
028: import javax.portlet.PortletConfig;
029: import javax.portlet.PortletContext;
030: import javax.portlet.PortletPreferences;
031: import javax.portlet.PortletSession;
032: import javax.portlet.PortletURL;
033: import javax.portlet.RenderRequest;
034: import javax.portlet.RenderResponse;
035: import javax.servlet.ServletConfig;
036:
037: import org.apache.avalon.framework.activity.Disposable;
038: import org.apache.avalon.framework.activity.Initializable;
039: import org.apache.avalon.framework.container.ContainerUtil;
040: import org.apache.avalon.framework.context.Context;
041: import org.apache.avalon.framework.context.ContextException;
042: import org.apache.avalon.framework.context.Contextualizable;
043: import org.apache.avalon.framework.logger.AbstractLogEnabled;
044: import org.apache.avalon.framework.service.ServiceException;
045: import org.apache.avalon.framework.service.ServiceManager;
046: import org.apache.avalon.framework.service.Serviceable;
047: import org.apache.cocoon.portal.pluto.factory.ActionRequestFactoryImpl;
048: import org.apache.cocoon.portal.pluto.factory.ControllerFactoryImpl;
049: import org.apache.cocoon.portal.pluto.factory.ObjectIDFactoryImpl;
050: import org.apache.cocoon.portal.pluto.factory.PortletInvokerFactoryImpl;
051: import org.apache.cocoon.portal.pluto.factory.PortletPreferencesFactoryImpl;
052: import org.apache.cocoon.portal.pluto.factory.RenderRequestFactoryImpl;
053: import org.apache.cocoon.servlet.CocoonServlet;
054: import org.apache.pluto.factory.Factory;
055: import org.apache.pluto.factory.ObjectIDFactory;
056: import org.apache.pluto.factory.impl.ActionResponseFactoryImpl;
057: import org.apache.pluto.factory.impl.PortalContextFactoryImpl;
058: import org.apache.pluto.factory.impl.PortletConfigFactoryImpl;
059: import org.apache.pluto.factory.impl.PortletContextFactoryImpl;
060: import org.apache.pluto.factory.impl.PortletSessionFactoryImpl;
061: import org.apache.pluto.factory.impl.PortletURLFactoryImpl;
062: import org.apache.pluto.factory.impl.RenderResponseFactoryImpl;
063: import org.apache.pluto.invoker.PortletInvoker;
064: import org.apache.pluto.om.ControllerFactory;
065: import org.apache.pluto.services.factory.FactoryManagerService;
066: import org.apache.pluto.util.NamespaceMapper;
067: import org.apache.pluto.util.impl.NamespaceMapperFactoryImpl;
068:
069: /**
070: * Manages the life-time of factories registered during container startup.
071: * A service has to derive from {@link Factory} and implement the
072: * <CODE>init()</CODE> and <CODE>destroy()</CODE> methods as appropriate.
073: *
074: * TODO This is a very lazy implementation, we need to improve it
075: *
076: * @see Factory
077: * @author <a href="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
078: *
079: * @version CVS $Id: FactoryManagerServiceImpl.java 433543 2006-08-22 06:22:54Z crossley $
080: */
081: public class FactoryManagerServiceImpl extends AbstractLogEnabled
082: implements FactoryManagerService, Initializable,
083: Contextualizable, Serviceable, Disposable {
084:
085: /** The servlet configuration */
086: protected ServletConfig servletConfig;
087:
088: /** The avalon context */
089: protected Context context;
090:
091: /** The service manager */
092: protected ServiceManager manager;
093:
094: /* (non-Javadoc)
095: * @see org.apache.avalon.framework.context.Contextualizable#contextualize(org.apache.avalon.framework.context.Context)
096: */
097: public void contextualize(Context context) throws ContextException {
098: this .context = context;
099: this .servletConfig = (ServletConfig) context
100: .get(CocoonServlet.CONTEXT_SERVLET_CONFIG);
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.avalon.framework.activity.Initializable#initialize()
112: */
113: public void initialize() throws Exception {
114: final Map factories = new HashMap();
115:
116: factories.put(ActionRequest.class.getName(),
117: ActionRequestFactoryImpl.class.getName());
118: factories.put(RenderRequest.class.getName(),
119: RenderRequestFactoryImpl.class.getName());
120: factories.put(RenderResponse.class.getName(),
121: RenderResponseFactoryImpl.class.getName());
122: factories.put(PortletSession.class.getName(),
123: PortletSessionFactoryImpl.class.getName());
124: factories.put(PortletConfig.class.getName(),
125: PortletConfigFactoryImpl.class.getName());
126: factories.put(PortletContext.class.getName(),
127: PortletContextFactoryImpl.class.getName());
128: factories.put(PortletPreferences.class.getName(),
129: PortletPreferencesFactoryImpl.class.getName());
130: factories.put(PortalContext.class.getName(),
131: PortalContextFactoryImpl.class.getName());
132: factories.put(ActionResponse.class.getName(),
133: ActionResponseFactoryImpl.class.getName());
134: factories.put(PortletURL.class.getName(),
135: PortletURLFactoryImpl.class.getName());
136: factories.put(PortletPreferences.class.getName(),
137: PortletPreferencesFactoryImpl.class.getName());
138:
139: factories.put(PortletInvoker.class.getName(),
140: PortletInvokerFactoryImpl.class.getName());
141:
142: factories.put(NamespaceMapper.class.getName(),
143: NamespaceMapperFactoryImpl.class.getName());
144:
145: factories.put(ObjectIDFactory.class.getName(),
146: ObjectIDFactoryImpl.class.getName());
147:
148: factories.put(ControllerFactory.class.getName(),
149: ControllerFactoryImpl.class.getName());
150:
151: for (Iterator iter = factories.entrySet().iterator(); iter
152: .hasNext();) {
153: Map.Entry me = (Map.Entry) iter.next();
154: String factoryInterfaceName = (String) me.getKey();
155: // try to get hold of the factory
156: Class factoryInterface;
157:
158: factoryInterface = Class.forName(factoryInterfaceName);
159:
160: String factoryImplName = (String) me.getValue();
161: Class factoryImpl = Class.forName(factoryImplName);
162: Factory factory = (Factory) factoryImpl.newInstance();
163:
164: ContainerUtil.enableLogging(factory, this .getLogger());
165: ContainerUtil.contextualize(factory, this .context);
166: ContainerUtil.service(factory, this .manager);
167: ContainerUtil.initialize(factory);
168:
169: factory.init(this .servletConfig, new HashMap());
170:
171: factoryMap.put(factoryInterface.getName(), factory);
172:
173: // build up list in reverse order for later destruction
174: factoryList.add(0, factory);
175:
176: }
177: }
178:
179: /* (non-Javadoc)
180: * @see org.apache.avalon.framework.activity.Disposable#dispose()
181: */
182: public void dispose() {
183:
184: // destroy the services in reverse order
185: for (Iterator iterator = factoryList.iterator(); iterator
186: .hasNext();) {
187: Factory factory = (Factory) iterator.next();
188:
189: try {
190: factory.destroy();
191: } catch (Exception exc) {
192: // ignore it
193: }
194: }
195:
196: factoryList.clear();
197: factoryMap.clear();
198:
199: }
200:
201: /**
202: ** Returns the service implementation for the given service class, or
203: ** <CODE>null</CODE> if no such service is registered.
204: **
205: ** @param theClass the service class
206: **
207: ** @return the service implementation
208: **/
209: public Factory getFactory(Class theClass) {
210: // at this state the services map is read-only,
211: // therefore we can go without synchronization
212: return ((Factory) factoryMap.get(theClass.getName()));
213: }
214:
215: private Map factoryMap = new HashMap();
216: private List factoryList = new ArrayList();
217:
218: }
|