1 26 27 package com.rift.coad.lib.db; 29 30 import java.util.Map ; 32 import java.util.HashMap ; 33 import java.util.HashSet ; 34 import java.util.Set ; 35 import java.util.Iterator ; 36 import java.util.StringTokenizer ; 37 import javax.naming.Reference ; 38 import javax.naming.StringRefAddr ; 39 import java.util.HashMap ; 40 41 import org.apache.log4j.Logger; 43 44 import com.rift.coad.lib.configuration.ConfigurationFactory; 46 import com.rift.coad.lib.configuration.Configuration; 47 import com.rift.coad.lib.naming.ContextManager; 48 import com.rift.coad.lib.loader.MasterClassLoader; 49 50 51 57 public class DBSourceManager { 58 59 private final static String DB_SOURCES = "db_sources"; 61 private final static String DB_SOURCE = "db_source"; 62 private final static String DB_CONTEXT = "java:comp/env/jdbc"; 63 private final static String DB_CONTEXT_ID = "db_context"; 64 65 private static DBSourceManager singleton = null; 67 68 protected Logger log = 70 Logger.getLogger(DBSourceManager.class.getName()); 71 private ContextManager contextManager = null; 72 private String jndiBase = null; 73 74 79 private DBSourceManager() throws DBException { 80 try { 81 Configuration sourceConfig = ConfigurationFactory.getInstance(). 82 getConfig(this.getClass()); 83 try { 85 jndiBase = sourceConfig.getString( 86 DB_CONTEXT); 87 contextManager = new ContextManager(jndiBase); 88 } catch (Exception ex) { 89 jndiBase = DB_CONTEXT; 90 contextManager = new ContextManager(jndiBase); 91 } 92 93 StringTokenizer dbSources = new 94 StringTokenizer (sourceConfig.getString(DB_SOURCES),","); 95 while(dbSources.hasMoreTokens()) { 96 String dbSource = dbSources.nextToken().trim(); 97 try { 98 Configuration dbSourceConfig = ConfigurationFactory.getInstance(). 99 getConfig(Class.forName(dbSource)); 100 Set keys = dbSourceConfig.getKeys(); 101 Reference ref = new Reference ("javax.sql.DataSource", 102 "org.objectweb.jndi.DataSourceFactory", 103 null); 104 ref.add(new StringRefAddr ("driverClassName",dbSource)); 105 for (Iterator iter = keys.iterator(); iter.hasNext();) { 106 String key = (String )iter.next(); 107 if (key.equals(DB_SOURCE) || 108 !dbSourceConfig.isString(key)) { 109 continue; 110 } 111 ref.add(new StringRefAddr (key, 112 dbSourceConfig.getString(key))); 113 } 114 contextManager.bind(dbSourceConfig.getString(DB_SOURCE),ref); 115 } catch (Exception ex) { 116 log.error("Failed to instanciate the datase source for [" + 117 dbSource + "] because : " + ex.getMessage(), ex); 118 } 119 } 120 } catch (Exception ex) { 121 throw new DBException( 122 "Failed to instanciate the db source manager : " + 123 ex.getMessage(),ex); 124 } 125 } 126 127 128 131 public static synchronized void init() throws DBException { 132 if (singleton == null) { 133 singleton = new DBSourceManager(); 134 } 135 } 136 137 138 144 public static synchronized DBSourceManager getInstance() throws DBException { 145 if (singleton == null) { 146 throw new DBException( 147 "The Database Source manager has not been initialized."); 148 } 149 return singleton; 150 } 151 152 153 163 public void addDBSource(String driverPath, String driverClass, String jndi, 164 HashMap env) throws DBException { 165 try { 166 MasterClassLoader.getInstance().addLib(driverPath); 167 Class.forName(driverClass); 168 Reference ref = new Reference ("javax.sql.DataSource", 169 "org.apache.commons.dbcp.BasicDatabSourceFactory", 170 null); 171 ref.add(new StringRefAddr ("driverClassName",driverClass)); 172 Set keys = env.keySet(); 173 for (Iterator iter = keys.iterator(); iter.hasNext();) { 174 String key = (String )iter.next(); 175 if (key.equals(DB_SOURCE)) { 176 continue; 177 } 178 ref.add(new StringRefAddr (key,(String )env.get(key))); 179 } 180 String jndiRef = jndi; 181 if (jndiRef.contains(jndiBase)) { 182 jndiRef = jndiRef.substring(jndiRef.indexOf(jndiBase) + 183 jndiBase.length()); 184 } 185 contextManager.bind(jndiRef,ref); 186 } catch (Exception ex) { 187 throw new DBException("Failed to add a data source because : " + 188 ex.getMessage(),ex); 189 } 190 } 191 192 193 201 public void addDBSource(String driverClass, String jndi, 202 HashMap env) throws DBException { 203 try { 204 Class.forName(driverClass); 205 Reference ref = new Reference ("javax.sql.DataSource", 206 "org.apache.commons.dbcp.BasicDatabSourceFactory", 207 null); 208 ref.add(new StringRefAddr ("driverClassName",driverClass)); 209 Set keys = env.keySet(); 210 for (Iterator iter = keys.iterator(); iter.hasNext();) { 211 String key = (String )iter.next(); 212 if (key.equals(DB_SOURCE)) { 213 continue; 214 } 215 ref.add(new StringRefAddr (key,(String )env.get(key))); 216 } 217 String jndiRef = jndi; 218 if (jndiRef.contains(jndiBase)) { 219 jndiRef = jndiRef.substring(jndiRef.indexOf(jndiBase) + 220 jndiBase.length()); 221 } 222 contextManager.bind(jndiRef,ref); 223 } catch (Exception ex) { 224 throw new DBException("Failed to add a data source because : " + 225 ex.getMessage(),ex); 226 } 227 } 228 229 230 237 public void removeDBSource(String jndi) throws DBException { 238 try { 239 String jndiRef = jndi; 240 if (jndiRef.contains(jndiBase)) { 241 jndiRef = jndiRef.substring(jndiRef.indexOf(jndiBase) + 242 jndiBase.length()); 243 } 244 contextManager.unbind(jndiRef); 245 } catch (Exception ex) { 246 throw new DBException("Failed to remove [" + jndi + "] because : " + 247 ex.getMessage(),ex); 248 } 249 } 250 } 251 | Popular Tags |