KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > webapps > session > components > ContextInputModule


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
17 package org.apache.cocoon.webapps.session.components;
18
19 import java.util.Iterator JavaDoc;
20 import java.util.Map JavaDoc;
21
22 import org.apache.avalon.framework.activity.Disposable;
23 import org.apache.avalon.framework.configuration.Configuration;
24 import org.apache.avalon.framework.configuration.ConfigurationException;
25 import org.apache.avalon.framework.service.ServiceException;
26 import org.apache.avalon.framework.service.ServiceManager;
27 import org.apache.avalon.framework.service.Serviceable;
28 import org.apache.avalon.framework.thread.ThreadSafe;
29 import org.apache.cocoon.ProcessingException;
30 import org.apache.cocoon.components.modules.input.InputModule;
31 import org.apache.cocoon.webapps.session.ContextManager;
32 import org.apache.cocoon.webapps.session.context.SessionContext;
33 import org.apache.cocoon.xml.dom.DOMUtil;
34 import org.w3c.dom.DocumentFragment JavaDoc;
35
36 /**
37  * This input module provides access to the information of a session
38  * context using an XPath. The XPath expression that can be used
39  * is the same as for the session transformer.
40  * The first information in the path is the context, so for example
41  * {session-context:authentication/authentication/ID} delivers the ID of the
42  * current user and therefore delivers the same information as:
43  * <session:getxml context="authentication" path="/authentication/ID"/>
44  * using the session transformer.
45  *
46  * @author <a HREF="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
47  * @version CVS $Id: ContextInputModule.java 30932 2004-07-29 17:35:38Z vgritsenko $
48  */

49 public class ContextInputModule
50     implements ThreadSafe, Serviceable, Disposable, InputModule {
51
52     protected ServiceManager manager;
53     
54     protected ContextManager contextManager;
55     
56     /* (non-Javadoc)
57      * @see org.apache.cocoon.components.modules.input.InputModule#getAttribute(java.lang.String, org.apache.avalon.framework.configuration.Configuration, java.util.Map)
58      */

59     public Object JavaDoc getAttribute(String JavaDoc name,
60                                Configuration modeConf,
61                                Map JavaDoc objectModel)
62     throws ConfigurationException {
63         if ( name == null ) {
64             return null;
65         }
66         int pos = name.indexOf('/');
67         if ( pos != -1 ) {
68             final String JavaDoc contextName = name.substring(0, pos);
69             final String JavaDoc path = name.substring(pos);
70             
71             try {
72                 SessionContext context = this.contextManager.getContext(contextName);
73                 if ( context != null ) {
74                     DocumentFragment JavaDoc frag = context.getXML(path);
75                     return DOMUtil.getValueOfNode(frag);
76                 }
77             } catch (ProcessingException pe) {
78                 throw new ConfigurationException("Unable to get information from context.", pe);
79             }
80             
81         }
82         return null;
83     }
84
85     /* (non-Javadoc)
86      * @see org.apache.cocoon.components.modules.input.InputModule#getAttributeNames(org.apache.avalon.framework.configuration.Configuration, java.util.Map)
87      */

88     public Iterator JavaDoc getAttributeNames(Configuration modeConf, Map JavaDoc objectModel)
89     throws ConfigurationException {
90         return null;
91     }
92
93     /* (non-Javadoc)
94      * @see org.apache.cocoon.components.modules.input.InputModule#getAttributeValues(java.lang.String, org.apache.avalon.framework.configuration.Configuration, java.util.Map)
95      */

96     public Object JavaDoc[] getAttributeValues(String JavaDoc name,
97                                         Configuration modeConf,
98                                         Map JavaDoc objectModel)
99     throws ConfigurationException {
100         final Object JavaDoc value = this.getAttribute(name, modeConf, objectModel);
101         if ( value != null ) {
102             return new Object JavaDoc[] {value};
103         }
104         return null;
105     }
106
107     /* (non-Javadoc)
108      * @see org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager)
109      */

110     public void service(ServiceManager manager) throws ServiceException {
111         this.manager = manager;
112         this.contextManager = (ContextManager) this.manager.lookup(ContextManager.ROLE);
113     }
114
115     /* (non-Javadoc)
116      * @see org.apache.avalon.framework.activity.Disposable#dispose()
117      */

118     public void dispose() {
119         if ( this.manager != null ) {
120             this.manager.release(this.contextManager);
121             this.contextManager = null;
122             this.manager = null;
123         }
124
125     }
126
127 }
128
Popular Tags