1 /*
2 * Copyright (c) 2003 Peter Antman, Teknik i Media <peter.antman@tim.se>
3 *
4 * $Id: MemContext.java 12937 2004-11-24 20:15:11Z ruff $
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20 package org.xmlBlaster.test.j2ee;
21 import java.util.Hashtable;
22 import java.util.Properties;
23 import java.util.Enumeration;
24 import javax.naming.*;
25 import javax.naming.spi.InitialContextFactory;
26 /**
27 * A partial impl of JNDI context to test J2ee services.
28 *
29 * @author <a href="mailto:pra@tim.se">Peter Antman</a>
30 * @version $Revision: 1.1 $
31 */
32
33 public class MemContext implements Context, InitialContextFactory{
34 NameParser parser = new NamingParser ();
35 static final Properties prop = new Properties();
36 static {
37 prop.setProperty("jndi.syntax.direction","left_to_right");
38 prop.setProperty("jndi.syntax.separator","/");
39 };
40
41 class NamingParser
42 implements NameParser {
43 public Name parse(String name)
44 throws NamingException {
45 return new CompoundName(name,prop);
46 }
47
48
49 }
50 String prefix;
51 Hashtable env;
52 Hashtable bindings;
53
54 //--- Factory impl ---
55 private static MemContext root;
56 private static Object lock = new Object();
57 /**
58 * A simpel factory, that saves the first created root context in a static.
59 */
60 public Context getInitialContext(Hashtable env) throws NamingException{
61 synchronized(lock) {
62 if ( root == null) {
63 root = new MemContext(env,"");
64 } // end of if ()
65 }
66 return root;
67 }
68
69 public MemContext (){
70
71 }
72 public MemContext (Hashtable env, String name)throws NamingException {
73 this.bindings = new Hashtable();
74 this.env = new Hashtable();
75 this.prefix = name;
76 // Populating the environment hashtable
77 if (env != null ) {
78 Enumeration envEntries = env.keys();
79 while (envEntries.hasMoreElements()) {
80 String entryName = (String) envEntries.nextElement();
81 addToEnvironment(entryName, env.get(entryName));
82 }
83 }
84 }
85 public MemContext (Hashtable env, String name, Hashtable bindings)throws NamingException{
86 this(env,name);
87 this.bindings = bindings;
88 }
89
90
91 private Context getNextContext(Object candidate, String name) throws NamingException{
92 if ( candidate == null) {
93 throw new NameNotFoundException(name);
94 } // end of if ()
95 if ( candidate instanceof Context) {
96 return (Context)candidate;
97 } else {
98 throw new NamingException("Can not continue, " +name +" is not a subcontext");
99 } // end of else
100
101 }
102
103 public Object addToEnvironment(String propName, Object propVal)
104 throws NamingException {
105 Object val = env.get(propName);
106 env.put(propName,propVal);
107 return val;
108 }
109
110 public Object lookup(Name name) throws NamingException {
111 if ( name==null) {
112 throw new NamingException("Name to allowed to be null");
113 } // end of if ()
114 if ( name.isEmpty()) {
115 return new MemContext(env,prefix,bindings);
116 } // end of if ()
117
118 //Lookup on this comp
119 String n = name.get(0);
120 Object entry = bindings.get(n);
121
122 if ( entry == null) {
123 throw new NameNotFoundException(n);
124 } // end of if ()
125
126 // if name contains more components continue lookup
127 if ( name.size()>1) {
128 return getNextContext(entry,n).lookup(name.getSuffix(1));
129 } else {
130 return entry;
131 } // end of else
132
133
134
135
136 }
137
138 public Object lookup(String name) throws NamingException {
139 return lookup (getNameParser (name).parse (name) ) ;
140 }
141
142 public void bind(Name name, Object obj) throws NamingException {
143 if ( name==null) {
144 throw new NamingException("Name to allowed to be null");
145 } // end of if ()
146
147 String n = name.get(0);
148 Object entry = bindings.get(n);
149
150 if ( name.size()>1 ) {
151 getNextContext(entry,n).bind(name.getSuffix(1),obj);
152
153 } else {
154 if ( entry != null) {
155 throw new NameAlreadyBoundException(n);
156 } // end of if ()
157 bindings.put(n,obj);
158 } // end of else
159
160
161
162 }
163
164 public void bind(String name, Object obj) throws NamingException {
165 bind (getNameParser (name).parse (name), obj);
166 }
167
168 public void rebind(Name name, Object obj) throws NamingException {
169 if ( name==null) {
170 throw new NamingException("Name to allowed to be null");
171 } // end of if ()
172
173 String n = name.get(0);
174 Object entry = bindings.get(n);
175
176 if ( name.size()>1 ) {
177 getNextContext(entry,n).rebind(name.getSuffix(1),obj);
178
179 } else {
180 if ( entry != null) {
181 unbind(n);
182 } // end of if ()
183 bind(n,obj);
184 } // end of else
185 }
186
187 public void rebind(String name, Object obj) throws NamingException {
188 rebind (getNameParser (name).parse (name), obj);
189 }
190
191 public void unbind(Name name) throws NamingException {
192 if ( name==null) {
193 throw new NamingException("Name to allowed to be null");
194 } // end of if ()
195 String n = name.get(0);
196 Object entry = bindings.get(n);
197 if ( name.size()>1 ) {
198 getNextContext(entry,n).unbind(name.getSuffix(1));
199 } else {
200 if ( entry == null) {
201 throw new NameNotFoundException(n);
202 } // end of if ()
203 bindings.remove(n);
204 } // end of else
205
206 }
207
208 public void unbind(String name) throws NamingException {
209 unbind (getNameParser (name).parse (name));
210 }
211
212 public Context createSubcontext(Name name) throws NamingException {
213 if ( name==null) {
214 throw new NamingException("Name to allowed to be null");
215 } // end of if ()
216
217 String n = name.get(0);
218 Object entry = bindings.get(n);
219 if ( name.size()>1 ) {
220 return getNextContext(entry,n).createSubcontext(name.getSuffix(1));
221 } else {
222 if ( entry != null) {
223 throw new NameAlreadyBoundException(n);
224 } // end of if ()
225 MemContext ctx = new MemContext(env,n);
226 bindings.put(n,ctx);
227 return ctx;
228 } // end of else
229 }
230 public Context createSubcontext(String name) throws NamingException {
231 return createSubcontext (getNameParser (name).parse (name));
232 }
233
234 public void close() throws NamingException {
235 ;//NOOP
236 }
237 public String getNameInNamespace() throws NamingException {
238 throw new OperationNotSupportedException();
239 }
240
241 public void rename(Name oldName, Name newName) throws NamingException {
242 throw new OperationNotSupportedException();
243 }
244
245 public void rename(String oldName, String newName) throws NamingException {
246 throw new OperationNotSupportedException();
247 }
248
249 public NamingEnumeration list(Name name) throws NamingException {
250 throw new OperationNotSupportedException();
251 }
252
253 public NamingEnumeration list(String name) throws NamingException {
254 throw new OperationNotSupportedException();
255 }
256
257 public NamingEnumeration listBindings(Name name) throws NamingException {
258 throw new OperationNotSupportedException();
259 }
260 public NamingEnumeration listBindings(String name) throws NamingException {
261 throw new OperationNotSupportedException();
262 }
263 public void destroySubcontext(Name name) throws NamingException {
264 throw new OperationNotSupportedException();
265 }
266 public void destroySubcontext(String name) throws NamingException {
267 throw new OperationNotSupportedException();
268 }
269 public Object lookupLink(Name name) throws NamingException {
270 throw new OperationNotSupportedException();
271 }
272 public Object lookupLink(String name) throws NamingException {
273 throw new OperationNotSupportedException();
274 }
275 public NameParser getNameParser(Name name) throws NamingException {
276 return getNameParser (name.toString ());
277 }
278 public NameParser getNameParser(String name) throws NamingException {
279 return parser;
280 }
281 public String composeName(String name, String prefix)
282 throws NamingException {
283 throw new OperationNotSupportedException();
284 }
285
286 public Name composeName(Name name, Name prefix)
287 throws NamingException {
288 throw new OperationNotSupportedException();
289 }
290
291 public Object removeFromEnvironment(String propName)
292 throws NamingException {
293 throw new OperationNotSupportedException();
294 }
295 public Hashtable getEnvironment() throws NamingException {
296 throw new OperationNotSupportedException();
297 }
298
299 public static void main (String[] args) {
300 try {
301 System.setProperty(Context.INITIAL_CONTEXT_FACTORY ,"org.xmlBlaster.test.j2ee.MemContext");
302
303 Context ctx = new InitialContext();
304 Context hej = ctx.createSubcontext("hej");
305 Context deep= ctx.createSubcontext("hej/deep");
306
307 ctx.rebind("hej/deep/hello","Hello");
308 String h = (String)ctx.lookup("hej/deep/hello");
309 System.out.println(h);
310 ctx.unbind("hej/deep/hello");
311 try {
312 h = (String)ctx.lookup("hej/deep/hello");
313 throw new NamingException("Name found altough it was unbound");
314 } catch (NamingException e) {
315
316 } // end of try-catch
317
318 // And the crucial test....
319 ctx.rebind("hej/deep/hello","Hello");
320
321
322 h = (String)new InitialContext().lookup("hej/deep/hello");
323 System.out.println(h);
324
325
326 } catch (Exception e) {
327 e.printStackTrace();
328 } // end of try-catch
329
330 } // end of main ()
331
332
333 }// MemContext
syntax highlighted by Code2HTML, v. 0.9.1