View Javadoc

1   package org.apache.turbine.modules.actions;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "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
42          extends Action
43  {
44      /***
45       * This action will place the contexts defined in the
46       * TurbineResources instance (if any) into the data.contexts
47       * Hashtable.
48       *
49       * @param data The RunData object for the current request.
50       * @exception NamingException could not create InitialContext
51       */
52      public void doPerform(RunData data)
53              throws NamingException
54      {
55          Configuration conf = Turbine.getConfiguration();
56  
57          // Context properties are specified in lines in the properties
58          // file that begin with "context.contextname.", allowing
59          // multiple named contexts to be used.  Everything after the
60          // "contextname." is the name of the property that will be
61          // used by the InitialContext class to create a new context
62          // instance.
63  
64          Hashtable contextPropsList = new Hashtable();
65          for (Iterator contextKeys = conf.getKeys("context.");
66                  contextKeys.hasNext();)
67          {
68              String key = (String) contextKeys.next();
69              int start = key.indexOf(".") + 1;
70              int end = key.indexOf(".", start);
71              String contextName = key.substring(start, end);
72              Properties contextProps = null;
73              if (contextPropsList.containsKey(contextName))
74              {
75                  contextProps = (Properties) contextPropsList.get(contextName);
76              }
77              else
78              {
79                  contextProps = new Properties();
80              }
81              contextProps.put(key.substring(end + 1),
82                               conf.getString(key));
83              contextPropsList.put(contextName, contextProps);
84          }
85          for (Iterator contextPropsKeys = contextPropsList.keySet().iterator();
86                  contextPropsKeys.hasNext();)
87          {
88              String key = (String) contextPropsKeys.next();
89              Properties contextProps = (Properties) contextPropsList.get(key);
90              InitialContext context = new InitialContext(contextProps);
91              data.getJNDIContexts().put(key, context);
92          }
93      }
94  }