KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > modules > input > NamingInputModule


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.components.modules.input;
17
18 import org.apache.avalon.framework.activity.Initializable;
19 import org.apache.avalon.framework.configuration.Configuration;
20 import org.apache.avalon.framework.configuration.ConfigurationException;
21 import org.apache.avalon.framework.thread.ThreadSafe;
22
23 import javax.naming.InitialContext JavaDoc;
24 import javax.naming.NamingException JavaDoc;
25 import java.util.Collections JavaDoc;
26 import java.util.Iterator JavaDoc;
27 import java.util.Map JavaDoc;
28 import java.util.Properties JavaDoc;
29
30 /**
31  * NamingInputModule accesses values stored in the JNDI context.
32  *
33  * <p>This module accept any configuration parameters and passes them as
34  * properties to the InitialContext. When connecting to the Naming context
35  * of the server Cocoon is running in, no parameters are required.</p>
36  *
37  * <p>Example module configuration when connecting to external WebLogic server:
38  * <pre>
39  * &lt;java.naming.factory.initial&gt;weblogic.jndi.WLInitialContextFactory&lt;/java.naming.factory.initial&gt;
40  * &lt;java.naming.provider.url&gt;t3://localhost:7001&lt;/java.naming.provider.url&gt;
41  * </pre>
42  *
43  * <p>Example usage:
44  * <pre>
45  * &lt;map:generate SRC="{naming:java:comp/env/greeting}"/&gt;
46  * </pre>
47  * This lookups <code>greeting</code> entry from the environment of the webapp.
48  * Webapp's web.xml should define this entry:
49  * <pre>
50  * &lt;env-entry&gt;
51  * &lt;env-entry-name&gt;greeting&lt;/env-entry-name&gt;
52  * &lt;env-entry-value&gt;Hello, World&lt;/env-entry-value&gt;
53  * &lt;env-entry-type&gt;java.lang.String&lt;/env-entry-type&gt;
54  * &lt;/env-entry&gt;
55  * </pre>
56  *
57  * @author <a HREF="mailto:vgritsenko@apache.org">Vadim Gritsenko</a>
58  * @version CVS $Id: NamingInputModule.java 36239 2004-08-11 18:28:06Z vgritsenko $
59  */

60 public class NamingInputModule extends AbstractInputModule implements ThreadSafe, Initializable {
61
62     /**
63      * Initial context properties.
64      */

65     private Properties JavaDoc properties;
66
67     /**
68      * Initial context.
69      */

70     private InitialContext JavaDoc context;
71
72     /**
73      * Fill in InitialContext properties from passed configuration.
74      */

75     public void configure(Configuration conf) throws ConfigurationException {
76         Configuration[] parameters = conf.getChildren();
77         this.properties = new Properties JavaDoc();
78         for (int i = 0; i < parameters.length; i++) {
79             String JavaDoc key = parameters[i].getName();
80             String JavaDoc val = parameters[i].getValue("");
81             this.properties.put(key, val);
82         }
83     }
84
85     /**
86      * Creates InitialContext with configured properties.
87      */

88     public void initialize() throws Exception JavaDoc {
89         this.context = new InitialContext JavaDoc(this.properties);
90     }
91
92     /**
93      * Close InitialContext.
94      */

95     public void dispose() {
96         super.dispose();
97         if (this.context != null) {
98             try {
99                 this.context.close();
100             } catch (NamingException JavaDoc ignored) {
101             }
102         }
103     }
104
105     /**
106      * Look up <code>name</code> from the InitialContext.
107      */

108     public Object JavaDoc getAttribute(String JavaDoc name, Configuration modeConf, Map JavaDoc objectModel)
109     throws ConfigurationException {
110
111         // Why properties can override passed name parameter? See RequestParameterModule
112
String JavaDoc pname = (String JavaDoc) this.properties.get("path");
113         if (pname == null) {
114             pname = name;
115         }
116
117         if (modeConf != null) {
118             pname = modeConf.getAttribute("path", pname);
119             // preferred
120
pname = modeConf.getChild("path").getValue(pname);
121         }
122
123         try {
124             return this.context.lookup(pname);
125         } catch (NamingException JavaDoc e) {
126             if (getLogger().isDebugEnabled()) {
127                 getLogger().debug("Can't get parameter " + pname, e);
128             }
129             return null;
130         }
131     }
132
133     /**
134      * Returns empty iterator
135      */

136     public Iterator JavaDoc getAttributeNames(Configuration modeConf, Map JavaDoc objectModel)
137     throws ConfigurationException {
138
139         return Collections.EMPTY_LIST.iterator();
140     }
141 }
142
Popular Tags