KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > hajdbc > sql > MockInitialContextFactory


1 /*
2  * HA-JDBC: High-Availability JDBC
3  * Copyright (c) 2004-2006 Paul Ferraro
4  *
5  * This library is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU Lesser General Public License as published by the
7  * Free Software Foundation; either version 2.1 of the License, or (at your
8  * option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
13  * for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public License
16  * along with this library; if not, write to the Free Software Foundation,
17  * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18  *
19  * Contact: ferraro@users.sourceforge.net
20  */

21 package net.sf.hajdbc.sql;
22
23 import java.util.HashMap JavaDoc;
24 import java.util.Hashtable JavaDoc;
25 import java.util.Map JavaDoc;
26
27 import javax.naming.CompositeName JavaDoc;
28 import javax.naming.Context JavaDoc;
29 import javax.naming.Name JavaDoc;
30 import javax.naming.NameNotFoundException JavaDoc;
31 import javax.naming.NameParser JavaDoc;
32 import javax.naming.NamingEnumeration JavaDoc;
33 import javax.naming.NamingException JavaDoc;
34 import javax.naming.Reference JavaDoc;
35 import javax.naming.Referenceable JavaDoc;
36 import javax.naming.spi.InitialContextFactory JavaDoc;
37 import javax.naming.spi.ObjectFactory JavaDoc;
38
39 /**
40  * Mock initial context factory that creates mock contexts.
41  *
42  * @author Paul Ferraro
43  * @since 1.1
44  */

45 public class MockInitialContextFactory implements InitialContextFactory JavaDoc
46 {
47     private static ThreadLocal JavaDoc threadLocal = new ThreadLocal JavaDoc()
48     {
49         /**
50          * @see java.lang.ThreadLocal#initialValue()
51          */

52         protected Object JavaDoc initialValue()
53         {
54             return new MockContext();
55         }
56     };
57     
58     /**
59      * @see javax.naming.spi.InitialContextFactory#getInitialContext(java.util.Hashtable)
60      */

61     public Context JavaDoc getInitialContext(Hashtable JavaDoc environment) throws NamingException JavaDoc
62     {
63         return (Context JavaDoc) threadLocal.get();
64     }
65     
66     /**
67      * @author Paul Ferraro
68      * @since 1.1
69      */

70     public static class MockContext implements Context JavaDoc
71     {
72         private Map JavaDoc referenceMap = new HashMap JavaDoc();
73         
74         /**
75          * @see javax.naming.Context#lookup(javax.naming.Name)
76          */

77         public Object JavaDoc lookup(Name JavaDoc name) throws NamingException JavaDoc
78         {
79             return this.lookup(name.toString());
80         }
81
82         /**
83          * @see javax.naming.Context#lookup(java.lang.String)
84          */

85         public Object JavaDoc lookup(String JavaDoc name) throws NamingException JavaDoc
86         {
87             Reference JavaDoc reference = (Reference JavaDoc) this.referenceMap.get(name.toString());
88             
89             if (reference == null)
90             {
91                 throw new NameNotFoundException JavaDoc(name.toString());
92             }
93             
94             try
95             {
96                 ObjectFactory JavaDoc factory = (ObjectFactory JavaDoc) Thread.currentThread().getContextClassLoader().loadClass(reference.getFactoryClassName()).newInstance();
97                 
98                 return factory.getObjectInstance(reference, new CompositeName JavaDoc(name), this, null);
99             }
100             catch (Exception JavaDoc e)
101             {
102                 NamingException JavaDoc exception = new NamingException JavaDoc();
103                 exception.setRootCause(e);
104                 exception.initCause(e);
105                 
106                 throw exception;
107             }
108         }
109
110         /**
111          * @see javax.naming.Context#bind(javax.naming.Name, java.lang.Object)
112          */

113         public void bind(Name JavaDoc name, Object JavaDoc object) throws NamingException JavaDoc
114         {
115             this.bind(name.toString(), object);
116         }
117
118         /**
119          * @see javax.naming.Context#bind(java.lang.String, java.lang.Object)
120          */

121         public void bind(String JavaDoc name, Object JavaDoc object) throws NamingException JavaDoc
122         {
123             Reference JavaDoc reference = null;
124             
125             if (Reference JavaDoc.class.isInstance(object))
126             {
127                 reference = (Reference JavaDoc) object;
128             }
129             else if (Referenceable JavaDoc.class.isInstance(object))
130             {
131                 reference = ((Referenceable JavaDoc) object).getReference();
132             }
133             else
134             {
135                 throw new NamingException JavaDoc("Must extend javax.naming.Reference or implement javax.naming.Referenceable");
136             }
137             
138             this.referenceMap.put(name, reference);
139         }
140
141         /**
142          * @see javax.naming.Context#rebind(javax.naming.Name, java.lang.Object)
143          */

144         public void rebind(Name JavaDoc name, Object JavaDoc object) throws NamingException JavaDoc
145         {
146             this.rebind(name.toString(), object);
147         }
148
149         /**
150          * @see javax.naming.Context#rebind(java.lang.String, java.lang.Object)
151          */

152         public void rebind(String JavaDoc name, Object JavaDoc object) throws NamingException JavaDoc
153         {
154             this.bind(name, object);
155         }
156
157         /**
158          * @see javax.naming.Context#unbind(javax.naming.Name)
159          */

160         public void unbind(Name JavaDoc name) throws NamingException JavaDoc
161         {
162             this.unbind(name.toString());
163         }
164
165         /**
166          * @see javax.naming.Context#unbind(java.lang.String)
167          */

168         public void unbind(String JavaDoc name) throws NamingException JavaDoc
169         {
170             this.referenceMap.remove(name);
171         }
172
173         /**
174          * @see javax.naming.Context#rename(javax.naming.Name, javax.naming.Name)
175          */

176         public void rename(Name JavaDoc oldName, Name JavaDoc newName) throws NamingException JavaDoc
177         {
178             this.rename(oldName.toString(), newName.toString());
179         }
180
181         /**
182          * @see javax.naming.Context#rename(java.lang.String, java.lang.String)
183          */

184         public void rename(String JavaDoc oldName, String JavaDoc newName) throws NamingException JavaDoc
185         {
186             this.referenceMap.put(newName, this.referenceMap.remove(oldName));
187         }
188
189         /**
190          * @see javax.naming.Context#list(javax.naming.Name)
191          */

192         public NamingEnumeration JavaDoc list(Name JavaDoc name) throws NamingException JavaDoc
193         {
194             return null;
195         }
196
197         /**
198          * @see javax.naming.Context#list(java.lang.String)
199          */

200         public NamingEnumeration JavaDoc list(String JavaDoc name) throws NamingException JavaDoc
201         {
202             return null;
203         }
204
205         /**
206          * @see javax.naming.Context#listBindings(javax.naming.Name)
207          */

208         public NamingEnumeration JavaDoc listBindings(Name JavaDoc name) throws NamingException JavaDoc
209         {
210             return null;
211         }
212
213         /**
214          * @see javax.naming.Context#listBindings(java.lang.String)
215          */

216         public NamingEnumeration JavaDoc listBindings(String JavaDoc name) throws NamingException JavaDoc
217         {
218             return null;
219         }
220
221         /**
222          * @see javax.naming.Context#destroySubcontext(javax.naming.Name)
223          */

224         public void destroySubcontext(Name JavaDoc name) throws NamingException JavaDoc
225         {
226         }
227
228         /**
229          * @see javax.naming.Context#destroySubcontext(java.lang.String)
230          */

231         public void destroySubcontext(String JavaDoc name) throws NamingException JavaDoc
232         {
233         }
234
235         /**
236          * @see javax.naming.Context#createSubcontext(javax.naming.Name)
237          */

238         public Context JavaDoc createSubcontext(Name JavaDoc name) throws NamingException JavaDoc
239         {
240             return null;
241         }
242
243         /**
244          * @see javax.naming.Context#createSubcontext(java.lang.String)
245          */

246         public Context JavaDoc createSubcontext(String JavaDoc name) throws NamingException JavaDoc
247         {
248             return null;
249         }
250
251         /**
252          * @see javax.naming.Context#lookupLink(javax.naming.Name)
253          */

254         public Object JavaDoc lookupLink(Name JavaDoc name) throws NamingException JavaDoc
255         {
256             return null;
257         }
258
259         /**
260          * @see javax.naming.Context#lookupLink(java.lang.String)
261          */

262         public Object JavaDoc lookupLink(String JavaDoc name) throws NamingException JavaDoc
263         {
264             return null;
265         }
266
267         /**
268          * @see javax.naming.Context#getNameParser(javax.naming.Name)
269          */

270         public NameParser JavaDoc getNameParser(Name JavaDoc name) throws NamingException JavaDoc
271         {
272             return null;
273         }
274
275         /**
276          * @see javax.naming.Context#getNameParser(java.lang.String)
277          */

278         public NameParser JavaDoc getNameParser(String JavaDoc name) throws NamingException JavaDoc
279         {
280             return null;
281         }
282
283         /**
284          * @see javax.naming.Context#composeName(javax.naming.Name, javax.naming.Name)
285          */

286         public Name JavaDoc composeName(Name JavaDoc arg0, Name JavaDoc arg1) throws NamingException JavaDoc
287         {
288             return null;
289         }
290
291         /**
292          * @see javax.naming.Context#composeName(java.lang.String, java.lang.String)
293          */

294         public String JavaDoc composeName(String JavaDoc arg0, String JavaDoc arg1) throws NamingException JavaDoc
295         {
296             return null;
297         }
298
299         /**
300          * @see javax.naming.Context#addToEnvironment(java.lang.String, java.lang.Object)
301          */

302         public Object JavaDoc addToEnvironment(String JavaDoc arg0, Object JavaDoc arg1) throws NamingException JavaDoc
303         {
304             return null;
305         }
306
307         /**
308          * @see javax.naming.Context#removeFromEnvironment(java.lang.String)
309          */

310         public Object JavaDoc removeFromEnvironment(String JavaDoc arg0) throws NamingException JavaDoc
311         {
312             return null;
313         }
314
315         /**
316          * @see javax.naming.Context#getEnvironment()
317          */

318         public Hashtable JavaDoc getEnvironment() throws NamingException JavaDoc
319         {
320             return null;
321         }
322
323         /**
324          * @see javax.naming.Context#close()
325          */

326         public void close() throws NamingException JavaDoc
327         {
328             this.referenceMap.clear();
329         }
330
331         /**
332          * @see javax.naming.Context#getNameInNamespace()
333          */

334         public String JavaDoc getNameInNamespace() throws NamingException JavaDoc
335         {
336             return null;
337         }
338     }
339 }
340
Popular Tags