1 16 package org.outerj.daisy.datasource; 17 18 import java.io.PrintWriter ; 19 import java.sql.Connection ; 20 import java.sql.DatabaseMetaData ; 21 import java.sql.SQLException ; 22 import java.sql.Statement ; 23 import java.util.regex.Matcher ; 24 import java.util.regex.Pattern ; 25 26 import javax.management.MBeanServer ; 27 import javax.management.ObjectName ; 28 29 import org.apache.avalon.framework.activity.Disposable; 30 import org.apache.avalon.framework.activity.Initializable; 31 import org.apache.avalon.framework.configuration.Configurable; 32 import org.apache.avalon.framework.configuration.Configuration; 33 import org.apache.avalon.framework.configuration.ConfigurationException; 34 import org.apache.avalon.framework.logger.AbstractLogEnabled; 35 import org.apache.avalon.framework.service.ServiceException; 36 import org.apache.avalon.framework.service.ServiceManager; 37 import org.apache.avalon.framework.service.Serviceable; 38 import org.apache.commons.dbcp.BasicDataSource; 39 40 48 public class JdbcDataSource extends AbstractLogEnabled implements javax.sql.DataSource , Configurable, 49 Disposable, Serviceable, Initializable, JdbcDataSourceMBean { 50 51 private BasicDataSource source = null; 52 private ServiceManager serviceManager; 53 private boolean isMySQL = false; 54 55 public void configure(Configuration configuration) throws ConfigurationException { 56 try { 58 source = new BasicDataSource(); 59 60 String url = configuration.getChild("url").getValue(null); 61 source.setUrl(url); 62 String username = configuration.getChild("username").getValue(null); 63 source.setUsername(username); 64 String password = configuration.getChild("password").getValue(null); 65 source.setPassword(password); 66 67 int maxActive = configuration.getChild("maxActive").getValueAsInteger(8); 68 int maxIdle = configuration.getChild("maxIdle").getValueAsInteger(8); 69 int minIdle = configuration.getChild("minIdle").getValueAsInteger(0); 70 int maxWait = configuration.getChild("maxWait").getValueAsInteger(5000); 71 72 source.setMaxActive(maxActive); 73 source.setMaxIdle(maxIdle); 74 source.setMinIdle(minIdle); 75 source.setMaxWait(maxWait); 76 77 Configuration propertyConf[] = configuration.getChild("connectionProperties").getChildren("property"); 78 for (int i = 0; i < propertyConf.length; i++) { 79 source.addConnectionProperty(propertyConf[i].getName(), propertyConf[i].getValue()); 80 } 81 82 final java.io.Writer writer = new java.io.CharArrayWriter () { 87 public void flush() { 88 if (JdbcDataSource.this.getLogger().isErrorEnabled()) { 90 JdbcDataSource.this.getLogger().error(toString()); 91 } 92 reset(); } 94 }; 95 96 source.setLogWriter(new PrintWriter (writer, true)); 97 98 Connection conn = source.getConnection(); 100 try { 101 DatabaseMetaData metaData = conn.getMetaData(); 102 if (getLogger().isInfoEnabled()) 103 getLogger().info("Running on database: \"" + metaData.getDatabaseProductName() + "\" version \"" + metaData.getDatabaseProductVersion() + "\"."); 104 if (metaData.getDatabaseProductName().equals("MySQL")) { 105 Pattern versionPattern = Pattern.compile("([0-9]+)\\.([0-9]+)\\.([0-9]+).*"); 106 Matcher versionMatcher = versionPattern.matcher(metaData.getDatabaseProductVersion()); 107 if (versionMatcher.matches()) { 108 int majorVersion = Integer.parseInt(versionMatcher.group(1)); 109 int minorVersion = Integer.parseInt(versionMatcher.group(2)); 110 if ((majorVersion == 4 && minorVersion >= 1) || majorVersion > 4) { 111 isMySQL = true; 112 } 113 } 114 } 115 } finally { 116 conn.close(); 117 } 118 } catch (Exception e) { 119 throw new ConfigurationException("Error configurable datasource", e); 120 } 121 } 122 123 127 public void service(ServiceManager serviceManager) throws ServiceException { 128 this.serviceManager = serviceManager; 129 } 130 131 public void initialize() throws Exception { 132 MBeanServer mbeanServer = (MBeanServer )serviceManager.lookup("mbeanserver"); 133 try { 134 mbeanServer.registerMBean(this, new ObjectName ("Daisy:name=JdbcDataSource")); 135 } finally { 136 serviceManager.release(mbeanServer); 137 } 138 } 139 140 public void dispose() { 141 try { 142 source.close(); 143 } catch (Throwable e) { 144 getLogger().error("Error disposing pool.", e); 145 } 146 } 147 148 public int getLoginTimeout() throws SQLException { 149 return source.getLoginTimeout(); 150 } 151 152 public void setLoginTimeout(int seconds) throws SQLException { 153 source.setLoginTimeout(seconds); 154 } 155 156 public PrintWriter getLogWriter() throws SQLException { 157 return source.getLogWriter(); 158 } 159 160 public void setLogWriter(PrintWriter out) throws SQLException { 161 throw new RuntimeException ("I won't let you overwrite my log writer."); 162 } 163 164 public Connection getConnection() throws SQLException { 165 Connection conn = source.getConnection(); 166 if (isMySQL) { 167 Statement stmt = null; 168 try { 169 stmt = conn.createStatement(); 170 stmt.execute("SET sql_mode='ANSI_QUOTES'"); 173 } finally { 174 if (stmt != null) 175 stmt.close(); 176 } 177 } 178 return conn; 179 } 180 181 public Connection getConnection(String username, String password) throws SQLException { 182 return source.getConnection(username, password); 183 } 184 185 public int getNumActive() { 186 return source.getNumActive(); 187 } 188 189 public int getNumIdle() { 190 return source.getNumIdle(); 191 } 192 193 public String getUrl() { 194 return source.getUrl(); 195 } 196 197 public String getUserName() { 198 return source.getUsername(); 199 } 200 201 public int getMinIdle() { 202 return source.getMinIdle(); 203 } 204 205 public int getMaxIdle() { 206 return source.getMaxIdle(); 207 } 208 209 public int getMaxActive() { 210 return source.getMaxActive(); 211 } 212 } 213 | Popular Tags |