001: /* tjws - SimpleJndi.java
002: * Copyright (C) 1999-2007 Dmitriy Rogatkin. All rights reserved.
003: * Redistribution and use in source and binary forms, with or without
004: * modification, are permitted provided that the following conditions
005: * are met:
006: * 1. Redistributions of source code must retain the above copyright
007: * notice, this list of conditions and the following disclaimer.
008: * 2. Redistributions in binary form must reproduce the above copyright
009: * notice, this list of conditions and the following disclaimer in the
010: * documentation and/or other materials provided with the distribution.
011: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
012: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
013: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
014: * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
015: * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
016: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
017: * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
018: * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
019: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
020: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
021: * SUCH DAMAGE.
022: *
023: * Visit http://tjws.sourceforge.net to get the latest infromation
024: * about Rogatkin's products.
025: * $Id: SimpleJndi.java,v 1.3 2007/04/25 04:43:16 rogatkin Exp $
026: * Created on Mar 25, 2007
027: * @author Dmitriy
028: */
029: package rogatkin.app;
030:
031: import java.sql.Connection;
032: import java.sql.SQLException;
033: import java.util.HashMap;
034: import java.util.Hashtable;
035:
036: import javax.naming.Binding;
037: import javax.naming.Context;
038: import javax.naming.InitialContext;
039: import javax.naming.Name;
040: import javax.naming.NameAlreadyBoundException;
041: import javax.naming.NameClassPair;
042: import javax.naming.NameParser;
043: import javax.naming.NamingEnumeration;
044: import javax.naming.NamingException;
045: import javax.naming.spi.InitialContextFactory;
046: import javax.sql.DataSource;
047:
048: public class SimpleJndi implements InitialContextFactory {
049:
050: public Context getInitialContext(Hashtable<?, ?> arg0)
051: throws NamingException {
052: return new SimpleContext(arg0);
053: }
054:
055: public static void main(String... args) {
056: try {
057: System.getProperties().setProperty(
058: Context.INITIAL_CONTEXT_FACTORY,
059: "rogatkin.app.SimpleJndi");
060: new SimpleDataSource(args[0]);
061:
062: Connection con = ((DataSource) new InitialContext()
063: .lookup("jdbc/access/MediaChest")).getConnection();
064: System.out.printf("Connection taken %s\n", con);
065: con.close();
066: } catch (IllegalArgumentException e) {
067: e.printStackTrace();
068: } catch (SQLException e) {
069: // TODO Auto-generated catch block
070: e.printStackTrace();
071: } catch (NamingException e) {
072: // TODO Auto-generated catch block
073: e.printStackTrace();
074: }
075: }
076:
077: static class SimpleContext implements Context {
078: private static HashMap<String, Object> environment = new HashMap<String, Object>();
079:
080: private static HashMap<String, Object> directory = new HashMap<String, Object>();
081:
082: SimpleContext(Hashtable<?, ?> env) {
083: for (Object key : env.keySet()) {
084: environment.put(key.toString(), env.get(key));
085: }
086: }
087:
088: public Object addToEnvironment(String arg0, Object arg1)
089: throws NamingException {
090: return environment.put(arg0, arg1);
091: }
092:
093: public void bind(Name arg0, Object arg1) throws NamingException {
094: bind(arg0.toString(), arg1);
095:
096: }
097:
098: public void bind(String arg0, Object arg1)
099: throws NamingException {
100: if (__debug)
101: System.err.printf("===== Binding: %s at %s%n", arg0,
102: arg1); ///////////////////////
103: synchronized (directory) {
104: arg0 = arg0.trim();
105: if (directory.containsKey(arg0))
106: throw new NameAlreadyBoundException();
107: directory.put(arg0, arg1);
108: }
109: }
110:
111: public void close() throws NamingException {
112: // TODO Auto-generated method stub
113:
114: }
115:
116: public Name composeName(Name arg0, Name arg1)
117: throws NamingException {
118: // TODO Auto-generated method stub
119: return null;
120: }
121:
122: public String composeName(String arg0, String arg1)
123: throws NamingException {
124: // TODO Auto-generated method stub
125: return null;
126: }
127:
128: public Context createSubcontext(Name arg0)
129: throws NamingException {
130: // TODO Auto-generated method stub
131: return null;
132: }
133:
134: public Context createSubcontext(String arg0)
135: throws NamingException {
136: // TODO Auto-generated method stub
137: return null;
138: }
139:
140: public void destroySubcontext(Name arg0) throws NamingException {
141: // TODO Auto-generated method stub
142:
143: }
144:
145: public void destroySubcontext(String arg0)
146: throws NamingException {
147: // TODO Auto-generated method stub
148:
149: }
150:
151: public Hashtable<?, ?> getEnvironment() throws NamingException {
152: // TODO Auto-generated method stub
153: return null;
154: }
155:
156: public String getNameInNamespace() throws NamingException {
157: // TODO Auto-generated method stub
158: return null;
159: }
160:
161: public NameParser getNameParser(Name arg0)
162: throws NamingException {
163: // TODO Auto-generated method stub
164: return null;
165: }
166:
167: public NameParser getNameParser(String arg0)
168: throws NamingException {
169: // TODO Auto-generated method stub
170: return null;
171: }
172:
173: public NamingEnumeration<NameClassPair> list(Name arg0)
174: throws NamingException {
175: // TODO Auto-generated method stub
176: return null;
177: }
178:
179: public NamingEnumeration<NameClassPair> list(String arg0)
180: throws NamingException {
181: // TODO Auto-generated method stub
182: return null;
183: }
184:
185: public NamingEnumeration<Binding> listBindings(Name arg0)
186: throws NamingException {
187: // TODO Auto-generated method stub
188: return null;
189: }
190:
191: public NamingEnumeration<Binding> listBindings(String arg0)
192: throws NamingException {
193: // TODO Auto-generated method stub
194: return null;
195: }
196:
197: public Object lookup(Name arg0) throws NamingException {
198: return lookup(arg0.toString());
199: }
200:
201: public Object lookup(String arg0) throws NamingException {
202: if (__debug)
203: System.err.printf("Requested %s = %s%n", arg0,
204: directory.get(arg0)); ///////////////
205: return directory.get(arg0.trim());
206: }
207:
208: public Object lookupLink(Name arg0) throws NamingException {
209: // TODO Auto-generated method stub
210: return null;
211: }
212:
213: public Object lookupLink(String arg0) throws NamingException {
214: // TODO Auto-generated method stub
215: return null;
216: }
217:
218: public void rebind(Name arg0, Object arg1)
219: throws NamingException {
220: // TODO Auto-generated method stub
221:
222: }
223:
224: public void rebind(String arg0, Object arg1)
225: throws NamingException {
226: // TODO Auto-generated method stub
227:
228: }
229:
230: public Object removeFromEnvironment(String arg0)
231: throws NamingException {
232: // TODO Auto-generated method stub
233: return null;
234: }
235:
236: public void rename(Name arg0, Name arg1) throws NamingException {
237: // TODO Auto-generated method stub
238:
239: }
240:
241: public void rename(String arg0, String arg1)
242: throws NamingException {
243: // TODO Auto-generated method stub
244:
245: }
246:
247: public void unbind(Name arg0) throws NamingException {
248: // TODO Auto-generated method stub
249:
250: }
251:
252: public void unbind(String arg0) throws NamingException {
253: // TODO Auto-generated method stub
254:
255: }
256:
257: final private static boolean __debug = false;
258: }
259: }
|