KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jndi > DataSourceFactory


1 package org.objectweb.jndi;
2
3 import java.util.Enumeration JavaDoc;
4 import java.util.Hashtable JavaDoc;
5 import javax.naming.Context JavaDoc;
6 import javax.naming.Name JavaDoc;
7 import javax.naming.NamingException JavaDoc;
8 import javax.naming.RefAddr JavaDoc;
9 import javax.naming.Reference JavaDoc;
10 import javax.naming.spi.ObjectFactory JavaDoc;
11
12 import org.enhydra.jdbc.pool.StandardXAPoolDataSource;
13 import org.enhydra.jdbc.standard.StandardXADataSource;
14
15 import org.objectweb.jotm.Jotm;
16 import org.objectweb.transaction.jta.TMService;
17
18 /**
19  * @author jmesnil
20  *
21  */

22 public class DataSourceFactory implements ObjectFactory JavaDoc {
23
24     private static Hashtable JavaDoc table = new Hashtable JavaDoc();
25
26     static {
27         try {
28             jotm = new Jotm(true, false);
29         } catch (NamingException JavaDoc e) {
30             e.printStackTrace();
31         }
32     }
33
34     public static TMService jotm;
35
36     /**
37     * @see javax.naming.spi.ObjectFactory#getObjectInstance(java.lang.Object, javax.naming.Name, javax.naming.Context, java.util.Hashtable)
38     */

39     public Object JavaDoc getObjectInstance(
40         Object JavaDoc obj,
41         Name JavaDoc n,
42         Context JavaDoc nameCtx,
43         Hashtable JavaDoc environment)
44         throws Exception JavaDoc {
45         StandardXAPoolDataSource xads = null;
46         StandardXADataSource ds = null;
47         try {
48
49             Reference JavaDoc ref = (Reference JavaDoc) obj;
50             ds = new StandardXADataSource();
51             xads = new StandardXAPoolDataSource(ds);
52             Enumeration JavaDoc addrs = ref.getAll();
53             while (addrs.hasMoreElements()) {
54                 RefAddr JavaDoc addr = (RefAddr JavaDoc) addrs.nextElement();
55                 String JavaDoc name = addr.getType();
56                 String JavaDoc value = (String JavaDoc) addr.getContent();
57                 if (name.equals("driverClassName")) {
58                     ds.setDriverName(value);
59                 } else if (name.equals("url")) {
60                     ds.setUrl(value);
61                 } else if (name.equals("username")) {
62                     xads.user = value;
63                     ds.setUser(value);
64                 } else if (name.equals("password")) {
65                     ds.setPassword(value);
66                     xads.password = value;
67                 } else if (name.equals("min")) {
68                     try {
69                         int min = Integer.parseInt(value);
70                         xads.setMinSize(min);
71                     } catch (NumberFormatException JavaDoc e) {
72                         // we do nothing (default value will be used)
73
}
74                 } else if (name.equals("max")) {
75                     try {
76                         int max = Integer.parseInt(value);
77                         xads.setMaxSize(max);
78                     } catch (NumberFormatException JavaDoc e) {
79                         // we do nothing (default value will be used)
80
}
81                 }
82             }
83             xads.setTransactionManager(jotm.getTransactionManager());
84             xads.setDataSource(ds);
85         } catch (Exception JavaDoc e) {
86             e.printStackTrace();
87         }
88         if (table.containsKey(ds)) {
89             return table.get(ds);
90         } else {
91             table.put(ds, xads);
92             return xads;
93         }
94     }
95 }
96
Popular Tags