1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.mortbay.jetty.plus.naming;
17
18 import java.util.ArrayList;
19 import java.util.List;
20
21 import javax.naming.Binding;
22 import javax.naming.Context;
23 import javax.naming.InitialContext;
24 import javax.naming.LinkRef;
25 import javax.naming.Name;
26 import javax.naming.NameNotFoundException;
27 import javax.naming.NameParser;
28 import javax.naming.NamingEnumeration;
29 import javax.naming.NamingException;
30
31 import org.mortbay.log.Log;
32 import org.mortbay.naming.NamingUtil;
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47 public abstract class NamingEntry
48 {
49 public static final int SCOPE_CONTAINER = 0;
50 public static final int SCOPE_WEBAPP = 1;
51 protected String jndiName;
52 protected Object objectToBind;
53 protected String absoluteObjectNameString;
54 protected String namingEntryNameString;
55 protected String objectNameString;
56 protected Context context;
57 protected boolean isContainerScope;
58 protected static ThreadLocal scope = new ThreadLocal();
59
60 public static void setScope (int scopeType)
61 {
62 scope.set(new Integer(scopeType));
63 }
64
65 public static int getScope ()
66 {
67 Integer val = (Integer)scope.get();
68 return (val == null?SCOPE_CONTAINER:val.intValue());
69 }
70
71
72 public static Name makeNamingEntryName (NameParser parser, String jndiName)
73 throws NamingException
74 {
75 if (jndiName==null || parser==null)
76 return null;
77
78 Name name = parser.parse("");
79 name.add(jndiName);
80 String lastAtom = (String)name.remove(name.size()-1);
81 lastAtom="__"+lastAtom;
82 name.add(lastAtom);
83 return name;
84 }
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99 public NamingEntry (String jndiName, Object object)
100 throws NamingException
101 {
102 this.jndiName = jndiName;
103 this.objectToBind = object;
104
105
106
107
108 isContainerScope=(getScope()==SCOPE_CONTAINER);
109 InitialContext icontext = new InitialContext();
110 if (isContainerScope)
111 context = icontext;
112 else
113 context = (Context)icontext.lookup("java:comp/env");
114 save();
115 }
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147 public void bindToENC(String localName)
148 throws NamingException
149 {
150
151
152 if (localName.equals(jndiName) && isLocal())
153 {
154 Log.warn("Already bound "+localName+" to java:comp/env with "+absoluteObjectNameString);
155 return;
156 }
157
158 InitialContext ic = new InitialContext();
159 Context env = (Context)ic.lookup("java:comp/env");
160 Log.debug("Binding java:comp/env/"+localName+" to "+absoluteObjectNameString);
161 NamingUtil.bind(env, localName, new LinkRef(absoluteObjectNameString));
162 }
163
164
165
166
167 public void unbindENC ()
168 {
169 try
170 {
171 InitialContext ic = new InitialContext();
172 Context env = (Context)ic.lookup("java:comp/env");
173 Log.debug("Unbinding java:comp/env/"+getJndiName());
174 env.unbind(getJndiName());
175 }
176 catch (NamingException e)
177 {
178 Log.warn(e);
179 }
180 }
181
182
183
184
185 public void release ()
186 {
187 try
188 {
189 context.unbind(objectNameString);
190 context.unbind(namingEntryNameString);
191 this.absoluteObjectNameString=null;
192 this.jndiName=null;
193 this.namingEntryNameString=null;
194 this.objectNameString=null;
195 this.objectToBind=null;
196 this.context=null;
197 }
198 catch (NamingException e)
199 {
200 Log.warn(e);
201 }
202 }
203
204
205
206
207
208 public String getJndiName ()
209 {
210 return this.jndiName;
211 }
212
213
214
215
216
217 public Object getObjectToBind()
218 throws NamingException
219 {
220 return this.objectToBind;
221 }
222
223
224
225
226
227 public boolean isGlobal ()
228 {
229 return this.isContainerScope;
230 }
231
232 public boolean isLocal()
233 {
234 return !this.isContainerScope;
235 }
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259 protected void save ()
260 throws NamingException
261 {
262 NameParser parser = context.getNameParser("");
263 Name contextName = parser.parse(context.getNameInNamespace());
264
265
266 Name namingEntryName = makeNamingEntryName(parser, jndiName);
267 namingEntryNameString = namingEntryName.toString();
268 NamingUtil.bind(context, namingEntryNameString, this);
269 Log.debug("Bound "+(isGlobal()?"":"java:")+namingEntryName.addAll(0,contextName));
270
271
272 Name objectName = parser.parse(getJndiName());
273 objectNameString = objectName.toString();
274 NamingUtil.bind(context, objectNameString, getObjectToBind());
275
276
277
278 Name fullName = objectName.addAll(0,contextName);
279 absoluteObjectNameString = (isContainerScope?"":"java:")+fullName.toString();
280 Log.debug("Bound "+absoluteObjectNameString);
281 }
282
283
284 }