1 package org.apache.turbine.services.naming;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 import java.util.HashMap;
20 import java.util.Iterator;
21 import java.util.Map;
22 import java.util.Properties;
23
24 import javax.naming.Context;
25 import javax.naming.InitialContext;
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 264148 2005-08-29 14:21:04Z henning $
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 Map contextPropsList = null;
58
59 /*** All initial contexts known to this service */
60 private Map initialContexts = new HashMap();
61
62 /***
63 * Called the first time the Service is used.<br>
64 *
65 */
66 public void init()
67 throws InitializationException
68 {
69
70
71
72
73
74
75
76 Configuration conf = Turbine.getConfiguration();
77 try
78 {
79 contextPropsList = new HashMap();
80
81 for (Iterator contextKeys = conf.subset("context").getKeys();
82 contextKeys.hasNext();)
83 {
84 String key = (String) contextKeys.next();
85 int end = key.indexOf(".");
86
87 if (end == -1)
88 {
89 continue;
90 }
91
92 String contextName = key.substring(0, end);
93 Properties contextProps = null;
94
95 if (contextPropsList.containsKey(contextName))
96 {
97 contextProps = (Properties)
98 contextPropsList.get(contextName);
99 }
100 else
101 {
102 contextProps = new Properties();
103 }
104
105 contextProps.put(key.substring(end + 1),
106 conf.getString(key));
107
108 contextPropsList.put(contextName, contextProps);
109 }
110
111 for (Iterator contextPropsKeys = contextPropsList.keySet().iterator();
112 contextPropsKeys.hasNext();)
113 {
114 String key = (String) contextPropsKeys.next();
115 Properties contextProps = (Properties) contextPropsList.get(key);
116 InitialContext context = new InitialContext(contextProps);
117 initialContexts.put(key, context);
118 }
119
120 setInit(true);
121 }
122 catch (Exception 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 it = contextPropsList.keySet().iterator(); it.hasNext();)
150 {
151 String key = (String) it.next();
152 Properties contextProps =
153 (Properties) contextPropsList.get(key);
154 InitialContext context = new InitialContext(contextProps);
155 data.getJNDIContexts().put(key, context);
156 }
157 }
158 catch (Exception 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 getContext(String contextName)
178 {
179
180
181 Properties contextProps = null;
182
183 if (contextPropsList.containsKey(contextName))
184 {
185 contextProps = (Properties) contextPropsList.get(contextName);
186 }
187 else
188 {
189 contextProps = new Properties();
190 }
191
192
193 try
194 {
195 return new InitialContext(contextProps);
196 }
197 catch (Exception e)
198 {
199 return null;
200 }
201 }
202 }