KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 1999-2002,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.impl;
17
18 import java.util.ArrayList JavaDoc;
19 import java.util.Collections JavaDoc;
20 import java.util.Enumeration JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.Map JavaDoc;
25
26 import org.apache.cocoon.environment.ObjectModelHelper;
27 import org.apache.cocoon.environment.Session;
28 import org.apache.cocoon.portal.Constants;
29 import org.apache.cocoon.portal.PortalComponentManager;
30
31 /**
32  * The portal information for the current request
33  *
34  * @author <a HREF="mailto:cziegeler@s-und-n.de">Carsten Ziegeler</a>
35  *
36  * @version CVS $Id: PortalServiceInfo.java 148910 2005-01-28 18:19:44Z cziegeler $
37  */

38 public class PortalServiceInfo {
39     
40     private Map JavaDoc portalComponentManagers;
41
42     private Map JavaDoc objectModel;
43
44     protected Map JavaDoc temporaryAttributes = new HashMap JavaDoc();
45     
46     protected String JavaDoc portalName;
47
48     protected String JavaDoc attributePrefix;
49     
50     protected PortalComponentManager portalComponentManager;
51     
52     public void setup(Map JavaDoc objectModel, Map JavaDoc managers) {
53         this.objectModel = objectModel;
54         this.portalComponentManagers = managers;
55         Map JavaDoc context = (Map JavaDoc)objectModel.get(ObjectModelHelper.PARENT_CONTEXT);
56         if (context != null) {
57             String JavaDoc pm = (String JavaDoc)context.get(Constants.PORTAL_NAME_KEY);
58             if (pm != null) {
59                 this.setPortalName(pm);
60             }
61         }
62         if ( this.portalName == null && this.portalComponentManagers.size() == 1 ) {
63                 // if we only have one portal, just use it
64
String JavaDoc pm = this.portalComponentManagers.keySet().iterator().next().toString();
65             this.setPortalName(pm);
66         }
67     }
68
69     public String JavaDoc getPortalName() {
70         return this.portalName;
71     }
72
73     public void setPortalName(String JavaDoc value) {
74         this.portalName = value;
75         this.attributePrefix = this.getClass().getName() + '/' + this.portalName + '/';
76         this.portalComponentManager = (PortalComponentManager) this.portalComponentManagers.get(this.portalName);
77         if ( this.portalComponentManager == null ) {
78             throw new RuntimeException JavaDoc("Portal '"+this.portalName+"' is not configured.");
79         }
80     }
81
82     public Object JavaDoc getAttribute(String JavaDoc key) {
83         final Session session = ObjectModelHelper.getRequest(this.objectModel).getSession(false);
84         if (session == null) {
85             return null;
86         }
87         return session.getAttribute( this.attributePrefix + key);
88     }
89
90     public void setAttribute(String JavaDoc key, Object JavaDoc value) {
91         final Session session = ObjectModelHelper.getRequest(this.objectModel).getSession();
92         session.setAttribute( this.attributePrefix + key, value);
93     }
94
95     public void removeAttribute(String JavaDoc key) {
96         final Session session = ObjectModelHelper.getRequest(this.objectModel).getSession(false);
97         if ( session != null ) {
98             session.removeAttribute( this.attributePrefix + key);
99         }
100     }
101
102     public Iterator JavaDoc getAttributeNames() {
103         final Session session = ObjectModelHelper.getRequest(this.objectModel).getSession(false);
104         if ( session != null ) {
105             List JavaDoc names = new ArrayList JavaDoc();
106             Enumeration JavaDoc e = session.getAttributeNames();
107             final int pos = this.attributePrefix.length() + 1;
108             if ( e != null ) {
109                 while ( e.hasMoreElements() ) {
110                     final String JavaDoc name = (String JavaDoc)e.nextElement();
111                     if ( name.startsWith( this.attributePrefix )) {
112                         names.add( name.substring( pos ) );
113                     }
114                 }
115             }
116             return names.iterator();
117         }
118         return Collections.EMPTY_MAP.keySet().iterator();
119     }
120
121     public Object JavaDoc getTemporaryAttribute(String JavaDoc key) {
122         return this.temporaryAttributes.get( key );
123     }
124     
125     public void setTemporaryAttribute(String JavaDoc key, Object JavaDoc value) {
126         this.temporaryAttributes.put( key, value );
127     }
128     
129     public void removeTemporaryAttribute(String JavaDoc key) {
130         this.temporaryAttributes.remove( key );
131     }
132     
133     public Iterator JavaDoc getTemporaryAttributeNames() {
134         return this.temporaryAttributes.keySet().iterator();
135     }
136
137     /**
138      * Return the component manager for the current portal
139      */

140     public PortalComponentManager getComponentManager() {
141         return this.portalComponentManager;
142     }
143
144 }
145
Popular Tags