1 23 24 package org.dbforms.conprovider; 25 26 import org.apache.commons.dbcp.BasicDataSource; 27 import org.apache.commons.logging.Log; 28 import org.apache.commons.logging.LogFactory; 29 30 import org.dbforms.util.Util; 31 32 import java.io.PrintWriter ; 33 34 import java.sql.Connection ; 35 import java.sql.SQLException ; 36 37 import java.util.Enumeration ; 38 import java.util.Properties ; 39 40 41 42 49 public class JakartaConnectionProvider extends ConnectionProvider { 50 54 protected static final String CP_PROPS_VALIDATION_QUERY = "validationQuery"; 55 56 60 protected static final String CP_PROPS_MAX_ACTIVE = "maxActive"; 61 62 66 protected static final String CP_PROPS_MAX_IDLE = "maxIdle"; 67 68 73 protected static final String CP_PROPS_MAX_WAIT = "maxWait"; 74 75 76 protected static final String CP_PROPS_USE_LOG = "useLog"; 77 private static Log logCat = LogFactory.getLog(JakartaConnectionProvider.class); 78 79 80 private BasicDataSource dataSource = null; 81 82 89 public JakartaConnectionProvider() throws Exception { 90 super(); 91 } 92 93 100 protected Connection getConnection() throws SQLException { 101 logCat.debug("::getConnection - MaxActive = " + dataSource.getMaxActive()); 102 logCat.debug("::getConnection - NumActive = " + dataSource.getNumActive()); 103 logCat.debug("::getConnection - NumIdle = " + dataSource.getNumIdle()); 104 105 return dataSource.getConnection(); 106 } 107 108 109 114 protected void init() throws Exception { 115 Properties props = null; 116 117 dataSource = new BasicDataSource(); 118 dataSource.setDriverClassName(getPrefs().getJdbcDriver()); 119 dataSource.setUrl(getPrefs().getJdbcURL()); 120 dataSource.setUsername(getPrefs().getUser()); 121 dataSource.setPassword(getPrefs().getPassword()); 122 123 if ((props = getPrefs() 125 .getProperties()) != null) { 126 for (Enumeration e = props.propertyNames(); e.hasMoreElements();) { 127 String key = (String ) e.nextElement(); 128 dataSource.addConnectionProperty(key, props.getProperty(key)); 129 logCat.info("::init - dataSource property [" + key + "] = [" 130 + props.getProperty(key) + "]"); 131 } 132 } 133 134 if ((props = getPrefs() 138 .getPoolProperties()) == null) { 139 props = new Properties (); 140 } 141 142 String validationQuery = props.getProperty(CP_PROPS_VALIDATION_QUERY, null); 143 144 if (!Util.isNull(validationQuery)) { 145 dataSource.setValidationQuery(validationQuery.trim()); 146 } 147 148 dataSource.setMaxActive(Integer.parseInt(props.getProperty(CP_PROPS_MAX_ACTIVE, 149 "20"))); 150 dataSource.setMaxIdle(Integer.parseInt(props.getProperty(CP_PROPS_MAX_IDLE, 151 "5"))); 152 dataSource.setMaxWait(Long.parseLong(props.getProperty(CP_PROPS_MAX_WAIT, 153 "-1"))); 154 155 String useLog = props.getProperty(CP_PROPS_USE_LOG, "false"); 157 158 if (!Util.isNull(useLog) && "true".equals(useLog.trim())) { 159 logCat.info("::init - dataSource log activated"); 160 dataSource.setLogWriter(new Log4jPrintWriter()); 161 } 162 } 163 164 private static class Log4jPrintWriter extends PrintWriter { 165 private StringBuffer text = new StringBuffer (""); 166 167 170 public Log4jPrintWriter() { 171 super(System.err); } 173 174 178 public void close() { 179 flush(); 180 } 181 182 183 186 public void flush() { 187 if (!text.toString() 188 .equals("")) { 189 logCat.info(text.toString()); 190 text.setLength(0); 191 } 192 } 193 194 195 200 public void print(boolean b) { 201 text.append(b); 202 } 203 204 205 210 public void print(char c) { 211 text.append(c); 212 } 213 214 215 220 public void print(char[] s) { 221 text.append(s); 222 } 223 224 225 230 public void print(double d) { 231 text.append(d); 232 } 233 234 235 240 public void print(float f) { 241 text.append(f); 242 } 243 244 245 250 public void print(int i) { 251 text.append(i); 252 } 253 254 255 260 public void print(long l) { 261 text.append(l); 262 } 263 264 265 270 public void print(Object obj) { 271 text.append(obj); 272 } 273 274 275 280 public void print(String s) { 281 text.append(s); 282 } 283 284 285 288 public void println() { 289 flush(); 290 } 291 292 293 298 public void println(boolean x) { 299 text.append(x); 300 flush(); 301 } 302 303 304 309 public void println(char x) { 310 text.append(x); 311 flush(); 312 } 313 314 315 320 public void println(char[] x) { 321 text.append(x); 322 flush(); 323 } 324 325 326 331 public void println(double x) { 332 text.append(x); 333 flush(); 334 } 335 336 337 342 public void println(float x) { 343 text.append(x); 344 flush(); 345 } 346 347 348 353 public void println(int x) { 354 text.append(x); 355 flush(); 356 } 357 358 359 364 public void println(long x) { 365 text.append(x); 366 flush(); 367 } 368 369 370 375 public void println(Object x) { 376 text.append(x); 377 flush(); 378 } 379 380 381 386 public void println(String x) { 387 text.append(x); 388 flush(); 389 } 390 } 391 } 392 | Popular Tags |