001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.components.modules.input;
018:
019: import org.apache.avalon.framework.activity.Initializable;
020: import org.apache.avalon.framework.configuration.Configuration;
021: import org.apache.avalon.framework.configuration.ConfigurationException;
022: import org.apache.avalon.framework.thread.ThreadSafe;
023:
024: import javax.naming.InitialContext;
025: import javax.naming.NamingException;
026: import java.util.Collections;
027: import java.util.Iterator;
028: import java.util.Map;
029: import java.util.Properties;
030:
031: /**
032: * NamingInputModule accesses values stored in the JNDI context.
033: *
034: * <p>This module accept any configuration parameters and passes them as
035: * properties to the InitialContext. When connecting to the Naming context
036: * of the server Cocoon is running in, no parameters are required.</p>
037: *
038: * <p>Example module configuration when connecting to external WebLogic server:
039: * <pre>
040: * <java.naming.factory.initial>weblogic.jndi.WLInitialContextFactory</java.naming.factory.initial>
041: * <java.naming.provider.url>t3://localhost:7001</java.naming.provider.url>
042: * </pre>
043: *
044: * <p>Example usage:
045: * <pre>
046: * <map:generate src="{naming:java:comp/env/greeting}"/>
047: * </pre>
048: * This lookups <code>greeting</code> entry from the environment of the webapp.
049: * Webapp's web.xml should define this entry:
050: * <pre>
051: * <env-entry>
052: * <env-entry-name>greeting</env-entry-name>
053: * <env-entry-value>Hello, World</env-entry-value>
054: * <env-entry-type>java.lang.String</env-entry-type>
055: * </env-entry>
056: * </pre>
057: *
058: * @author <a href="mailto:vgritsenko@apache.org">Vadim Gritsenko</a>
059: * @version $Id: NamingInputModule.java 36239 2004-08-11 18:28:06Z vgritsenko $
060: */
061: public class NamingInputModule extends AbstractInputModule implements
062: ThreadSafe, Initializable {
063:
064: /**
065: * Initial context properties.
066: */
067: private Properties properties;
068:
069: /**
070: * Initial context.
071: */
072: private InitialContext context;
073:
074: /**
075: * Fill in InitialContext properties from passed configuration.
076: */
077: public void configure(Configuration conf)
078: throws ConfigurationException {
079: Configuration[] parameters = conf.getChildren();
080: this .properties = new Properties();
081: for (int i = 0; i < parameters.length; i++) {
082: String key = parameters[i].getName();
083: String val = parameters[i].getValue("");
084: this .properties.put(key, val);
085: }
086: }
087:
088: /**
089: * Creates InitialContext with configured properties.
090: */
091: public void initialize() throws Exception {
092: this .context = new InitialContext(this .properties);
093: }
094:
095: /**
096: * Close InitialContext.
097: */
098: public void dispose() {
099: super .dispose();
100: if (this .context != null) {
101: try {
102: this .context.close();
103: } catch (NamingException ignored) {
104: }
105: }
106: }
107:
108: /**
109: * Look up <code>name</code> from the InitialContext.
110: */
111: public Object getAttribute(String name, Configuration modeConf,
112: Map objectModel) throws ConfigurationException {
113:
114: // Why properties can override passed name parameter? See RequestParameterModule
115: String pname = (String) this .properties.get("path");
116: if (pname == null) {
117: pname = name;
118: }
119:
120: if (modeConf != null) {
121: pname = modeConf.getAttribute("path", pname);
122: // preferred
123: pname = modeConf.getChild("path").getValue(pname);
124: }
125:
126: try {
127: return this .context.lookup(pname);
128: } catch (NamingException e) {
129: if (getLogger().isDebugEnabled()) {
130: getLogger().debug("Can't get parameter " + pname, e);
131: }
132: return null;
133: }
134: }
135:
136: /**
137: * Returns empty iterator
138: */
139: public Iterator getAttributeNames(Configuration modeConf,
140: Map objectModel) throws ConfigurationException {
141:
142: return Collections.EMPTY_LIST.iterator();
143: }
144: }
|