Revision 229 org.gvsig.projection.jcrs/trunk/org.gvsig.projection.jcrs/org.gvsig.projection.jcrs.lib/src/main/java/es/idr/teledeteccion/connection/EpsgConnection.java

View differences:

EpsgConnection.java
16 16
 * along with this program; if not, write to the Free Software
17 17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307,USA.
18 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 19
 */
40

  
41 20
package es.idr.teledeteccion.connection;
42 21

  
43 22
import java.sql.Connection;
......
56 35
 * Clase para la conexi?n con la base de datos de hsqldb.
57 36
 * Establece el driver necesario, as? como la cadena de
58 37
 * conexi?n a la base de datos de la EPSG y la IAU2000
59
 * 
38
 *
60 39
 * @author Jos? Luis G?mez Mart?nez (jolugomar@gmail.com)
61 40
 *
62 41
 */
63

  
64 42
public class EpsgConnection extends jdbcDataSource {
65 43

  
66
	private static final Logger logger = LoggerFactory.getLogger(EpsgConnection.class);
67
	Connection connect;
44
    private static final Logger logger = LoggerFactory.getLogger(EpsgConnection.class);
45
    
46
    Connection connect = null;
47
    String connectionType = null;
68 48

  
69
	public EpsgConnection() {	
70
	/*	try {
71
			Class.forName("org.hsqldb.jdbcDriver");
72
		} catch (ClassNotFoundException e) {
73
			logger.error("Can't create EpsgConnection",e); 
74
		}*/
75
	}
76
	
77
	public void setConnectionEPSG() {
78
		HSQLDataSource ds = new HSQLDataSource();
79
				
80
		try {
81
			connect = ds.getConnection();
82
		} catch (SQLException e) {
83
			logger.error("Cant get conection from HSQLDataSource.", e);
84
		}
85
	}
86
	
87
	/**
88
	 * Establece la conexi?n con la base de datos de la IAU2000
89
	 *
90
	 */
91
	public void setConnectionIAU2000() {
92
		String db = "jdbc:hsqldb:file:" + CrsFactory.getDataBaseFolder().getAbsolutePath() + "/iau2000";
93
		setDatabase(db);
94
		setUser("sa");
95
		try {
96
			connect = super.getConnection();
97
		} catch (SQLException e) {
98
			logger.error("Can't get conection from database '"+db+"'.", e);
99
		}
100
	}
101
	
102
	/**
103
	 * Establece la conexi?n con la base de datos de ESRI
104
	 *
105
	 */
106
	public void setConnectionEsri() {
107
		String db = "jdbc:hsqldb:file:" + CrsFactory.getDataBaseFolder().getAbsolutePath() + "/esri";
108
		setDatabase(db);
109
		setUser("sa");
110
		try {
111
			connect = super.getConnection();
112
		} catch (SQLException e) {
113
			logger.error("Can't get conection from database '"+db+"'.",e);
114
		}
115
	}
116
	
117
	  
118
	/**
49
    public EpsgConnection() {
50
        /*	try {
51
         Class.forName("org.hsqldb.jdbcDriver");
52
         } catch (ClassNotFoundException e) {
53
         logger.error("Can't create EpsgConnection",e); 
54
         }*/
55
    }
56

  
57
    private void setConnection(Connection connection, String type) {
58
        this.connect = connection;
59
        this.connectionType = type;
60
        logger.debug("Created connection to '"+connectionType+"'.");
61
    }
62

  
63
    public void setConnectionEPSG() {
64
        HSQLDataSource ds = new HSQLDataSource();
65

  
66
        try {
67
            setConnection(ds.getConnection(), "EPSG");
68
        } catch (SQLException e) {
69
            logger.warn("Cant get conection from HSQLDataSource.", e);
70
        }
71
    }
72

  
73
    /**
74
     * Establece la conexi?n con la base de datos de la IAU2000
75
     *
76
     */
77
    public void setConnectionIAU2000() {
78
        String db = "jdbc:hsqldb:file:" + CrsFactory.getDataBaseFolder().getAbsolutePath() + "/iau2000";
79
        setDatabase(db);
80
        setUser("sa");
81
        try {
82
            setConnection(super.getConnection(),"IAU2000");
83
        } catch (SQLException e) {
84
            logger.warn("Can't get conection from database '" + db + "'.", e);
85
        }
86
    }
87

  
88
    /**
89
     * Establece la conexi?n con la base de datos de ESRI
90
     *
91
     */
92
    public void setConnectionEsri() {
93
        String db = "jdbc:hsqldb:file:" + CrsFactory.getDataBaseFolder().getAbsolutePath() + "/esri";
94
        setDatabase(db);
95
        setUser("sa");
96
        try {
97
            setConnection(super.getConnection(),"Esri");
98
        } catch (SQLException e) {
99
            logger.warn("Can't get conection from database '" + db + "'.", e);
100
        }
101
    }
102

  
103
    /**
119 104
     * Sets connection for CRS:* coordinate reference systems
120 105
     * such as CRS:84
121 106
     *
122 107
     */
123 108
    public void setConnectionNoAuth() {
124
		String db = "jdbc:hsqldb:file:" + CrsFactory.getDataBaseFolder().getAbsolutePath() + "/noauth";
109
        String db = "jdbc:hsqldb:file:" + CrsFactory.getDataBaseFolder().getAbsolutePath() + "/noauth";
125 110
        setDatabase(db);
126 111
        setUser("sa");
127 112
        try {
128
            connect = super.getConnection();
113
            setConnection(super.getConnection(),"NoAuth");
129 114
        } catch (SQLException e) {
130
			logger.error("Can't get conection from database '"+db+"'.",e);
115
            logger.warn("Can't get conection from database '" + db + "'.", e);
131 116
        }
132 117
    }
133
	
134
	/**
135
	 * Establece la conexi?n con la base de datos de USR
136
	 *
137
	 */
138
	public void setConnectionUsr() {
139
		String db = "jdbc:hsqldb:file:" + CrsFactory.getDataBaseFolder().getAbsolutePath() + "/usr";
140
		setDatabase(db);
141
		setUser("sa");
142
		try {
143
			connect = super.getConnection();
144
		} catch (SQLException e) {
145
			logger.error("Can't get conection from database '"+db+"'.",e);
146
		}
147
	}
148
	
149
	public Connection getConnection(){
150
		return connect;
151
	}
152
	
153
	public void shutdown() throws SQLException {
154 118

  
119
    /**
120
     * Establece la conexi?n con la base de datos de USR
121
     *
122
     */
123
    public void setConnectionUsr() {
124
        String db = "jdbc:hsqldb:file:" + CrsFactory.getDataBaseFolder().getAbsolutePath() + "/usr";
125
        setDatabase(db);
126
        setUser("sa");
127
        try {
128
            setConnection(super.getConnection(),"Usr");
129
        } catch (SQLException e) {
130
            logger.warn("Can't get conection from database '" + db + "'.", e);
131
        }
132
    }
133

  
134
    public Connection getConnection() {
135
        return connect;
136
    }
137

  
138
    public void shutdown() throws SQLException {
139

  
155 140
        Statement st = connect.createStatement();
156 141

  
157
        // db writes out to files and performs clean shuts down
142
            // db writes out to files and performs clean shuts down
158 143
        // otherwise there will be an unclean shutdown
159 144
        // when program ends
160 145
        st.execute("SHUTDOWN");
161 146
        connect.close();    // if there are no other open connection
147
        logger.debug("Shutdown connection to '"+connectionType+"' database.");
162 148
    }
163
	
164
	public synchronized void update(String expression) throws SQLException {
165 149

  
166
        Statement st = null;
150
    public synchronized void update(String expression) throws SQLException {
167 151

  
168
        st = connect.createStatement();    // statements
169

  
170
        int i = st.executeUpdate(expression);    // run the query
171

  
172
        if (i == -1) {
173
            System.out.println("db error : " + expression);
152
        Statement st = null;
153
        st = connect.createStatement();
154
        int i = st.executeUpdate(expression);
155
        if ( i == -1 ) {
156
            logger.warn("db("+connectionType+")  error in statement: " + expression);
174 157
        }
175

  
176 158
        st.close();
177 159
    }
178
	/*
179
	*//**
180
	 * Establece la conexi?n con la base de datos de la EPSG
181
	 *
182
	 *//*
183
	public void setConnectionEPSG() {
184
		try {			
185
			connect =  DriverManager.getConnection("jdbc:hsqldb:file:gvSIG/extensiones/org.gvsig.crs.extension/db_epsg", "sa", "");
186
		} catch (SQLException e1) {
187
			logger....
188
		}
189
	}
190
	
191
	*//**
192
	 * Establece la conexi?n con la base de datos de la IAU2000
193
	 *
194
	 *//*
195
	public void setConnectionIAU2000() {
196
		try {			
197
			connect =  DriverManager.getConnection("jdbc:hsqldb:file:gvSIG/extensiones/org.gvsig.crs.extension/db_iau2000", "sa", "");			
198
		} catch (SQLException e1) {
199
			logger....
200
		}
201
	}
202
	
203
	*//**
204
	 * Establece la conexi?n con la base de datos de ESRI
205
	 *
206
	 *//*
207
	public void setConnectionEsri() {
208
		try {			
209
			connect =  DriverManager.getConnection("jdbc:hsqldb:file:/home/jlgomez/gvSIGF2/_fwAndami/gvSIG/extensiones/org.gvsig.crs.extension/db_esri", "sa", "");			
210
		} catch (SQLException e1) {
211
			logger....
212
		}
213
	}
214
	
215
	*//**
216
	 * Establece la conexi?n con la base de datos de USR
217
	 *
218
	 *//*
219
	public void setConnectionUsr() {
220
		try {			
221
			connect =  DriverManager.getConnection("jdbc:hsqldb:file:gvSIG/extensiones/org.gvsig.crs.extension/db_usr", "sa", "");			
222
		} catch (SQLException e1) {
223
			logger....
224
		}
225
	}
226
	
227
	public Connection getConnection(){
228
		return connect;
229
	}
230
	
231
	public void shutdown() throws SQLException {
232

  
233
        Statement st = connect.createStatement();
234

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

Also available in: Unified diff