001    //$HeadURL: svn+ssh://jwilden@svn.wald.intevation.org/deegree/base/branches/2.5_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        /**
052         * @param server
053         * @param instance
054         * @param database
055         * @param version
056         * @param user
057         * @param password
058         */
059        public SDEPool( final String server, final int instance, final String database, final String version,
060                        final String user, final String password ) {
061    
062            this.server = server;
063            this.instance = instance;
064            this.database = database;
065            this.version = version;
066            this.user = user;
067            this.password = password;
068        }
069    
070        /**
071         * get an object from the object pool
072         * 
073         * @return an object from the object pool
074         * @throws Exception
075         */
076        public synchronized Object acquireObject()
077                                throws Exception {
078            try {
079                // if the maximum amount of instances are in use
080                // wait until an instance has been released back
081                // to the pool or 20 seconds has passed
082                long timediff = 0;
083                while ( in_use.size() == getMaxInstances() && timediff < 20000 ) {
084                    Thread.sleep( 100 );
085                    timediff += 100;
086                }
087                // if no instance has been released within 20 seconds
088                // or can newly be instantiated return null
089                if ( timediff >= 20000 )
090                    return null;
091    
092                // if a none used is available from the pool
093                if ( available.size() > 0 ) {
094    
095                    // get/remove ojebct from the pool
096                    Object o = available.remove( available.size() - 1 );
097                    if ( ( (SDEConnection) o ).isClosed() ) {
098                        o = acquireObject();
099                    }
100    
101                    // add it to 'in use' container
102                    in_use.add( o );
103    
104                    // reset its start life time
105                    startLifeTime.put( o, new Long( System.currentTimeMillis() ) );
106                    // set the start of its usage
107                    startUsageTime.put( o, new Long( System.currentTimeMillis() ) );
108    
109                    // return the object
110                    return o;
111    
112                }
113                // else instatiate a new object
114                Object connection = new SDEConnection( server, instance, database, version, user, password );
115    
116                existingInstances++;
117    
118                // add it to 'in use' container
119                in_use.add( connection );
120                // set the start of its life time
121                startLifeTime.put( connection, new Long( System.currentTimeMillis() ) );
122                // set the start of its usage
123                startUsageTime.put( connection, new Long( System.currentTimeMillis() ) );
124                // return the object
125                return connection;
126            } catch ( Exception e ) {
127                e.printStackTrace();
128                throw new Exception( "Error while acquiring connection: " + e.getMessage(), e );
129            }
130        }
131    
132        /**
133         * will be called when the object is removed from the pool
134         */
135        @Override
136        public void onObjectKill( Object o ) {
137            try {
138                ( (SDEConnection) o ).close();
139            } catch ( Exception e ) {
140                e.printStackTrace();
141            }
142        }
143    
144    }