KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > portal > pluto > PortletContainerEnvironmentImpl


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;
17
18 import java.util.HashMap JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.Map JavaDoc;
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 /**
46  *
47  *
48  * @author <a HREF="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
49  *
50  * @version CVS $Id: PortletContainerEnvironmentImpl.java 30932 2004-07-29 17:35:38Z vgritsenko $
51  */

52 public class PortletContainerEnvironmentImpl
53 extends AbstractLogEnabled
54 implements PortletContainerEnvironment, Serviceable, Disposable, Initializable, Contextualizable {
55
56     /** The service manager */
57     protected ServiceManager manager;
58     
59     /** Services */
60     protected Map JavaDoc services = new HashMap JavaDoc();
61     
62     /** Static services */
63     protected Map JavaDoc staticServices = new HashMap JavaDoc();
64     
65     /** Context */
66     protected Context context;
67     
68     /**
69      * Serviceable
70      */

71     public void service(ServiceManager manager) {
72         this.manager = manager;
73     }
74     
75     /* (non-Javadoc)
76      * @see org.apache.avalon.framework.context.Contextualizable#contextualize(org.apache.avalon.framework.context.Context)
77      */

78     public void contextualize(Context context) {
79         this.context = context;
80     }
81     
82     /* (non-Javadoc)
83      * @see org.apache.avalon.framework.activity.Initializable#initialize()
84      */

85     public void initialize() throws Exception JavaDoc {
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     /**
101      * Initialize a service
102      */

103     protected Object JavaDoc init(Object JavaDoc o)
104     throws Exception JavaDoc {
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     /* (non-Javadoc)
116      * @see org.apache.avalon.framework.activity.Disposable#dispose()
117      */

118     public void dispose() {
119         if ( this.manager != null ) {
120             Iterator JavaDoc 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     /* (non-Javadoc)
130      * @see org.apache.pluto.services.PortletContainerEnvironment#getContainerService(java.lang.Class)
131      */

132     public ContainerService getContainerService(Class JavaDoc serviceClazz) {
133         final String JavaDoc 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