Statistics
| Revision:

root / branches / F2 / libraries / libJCRS / src / es / idr / teledeteccion / connection / EpsgConnection.java @ 11459

History | View | Annotate | Download (7.36 KB)

1
/* gvSIG. Sistema de Informaci?n Geogr?fica de la Generalitat Valenciana
2
 *
3
 * Copyright (C) 2006 Instituto de Desarrollo Regional 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
 *   Instituto de Desarrollo Regional (Universidad de Castilla La-Mancha)
34
 *   Campus Universitario s/n
35
 *   02071 Alabacete
36
 *   Spain
37
 *
38
 *   +34 967 599 200
39
 */
40

    
41
package es.idr.teledeteccion.connection;
42

    
43
import java.io.BufferedReader;
44
import java.io.File;
45
import java.io.IOException;
46
import java.io.InputStreamReader;
47
import java.sql.Connection;
48
import java.sql.ResultSet;
49
import java.sql.SQLException;
50
import java.sql.Statement;
51
import java.util.logging.Logger;
52

    
53
import org.geotools.referencing.factory.epsg.HSQLDataSource;
54
import org.geotools.referencing.factory.iau2000.FactoryUsingHSQL;
55
import org.hsqldb.jdbc.jdbcDataSource;
56

    
57
/**
58
 * Clase para la conexi?n con la base de datos de hsqldb.
59
 * Establece el driver necesario, as? como la cadena de
60
 * conexi?n a la base de datos de la EPSG y la IAU2000
61
 * 
62
 * @author Jos? Luis G?mez Mart?nez (jolugomar@gmail.com)
63
 *
64
 */
65

    
66
public class EpsgConnection extends jdbcDataSource {
67
        
68
        Connection connect;
69

    
70
        public EpsgConnection() {        
71
        /*        try {
72
                        Class.forName("org.hsqldb.jdbcDriver");
73
                } catch (ClassNotFoundException e) {
74
                        e.printStackTrace();
75
                }*/
76
        }
77
        
78
        public void setConnectionEPSG() {
79
                HSQLDataSource ds = new HSQLDataSource();
80
                                
81
                try {
82
                        connect = ds.getConnection();
83
                } catch (SQLException e) {
84
                        // TODO Auto-generated catch block
85
                        e.printStackTrace();
86
                }
87
        }
88
        
89
        /**
90
         * Establece la conexi?n con la base de datos de la IAU2000
91
         *
92
         */
93
        public void setConnectionIAU2000() {
94
                File directory = new File(System.getProperty("java.io.tmpdir", "."), "Geotools");
95
        if (directory.isDirectory() || directory.mkdir()) {
96
            directory = new File(directory, "Cached databases");
97
            if (directory.isDirectory() || directory.mkdir()) {
98
                /*
99
                 * Constructs the full path to the HSQL database. Note: we do not use
100
                 * File.toURI() because HSQL doesn't seem to expect an encoded URL
101
                 * (e.g. "%20" instead of spaces).
102
                 */
103
                final StringBuffer url = new StringBuffer("jdbc:hsqldb:file:");
104
                final String path = directory.getAbsolutePath().replace(File.separatorChar, '/');
105
                if (path.length()==0 || path.charAt(0)!='/') {
106
                    url.append('/');
107
                }
108
                url.append(path);
109
                if (url.charAt(url.length()-1) != '/') {
110
                    url.append('/');
111
                }
112
                url.append("iau2000");
113
                setDatabase(url.toString());
114
            }
115
            /*
116
             * If the temporary directory do not exists or can't be created,
117
             * lets the 'database' attribute unset. If the user do not set it
118
             * explicitly (for example through JNDI), an exception will be thrown
119
             * when 'getConnection()' will be invoked.
120
             */
121
        }
122
        setUser("sa"); // System administrator. No password.
123
                
124
        try {
125
                connect = super.getConnection();
126
        
127
        } catch (SQLException e1) {
128
                        // TODO Auto-generated catch block
129
                        e1.printStackTrace();
130
                }
131
        }
132
        
133
        /**
134
         * Establece la conexi?n con la base de datos de ESRI
135
         *
136
         */
137
        public void setConnectionEsri() {
138
                setDatabase("jdbc:hsqldb:file:../gt2-esri-hsql/src/org/geotools/referencing/factory/esri/esri");
139
                setUser("sa");
140
                try {
141
                        connect = super.getConnection();
142
                } catch (SQLException e) {
143
                        // TODO Auto-generated catch block
144
                        e.printStackTrace();
145
                }
146
        }
147
        
148
        /**
149
         * Establece la conexi?n con la base de datos de USR
150
         *
151
         */
152
        public void setConnectionUsr() {
153
                setDatabase("jdbc:hsqldb:file:../gt2-usr-hsql/src/org/geotools/referencing/factory/usr/usr");
154
                setUser("sa");
155
                try {
156
                        connect = super.getConnection();
157
                } catch (SQLException e) {
158
                        // TODO Auto-generated catch block
159
                        e.printStackTrace();
160
                }
161
        }
162
        
163
        public Connection getConnection(){
164
                return connect;
165
        }
166
        
167
        public void shutdown() throws SQLException {
168

    
169
        Statement st = connect.createStatement();
170

    
171
        // db writes out to files and performs clean shuts down
172
        // otherwise there will be an unclean shutdown
173
        // when program ends
174
        st.execute("SHUTDOWN");
175
        connect.close();    // if there are no other open connection
176
    }
177
        /*
178
        *//**
179
         * Establece la conexi?n con la base de datos de la EPSG
180
         *
181
         *//*
182
        public void setConnectionEPSG() {
183
                try {                        
184
                        connect =  DriverManager.getConnection("jdbc:hsqldb:file:gvSIG/extensiones/org.gvsig.crs/db_epsg", "sa", "");
185
                } catch (SQLException e1) {
186
                        e1.printStackTrace();
187
                }
188
        }
189
        
190
        *//**
191
         * Establece la conexi?n con la base de datos de la IAU2000
192
         *
193
         *//*
194
        public void setConnectionIAU2000() {
195
                try {                        
196
                        connect =  DriverManager.getConnection("jdbc:hsqldb:file:gvSIG/extensiones/org.gvsig.crs/db_iau2000", "sa", "");                        
197
                } catch (SQLException e1) {
198
                        e1.printStackTrace();
199
                }
200
        }
201
        
202
        *//**
203
         * Establece la conexi?n con la base de datos de ESRI
204
         *
205
         *//*
206
        public void setConnectionEsri() {
207
                try {                        
208
                        connect =  DriverManager.getConnection("jdbc:hsqldb:file:/home/jlgomez/gvSIGF2/_fwAndami/gvSIG/extensiones/org.gvsig.crs/db_esri", "sa", "");                        
209
                } catch (SQLException e1) {
210
                        e1.printStackTrace();
211
                }
212
        }
213
        
214
        *//**
215
         * Establece la conexi?n con la base de datos de USR
216
         *
217
         *//*
218
        public void setConnectionUsr() {
219
                try {                        
220
                        connect =  DriverManager.getConnection("jdbc:hsqldb:file:gvSIG/extensiones/org.gvsig.crs/db_usr", "sa", "");                        
221
                } catch (SQLException e1) {
222
                        e1.printStackTrace();
223
                }
224
        }
225
        
226
        public Connection getConnection(){
227
                return connect;
228
        }
229
        
230
        public void shutdown() throws SQLException {
231

232
        Statement st = connect.createStatement();
233

234
        // db writes out to files and performs clean shuts down
235
        // otherwise there will be an unclean shutdown
236
        // when program ends
237
        st.execute("SHUTDOWN");
238
        connect.close();    // if there are no other open connection
239
    }*/
240
        
241
         /**
242
     * Returns {@code true} if the database contains data. This method returns {@code false}
243
     * if an empty EPSG database has been automatically created by HSQL and not yet populated.
244
     */
245
    private static boolean dataExists(final Connection connection) throws SQLException {
246
        final ResultSet tables = connection.getMetaData().getTables(
247
                null, null, "IAU2000_%", new String[] {"TABLE"});
248
        final boolean exists = tables.next();
249
        tables.close();
250
        return exists;
251
    }
252
}