001    //$HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/branches/2.2_testing/src/org/deegree/io/sdeapi/SDEPool.java $
002    /*----------------    FILE HEADER  ------------------------------------------
003    
004     This file is part of deegree.
005     Copyright (C) 2006 by: M.O.S.S. Computer Grafik Systeme GmbH
006     Hohenbrunner Weg 13
007     D-82024 Taufkirchen
008     http://www.moss.de/
009    
010     This library is free software; you can redistribute it and/or
011     modify it under the terms of the GNU Lesser General Public
012     License as published by the Free Software Foundation; either
013     version 2.1 of the License, or (at your option) any later version.
014    
015     This library is distributed in the hope that it will be useful,
016     but WITHOUT ANY WARRANTY; without even the implied warranty of
017     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
018     Lesser General Public License for more details.
019    
020     You should have received a copy of the GNU Lesser General Public
021     License along with this library; if not, write to the Free Software
022     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
023    
024     ---------------------------------------------------------------------------*/
025    package org.deegree.io.sdeapi;
026    
027    import org.deegree.framework.util.ObjectPool;
028    
029    /**
030     * class to manage a pool of sde connections.
031     *
032     *
033     * @author <a href="mailto:cpollmann@moss.de">Christoph Pollmann</a>
034     * @version 2.0
035     */
036    public class SDEPool extends ObjectPool {
037    
038        private String server = null;
039    
040        private String database = null;
041    
042        private int instance = 5151;
043    
044        private String version = null;
045    
046        private String user = null;
047    
048        private String password = null;
049    
050        // private constructor to protect initializing
051        public SDEPool( final String server, final int instance, final String database,
052                       final String version, final String user, final String password ) {
053    
054            this.server = server;
055            this.instance = instance;
056            this.database = database;
057            this.version = version;
058            this.user = user;
059            this.password = password;
060        }
061    
062        /**
063         * get an object from the object pool
064         */
065        public synchronized Object acquireObject()
066                                throws Exception {
067            try {
068                // if the maximum amount of instances are in use
069                // wait until an instance has been released back
070                // to the pool or 20 seconds has passed
071                long timediff = 0;
072                while ( in_use.size() == getMaxInstances() && timediff < 20000 ) {
073                    Thread.sleep( 100 );
074                    timediff += 100;
075                }
076                // if no instance has been released within 20 seconds
077                // or can newly be instantiated return null
078                if ( timediff >= 20000 )
079                    return null;
080    
081                // if a none used is available from the pool
082                if ( available.size() > 0 ) {
083    
084                    // get/remove ojebct from the pool
085                    Object o = available.remove( available.size() - 1 );
086                    if ( ( (SDEConnection) o ).isClosed() ) {
087                        o = acquireObject();
088                    }
089    
090                    // add it to 'in use' container
091                    in_use.add( o );
092    
093                    // reset its start life time
094                    startLifeTime.put( o, new Long( System.currentTimeMillis() ) );
095                    // set the start of its usage
096                    startUsageTime.put( o, new Long( System.currentTimeMillis() ) );
097    
098                    // return the object
099                    return o;
100    
101                }
102                // else instatiate a new object
103                Object connection = new SDEConnection( server, instance, database, version, user,
104                                                       password );
105    
106                existingInstances++;
107    
108                // add it to 'in use' container
109                in_use.add( connection );
110                // set the start of its life time
111                startLifeTime.put( connection, new Long( System.currentTimeMillis() ) );
112                // set the start of its usage
113                startUsageTime.put( connection, new Long( System.currentTimeMillis() ) );
114                // return the object
115                return connection;
116            } catch ( Exception e ) {
117                e.printStackTrace();
118                throw new Exception( "Error while acquiring connection: " + e.getMessage(), e );
119            }
120        }
121    
122        /**
123         * will be called when the object is removed from the pool
124         */
125        public void onObjectKill( Object o ) {
126            try {
127                ( (SDEConnection) o ).close();
128            } catch ( Exception e ) {
129                e.printStackTrace();
130            }
131        }
132    
133    }