001: /*
002: * HA-JDBC: High-Availability JDBC
003: * Copyright (c) 2004-2007 Paul Ferraro
004: *
005: * This library is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU Lesser General Public License as published by the
007: * Free Software Foundation; either version 2.1 of the License, or (at your
008: * option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful, but WITHOUT
011: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
012: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
013: * for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public License
016: * along with this library; if not, write to the Free Software Foundation,
017: * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018: *
019: * Contact: ferraro@users.sourceforge.net
020: */
021: package net.sf.hajdbc.sql;
022:
023: import java.util.HashMap;
024: import java.util.Hashtable;
025: import java.util.Map;
026:
027: import javax.naming.Binding;
028: import javax.naming.CompositeName;
029: import javax.naming.Context;
030: import javax.naming.Name;
031: import javax.naming.NameClassPair;
032: import javax.naming.NameNotFoundException;
033: import javax.naming.NameParser;
034: import javax.naming.NamingEnumeration;
035: import javax.naming.NamingException;
036: import javax.naming.Reference;
037: import javax.naming.Referenceable;
038: import javax.naming.spi.InitialContextFactory;
039: import javax.naming.spi.ObjectFactory;
040:
041: /**
042: * Mock initial context factory that creates mock contexts.
043: *
044: * @author Paul Ferraro
045: * @since 1.1
046: */
047: public class MockInitialContextFactory implements InitialContextFactory {
048: private static ThreadLocal<Context> threadLocal = new ThreadLocal<Context>() {
049: /**
050: * @see java.lang.ThreadLocal#initialValue()
051: */
052: @Override
053: protected Context initialValue() {
054: return new MockContext();
055: }
056: };
057:
058: /**
059: * @see javax.naming.spi.InitialContextFactory#getInitialContext(java.util.Hashtable)
060: */
061: public Context getInitialContext(Hashtable<?, ?> environment) {
062: return threadLocal.get();
063: }
064:
065: /**
066: * @author Paul Ferraro
067: * @since 1.1
068: */
069: public static class MockContext implements Context {
070: private Map<String, Reference> referenceMap = new HashMap<String, Reference>();
071:
072: /**
073: * @see javax.naming.Context#lookup(javax.naming.Name)
074: */
075: public Object lookup(Name name) throws NamingException {
076: return this .lookup(name.toString());
077: }
078:
079: /**
080: * @see javax.naming.Context#lookup(java.lang.String)
081: */
082: public Object lookup(String name) throws NamingException {
083: Reference reference = this .referenceMap.get(name);
084:
085: if (reference == null) {
086: throw new NameNotFoundException(name);
087: }
088:
089: try {
090: ObjectFactory factory = (ObjectFactory) Thread
091: .currentThread().getContextClassLoader()
092: .loadClass(reference.getFactoryClassName())
093: .newInstance();
094:
095: return factory.getObjectInstance(reference,
096: new CompositeName(name), this , null);
097: } catch (Exception e) {
098: NamingException exception = new NamingException();
099: exception.setRootCause(e);
100: exception.initCause(e);
101:
102: throw exception;
103: }
104: }
105:
106: /**
107: * @see javax.naming.Context#bind(javax.naming.Name, java.lang.Object)
108: */
109: public void bind(Name name, Object object)
110: throws NamingException {
111: this .bind(name.toString(), object);
112: }
113:
114: /**
115: * @see javax.naming.Context#bind(java.lang.String, java.lang.Object)
116: */
117: public void bind(String name, Object object)
118: throws NamingException {
119: Reference reference = null;
120:
121: if (Reference.class.isInstance(object)) {
122: reference = (Reference) object;
123: } else if (Referenceable.class.isInstance(object)) {
124: reference = ((Referenceable) object).getReference();
125: } else {
126: throw new NamingException(
127: "Must extend javax.naming.Reference or implement javax.naming.Referenceable"); //$NON-NLS-1$
128: }
129:
130: this .referenceMap.put(name, reference);
131: }
132:
133: /**
134: * @see javax.naming.Context#rebind(javax.naming.Name, java.lang.Object)
135: */
136: public void rebind(Name name, Object object)
137: throws NamingException {
138: this .rebind(name.toString(), object);
139: }
140:
141: /**
142: * @see javax.naming.Context#rebind(java.lang.String, java.lang.Object)
143: */
144: public void rebind(String name, Object object)
145: throws NamingException {
146: this .bind(name, object);
147: }
148:
149: /**
150: * @see javax.naming.Context#unbind(javax.naming.Name)
151: */
152: public void unbind(Name name) {
153: this .unbind(name.toString());
154: }
155:
156: /**
157: * @see javax.naming.Context#unbind(java.lang.String)
158: */
159: public void unbind(String name) {
160: this .referenceMap.remove(name);
161: }
162:
163: /**
164: * @see javax.naming.Context#rename(javax.naming.Name, javax.naming.Name)
165: */
166: public void rename(Name oldName, Name newName) {
167: this .rename(oldName.toString(), newName.toString());
168: }
169:
170: /**
171: * @see javax.naming.Context#rename(java.lang.String, java.lang.String)
172: */
173: public void rename(String oldName, String newName) {
174: this .referenceMap.put(newName, this .referenceMap
175: .remove(oldName));
176: }
177:
178: /**
179: * @see javax.naming.Context#list(javax.naming.Name)
180: */
181: public NamingEnumeration<NameClassPair> list(Name name) {
182: return null;
183: }
184:
185: /**
186: * @see javax.naming.Context#list(java.lang.String)
187: */
188: public NamingEnumeration<NameClassPair> list(String name) {
189: return null;
190: }
191:
192: /**
193: * @see javax.naming.Context#listBindings(javax.naming.Name)
194: */
195: public NamingEnumeration<Binding> listBindings(Name name) {
196: return null;
197: }
198:
199: /**
200: * @see javax.naming.Context#listBindings(java.lang.String)
201: */
202: public NamingEnumeration<Binding> listBindings(String name) {
203: return null;
204: }
205:
206: /**
207: * @see javax.naming.Context#destroySubcontext(javax.naming.Name)
208: */
209: public void destroySubcontext(Name name) {
210: }
211:
212: /**
213: * @see javax.naming.Context#destroySubcontext(java.lang.String)
214: */
215: public void destroySubcontext(String name) {
216: }
217:
218: /**
219: * @see javax.naming.Context#createSubcontext(javax.naming.Name)
220: */
221: public Context createSubcontext(Name name) {
222: return null;
223: }
224:
225: /**
226: * @see javax.naming.Context#createSubcontext(java.lang.String)
227: */
228: public Context createSubcontext(String name) {
229: return null;
230: }
231:
232: /**
233: * @see javax.naming.Context#lookupLink(javax.naming.Name)
234: */
235: public Object lookupLink(Name name) {
236: return null;
237: }
238:
239: /**
240: * @see javax.naming.Context#lookupLink(java.lang.String)
241: */
242: public Object lookupLink(String name) {
243: return null;
244: }
245:
246: /**
247: * @see javax.naming.Context#getNameParser(javax.naming.Name)
248: */
249: public NameParser getNameParser(Name name) {
250: return null;
251: }
252:
253: /**
254: * @see javax.naming.Context#getNameParser(java.lang.String)
255: */
256: public NameParser getNameParser(String name) {
257: return null;
258: }
259:
260: /**
261: * @see javax.naming.Context#composeName(javax.naming.Name, javax.naming.Name)
262: */
263: public Name composeName(Name arg0, Name arg1) {
264: return null;
265: }
266:
267: /**
268: * @see javax.naming.Context#composeName(java.lang.String, java.lang.String)
269: */
270: public String composeName(String arg0, String arg1) {
271: return null;
272: }
273:
274: /**
275: * @see javax.naming.Context#addToEnvironment(java.lang.String, java.lang.Object)
276: */
277: public Object addToEnvironment(String arg0, Object arg1) {
278: return null;
279: }
280:
281: /**
282: * @see javax.naming.Context#removeFromEnvironment(java.lang.String)
283: */
284: public Object removeFromEnvironment(String arg0) {
285: return null;
286: }
287:
288: /**
289: * @see javax.naming.Context#getEnvironment()
290: */
291: public Hashtable<?, ?> getEnvironment() {
292: return null;
293: }
294:
295: /**
296: * @see javax.naming.Context#close()
297: */
298: public void close() {
299: this .referenceMap.clear();
300: }
301:
302: /**
303: * @see javax.naming.Context#getNameInNamespace()
304: */
305: public String getNameInNamespace() {
306: return null;
307: }
308: }
309: }
|