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<String> contextKeys = conf.subset("context").getKeys();
87 contextKeys.hasNext();)
88 {
89 String key = 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 (Map.Entry<String, Properties> entry : contextPropsList.entrySet())
116 {
117 String key = entry.getKey();
118 Properties contextProps = entry.getValue();
119 InitialContext context = new InitialContext(contextProps);
120 initialContexts.put(key, context);
121 }
122
123 setInit(true);
124 }
125 catch (NamingException e)
126 {
127 log.error("Failed to initialize JDNI contexts!", e);
128
129 throw new InitializationException(
130 "Failed to initialize JDNI contexts!");
131 }
132 }
133
134
135
136
137
138
139
140
141
142
143
144
145 public Context getContext(String contextName)
146 {
147
148
149 Properties contextProps = null;
150
151 if (contextPropsList.containsKey(contextName))
152 {
153 contextProps = contextPropsList.get(contextName);
154 }
155 else
156 {
157 contextProps = new Properties();
158 }
159
160
161 try
162 {
163 return new InitialContext(contextProps);
164 }
165 catch (Exception e)
166 {
167 return null;
168 }
169 }
170 }