001: package com.ecyrd.jspwiki;
002:
003: import java.util.HashMap;
004: import java.util.Hashtable;
005: import java.util.Map;
006:
007: import javax.naming.Context;
008: import javax.naming.Name;
009: import javax.naming.NameParser;
010: import javax.naming.NamingEnumeration;
011: import javax.naming.NamingException;
012: import javax.naming.spi.InitialContextFactory;
013:
014: /**
015: * <p>Mock JNDI context that permits String names to be bound to objects.
016: * It is intended to simulate JNDI factories in web containers and
017: * application servers, and provides only the bare minimum functions.
018: * This class contains a static member InitialContextFactory class
019: * called Factory that installs a "root" instance of TestJNDIContext
020: * as the initial context for the entire JVM. Additional contexts can
021: * be bound by supplying pairs of Strings and Objects using
022: * {@link #bind(String, Object)}. Objects are looked up and retrieved
023: * using {@link #lookup(String)}. All other methods in this class no-op.</p>
024: * <p>For example, simulating a JNDI DataSource requires us to first
025: * establish an initial context of <code>java:comp/env</code>. Inside
026: * the initial context, we bind our data source:</p>
027: * <blockquote><code>Context initCtx = new InitialContext();<br/>
028: * initCtx.bind( "java:comp/env", new TestJNDIContext() );<br/>
029: * Context ctx = (Context)initCtx.lookup("java:comp/env");<br/>
030: * DataSource ds = new TestJDBCDataSource();<br/>
031: * ctx.bind( "jdbc/UserDatabase", ds);<br/>
032: * </code></blockquote>
033: *
034: * @author Andrew R. Jaquith
035: * @since 2.3
036: */
037: public class TestJNDIContext implements Context {
038:
039: private final Map m_bindings = new HashMap();
040:
041: private static boolean initialized = false;
042:
043: /**
044: * InitialContextFactory class that configures the JVM to
045: * always return a particular TestJNDIContext.
046: * @author Andrew R. Jaquith
047: */
048: public static class Factory implements InitialContextFactory {
049:
050: private static Context ctx = null;
051:
052: public Context getInitialContext(Hashtable environment)
053: throws NamingException {
054: return ctx;
055: }
056:
057: protected static void setContext(Context context) {
058: if (ctx == null) {
059: ctx = context;
060: }
061: }
062: }
063:
064: /**
065: * Constructs a new mock JNDI context. Note that the instance has no
066: * relationship to the JVM's initial context <em>per se</em>.
067: * To configure the JVM so that it always returns a TestJNDIContext
068: * instance, see {@link #initialize()}.
069: */
070: public TestJNDIContext() {
071: }
072:
073: /**
074: * Static factory method that creates a new TestJNDIContext and
075: * configures the JVM so that the <code>new InitialContext()</code>
076: * always returns this context.
077: */
078: public static void initialize() {
079: if (!initialized) {
080: System.setProperty(Context.INITIAL_CONTEXT_FACTORY,
081: Factory.class.getName());
082: Factory.setContext(new TestJNDIContext());
083: initialized = true;
084: }
085: }
086:
087: /**
088: * No-op; always returns <code>null</code>.
089: * @see javax.naming.Context#addToEnvironment(java.lang.String, java.lang.Object)
090: */
091: public Object addToEnvironment(String propName, Object propVal)
092: throws NamingException {
093: return null;
094: }
095:
096: /**
097: * No-op.
098: * @see javax.naming.Context#bind(javax.naming.Name, java.lang.Object)
099: */
100: public void bind(Name name, Object obj) throws NamingException {
101: }
102:
103: /**
104: * No-op.
105: * @see javax.naming.Context#close()
106: */
107: public void close() throws NamingException {
108: }
109:
110: /**
111: * No-op; always returns <code>null</code>.
112: * @see javax.naming.Context#composeName(javax.naming.Name, javax.naming.Name)
113: */
114: public Name composeName(Name name, Name prefix)
115: throws NamingException {
116: return null;
117: }
118:
119: /**
120: * No-op; always returns <code>null</code>.
121: * @see javax.naming.Context#composeName(java.lang.String, java.lang.String)
122: */
123: public String composeName(String name, String prefix)
124: throws NamingException {
125: return null;
126: }
127:
128: /**
129: * No-op; always returns <code>null</code>.
130: * @see javax.naming.Context#createSubcontext(javax.naming.Name)
131: */
132: public Context createSubcontext(Name name) throws NamingException {
133: return null;
134: }
135:
136: /**
137: * No-op; always returns <code>null</code>.
138: * @see javax.naming.Context#createSubcontext(java.lang.String)
139: */
140: public Context createSubcontext(String name) throws NamingException {
141: return null;
142: }
143:
144: /**
145: * No-op.
146: * @see javax.naming.Context#destroySubcontext(javax.naming.Name)
147: */
148: public void destroySubcontext(Name name) throws NamingException {
149: }
150:
151: /**
152: * No-op.
153: * @see javax.naming.Context#destroySubcontext(java.lang.String)
154: */
155: public void destroySubcontext(String name) throws NamingException {
156: }
157:
158: /**
159: * No-op; always returns <code>null</code>.
160: * @see javax.naming.Context#getEnvironment()
161: */
162: public Hashtable getEnvironment() throws NamingException {
163: // TODO Auto-generated method stub
164: return null;
165: }
166:
167: /**
168: * No-op; always returns <code>null</code>.
169: * @see javax.naming.Context#getNameInNamespace()
170: */
171: public String getNameInNamespace() throws NamingException {
172: return null;
173: }
174:
175: /**
176: * No-op; always returns <code>null</code>.
177: * @see javax.naming.Context#getNameParser(javax.naming.Name)
178: */
179: public NameParser getNameParser(Name name) throws NamingException {
180: return null;
181: }
182:
183: /**
184: * No-op; always returns <code>null</code>.
185: * @see javax.naming.Context#getNameParser(java.lang.String)
186: */
187: public NameParser getNameParser(String name) throws NamingException {
188: return null;
189: }
190:
191: /**
192: * No-op; always returns <code>null</code>.
193: * @see javax.naming.Context#list(javax.naming.Name)
194: */
195: public NamingEnumeration list(Name name) throws NamingException {
196: return null;
197: }
198:
199: /**
200: * No-op; always returns <code>null</code>.
201: * @see javax.naming.Context#list(java.lang.String)
202: */
203: public NamingEnumeration list(String name) throws NamingException {
204: return null;
205: }
206:
207: /**
208: * No-op; always returns <code>null</code>.
209: * @see javax.naming.Context#listBindings(javax.naming.Name)
210: */
211: public NamingEnumeration listBindings(Name name)
212: throws NamingException {
213: return null;
214: }
215:
216: /**
217: * No-op; always returns <code>null</code>.
218: * @see javax.naming.Context#listBindings(java.lang.String)
219: */
220: public NamingEnumeration listBindings(String name)
221: throws NamingException {
222: return null;
223: }
224:
225: /**
226: * No-op; always returns <code>null</code>.
227: * @see javax.naming.Context#lookup(javax.naming.Name)
228: */
229: public Object lookup(Name name) throws NamingException {
230: return null;
231: }
232:
233: /**
234: * Binds an object to a supplied String key.
235: * @see javax.naming.InitialContext#bind(java.lang.String, java.lang.Object)
236: */
237: public void bind(String name, Object obj) throws NamingException {
238: m_bindings.put(name, obj);
239: }
240:
241: /**
242: * Retrieves an object using a String key. If not found,
243: * throws a NamingException.
244: * @see javax.naming.InitialContext#lookup(java.lang.String)
245: */
246: public Object lookup(String name) throws NamingException {
247: Object obj = m_bindings.get(name);
248: if (obj == null) {
249: throw new NamingException("Object named '" + name
250: + "' not found in JNDI context.");
251: }
252: return obj;
253: }
254:
255: /**
256: * No-op; always returns <code>null</code>.
257: * @see javax.naming.Context#lookupLink(javax.naming.Name)
258: */
259: public Object lookupLink(Name name) throws NamingException {
260: return null;
261: }
262:
263: /**
264: * No-op; always returns <code>null</code>.
265: * @see javax.naming.Context#lookupLink(java.lang.String)
266: */
267: public Object lookupLink(String name) throws NamingException {
268: return null;
269: }
270:
271: /**
272: * No-op.
273: * @see javax.naming.Context#rebind(javax.naming.Name, java.lang.Object)
274: */
275: public void rebind(Name name, Object obj) throws NamingException {
276: }
277:
278: /**
279: * No-op.
280: * @see javax.naming.Context#rebind(java.lang.String, java.lang.Object)
281: */
282: public void rebind(String name, Object obj) throws NamingException {
283: }
284:
285: /**
286: * No-op; always returns <code>null</code>.
287: * @see javax.naming.Context#removeFromEnvironment(java.lang.String)
288: */
289: public Object removeFromEnvironment(String propName)
290: throws NamingException {
291: return null;
292: }
293:
294: /**
295: * No-op.
296: * @see javax.naming.Context#rename(javax.naming.Name, javax.naming.Name)
297: */
298: public void rename(Name oldName, Name newName)
299: throws NamingException {
300: }
301:
302: /**
303: * No-op.
304: * @see javax.naming.Context#rename(java.lang.String, java.lang.String)
305: */
306: public void rename(String oldName, String newName)
307: throws NamingException {
308: }
309:
310: /**
311: * No-op.
312: * @see javax.naming.Context#unbind(javax.naming.Name)
313: */
314: public void unbind(Name name) throws NamingException {
315: }
316:
317: /**
318: * No-op.
319: * @see javax.naming.Context#unbind(java.lang.String)
320: */
321: public void unbind(String name) throws NamingException {
322: }
323: }
|