[jboss-svn-commits] JBL Code SVN: r5272 - in labs/jbossesb/branches/refactor/product/core/common/src/org/jboss/soa/esb/common: . connection connection/exception

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Tue Jul 25 09:12:56 EDT 2006


Author: mohit309
Date: 2006-07-25 09:12:53 -0400 (Tue, 25 Jul 2006)
New Revision: 5272

Added:
   labs/jbossesb/branches/refactor/product/core/common/src/org/jboss/soa/esb/common/connection/
   labs/jbossesb/branches/refactor/product/core/common/src/org/jboss/soa/esb/common/connection/ConnectionFactory.java
   labs/jbossesb/branches/refactor/product/core/common/src/org/jboss/soa/esb/common/connection/ConnectionPool.java
   labs/jbossesb/branches/refactor/product/core/common/src/org/jboss/soa/esb/common/connection/ConnectionPoolFactory.java
   labs/jbossesb/branches/refactor/product/core/common/src/org/jboss/soa/esb/common/connection/ConnectionProperties.java
   labs/jbossesb/branches/refactor/product/core/common/src/org/jboss/soa/esb/common/connection/ConnectionWrapper.java
   labs/jbossesb/branches/refactor/product/core/common/src/org/jboss/soa/esb/common/connection/DefaultConnectionPoolImpl.java
   labs/jbossesb/branches/refactor/product/core/common/src/org/jboss/soa/esb/common/connection/DriverConnectionFactory.java
   labs/jbossesb/branches/refactor/product/core/common/src/org/jboss/soa/esb/common/connection/FreeConnectionPoolResources.java
   labs/jbossesb/branches/refactor/product/core/common/src/org/jboss/soa/esb/common/connection/PasswordDecoder.java
   labs/jbossesb/branches/refactor/product/core/common/src/org/jboss/soa/esb/common/connection/PoolDataSource.java
   labs/jbossesb/branches/refactor/product/core/common/src/org/jboss/soa/esb/common/connection/exception/
   labs/jbossesb/branches/refactor/product/core/common/src/org/jboss/soa/esb/common/connection/exception/ESBConnectionException.java
Log:
Initial Version - Connection Pool

Added: labs/jbossesb/branches/refactor/product/core/common/src/org/jboss/soa/esb/common/connection/ConnectionFactory.java
===================================================================
--- labs/jbossesb/branches/refactor/product/core/common/src/org/jboss/soa/esb/common/connection/ConnectionFactory.java	2006-07-25 09:56:42 UTC (rev 5271)
+++ labs/jbossesb/branches/refactor/product/core/common/src/org/jboss/soa/esb/common/connection/ConnectionFactory.java	2006-07-25 13:12:53 UTC (rev 5272)
@@ -0,0 +1,34 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2006, JBoss Inc., and individual contributors as indicated
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/
+package org.jboss.soa.esb.common.connection;
+
+import java.sql.Connection;
+import java.sql.SQLException;
+
+/**
+ * This is the factory interface for creating connections based on different sources such as DataSource, DriverConnection
+ * User: MohitK
+ * Date: Jul 18, 2006
+ */
+public interface ConnectionFactory {
+    public Connection createConnection() throws SQLException;
+}

Added: labs/jbossesb/branches/refactor/product/core/common/src/org/jboss/soa/esb/common/connection/ConnectionPool.java
===================================================================
--- labs/jbossesb/branches/refactor/product/core/common/src/org/jboss/soa/esb/common/connection/ConnectionPool.java	2006-07-25 09:56:42 UTC (rev 5271)
+++ labs/jbossesb/branches/refactor/product/core/common/src/org/jboss/soa/esb/common/connection/ConnectionPool.java	2006-07-25 13:12:53 UTC (rev 5272)
@@ -0,0 +1,45 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2006, JBoss Inc., and individual contributors as indicated
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/
+package org.jboss.soa.esb.common.connection;
+
+import org.jboss.soa.esb.common.connection.exception.ESBConnectionException;
+
+import java.sql.Connection;
+import java.sql.SQLException;
+
+/**
+ * TODO
+ * User: MohitK
+ * Date: Jul 18, 2006
+ */
+public interface ConnectionPool {
+
+    public void createPool(ConnectionProperties prop) throws ESBConnectionException;
+
+    public Connection getConnection() throws SQLException;
+
+    public void closeConnection(Connection connectionToClose) throws SQLException;
+
+    public void releasePool() throws SQLException;
+
+    public void setConnectionFactory(ConnectionFactory connectionFactory);
+}

Added: labs/jbossesb/branches/refactor/product/core/common/src/org/jboss/soa/esb/common/connection/ConnectionPoolFactory.java
===================================================================
--- labs/jbossesb/branches/refactor/product/core/common/src/org/jboss/soa/esb/common/connection/ConnectionPoolFactory.java	2006-07-25 09:56:42 UTC (rev 5271)
+++ labs/jbossesb/branches/refactor/product/core/common/src/org/jboss/soa/esb/common/connection/ConnectionPoolFactory.java	2006-07-25 13:12:53 UTC (rev 5272)
@@ -0,0 +1,50 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2006, JBoss Inc., and individual contributors as indicated
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/
+package org.jboss.soa.esb.common.connection;
+
+import org.jboss.soa.esb.common.connection.exception.ESBConnectionException;
+
+import java.util.Properties;
+import java.sql.SQLException;
+
+/**
+ * TODO
+ * User: MohitK
+ * Date: Jul 19, 2006
+ */
+public class ConnectionPoolFactory {
+
+    public static ConnectionPool createPool(ConnectionProperties connProperties) throws ESBConnectionException {
+        String connectionPoolClassName = connProperties.getConnectionPoolClass();
+        try {
+            ConnectionPool pool = (ConnectionPool)Class.forName(connectionPoolClassName).newInstance();
+            pool.createPool(connProperties);
+            FreeConnectionPoolResources freeResourcesThread = new FreeConnectionPoolResources(pool);
+            Runtime.getRuntime().addShutdownHook(freeResourcesThread);
+            return pool;
+        } catch (Throwable t) {
+            System.err.println("ConnectionPoolFactory: Error in Connection Pool");
+            System.err.println(t);
+            throw new ESBConnectionException(t);
+        }
+    }
+}

Added: labs/jbossesb/branches/refactor/product/core/common/src/org/jboss/soa/esb/common/connection/ConnectionProperties.java
===================================================================
--- labs/jbossesb/branches/refactor/product/core/common/src/org/jboss/soa/esb/common/connection/ConnectionProperties.java	2006-07-25 09:56:42 UTC (rev 5271)
+++ labs/jbossesb/branches/refactor/product/core/common/src/org/jboss/soa/esb/common/connection/ConnectionProperties.java	2006-07-25 13:12:53 UTC (rev 5272)
@@ -0,0 +1,217 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2006, JBoss Inc., and individual contributors as indicated
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/
+package org.jboss.soa.esb.common.connection;
+
+import java.util.Properties;
+
+/**
+ * TODO
+ * User: MohitK
+ * Date: Jul 18, 2006
+ */
+public class ConnectionProperties {
+
+    private Properties _connectionProperties;
+
+    public static final String DRIVER_CLASSNAME = "driver-class";
+    public static final String CONNECTION_URL = "connection-url";
+    public static final String USERNAME = "user-name";
+    public static final String PASSWORD = "password";
+    public static final String AUTO_COMMIT = "auto-commit";
+    public static final String ISOLATION_LEVEL = "isolation";
+    public static final String MIN_POOL_SIZE = "min-pool-size";
+    public static final String MAX_POOL_SIZE = "max-pool-size";
+    public static final String BLOCKING_CONNECTION_TIME = "blocking-timeout-millis";
+    public static final String CONNECTION_POOL_CLASS = "connection-pool-class";
+    public static final String PASSWORD_DECODER = "password-decrypter";
+
+    private boolean _autoCommit;
+    private Integer _isolationLvl;
+    private int _minPoolSize;
+    private int _maxPoolSize;
+    private long _connBlockTime;
+    private String _connectionPoolClass;
+    private String _driver;
+    private String _user;
+    private String _password;
+    private String _connectionFactoryClass;
+    private String _connectionUrl;
+
+    public static final String DEFAULT_MIN_POOL_SIZE = "5";
+    public static final String DEFAULT_MAX_POOL_SIZE = "10";
+    public static final String DEFAULT_CONN_BLOCK_TIME_MILLIS = "1000";
+    public static final String DEFAULT_CONNECTION_POOL_CLASS = DefaultConnectionPoolImpl.class.getName();
+    public static final String DEFAULT_CONNECTION_FACTORY = DriverConnectionFactory.class.getName();
+
+
+    public ConnectionProperties(Properties connectionProperties) {
+        this._connectionProperties = connectionProperties;
+        setDriverClassName();
+        setConnectionUrl();
+        setUser();
+        setPassword();
+        setConnectionPoolClass();
+        setAutoCommit();
+        setIsolationLevel();
+        setMinPoolSize();
+        setMaxPoolSize();
+        setBlockingConnectionTime();
+    }
+
+    private void setDriverClassName() {
+        _driver = _connectionProperties.getProperty(DRIVER_CLASSNAME);
+        if (_driver == null) {
+            _driver = System.getProperty(DRIVER_CLASSNAME);
+        }
+    }
+
+    protected String getDriverClassName() {
+        return _connectionProperties.getProperty(DRIVER_CLASSNAME);
+    }
+
+    protected String getConnectionUrl() {
+        return _connectionUrl;
+    }
+
+    private void setConnectionUrl() {
+        _connectionUrl = _connectionProperties.getProperty(CONNECTION_URL);
+        if (_connectionUrl == null) {
+            _connectionUrl = System.getProperty(CONNECTION_URL);
+        }
+    }
+
+    protected boolean getAutoCommit() {
+        return _autoCommit;
+    }
+
+    protected Integer getIsolationLevel() {
+        return _isolationLvl;
+    }
+
+    private void setIsolationLevel() {
+        String isolation = (String) _connectionProperties.get(ISOLATION_LEVEL);
+        if (isolation == null) {
+            isolation = System.getProperty(ISOLATION_LEVEL);
+        }
+        this._isolationLvl = (isolation != null) ? new Integer(isolation) : null;
+    }
+
+    protected int getMinPoolSize() {
+        return _minPoolSize;
+    }
+
+    protected int getMaxPoolSize() {
+        return _maxPoolSize;
+    }
+
+    private void setMinPoolSize() {
+        String strPoolSize = _connectionProperties.getProperty(MIN_POOL_SIZE);
+        if (strPoolSize == null) {
+            strPoolSize = System.getProperty(MIN_POOL_SIZE, DEFAULT_MIN_POOL_SIZE);
+            this._minPoolSize = Integer.parseInt(strPoolSize);
+        }
+    }
+
+    private void setMaxPoolSize() {
+        String strPoolSize = (String) _connectionProperties.get(MAX_POOL_SIZE);
+        if (strPoolSize == null) {
+            strPoolSize = System.getProperty(MAX_POOL_SIZE, DEFAULT_MAX_POOL_SIZE);
+            this._maxPoolSize = Integer.parseInt(strPoolSize);
+        }
+    }
+
+    private void setBlockingConnectionTime() {
+        String strBlockTimeMillis = (String) _connectionProperties.get(BLOCKING_CONNECTION_TIME);
+        if (strBlockTimeMillis == null) {
+            strBlockTimeMillis = System.getProperty(BLOCKING_CONNECTION_TIME, DEFAULT_CONN_BLOCK_TIME_MILLIS);
+            this._connBlockTime = Integer.parseInt(strBlockTimeMillis);
+        }
+    }
+
+    protected long getConnectionBlockTimeInMillis() {
+        return _connBlockTime;
+    }
+
+    private void setAutoCommit() {
+        String autoCommit = (String) _connectionProperties.get(AUTO_COMMIT);
+        if (autoCommit == null || autoCommit.equals("false")) {
+            _autoCommit = Boolean.FALSE;
+        } else {
+            _autoCommit = Boolean.TRUE;
+        }
+    }
+
+    private void setConnectionPoolClass() {
+        _connectionPoolClass = _connectionProperties.getProperty(CONNECTION_POOL_CLASS);
+        if (_connectionPoolClass == null) {
+            _connectionPoolClass = System.getProperty(CONNECTION_POOL_CLASS, DEFAULT_CONNECTION_POOL_CLASS);
+        }
+    }
+
+
+    public String getUser() {
+        return _user;
+    }
+
+    protected void setUser() {
+        this._user = _connectionProperties.getProperty(USERNAME);
+        if (_user == null) {
+            _user = System.getProperty(USERNAME);
+        }
+    }
+
+    public String getPassword() {
+        return _password;
+    }
+
+    protected void setPassword() {
+        this._password = _connectionProperties.getProperty(PASSWORD);
+        if (_password == null) {
+            _password = System.getProperty(PASSWORD);
+        }
+        String strPasswordDecoder = _connectionProperties.getProperty(PASSWORD_DECODER);
+        if (strPasswordDecoder == null) {
+            strPasswordDecoder = System.getProperty(PASSWORD_DECODER);
+        }
+        if (strPasswordDecoder != null) {
+            try {
+                PasswordDecoder decoder = (PasswordDecoder) Class.forName(strPasswordDecoder).newInstance();
+                this._password = decoder.decode(_password);
+            } catch (InstantiationException e) {
+                System.err.println("Error in creating instance of Password Decoder Class:-" + strPasswordDecoder);
+                System.err.println(e);
+            } catch (IllegalAccessException e) {
+                System.err.println("Error in creating instance of Password Decoder Class:-" + strPasswordDecoder);
+                System.err.println(e);
+            } catch (ClassNotFoundException e) {
+                System.err.println("Password Decoder Class:-" + strPasswordDecoder + " not found in Classpath");
+                System.err.println("Please check the property:-" + PASSWORD_DECODER);
+                System.err.println(e);
+            }
+        }
+    }
+
+    protected String getConnectionPoolClass() {
+        return _connectionPoolClass;
+    }
+
+}

Added: labs/jbossesb/branches/refactor/product/core/common/src/org/jboss/soa/esb/common/connection/ConnectionWrapper.java
===================================================================
--- labs/jbossesb/branches/refactor/product/core/common/src/org/jboss/soa/esb/common/connection/ConnectionWrapper.java	2006-07-25 09:56:42 UTC (rev 5271)
+++ labs/jbossesb/branches/refactor/product/core/common/src/org/jboss/soa/esb/common/connection/ConnectionWrapper.java	2006-07-25 13:12:53 UTC (rev 5272)
@@ -0,0 +1,198 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2006, JBoss Inc., and individual contributors as indicated
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/
+package org.jboss.soa.esb.common.connection;
+
+import java.sql.*;
+import java.util.Map;
+
+/**
+ * TODO
+ * User: MohitK
+ * Date: Jul 18, 2006
+ */
+public class ConnectionWrapper implements Connection {
+
+    private Connection _originalHeavyConnection;
+    private ConnectionPool _connectionPool;
+    private boolean isClosed=false;
+
+    public ConnectionWrapper(Connection originalHeavyConnection,ConnectionPool pool) throws SQLException{
+        if(originalHeavyConnection==null){
+            throw new SQLException("Original Connection is Null");
+        }
+        this._originalHeavyConnection=originalHeavyConnection;
+        if(pool == null){
+            throw new SQLException("Connection Pool is null");
+        }
+        this._connectionPool=pool;
+    }
+
+    public Statement createStatement() throws SQLException {
+        return _originalHeavyConnection.createStatement();
+    }
+
+    public PreparedStatement prepareStatement(String sql) throws SQLException {
+        return _originalHeavyConnection.prepareStatement(sql);
+    }
+
+    public CallableStatement prepareCall(String sql) throws SQLException {
+        return _originalHeavyConnection.prepareCall(sql);
+    }
+
+    public String nativeSQL(String sql) throws SQLException {
+        return _originalHeavyConnection.nativeSQL(sql);
+    }
+
+    public void setAutoCommit(boolean autoCommit) throws SQLException {
+        _originalHeavyConnection.setAutoCommit(autoCommit);
+    }
+
+    public boolean getAutoCommit() throws SQLException {
+        return _originalHeavyConnection.getAutoCommit();
+    }
+
+    public void commit() throws SQLException {
+        _originalHeavyConnection.commit();
+    }
+
+    public void rollback() throws SQLException {
+        _originalHeavyConnection.rollback();
+    }
+
+    public synchronized void close() throws SQLException {
+        _connectionPool.closeConnection(this);
+        this.isClosed = true;
+    }
+
+    public boolean isClosed() throws SQLException {
+        return isClosed;
+    }
+
+    public DatabaseMetaData getMetaData() throws SQLException {
+        return _originalHeavyConnection.getMetaData();
+    }
+
+    public void setReadOnly(boolean readOnly) throws SQLException {
+        _originalHeavyConnection.setReadOnly(readOnly);
+    }
+
+    public boolean isReadOnly() throws SQLException {
+        return _originalHeavyConnection.isReadOnly();
+    }
+
+    public void setCatalog(String catalog) throws SQLException {
+        _originalHeavyConnection.setCatalog(catalog);
+    }
+
+    public String getCatalog() throws SQLException {
+        return _originalHeavyConnection.getCatalog();
+    }
+
+    public void setTransactionIsolation(int level) throws SQLException {
+        _originalHeavyConnection.setTransactionIsolation(level);
+    }
+
+    public int getTransactionIsolation() throws SQLException {
+        return _originalHeavyConnection.getTransactionIsolation();
+    }
+
+    public SQLWarning getWarnings() throws SQLException {
+        return _originalHeavyConnection.getWarnings();
+    }
+
+    public void clearWarnings() throws SQLException {
+        _originalHeavyConnection.clearWarnings();
+    }
+
+    public Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException {
+        return _originalHeavyConnection.createStatement(resultSetType,resultSetConcurrency);
+    }
+
+    public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency) throws SQLException {
+        return _originalHeavyConnection.prepareStatement(sql,resultSetType,resultSetConcurrency);
+    }
+
+    public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency) throws SQLException {
+        return _originalHeavyConnection.prepareCall(sql,resultSetType,resultSetConcurrency);
+    }
+
+    public Map<String, Class<?>> getTypeMap() throws SQLException {
+        return _originalHeavyConnection.getTypeMap();
+    }
+
+    public void setTypeMap(Map<String, Class<?>> map) throws SQLException {
+        _originalHeavyConnection.setTypeMap(map);
+    }
+
+    public void setHoldability(int holdability) throws SQLException {
+        _originalHeavyConnection.setHoldability(holdability);
+    }
+
+    public int getHoldability() throws SQLException {
+        return _originalHeavyConnection.getHoldability();
+    }
+
+    public Savepoint setSavepoint() throws SQLException {
+        return _originalHeavyConnection.setSavepoint();
+    }
+
+    public Savepoint setSavepoint(String name) throws SQLException {
+        return _originalHeavyConnection.setSavepoint(name);
+    }
+
+    public void rollback(Savepoint savepoint) throws SQLException {
+        _originalHeavyConnection.rollback(savepoint);
+    }
+
+    public void releaseSavepoint(Savepoint savepoint) throws SQLException {
+        _originalHeavyConnection.releaseSavepoint(savepoint);
+    }
+
+    public Statement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException {
+        return _originalHeavyConnection.createStatement(resultSetType,resultSetConcurrency,resultSetHoldability);
+    }
+
+    public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException {
+        return _originalHeavyConnection.prepareStatement(sql,resultSetType,resultSetConcurrency,resultSetHoldability);
+    }
+
+    public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException {
+        return _originalHeavyConnection.prepareCall(sql,resultSetType,resultSetConcurrency,resultSetHoldability);
+    }
+
+    public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws SQLException {
+        return _originalHeavyConnection.prepareStatement(sql,autoGeneratedKeys);
+    }
+
+    public PreparedStatement prepareStatement(String sql, int columnIndexes[]) throws SQLException {
+        return _originalHeavyConnection.prepareStatement(sql,columnIndexes);
+    }
+
+    public PreparedStatement prepareStatement(String sql, String columnNames[]) throws SQLException {
+        return _originalHeavyConnection.prepareStatement(sql,columnNames);
+    }
+
+    protected Connection getActualConnection(){
+        return _originalHeavyConnection;
+    }
+
+}

Added: labs/jbossesb/branches/refactor/product/core/common/src/org/jboss/soa/esb/common/connection/DefaultConnectionPoolImpl.java
===================================================================
--- labs/jbossesb/branches/refactor/product/core/common/src/org/jboss/soa/esb/common/connection/DefaultConnectionPoolImpl.java	2006-07-25 09:56:42 UTC (rev 5271)
+++ labs/jbossesb/branches/refactor/product/core/common/src/org/jboss/soa/esb/common/connection/DefaultConnectionPoolImpl.java	2006-07-25 13:12:53 UTC (rev 5272)
@@ -0,0 +1,159 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2006, JBoss Inc., and individual contributors as indicated
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/
+package org.jboss.soa.esb.common.connection;
+
+import org.jboss.soa.esb.common.connection.exception.ESBConnectionException;
+
+import java.sql.Connection;
+import java.sql.Driver;
+import java.sql.DriverManager;
+import java.sql.SQLException;
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Properties;
+import java.util.Stack;
+
+/**
+ * TODO
+ * User: MohitK
+ * Date: Jul 18, 2006
+ */
+public class DefaultConnectionPoolImpl implements ConnectionPool {
+
+    private final Stack<ConnectionWrapper> m_oConnectionStack = new Stack<ConnectionWrapper>();
+    private final List<ConnectionWrapper> m_oUsedConnectionList = new ArrayList<ConnectionWrapper>();
+    private int m_iConnCountInPool;
+    private ConnectionProperties m_oConnProperties;
+    private ConnectionFactory _connectionFactory;
+
+    public void createPool(ConnectionProperties prop) throws ESBConnectionException {
+        this.m_oConnProperties = prop;
+        try {
+            initPool();
+        } catch (SQLException e) {
+            throw new ESBConnectionException("DefaultConnectionPoolImpl: Error in creating Connection Pool", e);
+        }
+    }
+
+    private void initPool() throws SQLException {
+
+        if (_connectionFactory == null) {
+            Properties properties = new Properties();
+            properties.put("user", m_oConnProperties.getUser());
+            properties.put("password", m_oConnProperties.getPassword());
+            _connectionFactory = new DriverConnectionFactory(getDriver(m_oConnProperties.getDriverClassName()), m_oConnProperties.getConnectionUrl(), properties);
+        }
+        while (m_iConnCountInPool < m_oConnProperties.getMinPoolSize()) {
+            m_oConnectionStack.add(new ConnectionWrapper(_connectionFactory.createConnection(), this));
+            m_iConnCountInPool++;
+        }
+    }
+
+    private Driver getDriver(String driverClassName) throws SQLException {
+        if (driverClassName != null) {
+            try {
+                Class.forName(driverClassName);
+            } catch (Throwable e) {
+                System.err.println("DefaultConnectionPoolImpl: Error in loading class " + driverClassName + " to create connection pool");
+                System.err.println(e);
+                throw new SQLException("DefaultConnectionPoolImpl: Error in loading class " + driverClassName + " to create connection pool");
+            }
+            return DriverManager.getDriver(m_oConnProperties.getConnectionUrl());
+        }
+        throw new SQLException("DefaultConnectionPoolImpl: Driver Class could not be loaded");
+    }
+
+    public synchronized Connection getConnection() throws SQLException {
+        ConnectionWrapper newPoolConnection;
+        if (!m_oConnectionStack.isEmpty()) {
+            newPoolConnection = m_oConnectionStack.pop();
+            m_oUsedConnectionList.add(newPoolConnection);
+        } else if (m_iConnCountInPool < m_oConnProperties.getMaxPoolSize()) {
+            newPoolConnection = new ConnectionWrapper(_connectionFactory.createConnection(), this);
+            m_oUsedConnectionList.add(newPoolConnection);
+            m_iConnCountInPool++;
+        } else {
+            try {
+                Thread.sleep(m_oConnProperties.getConnectionBlockTimeInMillis());
+            } catch (InterruptedException e) {
+                //Do Nothing
+            }
+            if (!m_oConnectionStack.empty()) {
+                newPoolConnection = m_oConnectionStack.pop();
+                m_oUsedConnectionList.add(newPoolConnection);
+            } else {
+                throw new SQLException("Connection Pool exhausted. Please increase number of connections");
+            }
+
+        }
+        Integer isolation = m_oConnProperties.getIsolationLevel();
+        if (isolation != null) newPoolConnection.setTransactionIsolation(isolation);
+        newPoolConnection.setAutoCommit(m_oConnProperties.getAutoCommit());
+        return newPoolConnection;
+    }
+
+    public void closeConnection(Connection connectionToClose) {
+        synchronized (m_oConnectionStack) {
+            m_oConnectionStack.push((ConnectionWrapper) connectionToClose);
+        }
+        synchronized (m_oUsedConnectionList) {
+            m_oUsedConnectionList.remove(connectionToClose);
+        }
+    }
+
+    public void releasePool() {
+        try {
+            synchronized (m_oConnectionStack) {
+                for (ConnectionWrapper connection : m_oConnectionStack) {
+                    Connection origConn = connection.getActualConnection();
+                    origConn.close();
+                    m_iConnCountInPool--;
+                }
+            }
+            synchronized (m_oUsedConnectionList) {
+                for (int i = 0; i < m_oUsedConnectionList.size(); i++) {
+                    ConnectionWrapper connectionWrapper = m_oUsedConnectionList.get(i);
+                    Connection con = connectionWrapper.getActualConnection();
+                    if (con != null && !con.isClosed()) {
+                        con.close();
+                    }
+                    m_iConnCountInPool--;
+                    m_oUsedConnectionList.remove(i);
+
+                }
+            }
+        } catch (SQLException e) {
+            System.err.println("DefaultConnectionPoolImpl: Error in closing physical Connection");
+            System.err.println(e);
+        }
+    }
+
+    protected void finalize() throws Throwable {
+        super.finalize();
+        releasePool();
+    }
+
+    public void setConnectionFactory(ConnectionFactory _connectionFactory) {
+        this._connectionFactory = _connectionFactory;
+    }
+
+}

Added: labs/jbossesb/branches/refactor/product/core/common/src/org/jboss/soa/esb/common/connection/DriverConnectionFactory.java
===================================================================
--- labs/jbossesb/branches/refactor/product/core/common/src/org/jboss/soa/esb/common/connection/DriverConnectionFactory.java	2006-07-25 09:56:42 UTC (rev 5271)
+++ labs/jbossesb/branches/refactor/product/core/common/src/org/jboss/soa/esb/common/connection/DriverConnectionFactory.java	2006-07-25 13:12:53 UTC (rev 5272)
@@ -0,0 +1,51 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2006, JBoss Inc., and individual contributors as indicated
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/
+package org.jboss.soa.esb.common.connection;
+
+import java.sql.Connection;
+import java.sql.SQLException;
+import java.sql.Driver;
+import java.util.Properties;
+
+/**
+ * TODO
+ * User: MohitK
+ * Date: Jul 18, 2006
+ */
+public class DriverConnectionFactory implements ConnectionFactory{
+
+    private Driver _driver;
+    private String _driverUrl;
+    private Properties _properties;
+
+    public DriverConnectionFactory(Driver driver,String driverUrl,Properties connectionProps){
+        this._driver=driver;
+        this._driverUrl=driverUrl;
+        this._properties=connectionProps;
+
+    }
+
+    public Connection createConnection() throws SQLException {
+        return _driver.connect(_driverUrl,_properties);
+    }
+
+}

Added: labs/jbossesb/branches/refactor/product/core/common/src/org/jboss/soa/esb/common/connection/FreeConnectionPoolResources.java
===================================================================
--- labs/jbossesb/branches/refactor/product/core/common/src/org/jboss/soa/esb/common/connection/FreeConnectionPoolResources.java	2006-07-25 09:56:42 UTC (rev 5271)
+++ labs/jbossesb/branches/refactor/product/core/common/src/org/jboss/soa/esb/common/connection/FreeConnectionPoolResources.java	2006-07-25 13:12:53 UTC (rev 5272)
@@ -0,0 +1,47 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2006, JBoss Inc., and individual contributors as indicated
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/
+package org.jboss.soa.esb.common.connection;
+
+import java.sql.SQLException;
+
+/**
+ * TODO
+ * User: MohitK
+ * Date: Jul 22, 2006
+ */
+public class FreeConnectionPoolResources extends Thread{
+
+    ConnectionPool pool;
+
+    public FreeConnectionPoolResources(ConnectionPool pool){
+        this.pool = pool;
+    }
+
+    public void run() {
+        try {
+            pool.releasePool();
+        } catch (Exception e) {
+            System.err.println("Error in freeing resources");
+        }
+    }
+
+}

Added: labs/jbossesb/branches/refactor/product/core/common/src/org/jboss/soa/esb/common/connection/PasswordDecoder.java
===================================================================
--- labs/jbossesb/branches/refactor/product/core/common/src/org/jboss/soa/esb/common/connection/PasswordDecoder.java	2006-07-25 09:56:42 UTC (rev 5271)
+++ labs/jbossesb/branches/refactor/product/core/common/src/org/jboss/soa/esb/common/connection/PasswordDecoder.java	2006-07-25 13:12:53 UTC (rev 5272)
@@ -0,0 +1,38 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2006, JBoss Inc., and individual contributors as indicated
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/
+package org.jboss.soa.esb.common.connection;
+
+/**
+ * TODO
+ * User: MohitK
+ * Date: Jul 21, 2006
+ */
+public interface PasswordDecoder {
+    /**
+     * This method decodes the password for JDBC Connection, it will be called when setting JDBC connection properties.
+     * Users who need to decode the password need to implement this interface and write their own
+     * decoding logic to send the actual DB password. If this method returns null
+     * @param encodedPassword The encoded password sent by JDBC framework
+     * @return Decoded password .
+     */
+    public String decode(String encodedPassword);
+}

Added: labs/jbossesb/branches/refactor/product/core/common/src/org/jboss/soa/esb/common/connection/PoolDataSource.java
===================================================================
--- labs/jbossesb/branches/refactor/product/core/common/src/org/jboss/soa/esb/common/connection/PoolDataSource.java	2006-07-25 09:56:42 UTC (rev 5271)
+++ labs/jbossesb/branches/refactor/product/core/common/src/org/jboss/soa/esb/common/connection/PoolDataSource.java	2006-07-25 13:12:53 UTC (rev 5272)
@@ -0,0 +1,98 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2006, JBoss Inc., and individual contributors as indicated
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/
+package org.jboss.soa.esb.common.connection;
+
+import org.jboss.soa.esb.common.connection.exception.ESBConnectionException;
+
+import javax.sql.DataSource;
+import java.io.PrintWriter;
+import java.sql.Connection;
+import java.sql.SQLException;
+import java.util.Properties;
+
+/**
+ * TODO
+ * User: MohitK
+ * Date: Jul 19, 2006
+ */
+public class PoolDataSource implements DataSource {
+
+    private ConnectionPool _pool;
+    private PrintWriter _logWriter = new PrintWriter(System.out);
+    private int _loginTimeOut;
+    private final Properties _connProps = new Properties();
+    ConnectionProperties _connectionProperties;
+
+    private synchronized ConnectionPool createConnectionPool() throws SQLException {
+        if (_pool == null) {
+            try {
+                if (_connProps != null) {
+                    _connectionProperties = new ConnectionProperties(_connProps);
+                    _pool = ConnectionPoolFactory.createPool(_connectionProperties);
+                }
+            } catch (ESBConnectionException e) {
+                _logWriter.println("PoolDataSource: Error in creating Connection Pool");
+                throw new SQLException("Error in Connection Pool Creation");
+            }
+        }
+        return _pool;
+    }
+
+
+    public Properties getConnectionProperties() {
+        return _connProps;
+    }
+
+    public void setConnectionProperties(Properties _connProperties) {
+        _connProps.putAll(_connProperties);
+    }
+
+
+    public Connection getConnection() throws SQLException {
+        return createConnectionPool().getConnection();
+    }
+
+    public Connection getConnection(String username, String password) throws SQLException {
+        synchronized(_connProps){
+            _connProps.setProperty(ConnectionProperties.USERNAME, username);
+            _connProps.setProperty(ConnectionProperties.PASSWORD, password);
+        }
+        return createConnectionPool().getConnection();
+    }
+
+    public PrintWriter getLogWriter() throws SQLException {
+        return null;
+    }
+
+    public void setLogWriter(PrintWriter out) throws SQLException {
+        this._logWriter = out;
+    }
+
+    public void setLoginTimeout(int seconds) throws SQLException {
+        this._loginTimeOut = seconds;
+    }
+
+    public int getLoginTimeout() throws SQLException {
+        return _loginTimeOut;
+    }
+
+}

Added: labs/jbossesb/branches/refactor/product/core/common/src/org/jboss/soa/esb/common/connection/exception/ESBConnectionException.java
===================================================================
--- labs/jbossesb/branches/refactor/product/core/common/src/org/jboss/soa/esb/common/connection/exception/ESBConnectionException.java	2006-07-25 09:56:42 UTC (rev 5271)
+++ labs/jbossesb/branches/refactor/product/core/common/src/org/jboss/soa/esb/common/connection/exception/ESBConnectionException.java	2006-07-25 13:12:53 UTC (rev 5272)
@@ -0,0 +1,45 @@
+/*
+* JBoss, Home of Professional Open Source
+* Copyright 2006, JBoss Inc., and individual contributors as indicated
+* by the @authors tag. See the copyright.txt in the distribution for a
+* full listing of individual contributors.
+*
+* This is free software; you can redistribute it and/or modify it
+* under the terms of the GNU Lesser General Public License as
+* published by the Free Software Foundation; either version 2.1 of
+* the License, or (at your option) any later version.
+*
+* This software is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+* Lesser General Public License for more details.
+*
+* You should have received a copy of the GNU Lesser General Public
+* License along with this software; if not, write to the Free
+* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+*/
+package org.jboss.soa.esb.common.connection.exception;
+
+/**
+ * This exception is thrown when Connection Pool fails to initialize
+ * User: MohitK
+ * Date: Jul 21, 2006
+ */
+public class ESBConnectionException extends Exception{
+    public ESBConnectionException() {
+        super();
+    }
+
+    public ESBConnectionException(String message) {
+        super(message);
+    }
+
+    public ESBConnectionException(String message, Throwable cause) {
+        super(message, cause);
+    }
+
+    public ESBConnectionException(Throwable cause) {
+        super(cause);
+    }
+}




More information about the jboss-svn-commits mailing list