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
20
21
22
23
24 import java.util.HashMap;
25 import java.util.Iterator;
26 import java.util.Map;
27 import java.util.Properties;
28
29 import javax.naming.Context;
30 import javax.naming.InitialContext;
31 import javax.naming.NamingException;
32
33 import org.apache.commons.configuration.Configuration;
34 import org.apache.commons.logging.Log;
35 import org.apache.commons.logging.LogFactory;
36 import org.apache.turbine.Turbine;
37 import org.apache.turbine.services.InitializationException;
38 import org.apache.turbine.services.TurbineBaseService;
39
40
41
42
43
44
45
46
47
48
49
50 public class TurbineNamingService
51 extends TurbineBaseService
52 implements NamingService
53 {
54
55 private static Log log = LogFactory.getLog(TurbineNamingService.class);
56
57
58
59
60
61 private static Map<String, Properties> contextPropsList = null;
62
63
64 private final Map<String, InitialContext> initialContexts = new HashMap<String, InitialContext>();
65
66
67
68
69
70 @Override
71 public void init()
72 throws InitializationException
73 {
74
75
76
77
78
79
80
81 Configuration conf = Turbine.getConfiguration();
82 try
83 {
84 contextPropsList = new HashMap<String, Properties>();
85
86 for (Iterator contextKeys = conf.subset("context").getKeys();
87 contextKeys.hasNext();)
88 {
89 String key = (String) contextKeys.next();
90 int end = key.indexOf(".");
91
92 if (end == -1)
93 {
94 continue;
95 }
96
97 String contextName = key.substring(0, end);
98 Properties contextProps = null;
99
100 if (contextPropsList.containsKey(contextName))
101 {
102 contextProps = contextPropsList.get(contextName);
103 }
104 else
105 {
106 contextProps = new Properties();
107 }
108
109 contextProps.put(key.substring(end + 1),
110 conf.getString(key));
111
112 contextPropsList.put(contextName, contextProps);
113 }
114
115 for (Iterator contextPropsKeys = contextPropsList.keySet().iterator();
116 contextPropsKeys.hasNext();)
117 {
118 String key = (String) contextPropsKeys.next();
119 Properties contextProps = contextPropsList.get(key);
120 InitialContext context = new InitialContext(contextProps);
121 initialContexts.put(key, context);
122 }
123
124 setInit(true);
125 }
126 catch (NamingException e)
127 {
128 log.error("Failed to initialize JDNI contexts!", e);
129
130 throw new InitializationException(
131 "Failed to initialize JDNI contexts!");
132 }
133 }
134
135
136
137
138
139
140
141
142
143
144
145
146 public Context getContext(String contextName)
147 {
148
149
150 Properties contextProps = null;
151
152 if (contextPropsList.containsKey(contextName))
153 {
154 contextProps = contextPropsList.get(contextName);
155 }
156 else
157 {
158 contextProps = new Properties();
159 }
160
161
162 try
163 {
164 return new InitialContext(contextProps);
165 }
166 catch (Exception e)
167 {
168 return null;
169 }
170 }
171 }