KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > webapps > portal > context > SessionContextProviderImpl


1 /*
2  * Copyright 1999-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.webapps.portal.context;
17
18 import java.io.IOException JavaDoc;
19 import java.util.HashMap JavaDoc;
20 import java.util.Map JavaDoc;
21
22 import org.apache.avalon.framework.activity.Disposable;
23 import org.apache.avalon.framework.component.Component;
24 import org.apache.avalon.framework.context.Context;
25 import org.apache.avalon.framework.context.ContextException;
26 import org.apache.avalon.framework.context.Contextualizable;
27 import org.apache.avalon.framework.logger.AbstractLogEnabled;
28 import org.apache.avalon.framework.service.ServiceException;
29 import org.apache.avalon.framework.service.ServiceManager;
30 import org.apache.avalon.framework.service.Serviceable;
31 import org.apache.avalon.framework.thread.ThreadSafe;
32 import org.apache.cocoon.ProcessingException;
33 import org.apache.cocoon.components.ContextHelper;
34 import org.apache.cocoon.environment.ObjectModelHelper;
35 import org.apache.cocoon.environment.Request;
36 import org.apache.cocoon.environment.Session;
37 import org.apache.cocoon.webapps.portal.PortalConstants;
38 import org.apache.cocoon.webapps.portal.components.PortalManager;
39 import org.apache.cocoon.webapps.session.context.SessionContext;
40 import org.apache.cocoon.webapps.session.context.SessionContextProvider;
41 import org.apache.excalibur.source.SourceParameters;
42 import org.apache.excalibur.xml.xpath.XPathProcessor;
43 import org.xml.sax.SAXException JavaDoc;
44
45 /**
46  * Context provider for the portal context
47  *
48  * @author <a HREF="mailto:cziegeler@s-und-n.de">Carsten Ziegeler</a>
49  * @version CVS $Id: SessionContextProviderImpl.java 30932 2004-07-29 17:35:38Z vgritsenko $
50 */

51 public final class SessionContextProviderImpl extends AbstractLogEnabled
52         implements SessionContextProvider, ThreadSafe, Component, Serviceable, Contextualizable, Disposable {
53
54     private ServiceManager manager;
55     private Context context;
56     
57     /** The XPath Processor */
58     protected XPathProcessor xpathProcessor;
59     
60     /**
61      * Get the context
62      * @param name The name of the context
63      * @return The context
64      * @throws ProcessingException If the context is not available.
65      */

66     public SessionContext getSessionContext(String JavaDoc name)
67     throws ProcessingException {
68         Map JavaDoc objectModel = ContextHelper.getObjectModel(this.context);
69
70         SessionContext context = this.getContext( objectModel, name );
71         if (name.equals(PortalConstants.SESSION_CONTEXT_NAME) && context == null) {
72             Request req = ObjectModelHelper.getRequest(objectModel);
73             Session session = req.getSession(false);
74             if (session != null) {
75
76                 PortalManager portal = null;
77                 try {
78                     portal = (PortalManager)manager.lookup(PortalManager.ROLE);
79                     // is this an external resource which wants access to a coplet?
80
String JavaDoc value = req.getParameter("portalcontext");
81                     if (value != null) {
82                         int sepOne, sepTwo;
83                         sepOne = value.indexOf('_');
84                         if (sepOne != -1) {
85                             sepTwo = value.indexOf('_', sepOne+1);
86                             if (sepTwo != -1) {
87                                 String JavaDoc copletIdentifier = value.substring(0, sepOne);
88                                 String JavaDoc copletID = value.substring(sepOne+1, sepTwo);
89                                 String JavaDoc copletNumber = value.substring(sepTwo+1);
90
91                                 if (copletIdentifier.equals("coplet")) {
92                                     Map JavaDoc info = new HashMap JavaDoc(3);
93                                     SessionContextImpl.copletInfo.set(info);
94
95                                     SourceParameters pars = new SourceParameters();
96                                     info.put(PortalConstants.COPLETINFO_PARAMETERS, pars);
97                                     pars.setSingleParameterValue(PortalConstants.PARAMETER_ID, copletID);
98                                     pars.setSingleParameterValue(PortalConstants.PARAMETER_NUMBER, copletNumber);
99                                     pars.setSingleParameterValue(PortalConstants.PARAMETER_MEDIA, portal.getMediaType());
100
101                                     info.put(PortalConstants.COPLETINFO_STATUSPROFILE, portal.getStatusProfile());
102                                     info.put(PortalConstants.COPLETINFO_PORTALURI, req.getRequestURI());
103                                 }
104                             }
105                         }
106                     } else {
107                         if (SessionContextImpl.copletInfo.get() == null) {
108                             throw new ProcessingException("Portal context not available outside a coplet.");
109                         }
110                     }
111                     context = new SessionContextImpl(name, objectModel, portal, this.xpathProcessor);
112                     objectModel.put(this.getClass().getName()+name, context);
113                 } catch (SAXException JavaDoc se) {
114                     throw new ProcessingException("SAXException", se);
115                 } catch (IOException JavaDoc ioe) {
116                     throw new ProcessingException("IOException", ioe);
117                 } catch (ServiceException se) {
118                     throw new ProcessingException("Unable to lookup portal.", se);
119                 } finally {
120                     manager.release(portal);
121                 }
122             }
123         }
124         return context;
125     }
126     
127     /**
128      * Does the context exist?
129      */

130     public boolean existsSessionContext(String JavaDoc name)
131     throws ProcessingException {
132         final Map JavaDoc objectModel = ContextHelper.getObjectModel(this.context);
133         return (this.getContext( objectModel, name) != null);
134     }
135
136     private SessionContext getContext(Map JavaDoc objectModel, String JavaDoc name) {
137         SessionContext context = (SessionContext) objectModel.get(this.getClass().getName()+name);
138         if ( context != null ) {
139             SessionContextImpl r = (SessionContextImpl)context;
140             if (!(r.getRequest() == ObjectModelHelper.getRequest( objectModel))) {
141                 context = null;
142                 objectModel.remove(this.getClass().getName()+name);
143             }
144         }
145         return context;
146     }
147     
148     /* (non-Javadoc)
149      * @see org.apache.avalon.framework.service.Serviceable#Service(org.apache.avalon.framework.Service.ServiceManager)
150      */

151     public void service(ServiceManager manager) throws ServiceException {
152         this.manager = manager;
153         this.xpathProcessor = (XPathProcessor)this.manager.lookup(XPathProcessor.ROLE);
154     }
155
156     /* (non-Javadoc)
157      * @see org.apache.avalon.framework.context.Contextualizable#contextualize(org.apache.avalon.framework.context.Context)
158      */

159     public void contextualize(Context context) throws ContextException {
160         this.context = context;
161     }
162
163     /* (non-Javadoc)
164      * @see org.apache.avalon.framework.activity.Disposable#dispose()
165      */

166     public void dispose() {
167         if ( this.manager != null ) {
168             this.manager.release(this.xpathProcessor);
169             this.xpathProcessor = null;
170             this.manager = null;
171         }
172     }
173 }
174
Popular Tags