1 16 package org.apache.cocoon.portal.pluto.services.factory; 17 18 import java.util.ArrayList ; 19 import java.util.HashMap ; 20 import java.util.Iterator ; 21 import java.util.List ; 22 import java.util.Map ; 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 ; 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 80 public class FactoryManagerServiceImpl 81 extends AbstractLogEnabled 82 implements FactoryManagerService, Initializable, Contextualizable, Serviceable, Disposable { 83 84 85 protected ServletConfig servletConfig; 86 87 88 protected Context context; 89 90 91 protected ServiceManager manager; 92 93 96 public void contextualize(Context context) throws ContextException { 97 this.context = context; 98 this.servletConfig = (ServletConfig ) context.get(CocoonServlet.CONTEXT_SERVLET_CONFIG); 99 } 100 101 104 public void service(ServiceManager manager) throws ServiceException { 105 this.manager = manager; 106 } 107 108 111 public void initialize() 112 throws Exception { 113 final Map factories = new HashMap (); 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 iter = factories.entrySet().iterator(); iter.hasNext (); ) { 136 Map.Entry me = (Map.Entry )iter.next(); 137 String factoryInterfaceName = (String )me.getKey(); 138 Class factoryInterface; 140 141 factoryInterface = Class.forName (factoryInterfaceName); 142 143 String factoryImplName = (String )me.getValue(); 144 Class 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 ()); 153 154 factoryMap.put (factoryInterface.getName(), factory); 155 156 factoryList.add (0, factory); 158 159 } 160 } 161 162 165 public void dispose() { 166 167 for (Iterator iterator = factoryList.iterator (); iterator.hasNext (); ) { 169 Factory factory = (Factory) iterator.next (); 170 171 try { 172 factory.destroy (); 173 } catch (Exception exc) { 174 } 176 } 177 178 factoryList.clear(); 179 factoryMap.clear(); 180 181 } 182 183 191 public Factory getFactory (Class theClass) { 192 return ((Factory) factoryMap.get (theClass.getName())); 195 } 196 197 private Map factoryMap = new HashMap (); 198 private List factoryList = new ArrayList (); 199 200 } 201 | Popular Tags |