1 17 18 package org.sape.carbon.services.sql.connection; 19 20 import java.sql.Connection ; 21 import java.sql.Driver ; 22 import java.sql.SQLException ; 23 import java.util.Map ; 24 import java.util.Properties ; 25 26 import org.sape.carbon.core.component.ComponentConfiguration; 27 import org.sape.carbon.core.component.lifecycle.Configurable; 28 import org.sape.carbon.core.component.lifecycle.StateTransitionException; 29 30 import org.apache.commons.logging.Log; 31 import org.apache.commons.logging.LogFactory; 32 33 46 public class StandaloneConnectionFactory 47 implements ConnectionFactory, Configurable { 48 49 50 54 protected static final String USER_KEY = "user"; 55 56 60 protected static final String PASSWORD_KEY = "password"; 61 62 66 protected Driver driver = null; 67 68 71 protected String dbUrl = null; 72 73 78 protected Properties dbConnectionProperties; 79 80 83 private Log log = LogFactory.getLog(this.getClass()); 84 85 86 94 public Connection getConnection() throws SQLException { 95 Connection con = driver.connect(dbUrl, dbConnectionProperties); 96 return con; 97 } 98 99 105 public void configure(ComponentConfiguration configuration) { 106 107 try { 108 StandaloneConnectionFactoryConfiguration config = 109 (StandaloneConnectionFactoryConfiguration) configuration; 110 Class driverClass = config.getDriverClass(); 111 String dbUserId = config.getDbUserId(); 112 String dbPassword = config.getDbPassword(); 113 this.dbUrl = config.getDbUrl(); 114 this.driver = (Driver ) driverClass.newInstance(); 115 116 if (null == driver) { 117 throw new StateTransitionException(this.getClass(), 118 "Configuration Failed: Could not instantiate a Driver"); 119 } 120 Properties props = new Properties (); 121 122 if (null != dbUserId) { 125 props.put(StandaloneConnectionFactory.USER_KEY, 126 dbUserId); 127 if (null != dbPassword) { 128 props.put(StandaloneConnectionFactory.PASSWORD_KEY, 129 dbPassword); 130 } 131 } else { 132 if (log.isInfoEnabled()) { 133 log.info("No username provided. Logins will be anonymous. " 134 + "([user] and [password] keys/entries omitted from " 135 + "connection properties object"); 136 } 137 } 138 139 Map connectionProperties = config.getConnectionProperties(); 141 if ( connectionProperties == null || connectionProperties.size() == 0 ) { 142 if( log.isInfoEnabled() ) { 143 log.info("No additional properties for the connection have been specified"); 144 } 145 } else { 146 props.putAll(connectionProperties); 148 if( log.isInfoEnabled() ) { 149 log.info(connectionProperties.size() + " additional properties " + 150 connectionProperties+" for the connection has been set"); 151 } 152 } 153 154 this.dbConnectionProperties = props; 155 } catch (InstantiationException ie) { 156 throw new StateTransitionException(this.getClass(), 157 "Configuration Failed: Could not instantiate a Driver", ie); 158 } catch (IllegalAccessException ae) { 159 throw new StateTransitionException(this.getClass(), 160 "Configuration Failed: Could not instantiate a Driver", ae); 161 } 162 } 163 } | Popular Tags |