KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > webapps > session > context > StandardSessionContextProvider


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.session.context;
17
18 import java.util.Map JavaDoc;
19
20 import org.apache.avalon.framework.activity.Disposable;
21 import org.apache.avalon.framework.component.Component;
22 import org.apache.avalon.framework.context.Context;
23 import org.apache.avalon.framework.context.ContextException;
24 import org.apache.avalon.framework.context.Contextualizable;
25 import org.apache.avalon.framework.logger.AbstractLogEnabled;
26 import org.apache.avalon.framework.service.ServiceException;
27 import org.apache.avalon.framework.service.ServiceManager;
28 import org.apache.avalon.framework.service.Serviceable;
29 import org.apache.avalon.framework.thread.ThreadSafe;
30 import org.apache.cocoon.ProcessingException;
31 import org.apache.cocoon.components.ContextHelper;
32 import org.apache.cocoon.environment.ObjectModelHelper;
33 import org.apache.cocoon.webapps.session.SessionConstants;
34 import org.apache.excalibur.source.SourceResolver;
35 import org.apache.excalibur.xml.xpath.XPathProcessor;
36
37 /**
38  * Context provider for the temporarily context, the request and the
39  * response context.
40  *
41  * @author <a HREF="mailto:cziegeler@s-und-n.de">Carsten Ziegeler</a>
42  * @version CVS $Id: StandardSessionContextProvider.java 306580 2005-10-06 10:33:31Z cziegeler $
43 */

44 public final class StandardSessionContextProvider
45 extends AbstractLogEnabled
46 implements SessionContextProvider, ThreadSafe, Contextualizable, Serviceable, Component, Disposable {
47
48     protected Context context;
49     
50     protected ServiceManager manager;
51     
52     /** The xpath processor */
53     protected XPathProcessor xpathProcessor;
54
55     /** The Source Resolver */
56     protected SourceResolver resolver;
57     
58     /**
59      * Get the context
60      * @param name The name of the context
61      * @return The context
62      * @throws ProcessingException If the context is not available.
63      */

64     public SessionContext getSessionContext(String JavaDoc name)
65     throws ProcessingException {
66         final Map JavaDoc objectModel = ContextHelper.getObjectModel( this.context );
67         
68         // get the context from the object model
69
SessionContext context = this.getContext( objectModel, name );
70         if ( context == null ) {
71             if ( name.equals(SessionConstants.TEMPORARY_CONTEXT) ) {
72                 context = new SimpleSessionContext(this.xpathProcessor, this.resolver);
73                 context.setup(name, null, null);
74             } else if ( name.equals(SessionConstants.REQUEST_CONTEXT) ) {
75                 context = new RequestSessionContext(this.getLogger());
76                 context.setup(name, null, null);
77                 ((RequestSessionContext)context).setup( objectModel, this.manager, this.xpathProcessor );
78             }
79             objectModel.put(this.getClass().getName()+name, context);
80         }
81         return context;
82     }
83
84     /**
85      * Does the context exist?
86      */

87     public boolean existsSessionContext(String JavaDoc name)
88     throws ProcessingException {
89         final Map JavaDoc objectModel = ContextHelper.getObjectModel(this.context);
90         return (this.getContext( objectModel, name) != null);
91     }
92
93     private SessionContext getContext(Map JavaDoc objectModel, String JavaDoc name) {
94         SessionContext context = (SessionContext) objectModel.get(this.getClass().getName()+name);
95         if ( context != null && !name.equals(SessionConstants.TEMPORARY_CONTEXT)) {
96             if ( name.equals(SessionConstants.REQUEST_CONTEXT)) {
97                 RequestSessionContext r = (RequestSessionContext)context;
98                 if (!(r.getRequest() == ObjectModelHelper.getRequest( objectModel))) {
99                     context = null;
100                     objectModel.remove(this.getClass().getName()+name);
101                 }
102             }
103         }
104         return context;
105     }
106     
107     /* (non-Javadoc)
108      * @see org.apache.avalon.framework.context.Contextualizable#contextualize(org.apache.avalon.framework.context.Context)
109      */

110     public void contextualize(Context context) throws ContextException {
111         this.context = context;
112     }
113
114     /* (non-Javadoc)
115      * @see org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager)
116      */

117     public void service(ServiceManager manager) throws ServiceException {
118         this.manager = manager;
119         this.xpathProcessor = (XPathProcessor)this.manager.lookup(XPathProcessor.ROLE);
120         this.resolver = (SourceResolver)this.manager.lookup(SourceResolver.ROLE);
121     }
122
123     /* (non-Javadoc)
124      * @see org.apache.avalon.framework.activity.Disposable#dispose()
125      */

126     public void dispose() {
127         if ( this.manager != null) {
128             this.manager.release( this.xpathProcessor );
129             this.manager.release(this.resolver);
130             this.resolver = null;
131             this.xpathProcessor = null;
132             this.manager = null;
133         }
134     }
135
136 }
137
Popular Tags