1 4 package org.ofbiz.minerva.pool.jdbc; 5 6 import java.io.PrintWriter ; 7 import java.sql.Connection ; 8 import java.util.Collection ; 9 import java.util.HashMap ; 10 import java.util.HashSet ; 11 import java.util.Hashtable ; 12 import java.util.Properties ; 13 14 import javax.naming.Context ; 15 import javax.naming.InitialContext ; 16 import javax.naming.Name ; 17 import javax.naming.NamingException ; 18 import javax.naming.RefAddr ; 19 import javax.naming.Reference ; 20 import javax.naming.Referenceable ; 21 import javax.naming.StringRefAddr ; 22 import javax.naming.spi.ObjectFactory ; 23 import javax.sql.DataSource ; 24 25 import org.apache.log4j.Logger; 26 import org.ofbiz.base.util.Log4jLoggerWriter; 27 import org.ofbiz.minerva.pool.ObjectPool; 28 29 41 public class JDBCPoolDataSource implements DataSource , Referenceable , ObjectFactory { 42 43 private static Logger log = Logger.getLogger(JDBCPoolDataSource.class); 44 45 private static HashMap sources = new HashMap (); 46 47 50 public static Collection getDataSources() { 51 return new HashSet (sources.values()); 52 } 53 54 57 public static JDBCPoolDataSource getDataSource(String poolName) { 58 return (JDBCPoolDataSource) sources.get(poolName); 59 } 60 61 private ObjectPool pool; 62 private JDBCConnectionFactory factory; 63 private PrintWriter logWriter; 64 private int timeout; 65 private boolean initialized = false; 66 private String jndiName; 67 68 72 public JDBCPoolDataSource() { 73 pool = new ObjectPool(); 74 factory = new JDBCConnectionFactory(); 75 PoolDriver.instance(); 76 } 77 78 84 public void setJNDIName(String name) throws NamingException { 85 InitialContext ctx = new InitialContext (); 86 if (jndiName != null && !jndiName.equals(name)) 87 ctx.unbind(jndiName); 88 if (name != null) 89 ctx.bind(name, this); 90 jndiName = name; 91 } 92 93 98 public String getJNDIName() { 99 return jndiName; 100 } 101 102 public void setJDBCURL(String url) { 104 factory.setConnectURL(url); 105 } 106 107 public String getJDBCURL() { 108 return factory.getConnectURL(); 109 } 110 111 public void setJDBCProperties(Properties props) { 112 factory.setConnectProperties(props); 113 } 114 115 public void setProperties(String props) { 116 setJDBCProperties(parseProperties(props)); 117 } 118 119 public Properties getJDBCProperties() { 120 return factory.getConnectProperties(); 121 } 122 123 public void setJDBCUser(String user) { 124 factory.setUser(user); 125 } 126 127 public String getJDBCUser() { 128 return factory.getUser(); 129 } 130 131 public void setJDBCPassword(String password) { 132 factory.setPassword(password); 133 } 134 135 public String getJDBCPassword() { 136 return factory.getPassword(); 137 } 138 public void setPoolName(String name) { 140 pool.setName(name); 141 sources.put(pool.getName(), this); 142 } 143 144 public String getPoolName() { 145 return pool.getName(); 146 } 147 148 public void setMinSize(int size) { 149 pool.setMinSize(size); 150 } 151 152 public int getMinSize() { 153 return pool.getMinSize(); 154 } 155 156 public void setMaxSize(int size) { 157 pool.setMaxSize(size); 158 } 159 160 public int getMaxSize() { 161 return pool.getMaxSize(); 162 } 163 164 public void setBlocking(boolean blocking) { 165 pool.setBlocking(blocking); 166 } 167 168 public boolean isBlocking() { 169 return pool.isBlocking(); 170 } 171 172 public void setIdleTimeoutEnabled(boolean allowShrinking) { 173 pool.setIdleTimeoutEnabled(allowShrinking); 174 } 175 176 public boolean isIdleTimeoutEnabled() { 177 return pool.isIdleTimeoutEnabled(); 178 } 179 180 public void setGCEnabled(boolean allowGC) { 181 pool.setGCEnabled(allowGC); 182 } 183 184 public boolean isGCEnabled() { 185 return pool.isGCEnabled(); 186 } 187 188 public void setMaxIdleTimeoutPercent(float percent) { 189 pool.setMaxIdleTimeoutPercent(percent); 190 } 191 192 public float getMaxIdleTimeoutPercent() { 193 return pool.getMaxIdleTimeoutPercent(); 194 } 195 196 public void setIdleTimeout(long millis) { 197 pool.setIdleTimeout(millis); 198 } 199 200 public long getIdleTimeout() { 201 return pool.getIdleTimeout(); 202 } 203 204 public void setGCMinIdleTime(long millis) { 205 pool.setGCMinIdleTime(millis); 206 } 207 208 public long getGCMinIdleTime() { 209 return pool.getGCMinIdleTime(); 210 } 211 212 public void setGCInterval(long millis) { 213 pool.setGCInterval(millis); 214 } 215 216 public long getGCInterval() { 217 return pool.getGCInterval(); 218 } 219 220 public void setInvalidateOnError(boolean invalidate) { 221 pool.setInvalidateOnError(invalidate); 222 } 223 224 public boolean isInvalidateOnError() { 225 return pool.isInvalidateOnError(); 226 } 227 228 public void setTimestampUsed(boolean timestamp) { 229 pool.setTimestampUsed(timestamp); 230 } 231 232 public boolean isTimestampUsed() { 233 return pool.isTimestampUsed(); 234 } 235 236 238 242 public void initialize() { 243 initialized = true; 244 pool.setObjectFactory(factory); 245 pool.initialize(); 246 } 247 248 252 public String getPoolStatus() { 253 return pool.toString(); 254 } 255 256 260 public void close() { 261 try { 262 setJNDIName(null); 263 } catch (NamingException e) { 264 log.warn("Can't unbind from JNDI", e); 265 } 266 sources.remove(pool.getName()); 267 pool.shutDown(); 268 pool = null; 269 factory = null; 270 } 271 272 275 public Connection getConnection() throws java.sql.SQLException { 276 if (!initialized) 277 initialize(); 278 279 return (Connection ) pool.getObject(); 280 } 281 282 288 public Connection getConnection(String user, String password) throws java.sql.SQLException { 289 if (!initialized) 290 initialize(); 291 292 factory.setUser(user); 293 factory.setPassword(password); 294 return (Connection ) pool.getObject(); 295 } 296 297 300 public PrintWriter getLogWriter() throws java.sql.SQLException { 301 return logWriter; 302 } 303 304 307 public void setLogWriter(PrintWriter writer) throws java.sql.SQLException { 308 if (writer == null) { 309 logWriter = null; 310 } else { 311 if (logWriter == null) { 312 logWriter = new Log4jLoggerWriter(log); 313 } 314 } 315 } 316 317 320 public int getLoginTimeout() throws java.sql.SQLException { 321 return timeout; 322 } 323 324 327 public void setLoginTimeout(int timeout) throws java.sql.SQLException { 328 this.timeout = timeout; 329 } 330 331 334 private static Properties parseProperties(String string) { 335 Properties props = new Properties (); 336 if (string == null || string.length() == 0) 337 return props; 338 339 int lastPos = -1; 340 int pos = string.indexOf(";"); 341 while (pos > -1) { 342 addProperty(props, string.substring(lastPos + 1, pos)); 343 lastPos = pos; 344 pos = string.indexOf(";", lastPos + 1); 345 } 346 addProperty(props, string.substring(lastPos + 1)); 347 return props; 348 } 349 350 353 private static void addProperty(Properties props, String property) { 354 int pos = property.indexOf("="); 355 if (pos < 0) { 356 System.err.println("Unable to parse property '" + property + "' - please use 'name=value'"); 357 return; 358 } 359 props.setProperty(property.substring(0, pos), property.substring(pos + 1)); 360 } 361 362 366 public Reference getReference() { 367 return new Reference (getClass().getName(), new StringRefAddr ("JDBCPool", pool.getName()), getClass().getName(), null); 368 } 369 370 374 public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable environment) { 375 if (obj instanceof Reference ) { 376 Reference ref = (Reference ) obj; 377 if (ref.getClassName().equals(getClass().getName())) { 378 RefAddr addr = ref.get("JDBCPool"); 379 return sources.get(addr.getContent()); 380 } 381 } 382 return null; 383 } 384 } 385 386 389 | Popular Tags |