01: package org.apache.turbine.modules.actions;
02:
03: /*
04: * Licensed to the Apache Software Foundation (ASF) under one
05: * or more contributor license agreements. See the NOTICE file
06: * distributed with this work for additional information
07: * regarding copyright ownership. The ASF licenses this file
08: * to you under the Apache License, Version 2.0 (the
09: * "License"); you may not use this file except in compliance
10: * with the License. You may obtain a copy of the License at
11: *
12: * http://www.apache.org/licenses/LICENSE-2.0
13: *
14: * Unless required by applicable law or agreed to in writing,
15: * software distributed under the License is distributed on an
16: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17: * KIND, either express or implied. See the License for the
18: * specific language governing permissions and limitations
19: * under the License.
20: */
21:
22: import java.util.Hashtable;
23: import java.util.Iterator;
24: import java.util.Properties;
25: import javax.naming.InitialContext;
26: import javax.naming.NamingException;
27:
28: import org.apache.commons.configuration.Configuration;
29:
30: import org.apache.turbine.Turbine;
31: import org.apache.turbine.modules.Action;
32: import org.apache.turbine.util.RunData;
33:
34: /**
35: * Used to initialize JNDI contexts.
36: *
37: * @author <a href="mailto:greg@shwoop.com">Greg Ritter</a>
38: * @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
39: * @version $Id: InitContextsAction.java 534527 2007-05-02 16:10:59Z tv $
40: */
41: public class InitContextsAction extends Action {
42: /**
43: * This action will place the contexts defined in the
44: * TurbineResources instance (if any) into the data.contexts
45: * Hashtable.
46: *
47: * @param data The RunData object for the current request.
48: * @exception NamingException could not create InitialContext
49: */
50: public void doPerform(RunData data) throws NamingException {
51: Configuration conf = Turbine.getConfiguration();
52:
53: // Context properties are specified in lines in the properties
54: // file that begin with "context.contextname.", allowing
55: // multiple named contexts to be used. Everything after the
56: // "contextname." is the name of the property that will be
57: // used by the InitialContext class to create a new context
58: // instance.
59:
60: Hashtable contextPropsList = new Hashtable();
61: for (Iterator contextKeys = conf.getKeys("context."); contextKeys
62: .hasNext();) {
63: String key = (String) contextKeys.next();
64: int start = key.indexOf(".") + 1;
65: int end = key.indexOf(".", start);
66: String contextName = key.substring(start, end);
67: Properties contextProps = null;
68: if (contextPropsList.containsKey(contextName)) {
69: contextProps = (Properties) contextPropsList
70: .get(contextName);
71: } else {
72: contextProps = new Properties();
73: }
74: contextProps.put(key.substring(end + 1), conf
75: .getString(key));
76: contextPropsList.put(contextName, contextProps);
77: }
78: for (Iterator contextPropsKeys = contextPropsList.keySet()
79: .iterator(); contextPropsKeys.hasNext();) {
80: String key = (String) contextPropsKeys.next();
81: Properties contextProps = (Properties) contextPropsList
82: .get(key);
83: InitialContext context = new InitialContext(contextProps);
84: data.getJNDIContexts().put(key, context);
85: }
86: }
87: }
|