JavaDoc


001    /**
002     * Copyright (c) 2003 Daffodil Software Ltd all rights reserved,
003     * Modifications Copyright (c) 2008 Regiscope Digital Imaging Co, LLC, All rights reserved.
004     * This program is free software; you can redistribute it and/or modify
005     * it under the terms of version 2 of the GNU General Public License as
006     * published by the Free Software Foundation.
007     * There are special exceptions to the terms and conditions of the GPL
008     * as it is applied to this software. See the GNU General Public License for more details.
009     *
010     * This program is distributed in the hope that it will be useful,
011     * but WITHOUT ANY WARRANTY; without even the implied warranty of
012     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013     * GNU General Public License for more details.
014     *
015     * You should have received a copy of the GNU General Public License
016     * along with this program; if not, write to the Free Software
017     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
018     */
019    
020    package org.dbreplicator.replication;
021    
022    import java.io.*;
023    import java.net.*;
024    import java.sql.*;
025    import java.util.*;
026    
027    import javax.sql.DataSource;
028    import org.apache.log4j.Logger;
029    
030    /**
031     * This class holds the mappings of all the connections in a perticular
032     * replication server. Every replication server has one copy of this class,
033     * that helps replication server to get different connections for different
034     * publications or subscriptions or default connection.
035     * This class stores this connection information in to a Map (connectionMap).
036     */
037    
038    public class ConnectionPool implements java.io.Serializable{
039    
040      private DataSource dataSource;
041      private String url, driver, user, password, port;
042      private HashMap connectionMap;
043      String localAddress;
044      int localPortNo;
045      protected static Logger log =Logger.getLogger(ConnectionPool.class.getName());
046    
047      // for debugging
048      private HashSet openConnectionNames = new HashSet();
049    
050      public ConnectionPool() {
051      }
052    
053      public ConnectionPool(String url0, String driver0, String user0,
054                            String password0) {
055        url = url0;
056        driver = driver0;
057        user = user0;
058        password = password0;
059        connectionMap = new HashMap();
060      }
061    
062      public ConnectionPool(DataSource dataSource) {
063        this.dataSource = dataSource;
064        connectionMap = new HashMap();
065      }
066    
067      public ConnectionPool(DataSource dataSource, String username, String password) {
068          this.dataSource = dataSource;
069          this.user       = username;
070          this.password   = password;
071          connectionMap   = new HashMap();
072        }
073    
074      private Connection getConnectionFromDataSource() throws RepException
075      {
076        try {
077          Connection connection = (user != null ? dataSource.getConnection(user, password) : dataSource.getConnection());
078    
079          if (log.isDebugEnabled()) {
080            log.debug("Got connection "+connection.toString()+" from data source");
081            openConnectionNames.add(connection.toString());
082          }
083          return connection;
084        }
085        catch (SQLException ex) {
086          throw new RepException("REP007", null);
087        }
088      }
089    
090      public synchronized Connection getConnection(String pubsubName) throws RepException {
091        if (dataSource != null) {
092          return getConnectionFromDataSource();
093        }
094        Connection connection = (Connection) connectionMap.get(pubsubName);
095        if (connection != null) {
096          return connection;
097        }
098        try {
099          Class.forName(driver);
100        }
101        catch (ClassNotFoundException ex) {
102                log.error(ex.getMessage(),ex);
103          throw new RepException("REP005", new Object[] {driver});
104        }
105        try {
106          connection = DriverManager.getConnection(url, user, password);
107        }
108        catch (SQLException ex1) {
109                log.error(ex1.getMessage(),ex1);
110          throw new RepException("REP007", null);
111        }
112        connectionMap.put(pubsubName, connection);
113        return connection;
114      }
115    
116      public synchronized Connection getFreshConnection(String pubsubName) throws RepException {
117        if (dataSource != null) {
118          return getConnectionFromDataSource();
119        }
120        try {
121          Class.forName(driver);
122        }
123        catch (ClassNotFoundException ex) {
124                log.error(ex.getMessage(),ex);
125          throw new RepException("REP005", new Object[] {driver});
126        }
127        try {
128    
129          String urlWithoutSchema = url.toLowerCase();
130          int indexscehma = urlWithoutSchema.indexOf("schema");
131          if (indexscehma != -1) {
132            int indexcolon = urlWithoutSchema.indexOf(';', indexscehma);
133            if (indexcolon == -1) {
134              urlWithoutSchema = url.substring(0, indexscehma - 1);
135            }
136            else {
137              urlWithoutSchema = url.substring(0, indexscehma);
138              urlWithoutSchema = urlWithoutSchema +
139                  url.substring(indexscehma, indexcolon - 1);
140    
141            }
142          }
143          else {
144            urlWithoutSchema = url;
145          }
146    
147          return DriverManager.getConnection(urlWithoutSchema, user, password);
148        }
149        catch (SQLException ex1) {
150                log.error(ex1.getMessage(),ex1);
151          throw new RepException("REP007", null);
152        }
153      }
154    
155      public void setLocalAddress(String localAddress0) {
156        localAddress = localAddress0;
157      }
158    
159      public String getLocalAddress() {
160        return localAddress;
161      }
162    
163      public void setLocalPortNo(int localPort0) {
164        localPortNo = localPort0;
165      }
166    
167      public int getLocalPortNo() {
168        return localPortNo;
169      }
170    
171      public ServerSocket startServerSocket() throws RepException {
172        int port = 3457;
173        ServerSocket serverSocket = null;
174        while (port < 6000) {
175          try {
176            //return new ServerSocket(port);
177                    serverSocket = new ServerSocket(port);
178                    log.debug(" bjt - Server socket bound to inet address " + serverSocket.getInetAddress());
179                    return serverSocket;
180          }
181          catch (IOException ex) {
182            port++;
183          }
184        }
185        throw new RepException("REP085", null);
186      }
187    
188      public String getUserName() {
189        return user;
190      }
191    
192      public synchronized Connection getDefaultConnection() throws RepException {
193        if (dataSource != null) {
194          return getConnectionFromDataSource();
195        }
196        try {
197          Class.forName(driver);
198        }
199        catch (ClassNotFoundException ex1) {
200          RepConstants.writeERROR_FILE(ex1);
201          throw new RepException("REP005", new Object[] {driver});
202        }
203        try {
204          return DriverManager.getConnection(url, user, password);
205        }
206        catch (Exception ex) {
207          RepConstants.writeERROR_FILE(ex);
208          throw new RepException("REP001", new Object[] {ex.getMessage()});
209        }
210      }
211    
212      public synchronized void returnConnection(Connection connection) throws RepException {
213        if ((dataSource != null) && (connection != null)) {
214          try {
215            openConnectionNames.remove(connection.toString());
216            if (!connection.isClosed()) {
217              if (log.isDebugEnabled()) {
218                StringBuffer logMsg = new StringBuffer();
219    
220                logMsg.append("Returning connection ");
221                logMsg.append(connection.toString());
222                logMsg.append(" to data source.\nRemaining connections:");
223                if (openConnectionNames.isEmpty()) {
224                  logMsg.append(" None");
225                }
226                else {
227                  for (Iterator it = openConnectionNames.iterator(); it.hasNext();) {
228                    logMsg.append("\n    ");
229                    logMsg.append(it.next().toString());
230                  }
231                }
232                log.debug(logMsg.toString());
233              }
234              connection.close();
235            }
236          }
237          catch (SQLException ex) {
238            throw new RepException("REP001", new Object[] {ex.getMessage()});
239          }
240        }
241      }
242      public void removeSubPubFromMap(String subPubName){
243    //System.out.println(" Removed called with "+ subPubName);
244    //Thread.dumpStack();
245        if(connectionMap.containsKey(subPubName)){
246         Object object = connectionMap.remove(subPubName);
247    //     System.out.println("  Object remvoded for "+subPubName+" -- "+ object);
248        }
249      }
250    
251    
252    public static void closeStatementAndResultSet(Statement stmt,ResultSet rs) {
253       try {
254         if (rs != null) {
255           rs.close();
256         }
257       }
258       catch (SQLException ex2) {
259       }
260       try {
261         if (stmt != null)
262           stmt.close();
263       }
264       catch (SQLException ex) {
265       }
266    
267    }
268    
269    }





























































Powered by Drupal - Theme by Danger4k