root / trunk / extensions / extSDE / src / com / iver / cit / gvsig / sde / gui / sdewizard2 / SingleSDEConnectionManager.java @ 11193
History | View | Annotate | Download (7.85 KB)
1 |
package com.iver.cit.gvsig.sde.gui.sdewizard2; |
---|---|
2 |
|
3 |
import java.sql.SQLException; |
4 |
import java.util.ArrayList; |
5 |
import java.util.HashMap; |
6 |
import java.util.Iterator; |
7 |
|
8 |
import org.apache.log4j.Logger; |
9 |
|
10 |
import com.esri.sde.sdk.client.SeConnection; |
11 |
import com.esri.sde.sdk.client.SeException; |
12 |
import com.iver.cit.gvsig.fmap.drivers.sde.VectorialSDEDriver; |
13 |
import com.iver.cit.gvsig.fmap.layers.LayerFactory; |
14 |
|
15 |
/**
|
16 |
* Utility class to handle connections properly. One connection instance per
|
17 |
* (db, user, gvsig session)
|
18 |
*
|
19 |
* @author jldominguez
|
20 |
*
|
21 |
*/
|
22 |
public class SingleSDEConnectionManager { |
23 |
|
24 |
private static Logger logger = Logger.getLogger(SingleSDEConnectionManager.class.getName()); |
25 |
private static SingleSDEConnectionManager single_instance = null; |
26 |
private HashMap connections = new HashMap(); |
27 |
|
28 |
/**
|
29 |
* Non-public to avoid unwanted instances.
|
30 |
*
|
31 |
*/
|
32 |
protected SingleSDEConnectionManager() {
|
33 |
|
34 |
} |
35 |
|
36 |
/**
|
37 |
* Singleton model to keep single instances.
|
38 |
*
|
39 |
* @return single instance
|
40 |
*/
|
41 |
public static SingleSDEConnectionManager instance() { |
42 |
if (single_instance == null) { |
43 |
single_instance = new SingleSDEConnectionManager();
|
44 |
} |
45 |
return single_instance;
|
46 |
} |
47 |
|
48 |
/**
|
49 |
* Utility metho to find a connection with its parameters
|
50 |
* given the connection object.
|
51 |
*
|
52 |
* @param co the connection object
|
53 |
* @return
|
54 |
*/
|
55 |
public ConnectionWithParamsSDE findConnection(SeConnection co) {
|
56 |
|
57 |
Iterator iter = connections.keySet().iterator();
|
58 |
while (iter.hasNext()) {
|
59 |
String keyitem = (String) iter.next(); |
60 |
ConnectionWithParamsSDE cwp = (ConnectionWithParamsSDE) connections.get(keyitem); |
61 |
if (cwp.getConnection() == co) {
|
62 |
return cwp;
|
63 |
} |
64 |
} |
65 |
return null; |
66 |
} |
67 |
|
68 |
|
69 |
/**
|
70 |
* Creates a new connection with its parameters if not created yet.
|
71 |
*
|
72 |
* @param _drvName driver name
|
73 |
* @param _user user name
|
74 |
* @param _pw password
|
75 |
* @param _name connection name
|
76 |
* @param _host host url
|
77 |
* @param _port port number as string
|
78 |
* @param _db databade name
|
79 |
* @param _connected whether or not to connect the connection
|
80 |
* @return the connection with parameters object
|
81 |
* @throws SQLException
|
82 |
*/
|
83 |
public ConnectionWithParamsSDE getConnection (
|
84 |
String _drvName,
|
85 |
String _user,
|
86 |
String _pw,
|
87 |
String _name,
|
88 |
String _host,
|
89 |
String _port,
|
90 |
String _db,
|
91 |
boolean _connected
|
92 |
) throws SQLException { |
93 |
|
94 |
VectorialSDEDriver drv = getInstanceFromName(_drvName); |
95 |
String conn_str = drv.getConnectionString(_host, _port, _db, _user, _pw);
|
96 |
|
97 |
String key = getConnectionKey(_drvName, _host, _port, _user, _db, _name);
|
98 |
|
99 |
if (!connections.containsKey(key)) {
|
100 |
|
101 |
ConnectionWithParamsSDE cwp = null;
|
102 |
|
103 |
if (_connected) {
|
104 |
SeConnection new_connection = null;
|
105 |
try {
|
106 |
new_connection = new SeConnection(_host, Integer.parseInt(_port), _db, _user, _pw); |
107 |
} catch (NumberFormatException e) { |
108 |
e.printStackTrace(); |
109 |
} catch (SeException e) {
|
110 |
e.printStackTrace(); |
111 |
} |
112 |
|
113 |
cwp = new ConnectionWithParamsSDE(
|
114 |
conn_str, |
115 |
new_connection, |
116 |
_drvName, |
117 |
_user, |
118 |
_pw, |
119 |
_name, |
120 |
_host, |
121 |
_port, |
122 |
_db, |
123 |
true);
|
124 |
} else {
|
125 |
|
126 |
cwp = new ConnectionWithParamsSDE(
|
127 |
conn_str, |
128 |
null,
|
129 |
_drvName, |
130 |
_user, |
131 |
null,
|
132 |
_name, |
133 |
_host, |
134 |
_port, |
135 |
_db, |
136 |
false);
|
137 |
} |
138 |
connections.put(key, cwp); |
139 |
} |
140 |
|
141 |
ConnectionWithParamsSDE _cwp = (ConnectionWithParamsSDE) connections.get(key); |
142 |
|
143 |
if (_cwp.getName().compareTo(_name) != 0) { |
144 |
// connections.remove(key);
|
145 |
_cwp.setName(_name); |
146 |
connections.put(key, _cwp); |
147 |
} |
148 |
|
149 |
if ((!_cwp.isConnected()) && (_connected)) {
|
150 |
_cwp.connect(_pw); |
151 |
} |
152 |
|
153 |
return _cwp;
|
154 |
} |
155 |
|
156 |
/**
|
157 |
* Gets available open connections.
|
158 |
*
|
159 |
* @return array of open connections with parameters
|
160 |
*/
|
161 |
public ConnectionWithParamsSDE[] getConnectedConnections() { |
162 |
Iterator iter = connections.keySet().iterator();
|
163 |
if (!iter.hasNext()) return null; |
164 |
|
165 |
ArrayList aux = new ArrayList(); |
166 |
|
167 |
while (iter.hasNext()) {
|
168 |
ConnectionWithParamsSDE _cwp = |
169 |
(ConnectionWithParamsSDE) connections.get(iter.next()); |
170 |
if (_cwp.isConnected()) {
|
171 |
aux.add(_cwp); |
172 |
} |
173 |
} |
174 |
|
175 |
ConnectionWithParamsSDE[] resp = new ConnectionWithParamsSDE[aux.size()]; |
176 |
for (int i=0; i<aux.size(); i++) { |
177 |
resp[i] = (ConnectionWithParamsSDE) aux.get(i); |
178 |
} |
179 |
return resp;
|
180 |
} |
181 |
|
182 |
/**
|
183 |
* Gets all available connections.
|
184 |
*
|
185 |
* @return array of all connections with parameters
|
186 |
*/
|
187 |
public ConnectionWithParamsSDE[] getAllConnections() { |
188 |
Iterator iter = connections.keySet().iterator();
|
189 |
if (!iter.hasNext()) return null; |
190 |
|
191 |
ArrayList aux = new ArrayList(); |
192 |
|
193 |
while (iter.hasNext()) {
|
194 |
ConnectionWithParamsSDE _cwp = |
195 |
(ConnectionWithParamsSDE) connections.get(iter.next()); |
196 |
aux.add(_cwp); |
197 |
} |
198 |
|
199 |
ConnectionWithParamsSDE[] resp = new ConnectionWithParamsSDE[aux.size()]; |
200 |
for (int i=0; i<aux.size(); i++) { |
201 |
resp[i] = (ConnectionWithParamsSDE) aux.get(i); |
202 |
} |
203 |
return resp;
|
204 |
} |
205 |
|
206 |
/**
|
207 |
* Removes connection with its params.
|
208 |
*
|
209 |
* @param _cwp connection with params to be removed
|
210 |
*/
|
211 |
private void removeConnectionWP(ConnectionWithParamsSDE _cwp) { |
212 |
|
213 |
ArrayList keysToRemove = new ArrayList(); |
214 |
|
215 |
Iterator iter = connections.keySet().iterator();
|
216 |
while (iter.hasNext()) {
|
217 |
Object key = iter.next();
|
218 |
ConnectionWithParamsSDE cwp = (ConnectionWithParamsSDE) connections.get(key); |
219 |
if (cwp == _cwp) {
|
220 |
keysToRemove.add(key); |
221 |
} |
222 |
} |
223 |
for (int i=0; i<keysToRemove.size(); i++) { |
224 |
connections.remove(keysToRemove.get(i)); |
225 |
} |
226 |
} |
227 |
|
228 |
|
229 |
/**
|
230 |
* Closes and removes a connection with params object
|
231 |
*
|
232 |
* @param _cwp
|
233 |
* @return whether the connection was actually closed (false if the
|
234 |
* connection was not open at the start)
|
235 |
*/
|
236 |
public boolean closeAndRemove(ConnectionWithParamsSDE _cwp) { |
237 |
|
238 |
boolean it_was_open = true; |
239 |
|
240 |
try {
|
241 |
it_was_open = (_cwp.getConnection() != null) && (!_cwp.getConnection().isClosed());
|
242 |
if (_cwp.getConnection() != null) _cwp.getConnection().close(); |
243 |
removeConnectionWP(_cwp); |
244 |
} catch (Exception se) { |
245 |
logger.error("While closing connection: " + se.getMessage(), se);
|
246 |
return false; |
247 |
} |
248 |
logger.info("Connection successfully closed.");
|
249 |
return it_was_open;
|
250 |
} |
251 |
|
252 |
/**
|
253 |
* Called by the extension object when gvsig terminates.
|
254 |
*
|
255 |
*/
|
256 |
public void closeAllBeforeTerminate() { |
257 |
|
258 |
boolean ok = true; |
259 |
String key = ""; |
260 |
ConnectionWithParamsSDE cwp = null;
|
261 |
Iterator iter = connections.keySet().iterator();
|
262 |
while (iter.hasNext()) {
|
263 |
key = (String) iter.next();
|
264 |
cwp = (ConnectionWithParamsSDE) connections.get(key); |
265 |
|
266 |
if (cwp.getConnection() == null) continue; |
267 |
|
268 |
try {
|
269 |
cwp.getConnection().close(); |
270 |
} catch (SeException se) {
|
271 |
ok = false;
|
272 |
logger.error("While closing connection: " + se.getMessage(), se);
|
273 |
} |
274 |
} |
275 |
|
276 |
connections.clear(); |
277 |
|
278 |
if (ok) {
|
279 |
logger.info("Successfully closed all connections.");
|
280 |
} else {
|
281 |
logger.warn("Problems while closing all connections.");
|
282 |
} |
283 |
} |
284 |
|
285 |
/**
|
286 |
* Gets the objects key to be used in the inner hashmap
|
287 |
* @param _drvName driver name
|
288 |
* @param _host host's url
|
289 |
* @param _port port number
|
290 |
* @param _user user name
|
291 |
* @param _db db name
|
292 |
* @return ConnectionKey
|
293 |
*/
|
294 |
private static String getConnectionKey( |
295 |
String _drvName,
|
296 |
String _host,
|
297 |
String _port,
|
298 |
String _user,
|
299 |
String _db,
|
300 |
String _name) {
|
301 |
|
302 |
String resp = "_driver_" + _drvName.toLowerCase(); |
303 |
resp = resp + "_host_" + _host.toLowerCase();
|
304 |
resp = resp + "_port_" + _port;
|
305 |
resp = resp + "_user_" + _user.toLowerCase();
|
306 |
resp = resp + "_db_" + _db.toLowerCase();
|
307 |
resp = resp + "_name_" + _name.toLowerCase();
|
308 |
|
309 |
return resp;
|
310 |
} |
311 |
|
312 |
/**
|
313 |
* Utility method to instantiate a driver given its name.
|
314 |
*
|
315 |
* @param drvname driver name
|
316 |
* @return driver instance
|
317 |
*/
|
318 |
public static VectorialSDEDriver getInstanceFromName(String drvname) { |
319 |
|
320 |
VectorialSDEDriver _driver = null;
|
321 |
try {
|
322 |
_driver = (VectorialSDEDriver) LayerFactory.getDM().getDriver(drvname); |
323 |
} catch (Exception e) { |
324 |
logger.error("While getting driver instance: " + e.getMessage(), e);
|
325 |
} |
326 |
return _driver;
|
327 |
} |
328 |
|
329 |
|
330 |
|
331 |
|
332 |
|
333 |
} |