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.impl;
018:
019: import java.util.ArrayList;
020: import java.util.HashMap;
021: import java.util.Iterator;
022: import java.util.List;
023: import java.util.Map;
024:
025: import javax.servlet.ServletConfig;
026:
027: import org.apache.avalon.framework.activity.Disposable;
028: import org.apache.avalon.framework.configuration.Configurable;
029: import org.apache.avalon.framework.configuration.Configuration;
030: import org.apache.avalon.framework.configuration.ConfigurationException;
031: import org.apache.avalon.framework.container.ContainerUtil;
032: import org.apache.avalon.framework.context.Context;
033: import org.apache.avalon.framework.context.ContextException;
034: import org.apache.avalon.framework.context.Contextualizable;
035: import org.apache.avalon.framework.logger.AbstractLogEnabled;
036: import org.apache.avalon.framework.service.ServiceException;
037: import org.apache.avalon.framework.service.ServiceManager;
038: import org.apache.avalon.framework.service.Serviceable;
039: import org.apache.avalon.framework.thread.ThreadSafe;
040: import org.apache.cocoon.components.ContextHelper;
041: import org.apache.cocoon.environment.Request;
042: import org.apache.cocoon.portal.PortalComponentManager;
043: import org.apache.cocoon.portal.PortalService;
044: import org.apache.cocoon.portal.layout.Layout;
045: import org.apache.cocoon.portal.layout.SkinDescription;
046: import org.apache.cocoon.servlet.CocoonServlet;
047:
048: /**
049: * Default implementation of a portal service using a session to store
050: * custom information.
051: *
052: * @author <a href="mailto:cziegeler@s-und-n.de">Carsten Ziegeler</a>
053: * @author <a href="mailto:volker.schmitt@basf-it-services.com">Volker Schmitt</a>
054: *
055: * @version CVS $Id: PortalServiceImpl.java 433543 2006-08-22 06:22:54Z crossley $
056: */
057: public class PortalServiceImpl extends AbstractLogEnabled implements
058: Serviceable, ThreadSafe, PortalService, Contextualizable,
059: Disposable, Configurable {
060:
061: protected Context context;
062:
063: protected ServiceManager manager;
064:
065: protected Map portalComponentManagers = new HashMap();
066:
067: protected Map portalConfigurations = new HashMap();
068:
069: protected Map skins = new HashMap();
070:
071: final protected static String KEY = PortalServiceImpl.class
072: .getName();
073:
074: /**
075: * @see org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager)
076: */
077: public void service(ServiceManager serviceManager)
078: throws ServiceException {
079: this .manager = serviceManager;
080: }
081:
082: protected PortalServiceInfo getInfo() {
083: final Request request = ContextHelper.getRequest(this .context);
084: PortalServiceInfo info = (PortalServiceInfo) request
085: .getAttribute(KEY);
086: if (info == null) {
087: info = new PortalServiceInfo();
088: info.setup(ContextHelper.getObjectModel(this .context),
089: this .portalComponentManagers);
090: request.setAttribute(KEY, info);
091: }
092: return info;
093: }
094:
095: /**
096: * @see org.apache.cocoon.portal.PortalService#getPortalName()
097: */
098: public String getPortalName() {
099: return this .getInfo().getPortalName();
100: }
101:
102: /**
103: * @see org.apache.cocoon.portal.PortalService#setPortalName(java.lang.String)
104: */
105: public void setPortalName(String value) {
106: this .getInfo().setPortalName(value);
107: }
108:
109: /**
110: * @see org.apache.cocoon.portal.PortalService#getAttribute(java.lang.String)
111: */
112: public Object getAttribute(String 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 key, Object 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 key) {
127: this .getInfo().removeAttribute(key);
128: }
129:
130: /**
131: * @see org.apache.cocoon.portal.PortalService#getAttributeNames()
132: */
133: public Iterator getAttributeNames() {
134: return this .getInfo().getAttributeNames();
135: }
136:
137: /**
138: * @see org.apache.cocoon.portal.PortalService#getTemporaryAttribute(java.lang.String)
139: */
140: public Object getTemporaryAttribute(String 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 key, Object 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 key) {
155: this .getInfo().removeTemporaryAttribute(key);
156: }
157:
158: /**
159: * @see org.apache.cocoon.portal.PortalService#getTemporaryAttributeNames()
160: */
161: public Iterator 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 servletConfig = (ServletConfig) context
180: .get(CocoonServlet.CONTEXT_SERVLET_CONFIG);
181: servletConfig.getServletContext().setAttribute(
182: PortalService.ROLE, this );
183: } catch (ContextException ignore) {
184: // we ignore the context exception
185: // this avoids startup errors if the portal is configured for the CLI
186: // environment
187: this
188: .getLogger()
189: .warn(
190: "The portal service is not stored in the servlet config.",
191: ignore);
192: }
193: }
194:
195: /**
196: * @see org.apache.avalon.framework.activity.Disposable#dispose()
197: */
198: public void dispose() {
199: final Iterator i = this .portalComponentManagers.values()
200: .iterator();
201: while (i.hasNext()) {
202: ContainerUtil.dispose(i.next());
203: }
204: this .portalComponentManagers.clear();
205: this .portalConfigurations.clear();
206: // remove the portal service to the servlet context - if available
207: try {
208: final ServletConfig servletConfig = (ServletConfig) context
209: .get(CocoonServlet.CONTEXT_SERVLET_CONFIG);
210: servletConfig.getServletContext().removeAttribute(
211: PortalService.ROLE);
212: } catch (ContextException ignore) {
213: // we ignore the context exception
214: }
215: }
216:
217: /**
218: * @see org.apache.avalon.framework.configuration.Configurable#configure(org.apache.avalon.framework.configuration.Configuration)
219: */
220: public void configure(Configuration config)
221: throws ConfigurationException {
222: final Configuration[] portals = config.getChild("portals")
223: .getChildren("portal");
224: for (int i = 0; i < portals.length; i++) {
225: final Configuration current = portals[i];
226: final String name = current.getAttribute("name");
227: try {
228: PortalComponentManager c = new DefaultPortalComponentManager(
229: this , this .context);
230: this .portalComponentManagers.put(name, c);
231: ContainerUtil.enableLogging(c, this .getLogger());
232: ContainerUtil.contextualize(c, this .context);
233: ContainerUtil.service(c, this .manager);
234: ContainerUtil.configure(c, current);
235: ContainerUtil.initialize(c);
236:
237: this .portalConfigurations.put(name, current);
238:
239: // scan for skins
240: final List skinList = new ArrayList();
241: this .skins.put(name, skinList);
242: final Configuration[] skinConfs = current.getChild(
243: "skins").getChildren("skin");
244: if (skinConfs != null) {
245: for (int s = 0; s < skinConfs.length; s++) {
246: final Configuration currentSkin = skinConfs[s];
247: final String skinName = currentSkin
248: .getAttribute("name");
249: final SkinDescription desc = new SkinDescription();
250: desc.setName(skinName);
251: desc.setBasePath(currentSkin
252: .getAttribute("base-path"));
253: desc.setThumbnailPath(currentSkin.getChild(
254: "thumbnail-path").getValue(null));
255: skinList.add(desc);
256: }
257: }
258: } catch (Exception e) {
259: throw new ConfigurationException(
260: "Unable to setup new portal component manager for portal "
261: + name, e);
262: }
263:
264: }
265: }
266:
267: /**
268: * @see org.apache.cocoon.portal.PortalService#setEntryLayout(java.lang.String, org.apache.cocoon.portal.layout.Layout)
269: */
270: public void setEntryLayout(String layoutKey, Layout object) {
271: if (layoutKey == null) {
272: layoutKey = this .getDefaultLayoutKey();
273: }
274: if (object == null) {
275: this
276: .removeTemporaryAttribute("DEFAULT_LAYOUT:"
277: + layoutKey);
278: } else {
279: this .setTemporaryAttribute("DEFAULT_LAYOUT:" + layoutKey,
280: object);
281: }
282: }
283:
284: /**
285: * @see org.apache.cocoon.portal.PortalService#getEntryLayout(java.lang.String)
286: */
287: public Layout getEntryLayout(String layoutKey) {
288: if (layoutKey == null) {
289: layoutKey = this .getDefaultLayoutKey();
290: }
291: return (Layout) this .getTemporaryAttribute("DEFAULT_LAYOUT:"
292: + layoutKey);
293: }
294:
295: /**
296: * @see org.apache.cocoon.portal.PortalService#setDefaultLayoutKey(java.lang.String)
297: */
298: public void setDefaultLayoutKey(String layoutKey) {
299: if (layoutKey == null) {
300: this .removeAttribute("default-layout-key");
301: } else {
302: this .setAttribute("default-layout-key", layoutKey);
303: }
304: }
305:
306: public void setRenderable(Boolean renderable) {
307: String key = getDefaultLayoutKey();
308: if (renderable == null) {
309: removeTemporaryAttribute("RENDER:" + key);
310: } else {
311: setTemporaryAttribute("RENDER:" + key, renderable);
312: }
313: }
314:
315: public Boolean isRenderable() {
316: String key = getDefaultLayoutKey();
317: Boolean bool = (Boolean) getTemporaryAttribute("RENDER:" + key);
318: if (bool != null) {
319: return bool;
320: }
321: return Boolean.TRUE;
322: }
323:
324: /**
325: * @see org.apache.cocoon.portal.PortalService#getDefaultLayoutKey()
326: */
327: public String getDefaultLayoutKey() {
328: String key = (String) this .getAttribute("default-layout-key");
329: if (key == null) {
330: Configuration config = (Configuration) this .portalConfigurations
331: .get(this .getPortalName());
332: key = config.getAttribute("default-layout-key", "portal");
333: if (key != null) {
334: this .setDefaultLayoutKey(key);
335: }
336: }
337: return key;
338: }
339:
340: /**
341: * @see org.apache.cocoon.portal.PortalService#getSkinDescriptions()
342: */
343: public List getSkinDescriptions() {
344: return (List) this .skins.get(this .getPortalName());
345: }
346:
347: /**
348: * @see org.apache.cocoon.portal.PortalService#getObjectModel()
349: */
350: public Map getObjectModel() {
351: return ContextHelper.getObjectModel(this.context);
352: }
353: }
|