[teiid-commits] teiid SVN: r1849 - in trunk/console/src/main/java/org/teiid/rhq: embedded/pool and 2 other directories.

teiid-commits at lists.jboss.org teiid-commits at lists.jboss.org
Fri Feb 19 11:29:34 EST 2010


Author: tejones
Date: 2010-02-19 11:29:33 -0500 (Fri, 19 Feb 2010)
New Revision: 1849

Removed:
   trunk/console/src/main/java/org/teiid/rhq/embedded/EmbeddedConnectionMgr.java
   trunk/console/src/main/java/org/teiid/rhq/embedded/pool/ConnectionPoolImpl.java
   trunk/console/src/main/java/org/teiid/rhq/enterprise/EnterpriseConnectionMgr.java
   trunk/console/src/main/java/org/teiid/rhq/enterprise/pool/ConnectionPoolImpl.java
Log:
TEIID-807: Cleaned up legacy code and added VDB deployment/un-deployment. Also added operation execution framework.

Deleted: trunk/console/src/main/java/org/teiid/rhq/embedded/EmbeddedConnectionMgr.java
===================================================================
--- trunk/console/src/main/java/org/teiid/rhq/embedded/EmbeddedConnectionMgr.java	2010-02-19 16:28:24 UTC (rev 1848)
+++ trunk/console/src/main/java/org/teiid/rhq/embedded/EmbeddedConnectionMgr.java	2010-02-19 16:29:33 UTC (rev 1849)
@@ -1,97 +0,0 @@
-package org.teiid.rhq.embedded;
-
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.Map;
-import java.util.Properties;
-import java.util.Set;
-
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-import org.rhq.core.pluginapi.inventory.InvalidPluginConfigurationException;
-import org.teiid.rhq.admin.ConnectionMgr;
-import org.teiid.rhq.comm.Connection;
-import org.teiid.rhq.comm.ConnectionException;
-import org.teiid.rhq.comm.ConnectionPool;
-import org.teiid.rhq.embedded.pool.ConnectionPoolImpl;
-import org.teiid.rhq.embedded.pool.EmbeddedConnectionConstants;
-
-
-
-public class EmbeddedConnectionMgr implements ConnectionMgr {
-	private static final Log log = LogFactory.getLog(EmbeddedConnectionMgr.class);
-		
-	private ConnectionPool pool;
-	private Properties props;
-	private ClassLoader loader;
-	
-	private Map<String, Connection> connectionList = new HashMap(1);
-	 
-
-	public Connection getConnection(String key) throws ConnectionException {
-		return pool.getConnection();
-	}
-	
-	
-
-	public Set<String> getInstallationSystemKeys() {
-		Set<String> keys = new HashSet<String>(1);
-		keys.add(EmbeddedConnectionConstants.SYSTEM_KEY);
-		return keys;
-	}
-
-
-
-	public Map getServerInstallations() {
-		connectionList = new HashMap(1);
-		try {
-			connectionList.put(pool.getKey(), pool.getConnection());
-		} catch (ConnectionException e) {
-			// TODO Auto-generated catch block
-			throw new InvalidPluginConfigurationException(e);
-		}
-		return connectionList;
-	}
-
-	public void shutdown() {
-
-			try {
-				pool.shutdown();
-			} catch (ConnectionException e) {
-				// TODO Auto-generated catch block
-				log.error("Error shutting down connection pool", e);
-
-			}
-			pool = null;
-			
-			connectionList.clear();
-		
-	}
-
-	   public boolean hasServersDefined() {
-		   return (pool !=null);
-	   }
-	   
-		  public void initialize(Properties props, ClassLoader cl) {
-				this.props = props;
-				this.loader = cl;
-		
-		            // allow override of the factory class
-		            // this was put in to allow testing to set the factory
-		            String factoryclass = System.getProperty(ConnectionPool.CONNECTION_FACTORY);
-		            if (factoryclass != null) {
-		                props.setProperty(ConnectionPool.CONNECTION_FACTORY, factoryclass);
-		            }
-		            
-		            try {	            
-		                 Class clzz = Class.forName(ConnectionPoolImpl.class.getName(), true, this.loader);
-		                 this.pool = (ConnectionPoolImpl) clzz.newInstance(); 
-		                	 //new ConnectionPoolImpl();
-		                this.pool.initialize(props, cl); 
-		           
-		                log.info("ConnectionPool created for key " + pool.getKey()); //$NON-NLS-1$ //$NON-NLS-2$
-		            } catch (Throwable t) {
-		                throw new InvalidPluginConfigurationException(t);
-		            }
-		      }
-}

Deleted: trunk/console/src/main/java/org/teiid/rhq/embedded/pool/ConnectionPoolImpl.java
===================================================================
--- trunk/console/src/main/java/org/teiid/rhq/embedded/pool/ConnectionPoolImpl.java	2010-02-19 16:28:24 UTC (rev 1848)
+++ trunk/console/src/main/java/org/teiid/rhq/embedded/pool/ConnectionPoolImpl.java	2010-02-19 16:29:33 UTC (rev 1849)
@@ -1,266 +0,0 @@
-package org.teiid.rhq.embedded.pool;
-
-import java.lang.reflect.InvocationHandler;
-import java.lang.reflect.InvocationTargetException;
-import java.lang.reflect.Method;
-import java.lang.reflect.Proxy;
-import java.util.Properties;
-import java.util.concurrent.ConcurrentLinkedQueue;
-import java.util.concurrent.Semaphore;
-import java.util.concurrent.TimeUnit;
-
-import org.teiid.rhq.comm.Connection;
-import org.teiid.rhq.comm.ConnectionException;
-import org.teiid.rhq.comm.ConnectionFactory;
-import org.teiid.rhq.comm.ConnectionPool;
-
-
-/*
- * JBoss, Home of Professional Open Source.
- * See the COPYRIGHT.txt file distributed with this work for information
- * regarding copyright ownership.  Some portions may be licensed
- * to Red Hat, Inc. under one or more contributor license agreements.
- * 
- * This library 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 library 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 library; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
- * 02110-1301 USA.
- */
-
-/**
- * Simple pooling built on JDBCUtil.  Not compatible with DataSources, etc.
- * Added due to poor 1.6 support among common connection pool implementations.
- * 
- * TODO: Should be switched to proxool or some implementation
- */
-public class ConnectionPoolImpl implements ConnectionPool
-
-{
-
-    public static final String WAIT_TIME_FOR_RESOURCE= "jbedsp.pool.wait.time"; //$NON-NLS-1$
-    public static final String MAXIMUM_RESOURCE_POOL_SIZE = "jbedsp.pool.maximum.size"; //$NON-NLS-1$
-    public static final String RESOURCE_TEST_INTERVAL = "jbedsp.pool.test.interval"; //$NON-NLS-1$
-    
-	private final class ConnectionProxy implements InvocationHandler {
-		private Connection c;
-		private long lastTest = System.currentTimeMillis();
-		private Boolean valid = Boolean.TRUE;
-		
-		public ConnectionProxy(Connection c) {
-			this.c = c;
-		}
-
-		public Object invoke(Object proxy, Method method, Object[] args)
-				throws Throwable {
-			try {
-				if (method.getName().equals("close")) { //$NON-NLS-1$
-					boolean isShutdown = shutdown;
-					boolean success = false;
-					try {
-						if (!isShutdown) {
-							connections.add((Connection)proxy);
-							success = true;
-						}
-					} finally {
-						connectionLock.release();
-						if (!success) {
-							c.close();
-							return null;
-						}
-					}
-					if (success) {
-						return null;
-					}
-				} else if (method.getName().equals("isValid")) { //$NON-NLS-1$
-					long now = System.currentTimeMillis();
-					if (lastTest + testInterval > now) {
-						return c.isValid();
-					} 
-					lastTest = now;
-					try {
-						valid = c.isAlive();						
-					} catch (AbstractMethodError e) {
-						valid = c.isValid();
-					} 
-					return valid;
-				}
-				return method.invoke(c, args);
-			} catch (InvocationTargetException e) {
-				valid = false;
-				throw e.getCause();
-			}
-		}
-	}
-	
-   /**
-     * The default connection factory if one is not specified in the environment
-     */
-     static final String CONNECTION_FACTORY_DEFAULT=ConnectionFactory.CONNECTION_FACTORY_DEFAULT;  //$NON-NLS-1$
-	
-	
-	private Semaphore connectionLock;
-	private ConcurrentLinkedQueue<Connection> connections = new ConcurrentLinkedQueue<Connection>();
-	private Properties p;
-	private int timeout;
-	private int testInterval;
-	private volatile boolean shutdown;
-    private ConnectionFactory factory = null;
-    
-    private ClassLoader loader = null;
-			
-	public void close(Connection connection) {
-
-		
-	}
-
-	
-
-	@Override
-	public int getAvailableConnectionCount() {
-		// TODO Auto-generated method stub
-		return 0;
-	}
-
-
-
-	@Override
-	public int getConnectionsInUseCount() {
-		// TODO Auto-generated method stub
-		return 0;
-	}
-
-
-
-	public ClassLoader getClassLoader() {
-		return this.loader;
-	}
-
-
-	public String getKey() {
-		return EmbeddedConnectionConstants.SYSTEM_KEY;
-	}
-
-
-
-	public void initialize(Properties env, ClassLoader cl) throws ConnectionException {
-		this.p = env;
-		this.loader = cl;
-		this.timeout = getIntProperty(p, WAIT_TIME_FOR_RESOURCE, 30000);
-		this.testInterval = getIntProperty(p, RESOURCE_TEST_INTERVAL, 30000);
-		this.connectionLock = new Semaphore(getIntProperty(p, MAXIMUM_RESOURCE_POOL_SIZE, 15));
-		
-//        liveAndUnusedTime = getIntProperty(LIVE_AND_UNUSED_TIME, DEFAULT_LIVE_AND_UNUSED_TIME);
-//        cleaningInterval = getIntProperty(CLEANING_INTERVAL, DEFAULT_CLEANING_INTERVAL);
-
-		
-        createFactory();
-	}
-
-
-
-	public Connection getConnection() throws ConnectionException {
-		if (shutdown) {
-			throw new ConnectionException("pool shutdown"); //$NON-NLS-1$
-		}
-		try {
-			if (!connectionLock.tryAcquire(timeout, TimeUnit.MILLISECONDS)) {
-				throw new ConnectionException("Timeout waiting for connection"); //$NON-NLS-1$
-			}
-		} catch (InterruptedException e) {
-			throw new ConnectionException(e);
-		}
-		boolean releaseLock = true;
-		try {
-			boolean valid = false;
-			Connection c = connections.poll();
-			if (c != null) {
-
-				valid  = c.isValid();
-				if (!valid) {
-					try {
-						factory.closeConnection(c);
-					} catch (Exception e) {
-						
-					}
-					c = null;
-				}
-			}
-			if (c == null) {
-				if (shutdown) {
-					throw new ConnectionException("pool shutdown"); //$NON-NLS-1$
-				}
-				c = factory.createConnection();
-				c = (Connection) Proxy.newProxyInstance(this.loader, new Class[] {Connection.class}, new ConnectionProxy(c));
-				connections.add(c);
-			}
-
-			releaseLock = false;
-			return c;
-		} catch (ConnectionException ce) {
-			throw ce;
-		} finally {
-			if (releaseLock) {
-				connectionLock.release();
-			}
-		}
-	}
-	
-	public void shutdown() {
-		this.shutdown = true;
-		Connection c = connections.poll();
-		while (c != null) {
-			try {
-				c.close();
-			} catch (Exception e) {
-				
-			}
-			c = connections.poll();
-		}		
-		
-	}
-
-
-    private void createFactory() throws ConnectionException {
-        
-        String factoryclass = p.getProperty(ConnectionPool.CONNECTION_FACTORY, CONNECTION_FACTORY_DEFAULT);
-  
-            try {
-            	Class<?> c = Class.forName(factoryclass, true, this.loader);
-                factory = (ConnectionFactory)c.newInstance();
-            } catch (Exception err) {
-                throw new ConnectionException(err.getMessage());
-            }
-            
-             // Initialize connector instance...
-            factory.initialize(p, this);
-      
-   }
-    
-    public static int getIntProperty(Properties props, String propName, int defaultValue) throws ConnectionException {
-        int val = defaultValue;
-        String stringVal = props.getProperty(propName);
-        if(stringVal != null && stringVal.trim().length() > 0) {
-        	try {
-        		val = Integer.parseInt(stringVal);
-        	} catch (NumberFormatException nfe) {
-        		throw new ConnectionException(nfe.getMessage());
-        	}
-        }
-        return val;
-    }
-    
-
-    
-}
-
-

Deleted: trunk/console/src/main/java/org/teiid/rhq/enterprise/EnterpriseConnectionMgr.java
===================================================================
--- trunk/console/src/main/java/org/teiid/rhq/enterprise/EnterpriseConnectionMgr.java	2010-02-19 16:28:24 UTC (rev 1848)
+++ trunk/console/src/main/java/org/teiid/rhq/enterprise/EnterpriseConnectionMgr.java	2010-02-19 16:29:33 UTC (rev 1849)
@@ -1,154 +0,0 @@
-package org.teiid.rhq.enterprise;
-
-import java.util.Collection;
-import java.util.Collections;
-import java.util.HashMap;
-import java.util.Iterator;
-import java.util.LinkedList;
-import java.util.Map;
-import java.util.Properties;
-import java.util.Set;
-
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-import org.rhq.core.pluginapi.inventory.InvalidPluginConfigurationException;
-import org.teiid.rhq.admin.ConnectionMgr;
-import org.teiid.rhq.comm.Connection;
-import org.teiid.rhq.comm.ConnectionConstants;
-import org.teiid.rhq.comm.ConnectionException;
-import org.teiid.rhq.comm.ConnectionPool;
-import org.teiid.rhq.enterprise.pool.ConnectionPoolImpl;
-
-
-public class EnterpriseConnectionMgr implements ConnectionMgr {
-	private static final Log log = LogFactory.getLog(EnterpriseConnectionMgr.class);
-	
-	
-	public static final String INSTALL_SERVER_PROP="mmservers";  //$NON-NLS-1$
-
-	private static Map poolMap =  Collections.synchronizedMap( new HashMap(5) );
-	
-
-	private Properties props = null;
-
-
-	public Connection getConnection(String key) throws ConnectionException {
-        Connection connection = null;
-        
-        log.info("Get Connection for  " + key); //$NON-NLS-1$
-        
-        ConnectionPool pool = (ConnectionPool) poolMap.get(key);
-        connection = pool.getConnection();
-                
-        return connection;
-	}
-	
-
-	public Set getInstallationSystemKeys() {
-		// TODO Auto-generated method stub
-		return poolMap.keySet();
-	}
-
-
-	public boolean hasServersDefined() {
-        
-        if (poolMap != null && poolMap.size() > 0) {
-            return true;
-        }
-        return false;
-        
-    }	
-
-	public Map getServerInstallations() {
-        Map connectionList = new HashMap();
-        Iterator installationIter = poolMap.keySet().iterator();
-
-         while (installationIter.hasNext()) {
-            String installDir = (String) installationIter.next();
-            try {
-                if (poolMap.get(installDir) != null) {
-                    ConnectionPool pool = (ConnectionPool) poolMap.get(installDir);
-                
-                    Connection connection = pool.getConnection(); 
-                    connectionList.put(installDir, connection);
-                } else {
-                    // this shouldn't happen
-                    connectionList.put(installDir, null);
-                }
-            } catch (Exception e) {
-                connectionList.put(installDir, null);
-            }
-        }
-        
-
-		return connectionList;
-	}
-
-
-	public void shutdown() {
-        Iterator installationIter = poolMap.keySet().iterator();
-        
-        while (installationIter.hasNext()) {
-           String installDir = (String) installationIter.next();
-               ConnectionPool pool = (ConnectionPool) poolMap.get(installDir);
-       		try {
-    			pool.shutdown();
-    		} catch (ConnectionException e) {
-    			// TODO Auto-generated catch block
-    			log.error("Error shutting down connection pool " + pool.getKey(), e);
-    			e.printStackTrace();
-    		}
-        }
-         
-        poolMap.clear();
-    }
-	  public void initialize(Properties props, ClassLoader cl) {
-			this.props = props;
-
-	        String servers = this.props.getProperty(INSTALL_SERVER_PROP);
-
-	        /**
-	         * split the server installation properties by the delimiter
-	         * to determine the number of servers installed on the local machine
-	         */
-	         Collection installationList = AdminUtil.getTokens(servers, ";");  //$NON-NLS-1$
-	        Iterator installationIter = installationList.iterator();
-
-	        while (installationIter.hasNext()) {
-	            String serverInfoValues = (String) installationIter.next();
-	            Collection serverInfoValuesList = new LinkedList();
-	            serverInfoValuesList = AdminUtil.getTokens(serverInfoValues, ",");  //$NON-NLS-1$
-	            Object[] serverInfoArray = serverInfoValuesList.toArray();
-	            String installDir = (String) serverInfoArray[0];
-	            String url = (String) serverInfoArray[1];
-	            String username = (String) serverInfoArray[2];
-	            String password = (String) serverInfoArray[3];
-
-	            props.setProperty(ConnectionConstants.PASSWORD, password);
-	            props.setProperty(ConnectionConstants.USERNAME, username);
-	            props.setProperty(ConnectionConstants.URL, url);                             
-	                        
-	            // allow override of the factory class
-	            // this was put in to allow testing to set the factory
-	            String factoryclass = System.getProperty(ConnectionPool.CONNECTION_FACTORY);
-	            if (factoryclass != null) {
-	                props.setProperty(ConnectionPool.CONNECTION_FACTORY, factoryclass);
-	            }
-	            
-	            try {	            
-	                 
-	                 Class clzz = Class.forName(ConnectionPoolImpl.class.getName(), true, cl);
-	                ConnectionPool pool = (ConnectionPool) clzz.newInstance();
-	                pool.initialize(props, cl); 
-	                          
-	                poolMap.put(pool.getKey(), pool);
-	           
-	                log.info("ConnectionPool created for key " + pool.getKey() + " at url " + url); //$NON-NLS-1$ //$NON-NLS-2$
-	            } catch (Throwable t) {
-	                throw new InvalidPluginConfigurationException(t);
-	            }
-	        }
-	    }	
-	
-
-}

Deleted: trunk/console/src/main/java/org/teiid/rhq/enterprise/pool/ConnectionPoolImpl.java
===================================================================
--- trunk/console/src/main/java/org/teiid/rhq/enterprise/pool/ConnectionPoolImpl.java	2010-02-19 16:28:24 UTC (rev 1848)
+++ trunk/console/src/main/java/org/teiid/rhq/enterprise/pool/ConnectionPoolImpl.java	2010-02-19 16:29:33 UTC (rev 1849)
@@ -1,424 +0,0 @@
-/*
- * JBoss, Home of Professional Open Source.
- * See the COPYRIGHT.txt file distributed with this work for information
- * regarding copyright ownership.  Some portions may be licensed
- * to Red Hat, Inc. under one or more contributor license agreements.
- * 
- * This library 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 library 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 library; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
- * 02110-1301 USA.
- */
-package org.teiid.rhq.enterprise.pool;
-
-import java.util.Collections;
-import java.util.HashSet;
-import java.util.Iterator;
-import java.util.Properties;
-import java.util.Set;
-import java.util.concurrent.Semaphore;
-import java.util.concurrent.TimeUnit;
-
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-import org.teiid.rhq.comm.Connection;
-import org.teiid.rhq.comm.ConnectionException;
-import org.teiid.rhq.comm.ConnectionFactory;
-import org.teiid.rhq.comm.ConnectionPool;
-
-
-
-
-/** 
- *  This is the connection pool used to manage connections to the MetaMatrix server.
- */
-public class ConnectionPoolImpl implements ConnectionPool {
-    private final Log LOG = LogFactory.getLog(ConnectionPoolImpl.class);
-    
-    /**
-     * The default connection factory if one is not specified in the environment
-     */
-    private static final String CONNECTION_FACTORY_DEFAULT=ConnectionFactory.CONNECTION_FACTORY_DEFAULT;  //$NON-NLS-1$
-    
-    public static final String WAIT_TIME_FOR_RESOURCE= "teiid.pool.wait.time"; //$NON-NLS-1$
-    public static final String MAXIMUM_RESOURCE_POOL_SIZE = "teiid.pool.maximum.size"; //$NON-NLS-1$
-    public static final String RESOURCE_TEST_INTERVAL = "teiid.pool.test.interval"; //$NON-NLS-1$
-
-
-     private static final int DEFAULT_LIVE_AND_UNUSED_TIME = 60;
-     private static final int DEFAULT_CLEANING_INTERVAL = 60;
-     private static final boolean DEFAULT_ENABLE_SHRINKING = true; 
-
-     
-     private int liveAndUnusedTime = DEFAULT_LIVE_AND_UNUSED_TIME;
-     private int cleaningInterval = DEFAULT_CLEANING_INTERVAL;
-     private boolean enableShrinking = DEFAULT_ENABLE_SHRINKING;
-     
- 	 private int timeout;
-	 private int testInterval;
-
-     
-    
-	private Set<ConnectionWrapper>  availConnections          = Collections.synchronizedSet( new HashSet(10) );
-    private Set<Connection>  inuseConnections   = Collections.synchronizedSet( new HashSet(10) );
-    
-	private Semaphore connectionLock;
-    
-    private ConnectionFactory factory = null;
-//    private String installDir = null;
-    private Properties envProps = null;
-    
-    private volatile boolean shuttingDownPool;
-    
-    private CleanUpThread cleaningThread;   
-    
-    private ClassLoader clzzloader;
-    
-    
- //   private Object lock = new Object();
-    
-    public ConnectionPoolImpl() {
-        
-        LOG.info("Creating Connection Pool");  //$NON-NLS-1$ 
-    }
-    
-
-    /** 
-     * @see org.teiid.rhq.comm.ConnectionPool#getClassLoader()
-     *  
-     */
-    public ClassLoader getClassLoader() {
-        return clzzloader;
-    }
-    
-    
-
-
-    @Override
-	public int getAvailableConnectionCount() {
-		return availConnections.size();
-	}
-    
-    
-
-
-	@Override
-	public int getConnectionsInUseCount() {
-		return inuseConnections.size();
-	}
-
-
-	public String getKey() {
-		// TODO Auto-generated method stub
-		return factory.getURL();
-	}
-
-
-	/** 
-     * @see com.metamatrix.rhq.admin.pool.ConnectionPool#close(org.teiid.rhq.comm.Connection)
-     *  
-     */
-    public void close(Connection connection) throws ConnectionException {
-        if (this.shuttingDownPool) {
-             return;
-        }        
-		try {
-			if (!connectionLock.tryAcquire(timeout, TimeUnit.MILLISECONDS)) {
-				throw new ConnectionException("Timeout waiting for lock"); //$NON-NLS-1$
-			}
-		} catch (InterruptedException e) {
-			throw new ConnectionException(e);
-		}
-		try {
-            inuseConnections.remove(connection);
-            if (connection.isValid()) {
-                ConnectionWrapper cw = new ConnectionWrapper(connection);
-                availConnections.add(cw);
-                LOG.debug("Connection checked in for system "); //$NON-NLS-1$
-            } else {
-               this.closeConnection(connection);  
-               LOG.debug("Connection returned and closed for system "); //$NON-NLS-1$                
-
-            }
-
-
-
-		} finally {
-			connectionLock.release();
-		}
-
-    }
-      
-
-    /** 
-     * @see com.metamatrix.rhq.admin.pool.ConnectionPool#getConnection()
-     *  
-     */
-    public Connection getConnection() throws ConnectionException {
-        
-        if (this.shuttingDownPool) {
-            return null;
-        }
-
-		try {
-			if (!connectionLock.tryAcquire(timeout, TimeUnit.MILLISECONDS)) {
-				throw new ConnectionException("Timeout waiting for lock trying to get connection"); //$NON-NLS-1$
-			}
-		} catch (InterruptedException e) {
-			throw new ConnectionException(e);
-		}
-		
-		try {
-            if (availConnections.isEmpty()) {
-                Connection conn = createConnection();                    
-                inuseConnections.add(conn);
-                return conn;
-            }                
-            
-        
-            for (Iterator it=availConnections.iterator(); it.hasNext();) {
-                ConnectionWrapper conn = (ConnectionWrapper) it.next();
-                it.remove();
-                if (conn.originalConnection.isValid()) {
-                    inuseConnections.add(conn.originalConnection);
-                    LOG.debug("Existing connection obtained for system "); //$NON-NLS-1$
-                    return conn.originalConnection;
-                } 
-                this.closeConnection(conn.originalConnection);                               
-                                       
-            }  
-            
-            Connection conn = createConnection();
-            inuseConnections.add(conn);
-            return conn;                
-
-		} finally {
-			connectionLock.release();
-		}
-       
-    }
-    
-    /** 
-     * @see com.metamatrix.rhq.admin.pool.ConnectionPool#initialize(java.lang.String, java.util.Properties)
-     *  
-     */
-    public void initialize(Properties env, ClassLoader cl) throws ConnectionException {
-        this.envProps = env;
- //       this.installDir = env.getProperty(EnterpriseConnectionConstants.INSTALL_DIR);
-               
-        this.clzzloader = cl; 
- 		this.connectionLock = new Semaphore(getIntProperty(MAXIMUM_RESOURCE_POOL_SIZE, 15));
-		
-		this.timeout = getIntProperty( WAIT_TIME_FOR_RESOURCE, 30000);
-		this.testInterval = getIntProperty( RESOURCE_TEST_INTERVAL, 30000);
-		this.connectionLock = new Semaphore(getIntProperty( MAXIMUM_RESOURCE_POOL_SIZE, 15));
-
-
-        initializeProps();        
-        createFactory();
-    }
-
-    /** 
-     * @see com.metamatrix.rhq.admin.pool.ConnectionPool#shutdown()
-     *  
-     */
-    public void shutdown()  throws ConnectionException {
-        shuttingDownPool = true;
-        
-		try {
-			if (!connectionLock.tryAcquire(timeout, TimeUnit.MILLISECONDS)) {
-				throw new ConnectionException("Timeout waiting for lock trying to get connection"); //$NON-NLS-1$
-			}
-		} catch (InterruptedException e) {
-			throw new ConnectionException(e);
-		}
-        
-		try {
-	        //close cleaning thread
-	        if (this.cleaningThread != null) {
-	            this.cleaningThread.stopCleanup();
-	            this.cleaningThread.interrupt();
-	        }        
-	        
-	        // cleanup, passing true, will close all available connections
-	        this.cleanUp(true);
-	        	
-	        
-	        for (Iterator i = inuseConnections.iterator(); i.hasNext();) {
-	            Connection conn = (Connection)i.next();
-	            this.closeConnection(conn);
-	        }
-	        inuseConnections.clear();
-	        
-	
-	        
-	        envProps.clear();
-	        factory = null;
-		} finally {
-			connectionLock.release();
-		}
-        
-        
-    }
-    
-    
-    private void createFactory() throws ConnectionException {
-        
-        String factoryclass = envProps.getProperty(ConnectionPool.CONNECTION_FACTORY, CONNECTION_FACTORY_DEFAULT);       
-        
-        Thread currentThread = Thread.currentThread();
-        ClassLoader threadContextLoader = currentThread.getContextClassLoader();
-        try {
-            currentThread.setContextClassLoader(this.clzzloader);
-            try {
-                Class c = Class.forName(factoryclass, true, this.clzzloader);
-                factory = (ConnectionFactory)c.newInstance();
-            } catch (Exception err) {
-                throw new ConnectionException(err.getMessage());
-            }
-            
-             // Initialize connector instance...
-            factory.initialize(this.envProps, this);
-
- 
-        } finally {
-            currentThread.setContextClassLoader(threadContextLoader);
-        }
-        
-   }
-    
-    private void initializeProps() throws ConnectionException {
-            liveAndUnusedTime = getIntProperty(LIVE_AND_UNUSED_TIME, DEFAULT_LIVE_AND_UNUSED_TIME);
-            cleaningInterval = getIntProperty(CLEANING_INTERVAL, DEFAULT_CLEANING_INTERVAL);
-
-            
-            if (!this.shuttingDownPool) {
-                this.cleaningThread = new CleanUpThread(cleaningInterval * 1000);
-                this.cleaningThread.setDaemon(true);
-                this.cleaningThread.start();
-            }
-            
-            String value = envProps.getProperty(ENABLE_SHRINKING);
-            if ( value != null ) {
-                enableShrinking = Boolean.valueOf(value).booleanValue();
-            }            
-               
-    }    
-
-    private int getIntProperty(String propertyName, int defaultValue) throws ConnectionException {
-        String value = this.envProps.getProperty(propertyName );
-        if (value == null || value.trim().length() == 0) {
-            return defaultValue;
-        }
-        return Integer.parseInt(value); 
-        
-    }
-        
-    
-    private Connection createConnection() throws ConnectionException {
-        Thread currentThread = Thread.currentThread();
-        ClassLoader threadContextLoader = currentThread.getContextClassLoader();
-        try {
-            currentThread.setContextClassLoader(this.clzzloader);
-            
-            // Initialize connector instance...
-            return factory.createConnection();
-
- 
-        } finally {
-            currentThread.setContextClassLoader(threadContextLoader);
-        }
-        
-        
-
-    }
-    
-    private void closeConnection(Connection conn) {
-        Thread currentThread = Thread.currentThread();
-        ClassLoader threadContextLoader = currentThread.getContextClassLoader();
-        try {
-            currentThread.setContextClassLoader(this.clzzloader);
-            // Initialize connector instance...
-            factory.closeConnection(conn);
-
- 
-        } finally {
-            currentThread.setContextClassLoader(threadContextLoader);
-        }
-        
-    }
-    
-    protected void cleanUp(boolean forceClose) {        
-        Set values = new HashSet(this.availConnections);
-
-            for (Iterator i = values.iterator(); i.hasNext();) {
-                ConnectionWrapper conn = (ConnectionWrapper)i.next();
-                
-                    if (forceClose || (enableShrinking && conn.getIdelTime() >= this.liveAndUnusedTime)
-                                    || !conn.originalConnection.isAlive()) {
-                       availConnections.remove(conn);
-                       this.closeConnection(conn.originalConnection);   
- 
-                    }
-            }
-
-    }    
-    
-    /**
-     * ConnectionWrapper is used to store the connection in the availableConnections and
-     * will provide the amount of idletime a connection has been unused so that
-     * it can be determined if the pool can be shrunk 
-     * 
-     */
-    class ConnectionWrapper {
-        Connection originalConnection;
-        private long timeReturnedToPool;
-
-        ConnectionWrapper(Connection originalConn) {
-            originalConnection = originalConn;
-            timeReturnedToPool = System.currentTimeMillis();
-        }
-
-        int getIdelTime() {
-            return (int) (System.currentTimeMillis() - timeReturnedToPool) / 1000;
-        }
-    }        
-    
-    class CleanUpThread extends Thread {
-        private long sleepTime;
-        private boolean continueChecks = true;
-
-        CleanUpThread(long sleepTime) {
-            super("MMConnectionPoolCleanUpThread");  //$NON-NLS-1$
-            this.sleepTime = sleepTime;
-        }
-
-        public void stopCleanup() {
-            this.continueChecks = false;
-        }
-
-        public void run() {
-            while ( this.continueChecks ) {
-                try {
-                    sleep(sleepTime);
-                } catch (InterruptedException e) {
-                    // ignore it
-                }
-                ConnectionPoolImpl.this.cleanUp(false);
-            }
-        }
-    }  
-        
-    
-
-}



More information about the teiid-commits mailing list