001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one
003     * or more contributor license agreements. See the NOTICE file
004     * distributed with this work for additional information
005     * regarding copyright ownership. The ASF licenses this file
006     * to you under the Apache License, Version 2.0 (the  "License");
007     * you may not use this file except in compliance with the License.
008     * You may obtain a copy of the License at
009     *
010     *     http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    /*
019     * $Id: ConnectionPoolManager.java 468638 2006-10-28 06:52:06Z minchau $
020     */
021    
022    
023     package org.apache.xalan.lib.sql;
024    
025    import java.util.Hashtable;
026    
027    import org.apache.xalan.res.XSLMessages;
028    import org.apache.xalan.res.XSLTErrorResources;
029    
030    /**
031     */
032    public class ConnectionPoolManager
033    {
034      /**
035       */
036      private static Hashtable m_poolTable = null;
037    
038      /**
039       */
040      public ConnectionPoolManager( )
041      {
042        init();
043      }
044    
045      /**
046       * Initialize the internal structures of the Pool Manager
047       *
048       */
049      private synchronized void init( )
050      {
051        /**
052         * Only do this process once
053         * Initialize the pool table
054         */   
055        if (m_poolTable == null)
056                m_poolTable = new Hashtable();
057      }
058    
059      /**
060       * Register a nuew connection pool to the global pool table.
061       * If a pool by that name currently exists, then throw an
062       * IllegalArgumentException stating that the pool already
063       * exist.
064       * @param name
065       * @param pool
066       *
067       * @link org.apache.xalan.lib.sql.ConnectionPool}
068       *
069       * @throws <code>IllegalArgumentException</code>, throw this exception
070       * if a pool with the same name currently exists.
071       */
072      public synchronized void registerPool( String name, ConnectionPool pool )
073      {
074        if ( m_poolTable.containsKey(name) )
075        {
076          throw new IllegalArgumentException(XSLMessages.createMessage(XSLTErrorResources.ER_POOL_EXISTS, null)); //"Pool already exists");
077        }
078    
079        m_poolTable.put(name, pool);
080      }
081    
082      /**
083       * Remove a pool from the global table. If the pool still has
084       * active connections, then only mark this pool as inactive and
085       * leave it around until all the existing connections are closed.
086       * @param name
087       *
088       */
089      public synchronized void removePool( String name )
090      {
091        ConnectionPool pool = getPool(name);
092    
093        if (null != pool)
094        {
095          //
096          // Disable future use of this pool under the Xalan
097          // extension only. This flag should only exist in the
098          // wrapper and not in the actual pool implementation.
099          pool.setPoolEnabled(false);
100    
101    
102          //
103          // Remove the pool from the Hashtable if we don'd have
104          // any active connections.
105          //
106          if ( ! pool.hasActiveConnections() ) m_poolTable.remove(name);
107        }
108    
109      }
110    
111    
112      /**
113       * Return the connection pool referenced by the name
114       * @param name
115       *
116       * @return <code>ConnectionPool</code> a reference to the ConnectionPool
117       * object stored in the Pool Table. If the named pool does not exist, return
118       * null
119       */
120      public synchronized ConnectionPool getPool( String name )
121      {
122        return (ConnectionPool) m_poolTable.get(name);
123      }
124    
125    }