1 package org.objectweb.jndi; 2 3 import java.util.Enumeration ; 4 import java.util.Hashtable ; 5 import javax.naming.Context ; 6 import javax.naming.Name ; 7 import javax.naming.NamingException ; 8 import javax.naming.RefAddr ; 9 import javax.naming.Reference ; 10 import javax.naming.spi.ObjectFactory ; 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 22 public class DataSourceFactory implements ObjectFactory { 23 24 private static Hashtable table = new Hashtable (); 25 26 static { 27 try { 28 jotm = new Jotm(true, false); 29 } catch (NamingException e) { 30 e.printStackTrace(); 31 } 32 } 33 34 public static TMService jotm; 35 36 39 public Object getObjectInstance( 40 Object obj, 41 Name n, 42 Context nameCtx, 43 Hashtable environment) 44 throws Exception { 45 StandardXAPoolDataSource xads = null; 46 StandardXADataSource ds = null; 47 try { 48 49 Reference ref = (Reference ) obj; 50 ds = new StandardXADataSource(); 51 xads = new StandardXAPoolDataSource(ds); 52 Enumeration addrs = ref.getAll(); 53 while (addrs.hasMoreElements()) { 54 RefAddr addr = (RefAddr ) addrs.nextElement(); 55 String name = addr.getType(); 56 String value = (String ) 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 e) { 72 } 74 } else if (name.equals("max")) { 75 try { 76 int max = Integer.parseInt(value); 77 xads.setMaxSize(max); 78 } catch (NumberFormatException e) { 79 } 81 } 82 } 83 xads.setTransactionManager(jotm.getTransactionManager()); 84 xads.setDataSource(ds); 85 } catch (Exception 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 |