KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > portal > impl > PortalServiceImpl


1 /*
2  * Copyright 1999-2002,2004-2005 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.impl;
17
18 import java.util.ArrayList JavaDoc;
19 import java.util.HashMap JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.Map JavaDoc;
23
24 import javax.servlet.ServletConfig JavaDoc;
25
26 import org.apache.avalon.framework.activity.Disposable;
27 import org.apache.avalon.framework.configuration.Configurable;
28 import org.apache.avalon.framework.configuration.Configuration;
29 import org.apache.avalon.framework.configuration.ConfigurationException;
30 import org.apache.avalon.framework.container.ContainerUtil;
31 import org.apache.avalon.framework.context.Context;
32 import org.apache.avalon.framework.context.ContextException;
33 import org.apache.avalon.framework.context.Contextualizable;
34 import org.apache.avalon.framework.logger.AbstractLogEnabled;
35 import org.apache.avalon.framework.service.ServiceException;
36 import org.apache.avalon.framework.service.ServiceManager;
37 import org.apache.avalon.framework.service.Serviceable;
38 import org.apache.avalon.framework.thread.ThreadSafe;
39 import org.apache.cocoon.components.ContextHelper;
40 import org.apache.cocoon.environment.Request;
41 import org.apache.cocoon.portal.PortalComponentManager;
42 import org.apache.cocoon.portal.PortalService;
43 import org.apache.cocoon.portal.layout.Layout;
44 import org.apache.cocoon.portal.layout.SkinDescription;
45 import org.apache.cocoon.servlet.CocoonServlet;
46
47 /**
48  * Default implementation of a portal service using a session to store
49  * custom information.
50  *
51  * @author <a HREF="mailto:cziegeler@s-und-n.de">Carsten Ziegeler</a>
52  * @author <a HREF="mailto:volker.schmitt@basf-it-services.com">Volker Schmitt</a>
53  *
54  * @version CVS $Id: PortalServiceImpl.java 330865 2005-11-04 18:54:05Z rgoers $
55  */

56 public class PortalServiceImpl
57     extends AbstractLogEnabled
58     implements Serviceable,
59                 ThreadSafe,
60                 PortalService,
61                 Contextualizable,
62                 Disposable,
63                 Configurable {
64
65     protected Context context;
66     
67     protected ServiceManager manager;
68
69     protected Map JavaDoc portalComponentManagers = new HashMap JavaDoc();
70     
71     protected Map JavaDoc portalConfigurations = new HashMap JavaDoc();
72     
73     protected Map JavaDoc skins = new HashMap JavaDoc();
74     
75     final protected static String JavaDoc KEY = PortalServiceImpl.class.getName();
76     
77     /**
78      * @see org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager)
79      */

80     public void service(ServiceManager serviceManager) throws ServiceException {
81         this.manager = serviceManager;
82     }
83
84     protected PortalServiceInfo getInfo() {
85         final Request request = ContextHelper.getRequest( this.context );
86         PortalServiceInfo info = (PortalServiceInfo) request.getAttribute(KEY);
87         if ( info == null ) {
88             info = new PortalServiceInfo();
89             info.setup(ContextHelper.getObjectModel(this.context), this.portalComponentManagers);
90             request.setAttribute(KEY, info);
91         }
92         return info;
93     }
94     
95     /**
96      * @see org.apache.cocoon.portal.PortalService#getPortalName()
97      */

98     public String JavaDoc getPortalName() {
99         return this.getInfo().getPortalName();
100     }
101
102     /**
103      * @see org.apache.cocoon.portal.PortalService#setPortalName(java.lang.String)
104      */

105     public void setPortalName(String JavaDoc value) {
106         this.getInfo().setPortalName(value);
107     }
108
109     /**
110      * @see org.apache.cocoon.portal.PortalService#getAttribute(java.lang.String)
111      */

112     public Object JavaDoc getAttribute(String JavaDoc key) {
113         return this.getInfo().getAttribute(key);
114     }
115
116     /**
117      * @see org.apache.cocoon.portal.PortalService#setAttribute(java.lang.String, java.lang.Object)
118      */

119     public void setAttribute(String JavaDoc key, Object JavaDoc value) {
120         this.getInfo().setAttribute(key, value);
121     }
122
123     /**
124      * @see org.apache.cocoon.portal.PortalService#removeAttribute(java.lang.String)
125      */

126     public void removeAttribute(String JavaDoc key) {
127         this.getInfo().removeAttribute(key);
128     }
129
130     /**
131      * @see org.apache.cocoon.portal.PortalService#getAttributeNames()
132      */

133     public Iterator JavaDoc getAttributeNames() {
134         return this.getInfo().getAttributeNames();
135     }
136
137     /**
138      * @see org.apache.cocoon.portal.PortalService#getTemporaryAttribute(java.lang.String)
139      */

140     public Object JavaDoc getTemporaryAttribute(String JavaDoc key) {
141         return this.getInfo().getTemporaryAttribute(key);
142     }
143     
144     /**
145      * @see org.apache.cocoon.portal.PortalService#setTemporaryAttribute(java.lang.String, java.lang.Object)
146      */

147     public void setTemporaryAttribute(String JavaDoc key, Object JavaDoc value) {
148         this.getInfo().setTemporaryAttribute(key, value);
149     }
150     
151     /**
152      * @see org.apache.cocoon.portal.PortalService#removeTemporaryAttribute(java.lang.String)
153      */

154     public void removeTemporaryAttribute(String JavaDoc key) {
155         this.getInfo().removeTemporaryAttribute(key);
156     }
157     
158     /**
159      * @see org.apache.cocoon.portal.PortalService#getTemporaryAttributeNames()
160      */

161     public Iterator JavaDoc getTemporaryAttributeNames() {
162         return this.getInfo().getTemporaryAttributeNames();
163     }
164
165     /**
166      * @see org.apache.cocoon.portal.PortalService#getComponentManager()
167      */

168     public PortalComponentManager getComponentManager() {
169         return this.getInfo().getComponentManager();
170     }
171
172     /**
173      * @see org.apache.avalon.framework.context.Contextualizable#contextualize(org.apache.avalon.framework.context.Context)
174      */

175     public void contextualize(Context context) throws ContextException {
176         this.context = context;
177         // add the portal service to the servlet context - if available
178
try {
179             final ServletConfig JavaDoc servletConfig = (ServletConfig JavaDoc) context.get(CocoonServlet.CONTEXT_SERVLET_CONFIG);
180             servletConfig.getServletContext().setAttribute(PortalService.ROLE, this);
181         } catch (ContextException ignore) {
182             // we ignore the context exception
183
// this avoids startup errors if the portal is configured for the CLI
184
// environment
185
this.getLogger().warn("The portal service is not stored in the servlet config.", ignore);
186         }
187     }
188
189     /**
190      * @see org.apache.avalon.framework.activity.Disposable#dispose()
191      */

192     public void dispose() {
193         final Iterator JavaDoc i = this.portalComponentManagers.values().iterator();
194         while ( i.hasNext() ) {
195             ContainerUtil.dispose( i.next() );
196         }
197         this.portalComponentManagers.clear();
198         this.portalConfigurations.clear();
199         // remove the portal service to the servlet context - if available
200
try {
201             final ServletConfig JavaDoc servletConfig = (ServletConfig JavaDoc) context.get(CocoonServlet.CONTEXT_SERVLET_CONFIG);
202             servletConfig.getServletContext().removeAttribute(PortalService.ROLE);
203         } catch (ContextException ignore) {
204             // we ignore the context exception
205
}
206     }
207
208     /**
209      * @see org.apache.avalon.framework.configuration.Configurable#configure(org.apache.avalon.framework.configuration.Configuration)
210      */

211     public void configure(Configuration config) throws ConfigurationException {
212         final Configuration[] portals = config.getChild("portals").getChildren("portal");
213         for(int i=0; i < portals.length; i++ ) {
214             final Configuration current = portals[i];
215             final String JavaDoc name = current.getAttribute("name");
216             try {
217                 PortalComponentManager c = new DefaultPortalComponentManager(this, this.context);
218                 this.portalComponentManagers.put( name, c );
219                 ContainerUtil.enableLogging( c, this.getLogger() );
220                 ContainerUtil.contextualize( c, this.context );
221                 ContainerUtil.service( c, this.manager );
222                 ContainerUtil.configure( c, current );
223                 ContainerUtil.initialize( c );
224                 
225                 this.portalConfigurations.put( name, current );
226                 
227                 // scan for skins
228
final List JavaDoc skinList = new ArrayList JavaDoc();
229                 this.skins.put(name, skinList);
230                 final Configuration[] skinConfs = current.getChild("skins").getChildren("skin");
231                 if ( skinConfs != null ) {
232                     for(int s=0;s<skinConfs.length;s++) {
233                         final Configuration currentSkin = skinConfs[s];
234                         final String JavaDoc skinName = currentSkin.getAttribute("name");
235                         final SkinDescription desc = new SkinDescription();
236                         desc.setName(skinName);
237                         desc.setBasePath(currentSkin.getAttribute("base-path"));
238                         desc.setThumbnailPath(currentSkin.getChild("thumbnail-path").getValue(null));
239                         skinList.add(desc);
240                     }
241                 }
242             } catch (Exception JavaDoc e) {
243                 throw new ConfigurationException("Unable to setup new portal component manager for portal " + name, e);
244             }
245             
246         }
247     }
248
249     /**
250      * @see org.apache.cocoon.portal.PortalService#setEntryLayout(java.lang.String, org.apache.cocoon.portal.layout.Layout)
251      */

252     public void setEntryLayout(String JavaDoc layoutKey, Layout object) {
253         if ( layoutKey == null ) {
254             layoutKey = this.getDefaultLayoutKey();
255         }
256         if ( object == null ) {
257             this.removeTemporaryAttribute("DEFAULT_LAYOUT:" + layoutKey);
258         } else {
259             this.setTemporaryAttribute("DEFAULT_LAYOUT:" + layoutKey, object);
260         }
261     }
262
263     /**
264      * @see org.apache.cocoon.portal.PortalService#getEntryLayout(java.lang.String)
265      */

266     public Layout getEntryLayout(String JavaDoc layoutKey) {
267         if ( layoutKey == null ) {
268             layoutKey = this.getDefaultLayoutKey();
269         }
270         return (Layout)this.getTemporaryAttribute("DEFAULT_LAYOUT:" + layoutKey);
271     }
272
273     /**
274      * @see org.apache.cocoon.portal.PortalService#setDefaultLayoutKey(java.lang.String)
275      */

276     public void setDefaultLayoutKey(String JavaDoc layoutKey) {
277         if ( layoutKey == null ) {
278             this.removeAttribute("default-layout-key");
279         } else {
280             this.setAttribute("default-layout-key", layoutKey);
281         }
282     }
283
284     public void setRenderable(Boolean JavaDoc renderable) {
285         String JavaDoc key = getDefaultLayoutKey();
286         if (renderable == null) {
287             removeTemporaryAttribute("RENDER:" + key);
288         } else {
289             setTemporaryAttribute("RENDER:" + key, renderable);
290         }
291     }
292
293     public Boolean JavaDoc isRenderable() {
294         String JavaDoc key = getDefaultLayoutKey();
295         Boolean JavaDoc bool = (Boolean JavaDoc)getTemporaryAttribute("RENDER:" + key);
296         if (bool != null) {
297             return bool;
298         }
299         return Boolean.TRUE;
300     }
301     
302     /**
303      * @see org.apache.cocoon.portal.PortalService#getDefaultLayoutKey()
304      */

305     public String JavaDoc getDefaultLayoutKey() {
306         String JavaDoc key = (String JavaDoc)this.getAttribute("default-layout-key");
307         if ( key == null ) {
308             Configuration config = (Configuration)this.portalConfigurations.get(this.getPortalName());
309             key = config.getAttribute("default-layout-key", "portal");
310             if ( key != null ) {
311                 this.setDefaultLayoutKey(key);
312             }
313         }
314         return key;
315     }
316    
317     /**
318      * @see org.apache.cocoon.portal.PortalService#getSkinDescriptions()
319      */

320     public List JavaDoc getSkinDescriptions() {
321         return (List JavaDoc)this.skins.get(this.getPortalName());
322     }
323
324     /**
325      * @see org.apache.cocoon.portal.PortalService#getObjectModel()
326      */

327     public Map JavaDoc getObjectModel() {
328         return ContextHelper.getObjectModel(this.context);
329     }
330 }
331
Popular Tags