View Javadoc

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