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.Collections;
021: import java.util.Enumeration;
022: import java.util.HashMap;
023: import java.util.Iterator;
024: import java.util.List;
025: import java.util.Map;
026:
027: import org.apache.cocoon.environment.ObjectModelHelper;
028: import org.apache.cocoon.environment.Session;
029: import org.apache.cocoon.portal.Constants;
030: import org.apache.cocoon.portal.PortalComponentManager;
031:
032: /**
033: * The portal information for the current request
034: *
035: * @author <a href="mailto:cziegeler@s-und-n.de">Carsten Ziegeler</a>
036: *
037: * @version CVS $Id: PortalServiceInfo.java 433543 2006-08-22 06:22:54Z crossley $
038: */
039: public class PortalServiceInfo {
040:
041: private Map portalComponentManagers;
042:
043: private Map objectModel;
044:
045: protected Map temporaryAttributes = new HashMap();
046:
047: protected String portalName;
048:
049: protected String attributePrefix;
050:
051: protected PortalComponentManager portalComponentManager;
052:
053: public void setup(Map objectModel, Map managers) {
054: this .objectModel = objectModel;
055: this .portalComponentManagers = managers;
056: Map context = (Map) objectModel
057: .get(ObjectModelHelper.PARENT_CONTEXT);
058: if (context != null) {
059: String pm = (String) context.get(Constants.PORTAL_NAME_KEY);
060: if (pm != null) {
061: this .setPortalName(pm);
062: }
063: }
064: if (this .portalName == null
065: && this .portalComponentManagers.size() == 1) {
066: // if we only have one portal, just use it
067: String pm = this .portalComponentManagers.keySet()
068: .iterator().next().toString();
069: this .setPortalName(pm);
070: }
071: }
072:
073: public String getPortalName() {
074: return this .portalName;
075: }
076:
077: public void setPortalName(String value) {
078: this .portalName = value;
079: this .attributePrefix = this .getClass().getName() + '/'
080: + this .portalName + '/';
081: this .portalComponentManager = (PortalComponentManager) this .portalComponentManagers
082: .get(this .portalName);
083: if (this .portalComponentManager == null) {
084: throw new RuntimeException("Portal '" + this .portalName
085: + "' is not configured.");
086: }
087: }
088:
089: public Object getAttribute(String key) {
090: final Session session = ObjectModelHelper.getRequest(
091: this .objectModel).getSession(false);
092: if (session == null) {
093: return null;
094: }
095: return session.getAttribute(this .attributePrefix + key);
096: }
097:
098: public void setAttribute(String key, Object value) {
099: final Session session = ObjectModelHelper.getRequest(
100: this .objectModel).getSession();
101: session.setAttribute(this .attributePrefix + key, value);
102: }
103:
104: public void removeAttribute(String key) {
105: final Session session = ObjectModelHelper.getRequest(
106: this .objectModel).getSession(false);
107: if (session != null) {
108: session.removeAttribute(this .attributePrefix + key);
109: }
110: }
111:
112: public Iterator getAttributeNames() {
113: final Session session = ObjectModelHelper.getRequest(
114: this .objectModel).getSession(false);
115: if (session != null) {
116: List names = new ArrayList();
117: Enumeration e = session.getAttributeNames();
118: final int pos = this .attributePrefix.length() + 1;
119: if (e != null) {
120: while (e.hasMoreElements()) {
121: final String name = (String) e.nextElement();
122: if (name.startsWith(this .attributePrefix)) {
123: names.add(name.substring(pos));
124: }
125: }
126: }
127: return names.iterator();
128: }
129: return Collections.EMPTY_MAP.keySet().iterator();
130: }
131:
132: public Object getTemporaryAttribute(String key) {
133: return this .temporaryAttributes.get(key);
134: }
135:
136: public void setTemporaryAttribute(String key, Object value) {
137: this .temporaryAttributes.put(key, value);
138: }
139:
140: public void removeTemporaryAttribute(String key) {
141: this .temporaryAttributes.remove(key);
142: }
143:
144: public Iterator getTemporaryAttributeNames() {
145: return this .temporaryAttributes.keySet().iterator();
146: }
147:
148: /**
149: * Return the component manager for the current portal
150: */
151: public PortalComponentManager getComponentManager() {
152: return this.portalComponentManager;
153: }
154:
155: }
|