KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > turbine > modules > actions > InitContextsAction


1 package org.apache.turbine.modules.actions;
2
3 /*
4  * Copyright 2001-2004 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 JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.Properties JavaDoc;
22 import javax.naming.InitialContext JavaDoc;
23 import javax.naming.NamingException JavaDoc;
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,v 1.8.2.2 2004/05/20 03:03:53 seade Exp $
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 JavaDoc
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 JavaDoc contextPropsList = new Hashtable JavaDoc();
62         for (Iterator JavaDoc contextKeys = conf.getKeys("context.");
63                 contextKeys.hasNext();)
64         {
65             String JavaDoc key = (String JavaDoc) contextKeys.next();
66             int start = key.indexOf(".") + 1;
67             int end = key.indexOf(".", start);
68             String JavaDoc contextName = key.substring(start, end);
69             Properties JavaDoc contextProps = null;
70             if (contextPropsList.containsKey(contextName))
71             {
72                 contextProps = (Properties JavaDoc) contextPropsList.get(contextName);
73             }
74             else
75             {
76                 contextProps = new Properties JavaDoc();
77             }
78             contextProps.put(key.substring(end + 1),
79                              conf.getString(key));
80             contextPropsList.put(contextName, contextProps);
81         }
82         for (Iterator JavaDoc contextPropsKeys = contextPropsList.keySet().iterator();
83                 contextPropsKeys.hasNext();)
84         {
85             String JavaDoc key = (String JavaDoc) contextPropsKeys.next();
86             Properties JavaDoc contextProps = (Properties JavaDoc) contextPropsList.get(key);
87             InitialContext JavaDoc context = new InitialContext JavaDoc(contextProps);
88             data.getJNDIContexts().put(key, context);
89         }
90     }
91 }
92
Popular Tags