1 6 7 package org.jfox.jdbc.datasource; 8 9 import java.util.Hashtable ; 10 import java.util.Map ; 11 import java.util.WeakHashMap ; 12 import javax.naming.Context ; 13 import javax.naming.Name ; 14 import javax.naming.Reference ; 15 import javax.naming.spi.ObjectFactory ; 16 17 20 21 public class DataSourceObjectFactory implements ObjectFactory { 22 23 28 private static Map dataSources = new WeakHashMap (); 29 30 40 42 public synchronized Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable environment) throws Exception { 43 Object result = null; 44 Reference ref = (Reference ) obj; 45 if(ref.getClassName().equals(PoolDataSource.class.getName())) { 46 String _dsName = (String ) ref.get("dsName").getContent(); 47 String _dbDriver = (String ) ref.get("dbDriver").getContent(); 48 String _dbURL = (String ) ref.get("dbURL").getContent(); 49 String _user = (String ) ref.get("user").getContent(); 50 String _password = (String ) ref.get("password").getContent(); 51 52 if(dataSources.containsKey(_dsName)) { 53 return dataSources.get(_dsName); 54 } 55 else { 56 int initNum = Integer.parseInt((String ) ref.get("initNum").getContent()); 57 int maxRest = Integer.parseInt((String ) ref.get("maxRest").getContent()); 58 PoolDataSource pds = new PoolDataSource(_dsName, _dbDriver, _dbURL, _user, _password); 59 pds.setInitNum(initNum); 60 pds.setMaxRest(maxRest); 61 pds.init(); 62 dataSources.put(_dsName, pds); 63 result = pds; 64 } 65 } 66 return result; 67 } 68 69 static void removeDataSource(String dsName) { 70 dataSources.remove(dsName); 71 } 72 73 public static void main(String [] args) { 74 75 } 76 } | Popular Tags |