1 25 26 package org.objectweb.easybeans.component.jdbcpool; 27 28 import javax.naming.InitialContext ; 29 import javax.naming.NamingException ; 30 31 import org.objectweb.easybeans.component.api.EZBComponent; 32 import org.objectweb.easybeans.component.api.EZBComponentException; 33 import org.objectweb.easybeans.log.JLog; 34 import org.objectweb.easybeans.log.JLogFactory; 35 import org.objectweb.easybeans.transaction.JTransactionManager; 36 37 41 public class JDBCPoolComponent implements EZBComponent { 42 43 46 private static JLog logger = JLogFactory.getLog(JDBCPoolComponent.class); 47 48 51 private static final String DEFAULT_USER = ""; 52 53 56 private static final String DEFAULT_PASSWORD = ""; 57 58 61 private static final int DEFAULT_MIN_POOL = 10; 62 63 66 private static final int DEFAULT_MAX_POOL = 30; 67 68 71 private static final int DEFAULT_PSTMT = 10; 72 73 76 private ConnectionManager connectionManager = null; 77 78 81 private String jndiName = null; 82 83 86 private String username = DEFAULT_USER; 87 88 91 private String password = DEFAULT_PASSWORD; 92 93 96 private String url = null; 97 98 101 private String driver = null; 102 103 106 private boolean useTM = true; 107 108 111 private int poolMin = DEFAULT_MIN_POOL; 112 113 116 private int poolMax = DEFAULT_MAX_POOL; 117 118 121 private int pstmtMax = DEFAULT_PSTMT; 122 123 126 public JDBCPoolComponent() { 127 connectionManager = new ConnectionManager(); 128 connectionManager.setTransactionIsolation("default"); 129 } 130 131 135 public void init() throws EZBComponentException { 136 validate(); 138 139 connectionManager.setDatasourceName(jndiName); 140 connectionManager.setDSName(jndiName); 141 connectionManager.setUrl(url); 142 try { 143 connectionManager.setClassName(driver); 144 } catch (ClassNotFoundException e) { 145 throw new IllegalStateException ("Cannot load jdbc driver '" + driver + "'.", e); 146 } 147 connectionManager.setUserName(username); 148 connectionManager.setPassword(password); 149 connectionManager.setTransactionIsolation("default"); 150 connectionManager.setPstmtMax(pstmtMax); 151 152 } 153 154 158 private void validate() throws EZBComponentException { 159 if (jndiName == null) { 161 throw new EZBComponentException("No JNDI name set"); 162 } 163 164 if (url == null) { 166 throw new EZBComponentException("No URL set"); 167 } 168 169 if (driver == null) { 171 throw new EZBComponentException("No driver set"); 172 } 173 } 174 175 179 public void start() throws EZBComponentException { 180 if (useTM) { 182 connectionManager.setTm(JTransactionManager.getTransactionManager()); 183 } 184 connectionManager.setPoolMin(poolMin); 185 connectionManager.setPoolMax(poolMax); 186 187 try { 189 Object o = new InitialContext ().lookup(jndiName); 190 if (o != null) { 191 logger.warn("Entry with JNDI name {0} already exist", jndiName); 192 } 193 } catch (NamingException e) { 194 logger.debug("Nothing with JNDI name {0}", jndiName); 195 } 196 197 try { 199 new InitialContext ().rebind(jndiName, connectionManager); 200 } catch (NamingException e) { 201 throw new EZBComponentException("Cannot bind a JDBC Datasource with the jndi name '" + jndiName + "'."); 202 } 203 204 logger.info("DS ''{0}'', URL ''{1}'', Driver = ''{2}''.", jndiName, url, driver); 205 } 206 207 212 public void stop() throws EZBComponentException { 213 try { 215 new InitialContext ().unbind(jndiName); 216 } catch (NamingException e) { 217 throw new EZBComponentException("Cannot unbind a JDBC Datasource with the jndi name '" + jndiName + "'."); 218 } 219 } 220 221 225 public void setDriver(final String driver) { 226 this.driver = driver; 227 } 228 229 233 public void setJndiName(final String jndiName) { 234 this.jndiName = jndiName; 235 } 236 237 241 public void setPassword(final String password) { 242 this.password = password; 243 } 244 245 249 public void setPoolMax(final int poolMax) { 250 this.poolMax = poolMax; 251 } 252 253 257 public void setPoolMin(final int poolMin) { 258 this.poolMin = poolMin; 259 } 260 261 265 public void setPstmtMax(final int pstmtMax) { 266 this.pstmtMax = pstmtMax; 267 } 268 269 273 public void setUrl(final String url) { 274 this.url = url; 275 } 276 277 281 public void setUsername(final String username) { 282 this.username = username; 283 } 284 285 289 public void setUseTM(final boolean useTM) { 290 this.useTM = useTM; 291 } 292 } 293 | Popular Tags |