1 16 package org.apache.cocoon.portal.pluto; 17 18 import java.util.HashMap ; 19 import java.util.Iterator ; 20 import java.util.Map ; 21 22 import org.apache.avalon.framework.CascadingRuntimeException; 23 import org.apache.avalon.framework.activity.Disposable; 24 import org.apache.avalon.framework.activity.Initializable; 25 import org.apache.avalon.framework.container.ContainerUtil; 26 import org.apache.avalon.framework.context.Context; 27 import org.apache.avalon.framework.context.Contextualizable; 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.Serviceable; 32 import org.apache.cocoon.portal.pluto.om.PortletDefinitionRegistry; 33 import org.apache.cocoon.portal.pluto.om.PortletDefinitionRegistryImpl; 34 import org.apache.cocoon.portal.pluto.service.log.LogServiceImpl; 35 import org.apache.cocoon.portal.pluto.services.PropertyManagerServiceImpl; 36 import org.apache.cocoon.portal.pluto.services.factory.FactoryManagerServiceImpl; 37 import org.apache.pluto.services.ContainerService; 38 import org.apache.pluto.services.PortletContainerEnvironment; 39 import org.apache.pluto.services.factory.FactoryManagerService; 40 import org.apache.pluto.services.information.InformationProviderService; 41 import org.apache.pluto.services.log.LogService; 42 import org.apache.pluto.services.property.PropertyManagerService; 43 import org.apache.pluto.services.title.DynamicTitleService; 44 45 52 public class PortletContainerEnvironmentImpl 53 extends AbstractLogEnabled 54 implements PortletContainerEnvironment, Serviceable, Disposable, Initializable, Contextualizable { 55 56 57 protected ServiceManager manager; 58 59 60 protected Map services = new HashMap (); 61 62 63 protected Map staticServices = new HashMap (); 64 65 66 protected Context context; 67 68 71 public void service(ServiceManager manager) { 72 this.manager = manager; 73 } 74 75 78 public void contextualize(Context context) { 79 this.context = context; 80 } 81 82 85 public void initialize() throws Exception { 86 this.staticServices.put(LogService.class.getName(), 87 this.init(new LogServiceImpl(this.getLogger()))); 88 this.staticServices.put(PortletDefinitionRegistry.class.getName(), 89 this.init(new PortletDefinitionRegistryImpl())); 90 this.staticServices.put(InformationProviderService.class.getName(), 91 this.init(new InformationProviderServiceImpl())); 92 this.staticServices.put(FactoryManagerService.class.getName(), 93 this.init(new FactoryManagerServiceImpl())); 94 this.staticServices.put(DynamicTitleService.class.getName(), 95 this.init(new DynamicTitleServiceImpl())); 96 this.staticServices.put(PropertyManagerService.class.getName(), 97 this.init(new PropertyManagerServiceImpl())); 98 } 99 100 103 protected Object init(Object o) 104 throws Exception { 105 ContainerUtil.enableLogging(o, this.getLogger()); 106 ContainerUtil.contextualize(o, this.context); 107 ContainerUtil.service(o, this.manager); 108 if ( o instanceof PortletContainerEnabled ) { 109 ((PortletContainerEnabled)o).setPortletContainerEnvironment(this); 110 } 111 ContainerUtil.initialize(o); 112 return o; 113 } 114 115 118 public void dispose() { 119 if ( this.manager != null ) { 120 Iterator i = this.services.entrySet().iterator(); 121 while ( i.hasNext() ) { 122 this.manager.release(i.next()); 123 } 124 this.services.clear(); 125 this.manager = null; 126 } 127 } 128 129 132 public ContainerService getContainerService(Class serviceClazz) { 133 final String key = serviceClazz.getName(); 134 ContainerService service = (ContainerService) this.staticServices.get(key); 135 if ( service == null ) { 136 service = (ContainerService) this.services.get(key); 137 if ( service == null ) { 138 try { 139 service = (ContainerService)this.manager.lookup(key); 140 this.services.put(key, service); 141 } catch (ServiceException se) { 142 throw new CascadingRuntimeException("Unable to lookup service " + key, se); 143 } 144 } 145 } 146 return service; 147 } 148 149 } 150 | Popular Tags |