KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > turbine > services > naming > TurbineNamingService


1 package org.apache.turbine.services.naming;
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.HashMap JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.Map JavaDoc;
22 import java.util.Properties JavaDoc;
23
24 import javax.naming.Context JavaDoc;
25 import javax.naming.InitialContext JavaDoc;
26
27 import org.apache.commons.configuration.Configuration;
28
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31
32 import org.apache.turbine.Turbine;
33 import org.apache.turbine.services.InitializationException;
34 import org.apache.turbine.services.TurbineBaseService;
35 import org.apache.turbine.util.RunData;
36
37 /**
38  * This class is the default implementation of NamingService, which
39  * provides JNDI naming contexts.
40  *
41  * @author <a HREF="mailto:greg@shwoop.com">Greg Ritter</a>
42  * @author <a HREF="mailto:colin.chalmers@maxware.nl">Colin Chalmers</a>
43  * @author <a HREF="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
44  * @version $Id: TurbineNamingService.java,v 1.11.2.2 2004/05/20 03:06:51 seade Exp $
45  */

46 public class TurbineNamingService
47         extends TurbineBaseService
48         implements NamingService
49 {
50     /** Logging */
51     private static Log log = LogFactory.getLog(TurbineNamingService.class);
52
53     /**
54      * A global Map of Property objects which are initialised using
55      * parameters from the ResourcesFile
56      */

57     private static Map JavaDoc contextPropsList = null;
58
59     /** All initial contexts known to this service */
60     private Map JavaDoc initialContexts = new HashMap JavaDoc();
61
62     /**
63      * Called the first time the Service is used.<br>
64      *
65      */

66     public void init()
67             throws InitializationException
68     {
69         // Context properties are specified in lines in the properties
70
// file that begin with "context.contextname.", allowing
71
// multiple named contexts to be used. Everything after the
72
// "contextname." is the name of the property that will be
73
// used by the InitialContext class to create a new context
74
// instance.
75

76         Configuration conf = Turbine.getConfiguration();
77         try
78         {
79             contextPropsList = new HashMap JavaDoc();
80
81             for (Iterator JavaDoc contextKeys = conf.subset("context").getKeys();
82                  contextKeys.hasNext();)
83             {
84                 String JavaDoc key = (String JavaDoc) contextKeys.next();
85                 int end = key.indexOf(".");
86
87                 if (end == -1)
88                 {
89                     continue;
90                 }
91
92                 String JavaDoc contextName = key.substring(0, end);
93                 Properties JavaDoc contextProps = null;
94
95                 if (contextPropsList.containsKey(contextName))
96                 {
97                     contextProps = (Properties JavaDoc)
98                             contextPropsList.get(contextName);
99                 }
100                 else
101                 {
102                     contextProps = new Properties JavaDoc();
103                 }
104
105                 contextProps.put(key.substring(end + 1),
106                         conf.getString(key));
107
108                 contextPropsList.put(contextName, contextProps);
109             }
110
111             for (Iterator JavaDoc contextPropsKeys = contextPropsList.keySet().iterator();
112                  contextPropsKeys.hasNext();)
113             {
114                 String JavaDoc key = (String JavaDoc) contextPropsKeys.next();
115                 Properties JavaDoc contextProps = (Properties JavaDoc) contextPropsList.get(key);
116                 InitialContext JavaDoc context = new InitialContext JavaDoc(contextProps);
117                 initialContexts.put(key, context);
118             }
119
120             setInit(true);
121         }
122         catch (Exception JavaDoc e)
123         {
124             log.error("Failed to initialize JDNI contexts!", e);
125
126             throw new InitializationException(
127                     "Failed to initialize JDNI contexts!");
128         }
129     }
130
131     /**
132      * Places the contexts defined in the TurbineResources instance
133      * (if any) into the data.contexts Map.
134      *
135      * @param data The RunData object for the current request.
136      * @exception InitializationException, if there was a problem
137      * during initialization.
138      * @deprecated This should never have been here. No replacement.
139      */

140     public void init(RunData data) throws InitializationException
141     {
142         try
143         {
144             if (contextPropsList == null)
145             {
146                 init();
147             }
148
149             for (Iterator JavaDoc it = contextPropsList.keySet().iterator(); it.hasNext();)
150             {
151                 String JavaDoc key = (String JavaDoc) it.next();
152                 Properties JavaDoc contextProps =
153                         (Properties JavaDoc) contextPropsList.get(key);
154                 InitialContext JavaDoc context = new InitialContext JavaDoc(contextProps);
155                 data.getJNDIContexts().put(key, context);
156             }
157         }
158         catch (Exception JavaDoc e)
159         {
160             log.error("Failed to initialize JDNI contexts!", e);
161
162             throw new InitializationException(
163                     "Failed to initialize JDNI contexts!");
164         }
165     }
166
167     /**
168      * Return the Context with the specified name. The Context is
169      * constructed using the properties for the context with the
170      * specified name; ie. those properties that start with
171      * "services.servicename.properties.name.".
172      *
173      * @param contextName The name of the context.
174      * @return The context with the specified name, or null if no
175      * context exists with that name.
176      */

177     public Context JavaDoc getContext(String JavaDoc contextName)
178     {
179         // Get just the properties for the context with the specified
180
// name.
181
Properties JavaDoc contextProps = null;
182
183         if (contextPropsList.containsKey(contextName))
184         {
185             contextProps = (Properties JavaDoc) contextPropsList.get(contextName);
186         }
187         else
188         {
189             contextProps = new Properties JavaDoc();
190         }
191
192         // Construct a new context with the properties.
193
try
194         {
195             return new InitialContext JavaDoc(contextProps);
196         }
197         catch (Exception JavaDoc e)
198         {
199             return null;
200         }
201     }
202 }
203
Popular Tags