View Javadoc

1   package com.explosion.expfmodules.rdbmsconn.connect;
2   
3   /*
4    * =============================================================================
5    * 
6    * Copyright 2004 Stephen Cowx
7    * 
8    * Licensed under the Apache License, Version 2.0 (the "License"); you may not
9    * use this file except in compliance with the License. You may obtain a copy of
10   * the License at
11   * 
12   * http://www.apache.org/licenses/LICENSE-2.0
13   * 
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
17   * License for the specific language governing permissions and limitations under
18   * the License.
19   * 
20   * =============================================================================
21   */
22  
23  import java.sql.Connection;
24  import java.sql.SQLException;
25  import java.util.HashMap;
26  import java.util.Hashtable;
27  import java.util.Map;
28  import com.explosion.expfmodules.rdbmsconn.DriverDescriptorManager;
29  import com.explosion.expfmodules.rdbmsconn.dbom.dialect.Dialect;
30  import com.explosion.expfmodules.rdbmsconn.dbom.dialect.SQLServerDialect;
31  import com.explosion.utilities.exception.ExceptionManagerFactory;
32  import com.explosion.utilities.preferences.groups.PreferenceGroup;
33  
34  /***
35   * This class maintains a hashtable of connections keyed on threadname.
36   * connection name.
37   * 
38   * @author Stephen Cowx
39   */
40  
41  public class ConnectionManager
42  {
43  
44      private Hashtable connections = new Hashtable();
45  
46      private Hashtable connectionDescriptors = new Hashtable();
47  
48      private int sequence = 0;
49  
50      private static ConnectionManager instance;
51      
52      private Map dialects = new HashMap();
53  
54      private ConnectionManager()
55      {
56      }
57  
58      public static ConnectionManager getInstance()
59      {
60          if (instance == null)
61          	instance = new ConnectionManager();
62          
63      	return instance;
64      }
65  
66      /***
67       * This method connectstothe specified database andreturns the unique
68       * connectionID for this connection. Returns -1 if there was a problem
69       * connecting to the database.
70       */
71      public int connect(PreferenceGroup connectionDescriptor, DriverDescriptorManager driverManager) throws Exception
72      {
73          ConnectionFactory factory = new ConnectionFactory();
74          Connection conn = factory.createConnection(connectionDescriptor, driverManager);
75          
76          if (conn != null)
77          {
78            connections.put(new Integer(sequence), conn);
79            connectionDescriptors.put(new Integer(sequence), connectionDescriptor);
80            dialects.put(conn, constructDialect(conn));
81            sequence++;
82            return (sequence - 1);
83          }
84          else
85          {
86          	return -1;
87          }
88      }
89      /***
90       * Constructs the right Dialect for this connection if it can otherwise it uses the default dialect
91       * @param conn
92       * @return
93       */
94      private Dialect constructDialect(Connection conn)
95      {
96          try
97          {
98              if (conn.getMetaData().getDatabaseProductName().toLowerCase().indexOf("sql server") > 0)
99              {
100                 return new SQLServerDialect();
101             }
102             else
103             {
104                 return new Dialect();
105             }
106         } catch (SQLException e)
107         {
108            ExceptionManagerFactory.getExceptionManager().manageException(e,"Exception caught while creating dialect.  Using default dialect.");
109            return new Dialect();
110         }
111     }
112     
113     /***
114      * Returns the dialect for this connection
115      * @param conn
116      * @return
117      */
118     public Dialect getDialect(Connection conn)
119     {
120         return (Dialect) dialects.get(conn);
121     }
122 
123     /***
124      * This method returns a connection object associated with this name
125      */
126     public Connection getConnection(int key)
127     {
128         return (Connection) connections.get(new Integer(key));
129     }
130 
131     /***
132      * This method returns the connection desctiptor usedtocreate this
133      * connection
134      */
135     public PreferenceGroup getConnectionDescriptor(int key)
136     {
137         return (PreferenceGroup) connectionDescriptors.get(new Integer(key));
138     }
139 
140     /***
141      * This method removes a connection from the connection manager. If the
142      * connection is not closed it will close the connection
143      */
144     public void disconnect(int key)
145     {
146         Connection conn = (Connection) connections.get(new Integer(key));
147         try
148         {
149             if (!conn.isClosed())
150                 conn.close();
151         } catch (Exception ex)
152         {
153             ExceptionManagerFactory.getExceptionManager().manageException(ex, "Exception caught while attempting to close connection.");
154         }
155         connections.remove(new Integer(key));
156     }
157 
158 }