root / trunk / extensions / extSDE / src / com / iver / cit / gvsig / sde / SingleSDEConnectionExtension.java @ 11193
History | View | Annotate | Download (6.73 KB)
1 |
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2006 Prodevelop and Generalitat Valenciana.
|
4 |
*
|
5 |
* This program is free software; you can redistribute it and/or
|
6 |
* modify it under the terms of the GNU General Public License
|
7 |
* as published by the Free Software Foundation; either version 2
|
8 |
* of the License, or (at your option) any later version.
|
9 |
*
|
10 |
* This program is distributed in the hope that it will be useful,
|
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
13 |
* GNU General Public License for more details.
|
14 |
*
|
15 |
* You should have received a copy of the GNU General Public License
|
16 |
* along with this program; if not, write to the Free Software
|
17 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,USA.
|
18 |
*
|
19 |
* For more information, contact:
|
20 |
*
|
21 |
* Generalitat Valenciana
|
22 |
* Conselleria d'Infraestructures i Transport
|
23 |
* Av. Blasco Ib??ez, 50
|
24 |
* 46010 VALENCIA
|
25 |
* SPAIN
|
26 |
*
|
27 |
* +34 963862235
|
28 |
* gvsig@gva.es
|
29 |
* www.gvsig.gva.es
|
30 |
*
|
31 |
* or
|
32 |
*
|
33 |
* Prodevelop Integraci?n de Tecnolog?as SL
|
34 |
* Conde Salvatierra de ?lava , 34-10
|
35 |
* 46004 Valencia
|
36 |
* Spain
|
37 |
*
|
38 |
* +34 963 510 612
|
39 |
* +34 963 510 968
|
40 |
* gis@prodevelop.es
|
41 |
* http://www.prodevelop.es
|
42 |
*/
|
43 |
package com.iver.cit.gvsig.sde; |
44 |
|
45 |
import java.sql.SQLException; |
46 |
import java.util.ArrayList; |
47 |
|
48 |
import javax.swing.JOptionPane; |
49 |
|
50 |
import org.apache.log4j.Logger; |
51 |
|
52 |
import com.iver.andami.PluginServices; |
53 |
import com.iver.andami.plugins.Extension; |
54 |
import com.iver.cit.gvsig.sde.gui.sdewizard2.ConnectionSettings; |
55 |
import com.iver.cit.gvsig.sde.gui.sdewizard2.ConnectionWithParamsSDE; |
56 |
import com.iver.cit.gvsig.sde.gui.sdewizard2.SDEConnectionManagerDialog; |
57 |
import com.iver.cit.gvsig.sde.gui.sdewizard2.SingleSDEConnectionManager; |
58 |
import com.iver.utiles.NotExistInXMLEntity; |
59 |
import com.iver.utiles.XMLEntity; |
60 |
|
61 |
|
62 |
/**
|
63 |
* This extension allows the user to access the single connection manager dialog.
|
64 |
*
|
65 |
* @see com.iver.cit.gvsig.fmap.drivers.jdbc.utils.SingleJDBCConnectionManager
|
66 |
*
|
67 |
* @author jldominguez
|
68 |
*
|
69 |
*/
|
70 |
public class SingleSDEConnectionExtension extends Extension { |
71 |
private static Logger logger = Logger.getLogger(SingleSDEConnectionExtension.class.getName()); |
72 |
|
73 |
/**
|
74 |
* This method simply loads the connections stored in gvSIG's persistence file
|
75 |
* as closed connections
|
76 |
*/
|
77 |
public void initialize() { |
78 |
loadPersistenceConnections(); |
79 |
} |
80 |
|
81 |
/**
|
82 |
* The only command available is the one to open the connection manager
|
83 |
* dialog ("GESTOR_SDE")
|
84 |
*/
|
85 |
public void execute(String actionCommand) { |
86 |
if (actionCommand.compareToIgnoreCase("GESTOR_SDE") == 0) { |
87 |
SDEConnectionManagerDialog dlg = new SDEConnectionManagerDialog();
|
88 |
dlg.showDialog(); |
89 |
saveAllToPersistence(); |
90 |
|
91 |
return;
|
92 |
} |
93 |
} |
94 |
public boolean isEnabled() { |
95 |
return true; |
96 |
} |
97 |
|
98 |
public boolean isVisible() { |
99 |
return true; |
100 |
} |
101 |
private void saveAllToPersistence() { |
102 |
XMLEntity xml = PluginServices.getPluginServices(this).getPersistentXML();
|
103 |
xml.remove("sde-connections");
|
104 |
|
105 |
ConnectionWithParamsSDE[] all = SingleSDEConnectionManager.instance()
|
106 |
.getAllConnections(); |
107 |
|
108 |
if (all == null) { |
109 |
return;
|
110 |
} |
111 |
|
112 |
for (int i = 0; i < all.length; i++) { |
113 |
addToPersistence(all[i]); |
114 |
} |
115 |
} |
116 |
|
117 |
|
118 |
private void addToPersistence(ConnectionWithParamsSDE cwp) { |
119 |
if (cwp == null) { |
120 |
return;
|
121 |
} |
122 |
|
123 |
ConnectionSettings _cs = new ConnectionSettings();
|
124 |
|
125 |
_cs.setHost(cwp.getHost()); |
126 |
_cs.setPort(cwp.getPort()); |
127 |
_cs.setDb(cwp.getDb()); |
128 |
_cs.setDriver(cwp.getDrvName()); |
129 |
_cs.setUser(cwp.getUser()); |
130 |
_cs.setName(cwp.getName()); |
131 |
|
132 |
String newstr = _cs.toString();
|
133 |
|
134 |
XMLEntity xml = PluginServices.getPluginServices(this).getPersistentXML();
|
135 |
|
136 |
String[] old = null; |
137 |
|
138 |
if (xml.contains("sde-connections")) { |
139 |
old = xml.getStringArrayProperty("sde-connections");
|
140 |
} |
141 |
|
142 |
ArrayList oldl = stringArrayToArrayList(old);
|
143 |
oldl.add(newstr); |
144 |
|
145 |
String[] newarr = (String[]) oldl.toArray(new String[0]); |
146 |
|
147 |
xml.remove("sde-connections");
|
148 |
xml.putProperty("sde-connections", newarr);
|
149 |
PluginServices.getPluginServices(this).setPersistentXML(xml);
|
150 |
} |
151 |
private ArrayList stringArrayToArrayList(String[] arr) { |
152 |
ArrayList resp = new ArrayList(); |
153 |
|
154 |
if ((arr != null) && (arr.length > 0)) { |
155 |
for (int i = 0; i < arr.length; i++) { |
156 |
resp.add(arr[i]); |
157 |
} |
158 |
} |
159 |
|
160 |
return resp;
|
161 |
} |
162 |
|
163 |
private void loadPersistenceConnections() { |
164 |
XMLEntity xml = PluginServices.getPluginServices(this).getPersistentXML();
|
165 |
|
166 |
if (xml == null) { |
167 |
xml = new XMLEntity();
|
168 |
} |
169 |
|
170 |
if (!xml.contains("sde-connections")) { |
171 |
String[] servers = new String[0]; |
172 |
xml.putProperty("sde-connections", servers);
|
173 |
} |
174 |
|
175 |
// add drivers to connection manager
|
176 |
String[] servers = null; |
177 |
|
178 |
try {
|
179 |
servers = xml.getStringArrayProperty("sde-connections");
|
180 |
} |
181 |
catch (NotExistInXMLEntity e) {
|
182 |
System.err.println(
|
183 |
"Error while getting projects sde-connections: " +
|
184 |
e.getMessage()); |
185 |
|
186 |
return;
|
187 |
} |
188 |
|
189 |
for (int i = 0; i < servers.length; i++) { |
190 |
ConnectionSettings cs = new ConnectionSettings();
|
191 |
boolean params_ok = true; |
192 |
try {
|
193 |
cs.setFromString(servers[i]); |
194 |
} catch (Exception ex) { |
195 |
logger.error("Found misconfigured connection: " + servers[i]);
|
196 |
params_ok = false;
|
197 |
} |
198 |
if (params_ok) addDisconnected(cs);
|
199 |
} |
200 |
} |
201 |
|
202 |
|
203 |
private void addDisconnected(ConnectionSettings _cs) { |
204 |
try {
|
205 |
SingleSDEConnectionManager.instance() |
206 |
.getConnection(_cs.getDriver(), |
207 |
_cs.getUser(), null, _cs.getName(), _cs.getHost(),
|
208 |
_cs.getPort(), _cs.getDb(), false);
|
209 |
} |
210 |
catch (SQLException e) { |
211 |
System.err.println("While getting connection: " + e.getMessage()); |
212 |
showConnectionErrorMessage(e.getMessage()); |
213 |
} |
214 |
} |
215 |
|
216 |
private void showConnectionErrorMessage(String _msg) { |
217 |
String msg = (_msg.length() > 300) ? "" : (": " + _msg); |
218 |
String title = PluginServices.getText(this, "connection_error"); |
219 |
JOptionPane.showMessageDialog(null, title + msg, title, |
220 |
JOptionPane.ERROR_MESSAGE);
|
221 |
} |
222 |
|
223 |
|
224 |
public void terminate() { |
225 |
SingleSDEConnectionManager.instance().closeAllBeforeTerminate(); |
226 |
} |
227 |
|
228 |
} |