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;
018:
019: import java.util.HashMap;
020: import java.util.Iterator;
021: import java.util.Map;
022:
023: import org.apache.avalon.framework.CascadingRuntimeException;
024: import org.apache.avalon.framework.activity.Disposable;
025: import org.apache.avalon.framework.activity.Initializable;
026: import org.apache.avalon.framework.container.ContainerUtil;
027: import org.apache.avalon.framework.context.Context;
028: import org.apache.avalon.framework.context.Contextualizable;
029: import org.apache.avalon.framework.logger.AbstractLogEnabled;
030: import org.apache.avalon.framework.service.ServiceException;
031: import org.apache.avalon.framework.service.ServiceManager;
032: import org.apache.avalon.framework.service.Serviceable;
033: import org.apache.cocoon.portal.pluto.om.PortletDefinitionRegistry;
034: import org.apache.cocoon.portal.pluto.om.PortletDefinitionRegistryImpl;
035: import org.apache.cocoon.portal.pluto.service.log.LogServiceImpl;
036: import org.apache.cocoon.portal.pluto.services.PropertyManagerServiceImpl;
037: import org.apache.cocoon.portal.pluto.services.factory.FactoryManagerServiceImpl;
038: import org.apache.pluto.services.ContainerService;
039: import org.apache.pluto.services.PortletContainerEnvironment;
040: import org.apache.pluto.services.factory.FactoryManagerService;
041: import org.apache.pluto.services.information.InformationProviderService;
042: import org.apache.pluto.services.log.LogService;
043: import org.apache.pluto.services.property.PropertyManagerService;
044: import org.apache.pluto.services.title.DynamicTitleService;
045:
046: /**
047: *
048: *
049: * @author <a href="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
050: *
051: * @version CVS $Id: PortletContainerEnvironmentImpl.java 433543 2006-08-22 06:22:54Z crossley $
052: */
053: public class PortletContainerEnvironmentImpl extends AbstractLogEnabled
054: implements PortletContainerEnvironment, Serviceable,
055: Disposable, Initializable, Contextualizable {
056:
057: /** The service manager */
058: protected ServiceManager manager;
059:
060: /** Services */
061: protected Map services = new HashMap();
062:
063: /** Static services */
064: protected Map staticServices = new HashMap();
065:
066: /** Context */
067: protected Context context;
068:
069: /**
070: * Serviceable
071: */
072: public void service(ServiceManager manager) {
073: this .manager = manager;
074: }
075:
076: /* (non-Javadoc)
077: * @see org.apache.avalon.framework.context.Contextualizable#contextualize(org.apache.avalon.framework.context.Context)
078: */
079: public void contextualize(Context context) {
080: this .context = context;
081: }
082:
083: /* (non-Javadoc)
084: * @see org.apache.avalon.framework.activity.Initializable#initialize()
085: */
086: public void initialize() throws Exception {
087: this .staticServices.put(LogService.class.getName(), this
088: .init(new LogServiceImpl(this .getLogger())));
089: this .staticServices.put(PortletDefinitionRegistry.class
090: .getName(), this
091: .init(new PortletDefinitionRegistryImpl()));
092: this .staticServices.put(InformationProviderService.class
093: .getName(), this
094: .init(new InformationProviderServiceImpl()));
095: this .staticServices.put(FactoryManagerService.class.getName(),
096: this .init(new FactoryManagerServiceImpl()));
097: this .staticServices.put(DynamicTitleService.class.getName(),
098: this .init(new DynamicTitleServiceImpl()));
099: this .staticServices.put(PropertyManagerService.class.getName(),
100: this .init(new PropertyManagerServiceImpl()));
101: }
102:
103: /**
104: * Initialize a service
105: */
106: protected Object init(Object o) throws Exception {
107: ContainerUtil.enableLogging(o, this .getLogger());
108: ContainerUtil.contextualize(o, this .context);
109: ContainerUtil.service(o, this .manager);
110: if (o instanceof PortletContainerEnabled) {
111: ((PortletContainerEnabled) o)
112: .setPortletContainerEnvironment(this );
113: }
114: ContainerUtil.initialize(o);
115: return o;
116: }
117:
118: /* (non-Javadoc)
119: * @see org.apache.avalon.framework.activity.Disposable#dispose()
120: */
121: public void dispose() {
122: if (this .manager != null) {
123: Iterator i = this .services.entrySet().iterator();
124: while (i.hasNext()) {
125: this .manager.release(i.next());
126: }
127: this .services.clear();
128: this .manager = null;
129: }
130: }
131:
132: /* (non-Javadoc)
133: * @see org.apache.pluto.services.PortletContainerEnvironment#getContainerService(java.lang.Class)
134: */
135: public ContainerService getContainerService(Class serviceClazz) {
136: final String key = serviceClazz.getName();
137: ContainerService service = (ContainerService) this .staticServices
138: .get(key);
139: if (service == null) {
140: service = (ContainerService) this .services.get(key);
141: if (service == null) {
142: try {
143: service = (ContainerService) this .manager
144: .lookup(key);
145: this .services.put(key, service);
146: } catch (ServiceException se) {
147: throw new CascadingRuntimeException(
148: "Unable to lookup service " + key, se);
149: }
150: }
151: }
152: return service;
153: }
154:
155: }
|