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