svn-gvsig-desktop / branches / v2_0_0_prep / applications / appgvSIG / src / com / iver / cit / gvsig / SingleVectorialDBConnectionExtension.java @ 21743
History | View | Annotate | Download (8.91 KB)
1 | 11971 | caballero | /* 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; |
||
44 | |||
45 | 12826 | jaume | import java.util.ArrayList; |
46 | |||
47 | import javax.swing.JOptionPane; |
||
48 | |||
49 | import org.apache.log4j.Logger; |
||
50 | |||
51 | 11971 | caballero | import com.iver.andami.PluginServices; |
52 | import com.iver.andami.plugins.Extension; |
||
53 | import com.iver.cit.gvsig.vectorialdb.ConnectionSettings; |
||
54 | import com.iver.utiles.NotExistInXMLEntity; |
||
55 | import com.iver.utiles.XMLEntity; |
||
56 | import com.prodevelop.cit.gvsig.vectorialdb.wizard.VectorialDBConnectionManagerDialog; |
||
57 | |||
58 | |||
59 | /**
|
||
60 | * This extension allows the user to access the single connection manager dialog
|
||
61 | *
|
||
62 | 20994 | jmvivo | * @see org.gvsig.fmap.drivers.formats.db.utils.SingleVectorialDBConnectionManager
|
63 | 11971 | caballero | *
|
64 | * @author jldominguez
|
||
65 | *
|
||
66 | */
|
||
67 | public class SingleVectorialDBConnectionExtension extends Extension { |
||
68 | private static Logger logger = Logger.getLogger(SingleVectorialDBConnectionExtension.class.getName()); |
||
69 | |||
70 | /**
|
||
71 | * This method simply loads the connections stored in gvSIG's persistence file
|
||
72 | * as closed connections
|
||
73 | */
|
||
74 | public void initialize() { |
||
75 | loadPersistenceConnections(); |
||
76 | 15647 | jmvivo | registerIcons(); |
77 | 11971 | caballero | } |
78 | |||
79 | 15647 | jmvivo | private void registerIcons(){ |
80 | PluginServices.getIconTheme().register( |
||
81 | "conn-image",
|
||
82 | this.getClass().getClassLoader().getResource("images/conn.png") |
||
83 | ); |
||
84 | PluginServices.getIconTheme().register( |
||
85 | "disconn-image",
|
||
86 | this.getClass().getClassLoader().getResource("images/disconn.png") |
||
87 | ); |
||
88 | |||
89 | } |
||
90 | |||
91 | 11971 | caballero | /**
|
92 | * The only command available is the one to open the connection manager
|
||
93 | * dialog ("GESTOR_JDBC")
|
||
94 | */
|
||
95 | public void execute(String actionCommand) { |
||
96 | if (actionCommand.compareToIgnoreCase("GESTOR_VECTORIALDB") == 0) { |
||
97 | VectorialDBConnectionManagerDialog dlg = new VectorialDBConnectionManagerDialog();
|
||
98 | dlg.showDialog(); |
||
99 | 15684 | vcaballero | // saveAllToPersistence();
|
100 | 11971 | caballero | |
101 | return;
|
||
102 | } |
||
103 | } |
||
104 | |||
105 | public boolean isEnabled() { |
||
106 | return true; |
||
107 | } |
||
108 | |||
109 | public boolean isVisible() { |
||
110 | return true; |
||
111 | } |
||
112 | |||
113 | 15684 | vcaballero | public static void saveAllToPersistence() { |
114 | SingleVectorialDBConnectionExtension ext=new SingleVectorialDBConnectionExtension();
|
||
115 | XMLEntity xml = PluginServices.getPluginServices(ext).getPersistentXML(); |
||
116 | 14543 | jmvivo | xml.putProperty("literalDBName",true); |
117 | 11971 | caballero | xml.remove("db-connections");
|
118 | |||
119 | 21743 | vcaballero | // ConnectionWithParams[] all = SingleVectorialDBConnectionManager.instance()
|
120 | // .getAllConnections();
|
||
121 | //
|
||
122 | // if (all == null) {
|
||
123 | // PluginServices.getPluginServices(ext).setPersistentXML(xml);
|
||
124 | // return;
|
||
125 | // }
|
||
126 | //
|
||
127 | // for (int i = 0; i < all.length; i++) {
|
||
128 | // addToPersistence(all[i],ext);
|
||
129 | // }
|
||
130 | 11971 | caballero | } |
131 | |||
132 | 21743 | vcaballero | // private static void addToPersistence(ConnectionWithParams cwp, Extension ext) {
|
133 | // if (cwp == null) {
|
||
134 | // return;
|
||
135 | // }
|
||
136 | //
|
||
137 | // ConnectionSettings _cs = new ConnectionSettings();
|
||
138 | //
|
||
139 | // _cs.setHost(cwp.getHost());
|
||
140 | // _cs.setPort(cwp.getPort());
|
||
141 | // _cs.setDb(cwp.getDb());
|
||
142 | // _cs.setDriver(cwp.getDrvName());
|
||
143 | // _cs.setUser(cwp.getUser());
|
||
144 | // _cs.setName(cwp.getName());
|
||
145 | //
|
||
146 | // String newstr = _cs.toString();
|
||
147 | //
|
||
148 | // XMLEntity xml = PluginServices.getPluginServices(ext).getPersistentXML();
|
||
149 | //
|
||
150 | // String[] old = null;
|
||
151 | //
|
||
152 | // if (xml.contains("db-connections")) {
|
||
153 | // old = xml.getStringArrayProperty("db-connections");
|
||
154 | // }
|
||
155 | //
|
||
156 | // ArrayList oldl = stringArrayToArrayList(old);
|
||
157 | // oldl.add(newstr);
|
||
158 | //
|
||
159 | // String[] newarr = (String[]) oldl.toArray(new String[0]);
|
||
160 | //
|
||
161 | // xml.remove("db-connections");
|
||
162 | // xml.putProperty("db-connections", newarr);
|
||
163 | // PluginServices.getPluginServices(ext).setPersistentXML(xml);
|
||
164 | // }
|
||
165 | 11971 | caballero | |
166 | 15684 | vcaballero | private static ArrayList stringArrayToArrayList(String[] arr) { |
167 | 11971 | caballero | ArrayList resp = new ArrayList(); |
168 | |||
169 | if ((arr != null) && (arr.length > 0)) { |
||
170 | for (int i = 0; i < arr.length; i++) { |
||
171 | resp.add(arr[i]); |
||
172 | } |
||
173 | } |
||
174 | |||
175 | return resp;
|
||
176 | } |
||
177 | |||
178 | private void loadPersistenceConnections() { |
||
179 | XMLEntity xml = PluginServices.getPluginServices(this).getPersistentXML();
|
||
180 | 14543 | jmvivo | boolean literalDBNames;
|
181 | 11971 | caballero | |
182 | 14543 | jmvivo | if (!xml.contains("literalDBName")){ |
183 | // For back compatibility
|
||
184 | literalDBNames = false;
|
||
185 | }else{
|
||
186 | // For back compatibility: Normaly always true
|
||
187 | literalDBNames = xml.getBooleanProperty("literalDBName");
|
||
188 | } |
||
189 | |||
190 | 11971 | caballero | if (xml == null) { |
191 | xml = new XMLEntity();
|
||
192 | } |
||
193 | |||
194 | if (!xml.contains("db-connections")) { |
||
195 | String[] servers = new String[0]; |
||
196 | xml.putProperty("db-connections", servers);
|
||
197 | } |
||
198 | |||
199 | // add drivers to connection manager
|
||
200 | String[] servers = null; |
||
201 | |||
202 | try {
|
||
203 | servers = xml.getStringArrayProperty("db-connections");
|
||
204 | } |
||
205 | catch (NotExistInXMLEntity e) {
|
||
206 | System.err.println(
|
||
207 | "Error while getting projects db-connections: " +
|
||
208 | e.getMessage()); |
||
209 | |||
210 | return;
|
||
211 | } |
||
212 | |||
213 | for (int i = 0; i < servers.length; i++) { |
||
214 | ConnectionSettings cs = new ConnectionSettings();
|
||
215 | boolean params_ok = true; |
||
216 | try {
|
||
217 | cs.setFromString(servers[i]); |
||
218 | 14543 | jmvivo | // For back compatibility
|
219 | if (!literalDBNames){
|
||
220 | cs.setDb(cs.getDb().toLowerCase()); |
||
221 | } |
||
222 | 11971 | caballero | } catch (Exception ex) { |
223 | logger.error("Found misconfigured connection: " + servers[i]);
|
||
224 | params_ok = false;
|
||
225 | } |
||
226 | if (params_ok) addDisconnected(cs);
|
||
227 | } |
||
228 | 14543 | jmvivo | |
229 | PluginServices ps= PluginServices.getPluginServices("com.iver.cit.gvsig.jdbc_spatial");
|
||
230 | if (ps==null) |
||
231 | return;
|
||
232 | XMLEntity xmlJDBC =ps.getPersistentXML(); |
||
233 | |||
234 | if (xmlJDBC==null || !xmlJDBC.contains("jdbc-connections")) |
||
235 | return;
|
||
236 | |||
237 | // add drivers to connection manager
|
||
238 | String[] serversOld = null; |
||
239 | |||
240 | try {
|
||
241 | serversOld = xmlJDBC.getStringArrayProperty("jdbc-connections");
|
||
242 | } |
||
243 | catch (NotExistInXMLEntity e) {
|
||
244 | System.err.println(
|
||
245 | "Error while getting projects jdbc-connections: " +
|
||
246 | e.getMessage()); |
||
247 | |||
248 | return;
|
||
249 | } |
||
250 | |||
251 | for (int i = 0; i < serversOld.length; i++) { |
||
252 | ConnectionSettings cs = new ConnectionSettings();
|
||
253 | boolean params_ok = true; |
||
254 | try {
|
||
255 | cs.setFromString(serversOld[i]); |
||
256 | cs.setDb(cs.getDb().toLowerCase()); |
||
257 | } catch (Exception ex) { |
||
258 | logger.error("Found misconfigured connection: " + serversOld[i]);
|
||
259 | params_ok = false;
|
||
260 | } |
||
261 | if (params_ok) addDisconnected(cs);
|
||
262 | } |
||
263 | |||
264 | |||
265 | 11971 | caballero | } |
266 | |||
267 | private void addDisconnected(ConnectionSettings _cs) { |
||
268 | 21743 | vcaballero | // try {
|
269 | // SingleVectorialDBConnectionManager.instance()
|
||
270 | // .getConnection(_cs.getDriver(),
|
||
271 | // _cs.getUser(), null, _cs.getName(), _cs.getHost(),
|
||
272 | // _cs.getPort(), _cs.getDb(), false);
|
||
273 | // }
|
||
274 | // catch (DBException e) {
|
||
275 | // System.err.println("While getting connection: " + e.getMessage());
|
||
276 | // showConnectionErrorMessage(e.getMessage());
|
||
277 | // }
|
||
278 | 11971 | caballero | } |
279 | |||
280 | private void showConnectionErrorMessage(String _msg) { |
||
281 | String msg = (_msg.length() > 300) ? "" : (": " + _msg); |
||
282 | String title = PluginServices.getText(this, "connection_error"); |
||
283 | JOptionPane.showMessageDialog(null, title + msg, title, |
||
284 | JOptionPane.ERROR_MESSAGE);
|
||
285 | } |
||
286 | |||
287 | public void terminate() { |
||
288 | 15684 | vcaballero | saveAllToPersistence(); |
289 | 21743 | vcaballero | // SingleVectorialDBConnectionManager.instance().closeAllBeforeTerminate();
|
290 | 11971 | caballero | } |
291 | } |