001: /* DirtyLittleCheatingContextFactory.java */
002: package jtaDiscRack;
003:
004: import java.util.Hashtable;
005: import java.util.Properties;
006:
007: import javax.naming.*;
008: import javax.naming.spi.InitialContextFactory;
009: import javax.transaction.TransactionManager;
010:
011: import org.enhydra.jdbc.standard.StandardXADataSource;
012: import org.objectweb.jotm.Jotm;
013:
014: public class DirtyLittleCheatingContextFactory implements
015: InitialContextFactory {
016:
017: private static class Instance {
018: static final Context value = new MyContext(null);
019: }
020:
021: public Context getInitialContext(Hashtable environment)
022: throws NamingException {
023: return Instance.value;
024: }
025:
026: public static void setup(String[] datasourceNames) throws Exception {
027: System.setProperty(Context.INITIAL_CONTEXT_FACTORY,
028: DirtyLittleCheatingContextFactory.class.getName());
029: System.setProperty(Context.URL_PKG_PREFIXES, "jtaDiscRack");
030: Properties p = new Properties();
031: InitialContext ictx = new InitialContext();
032: TransactionManager tm = (TransactionManager) ictx
033: .lookup("java:comp/UserTransaction");
034: for (int i = 0; i < datasourceNames.length; i++) {
035: try {
036: p.load(ClassLoader
037: .getSystemResourceAsStream(datasourceNames[i]
038: + ".properties"));
039: StandardXADataSource xads = new StandardXADataSource();
040: xads.setDriverName(p
041: .getProperty("datasource.classname"));
042: xads.setUrl(p.getProperty("datasource.url"));
043: xads.setUser(p.getProperty("datasource.username"));
044: xads.setPassword(p.getProperty("datasource.password"));
045: xads.setMinCon(Integer.parseInt(p
046: .getProperty("jdbc.minconpool")));
047: xads.setMaxCon(Integer.parseInt(p
048: .getProperty("jdbc.maxconpool")));
049: xads.setTransactionManager(tm);
050: ictx.bind("java:comp/env/" + datasourceNames[i], xads);
051: } catch (Exception e) {
052: System.err.println("Configuration read for "
053: + datasourceNames[i] + " failed!");
054: } finally {
055: p.clear();
056: }
057: }
058: }
059:
060: public static void main(String[] args) throws Throwable {
061: new InitialContext().lookup("java:/Vidi");
062: System.out
063: .println("\n" + new InitialContext().getEnvironment());
064: Instance.value.close();
065: }
066: }
067:
068: class MyContext implements javax.naming.Context {
069:
070: private Hashtable env;
071:
072: private Hashtable collective;
073:
074: private Jotm jotm;
075:
076: public MyContext(Hashtable environment) {
077: env = environment;
078: collective = new Hashtable();
079: try {
080: jotm = new Jotm(true, false);
081: bind("java:comp/env", this );
082: bind("java:comp/UserTransaction", jotm
083: .getTransactionManager());
084: } catch (Exception e) {
085: throw new Error("FATAL", e);
086: }
087: System.setProperty(Context.INITIAL_CONTEXT_FACTORY,
088: DirtyLittleCheatingContextFactory.class.getName());
089: System.setProperty(Context.URL_PKG_PREFIXES, "jtaDiscRack");
090: }
091:
092: public void close() throws NamingException {
093: collective = env = null;
094: jotm.stop();
095: }
096:
097: public String getNameInNamespace() throws NamingException {
098: new Throwable().printStackTrace();
099: return null;
100: }
101:
102: public void destroySubcontext(String name) throws NamingException {
103: new Throwable().printStackTrace();
104:
105: }
106:
107: public void unbind(String name) throws NamingException {
108: new Throwable().printStackTrace();
109:
110: }
111:
112: public Hashtable getEnvironment() throws NamingException {
113: return env;
114: }
115:
116: public void destroySubcontext(Name name) throws NamingException {
117: new Throwable().printStackTrace();
118:
119: }
120:
121: public void unbind(Name name) throws NamingException {
122: if (null == collective.remove(name))
123: throw new NamingException(name + " were not there!");
124: }
125:
126: public Object lookup(String name) throws NamingException {
127: return lookup(new CompositeName(name));
128: }
129:
130: public Object lookupLink(String name) throws NamingException {
131: new Throwable().printStackTrace();
132: return null;
133: }
134:
135: public Object removeFromEnvironment(String propName)
136: throws NamingException {
137: new Throwable().printStackTrace();
138: return null;
139: }
140:
141: public void bind(String name, Object obj) throws NamingException {
142: bind(new CompositeName(name), obj);
143: }
144:
145: public void rebind(String name, Object obj) throws NamingException {
146: rebind(new CompositeName(name), obj);
147: }
148:
149: public Object lookup(Name name) throws NamingException {
150: return collective.get(name);
151: }
152:
153: public Object lookupLink(Name name) throws NamingException {
154: new Throwable().printStackTrace();
155: return null;
156: }
157:
158: public void bind(Name name, Object obj) throws NamingException {
159: collective.put(name, obj);
160: }
161:
162: public void rebind(Name name, Object obj) throws NamingException {
163: collective.put(name, obj);
164: }
165:
166: public void rename(String oldName, String newName)
167: throws NamingException {
168: new Throwable().printStackTrace();
169:
170: }
171:
172: public Context createSubcontext(String name) throws NamingException {
173: new Throwable().printStackTrace();
174: return null;
175: }
176:
177: public Context createSubcontext(Name name) throws NamingException {
178: new Throwable().printStackTrace();
179: return null;
180: }
181:
182: public void rename(Name oldName, Name newName)
183: throws NamingException {
184: new Throwable().printStackTrace();
185:
186: }
187:
188: public NameParser getNameParser(String name) throws NamingException {
189: new Throwable().printStackTrace();
190: return null;
191: }
192:
193: public NameParser getNameParser(Name name) throws NamingException {
194: new Throwable().printStackTrace();
195: return null;
196: }
197:
198: public NamingEnumeration list(String name) throws NamingException {
199: new Throwable().printStackTrace();
200: return null;
201: }
202:
203: public NamingEnumeration listBindings(String name)
204: throws NamingException {
205: new Throwable().printStackTrace();
206: return null;
207: }
208:
209: public NamingEnumeration list(Name name) throws NamingException {
210: new Throwable().printStackTrace();
211: return null;
212: }
213:
214: public NamingEnumeration listBindings(Name name)
215: throws NamingException {
216: new Throwable().printStackTrace();
217: return null;
218: }
219:
220: public Object addToEnvironment(String propName, Object propVal)
221: throws NamingException {
222: new Throwable().printStackTrace();
223: return null;
224: }
225:
226: public String composeName(String name, String prefix)
227: throws NamingException {
228: new Throwable().printStackTrace();
229: return null;
230: }
231:
232: public Name composeName(Name name, Name prefix)
233: throws NamingException {
234: new Throwable().printStackTrace();
235: return null;
236: }
237:
238: }
|