001: package org.apache.turbine.services.naming;
002:
003: /*
004: * Copyright 2001-2005 The Apache Software Foundation.
005: *
006: * Licensed under the Apache License, Version 2.0 (the "License")
007: * you may not use this file except in compliance with the License.
008: * You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS,
014: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: import java.util.HashMap;
020: import java.util.Iterator;
021: import java.util.Map;
022: import java.util.Properties;
023:
024: import javax.naming.Context;
025: import javax.naming.InitialContext;
026:
027: import org.apache.commons.configuration.Configuration;
028:
029: import org.apache.commons.logging.Log;
030: import org.apache.commons.logging.LogFactory;
031:
032: import org.apache.turbine.Turbine;
033: import org.apache.turbine.services.InitializationException;
034: import org.apache.turbine.services.TurbineBaseService;
035: import org.apache.turbine.util.RunData;
036:
037: /**
038: * This class is the default implementation of NamingService, which
039: * provides JNDI naming contexts.
040: *
041: * @author <a href="mailto:greg@shwoop.com">Greg Ritter</a>
042: * @author <a href="mailto:colin.chalmers@maxware.nl">Colin Chalmers</a>
043: * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
044: * @version $Id: TurbineNamingService.java 264148 2005-08-29 14:21:04Z henning $
045: */
046: public class TurbineNamingService extends TurbineBaseService implements
047: NamingService {
048: /** Logging */
049: private static Log log = LogFactory
050: .getLog(TurbineNamingService.class);
051:
052: /**
053: * A global Map of Property objects which are initialised using
054: * parameters from the ResourcesFile
055: */
056: private Map contextPropsList = null;
057:
058: /** All initial contexts known to this service */
059: private Map initialContexts = new HashMap();
060:
061: /**
062: * Called the first time the Service is used.<br>
063: *
064: */
065: public void init() throws InitializationException {
066: // Context properties are specified in lines in the properties
067: // file that begin with "context.contextname.", allowing
068: // multiple named contexts to be used. Everything after the
069: // "contextname." is the name of the property that will be
070: // used by the InitialContext class to create a new context
071: // instance.
072:
073: Configuration conf = Turbine.getConfiguration();
074: try {
075: contextPropsList = new HashMap();
076:
077: for (Iterator contextKeys = conf.subset("context")
078: .getKeys(); contextKeys.hasNext();) {
079: String key = (String) contextKeys.next();
080: int end = key.indexOf(".");
081:
082: if (end == -1) {
083: continue;
084: }
085:
086: String contextName = key.substring(0, end);
087: Properties contextProps = null;
088:
089: if (contextPropsList.containsKey(contextName)) {
090: contextProps = (Properties) contextPropsList
091: .get(contextName);
092: } else {
093: contextProps = new Properties();
094: }
095:
096: contextProps.put(key.substring(end + 1), conf
097: .getString(key));
098:
099: contextPropsList.put(contextName, contextProps);
100: }
101:
102: for (Iterator contextPropsKeys = contextPropsList.keySet()
103: .iterator(); contextPropsKeys.hasNext();) {
104: String key = (String) contextPropsKeys.next();
105: Properties contextProps = (Properties) contextPropsList
106: .get(key);
107: InitialContext context = new InitialContext(
108: contextProps);
109: initialContexts.put(key, context);
110: }
111:
112: setInit(true);
113: } catch (Exception e) {
114: log.error("Failed to initialize JDNI contexts!", e);
115:
116: throw new InitializationException(
117: "Failed to initialize JDNI contexts!");
118: }
119: }
120:
121: /**
122: * Places the contexts defined in the TurbineResources instance
123: * (if any) into the data.contexts Map.
124: *
125: * @param data The RunData object for the current request.
126: * @exception InitializationException, if there was a problem
127: * during initialization.
128: * @deprecated This should never have been here. No replacement.
129: */
130: public void init(RunData data) throws InitializationException {
131: try {
132: if (contextPropsList == null) {
133: init();
134: }
135:
136: for (Iterator it = contextPropsList.keySet().iterator(); it
137: .hasNext();) {
138: String key = (String) it.next();
139: Properties contextProps = (Properties) contextPropsList
140: .get(key);
141: InitialContext context = new InitialContext(
142: contextProps);
143: data.getJNDIContexts().put(key, context);
144: }
145: } catch (Exception e) {
146: log.error("Failed to initialize JDNI contexts!", e);
147:
148: throw new InitializationException(
149: "Failed to initialize JDNI contexts!");
150: }
151: }
152:
153: /**
154: * Return the Context with the specified name. The Context is
155: * constructed using the properties for the context with the
156: * specified name; ie. those properties that start with
157: * "services.servicename.properties.name.".
158: *
159: * @param contextName The name of the context.
160: * @return The context with the specified name, or null if no
161: * context exists with that name.
162: */
163: public Context getContext(String contextName) {
164: // Get just the properties for the context with the specified
165: // name.
166: Properties contextProps = null;
167:
168: if (contextPropsList.containsKey(contextName)) {
169: contextProps = (Properties) contextPropsList
170: .get(contextName);
171: } else {
172: contextProps = new Properties();
173: }
174:
175: // Construct a new context with the properties.
176: try {
177: return new InitialContext(contextProps);
178: } catch (Exception e) {
179: return null;
180: }
181: }
182: }
|