1 6 package org.logicalcobwebs.proxool; 7 8 import org.apache.commons.logging.Log; 9 import org.apache.commons.logging.LogFactory; 10 import org.logicalcobwebs.proxool.resources.ResourceNamesIF; 11 12 import java.sql.Connection ; 13 import java.sql.Driver ; 14 import java.sql.DriverManager ; 15 import java.sql.DriverPropertyInfo ; 16 import java.sql.SQLException ; 17 import java.util.Properties ; 18 import java.util.ResourceBundle ; 19 20 26 public class ProxoolDriver implements Driver { 27 28 private static final Log LOG = LogFactory.getLog(ProxoolDriver.class); 29 30 static { 31 try { 32 DriverManager.registerDriver(new ProxoolDriver()); 33 } catch (SQLException e) { 34 System.out.println(e.toString()); 35 } 36 } 37 38 private static final ResourceBundle ATTRIBUTE_DESCRIPTIONS_RESOURCE = createAttributeDescriptionsResource (); 39 40 private static ResourceBundle createAttributeDescriptionsResource () { 41 try { 42 return ResourceBundle.getBundle (ResourceNamesIF.ATTRIBUTE_DESCRIPTIONS); 43 } catch (Exception e) { 44 LOG.error ("Could not find resource " + ResourceNamesIF.ATTRIBUTE_DESCRIPTIONS, e); 45 } 46 return null; 47 } 48 49 66 public Connection connect(String url, Properties info) 67 throws SQLException { 68 if (!url.startsWith("proxool")) { 69 return null; 70 } 71 72 ConnectionPool cp = null; 73 try { 74 String alias = ProxoolFacade.getAlias(url); 75 76 if (!ConnectionPoolManager.getInstance().isPoolExists(alias)) { 77 ProxoolFacade.registerConnectionPool(url, info, false); 78 cp = ConnectionPoolManager.getInstance().getConnectionPool(alias); 79 } else if (info != null && info.size() > 0) { 80 cp = ConnectionPoolManager.getInstance().getConnectionPool(alias); 82 ConnectionPoolDefinition cpd = cp.getDefinition(); 83 if (!cpd.isEqual(url, info)) { 84 cpd.redefine(url, info); 85 } 86 } else { 87 cp = ConnectionPoolManager.getInstance().getConnectionPool(alias); 88 } 89 return cp.getConnection(); 90 91 } catch (SQLException e) { 92 try { 96 String alias = ProxoolFacade.getAlias(url); 97 cp = ConnectionPoolManager.getInstance().getConnectionPool(alias); 98 if (FatalSqlExceptionHelper.testException(cp.getDefinition(), e)) { 99 FatalSqlExceptionHelper.throwFatalSQLException(cp.getDefinition().getFatalSqlExceptionWrapper(), e); 100 } 101 throw e; 103 } catch (ProxoolException e1) { 104 LOG.error("Problem", e); 105 throw new SQLException (e.toString()); 106 } 107 } catch (ProxoolException e) { 108 LOG.error("Problem", e); 109 throw new SQLException (e.toString()); 110 } 111 112 } 113 114 117 public boolean acceptsURL(String url) throws SQLException { 118 return (url.startsWith("proxool")); 119 } 120 121 124 public DriverPropertyInfo [] getPropertyInfo(String url, Properties info) 125 throws SQLException { 126 127 DriverPropertyInfo [] dpi = new DriverPropertyInfo [18]; 128 ConnectionPool cp = null; 129 try { 130 cp = ConnectionPoolManager.getInstance().getConnectionPool(url); 131 } catch (ProxoolException e) { 132 throw new SQLException (e.toString()); 133 } 134 135 ConnectionPoolDefinitionIF cpd = cp.getDefinition(); 136 137 dpi[0] = buildDriverPropertyInfo(ProxoolConstants.DELEGATE_DRIVER_PROPERTY, 138 String.valueOf(cpd.getDriver())); 139 140 dpi[1] = buildDriverPropertyInfo(ProxoolConstants.DELEGATE_URL_PROPERTY, 141 String.valueOf(cpd.getUrl())); 142 143 dpi[2] = buildDriverPropertyInfo(ProxoolConstants.MINIMUM_CONNECTION_COUNT_PROPERTY, 144 String.valueOf(cpd.getMinimumConnectionCount())); 145 146 dpi[3] = buildDriverPropertyInfo(ProxoolConstants.MAXIMUM_CONNECTION_COUNT_PROPERTY, 147 String.valueOf(cpd.getMaximumConnectionCount())); 148 149 dpi[4] = buildDriverPropertyInfo(ProxoolConstants.MAXIMUM_CONNECTION_LIFETIME_PROPERTY, 150 String.valueOf(cpd.getMaximumConnectionLifetime())); 151 152 dpi[5] = buildDriverPropertyInfo(ProxoolConstants.MAXIMUM_NEW_CONNECTIONS_PROPERTY, 153 String.valueOf(cpd.getMaximumNewConnections())); 154 155 dpi[6] = buildDriverPropertyInfo(ProxoolConstants.PROTOTYPE_COUNT_PROPERTY, 156 String.valueOf(cpd.getPrototypeCount())); 157 158 dpi[7] = buildDriverPropertyInfo(ProxoolConstants.HOUSE_KEEPING_SLEEP_TIME_PROPERTY, 159 String.valueOf(cpd.getHouseKeepingSleepTime())); 160 161 dpi[8] = buildDriverPropertyInfo(ProxoolConstants.HOUSE_KEEPING_TEST_SQL_PROPERTY, 162 cpd.getHouseKeepingTestSql()); 163 164 dpi[9] = buildDriverPropertyInfo(ProxoolConstants.RECENTLY_STARTED_THRESHOLD_PROPERTY, 165 String.valueOf(cpd.getRecentlyStartedThreshold())); 166 167 dpi[10] = buildDriverPropertyInfo(ProxoolConstants.OVERLOAD_WITHOUT_REFUSAL_LIFETIME_PROPERTY, 168 String.valueOf(cpd.getOverloadWithoutRefusalLifetime())); 169 170 dpi[11] = buildDriverPropertyInfo(ProxoolConstants.MAXIMUM_ACTIVE_TIME_PROPERTY, 171 String.valueOf(cpd.getMaximumActiveTime())); 172 173 dpi[12] = buildDriverPropertyInfo(ProxoolConstants.VERBOSE_PROPERTY, 174 String.valueOf(cpd.isVerbose())); 175 176 dpi[13] = buildDriverPropertyInfo(ProxoolConstants.TRACE_PROPERTY, 177 String.valueOf(cpd.isTrace())); 178 179 dpi[14] = buildDriverPropertyInfo(ProxoolConstants.FATAL_SQL_EXCEPTION_PROPERTY, 180 String.valueOf(cpd.getFatalSqlExceptions())); 181 182 dpi[15] = buildDriverPropertyInfo(ProxoolConstants.FATAL_SQL_EXCEPTION_PROPERTY, 183 String.valueOf(cpd.getFatalSqlExceptions())); 184 185 dpi[16] = buildDriverPropertyInfo(ProxoolConstants.STATISTICS_PROPERTY, 186 String.valueOf(cpd.getStatistics())); 187 188 dpi[17] = buildDriverPropertyInfo(ProxoolConstants.STATISTICS_LOG_LEVEL_PROPERTY, 189 String.valueOf(cpd.getStatisticsLogLevel())); 190 191 return dpi; 192 } 193 194 private DriverPropertyInfo buildDriverPropertyInfo(String propertyName, String value) { 195 DriverPropertyInfo dpi = new DriverPropertyInfo (propertyName, 196 ATTRIBUTE_DESCRIPTIONS_RESOURCE.getString (propertyName)); 197 if (value != null) { 198 dpi.value = value; 199 } 200 return dpi; 201 } 202 203 206 public int getMajorVersion() { 207 return 1; 208 } 209 210 213 public int getMinorVersion() { 214 return 0; 215 } 216 217 220 public boolean jdbcCompliant() { 221 return true; 222 } 223 224 } 225 226 346 | Popular Tags |