root / branches / v2_0_0_prep / libraries / libFMap_dataDB / src / org / gvsig / fmap / data / feature / db / jdbc / JDBCResource.java @ 23180
History | View | Annotate | Download (5.02 KB)
1 |
/* gvSIG. Geographic Information System of the Valencian Government
|
---|---|
2 |
*
|
3 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department
|
4 |
* of the Valencian Government (CIT)
|
5 |
*
|
6 |
* This program is free software; you can redistribute it and/or
|
7 |
* modify it under the terms of the GNU General Public License
|
8 |
* as published by the Free Software Foundation; either version 2
|
9 |
* of the License, or (at your option) any later version.
|
10 |
*
|
11 |
* This program is distributed in the hope that it will be useful,
|
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
* GNU General Public License for more details.
|
15 |
*
|
16 |
* You should have received a copy of the GNU General Public License
|
17 |
* along with this program; if not, write to the Free Software
|
18 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
19 |
* MA 02110-1301, USA.
|
20 |
*
|
21 |
*/
|
22 |
|
23 |
/*
|
24 |
* AUTHORS (In addition to CIT):
|
25 |
* 2008 IVER T.I {{Task}}
|
26 |
*/
|
27 |
|
28 |
/**
|
29 |
*
|
30 |
*/
|
31 |
package org.gvsig.fmap.data.feature.db.jdbc; |
32 |
|
33 |
import java.sql.Connection; |
34 |
import java.sql.SQLException; |
35 |
|
36 |
import org.gvsig.fmap.data.CloseException; |
37 |
import org.gvsig.fmap.data.DataException; |
38 |
import org.gvsig.fmap.data.InitializeException; |
39 |
import org.gvsig.fmap.data.OpenException; |
40 |
import org.gvsig.fmap.data.ReadException; |
41 |
import org.gvsig.fmap.data.Resource; |
42 |
import org.gvsig.fmap.data.ResourceChangedException; |
43 |
|
44 |
/**
|
45 |
* @author jmvivo
|
46 |
*
|
47 |
*/
|
48 |
public abstract class JDBCResource extends Resource { |
49 |
protected Connection connection = null; |
50 |
|
51 |
private String user; |
52 |
private String url; |
53 |
private String password; |
54 |
|
55 |
|
56 |
|
57 |
/* (non-Javadoc)
|
58 |
* @see org.gvsig.fmap.data.Resource#doDispose()
|
59 |
*/
|
60 |
protected void doDispose() throws DataException { |
61 |
Connection con =this.connection; |
62 |
this.connection =null; |
63 |
if (con != null){ |
64 |
try {
|
65 |
con.close(); |
66 |
} catch (SQLException e) { |
67 |
throw new DataException("Dispose Error",e); |
68 |
} |
69 |
} |
70 |
} |
71 |
|
72 |
/* (non-Javadoc)
|
73 |
* @see org.gvsig.fmap.data.Resource#doOpen()
|
74 |
*/
|
75 |
protected boolean doOpen() throws OpenException { |
76 |
try {
|
77 |
this.connection = this.createConnection(); |
78 |
} catch (ReadException e) {
|
79 |
throw new OpenException(this.getName(),e); |
80 |
} |
81 |
this.setOpened();
|
82 |
return true; |
83 |
} |
84 |
|
85 |
public JDBCResource(JDBCStoreParameters params){
|
86 |
this.loadParamsData(params);
|
87 |
} |
88 |
|
89 |
public JDBCResource(JDBCExplorerParameter params){
|
90 |
this.loadParamsData(params);
|
91 |
} |
92 |
|
93 |
/* (non-Javadoc)
|
94 |
* @see org.gvsig.fmap.data.Resource#doClose()
|
95 |
*/
|
96 |
protected boolean doClose() throws CloseException { |
97 |
try {
|
98 |
connection.close(); |
99 |
} catch (java.sql.SQLException e) {
|
100 |
throw new CloseException(this.getName(),e); |
101 |
} |
102 |
return super.doClose(); |
103 |
} |
104 |
|
105 |
/* (non-Javadoc)
|
106 |
* @see org.gvsig.fmap.data.Resource#getName()
|
107 |
*/
|
108 |
public String getName() { |
109 |
// TODO Auto-generated method stub
|
110 |
return null; |
111 |
} |
112 |
|
113 |
protected void loadParamsData(JDBCStoreParameters params){ |
114 |
this.url = params.getUrl();
|
115 |
this.user = params.getUser();
|
116 |
this.password = params.getPassw();
|
117 |
} |
118 |
|
119 |
protected void loadParamsData(JDBCExplorerParameter params){ |
120 |
this.url = params.getUrl();
|
121 |
this.user = params.getUser();
|
122 |
this.password = params.getPassw();
|
123 |
} |
124 |
|
125 |
|
126 |
protected abstract Connection createConnection() throws ReadException; |
127 |
|
128 |
|
129 |
/* (non-Javadoc)
|
130 |
* @see org.gvsig.fmap.data.Resource#description()
|
131 |
*/
|
132 |
public String description() { |
133 |
return this.getName()+" JDBC Connection Resource: url='"+this.getUrl()+"' user='"+this.getUser()+"'"; |
134 |
} |
135 |
|
136 |
/* (non-Javadoc)
|
137 |
* @see org.gvsig.fmap.data.Resource#doChanged()
|
138 |
*/
|
139 |
protected void doChanged() { |
140 |
// No Operation
|
141 |
} |
142 |
|
143 |
/**
|
144 |
* @return the password
|
145 |
*/
|
146 |
public String getPassword() { |
147 |
return password;
|
148 |
} |
149 |
|
150 |
/**
|
151 |
* @param password the password to set
|
152 |
* @throws InitializeException
|
153 |
*/
|
154 |
public void setPassword(String password) throws InitializeException { |
155 |
if (inUse()){
|
156 |
throw new InitializeException("Resource in use",this.description()); |
157 |
} |
158 |
this.password = password;
|
159 |
} |
160 |
|
161 |
public boolean inUse() { |
162 |
return this.connection !=null; |
163 |
} |
164 |
|
165 |
/**
|
166 |
* @return the url
|
167 |
*/
|
168 |
public String getUrl() { |
169 |
return url;
|
170 |
} |
171 |
|
172 |
/**
|
173 |
* @param url the url to set
|
174 |
* @throws InitializeException
|
175 |
*/
|
176 |
public void setUrl(String url) throws InitializeException { |
177 |
if (inUse()){
|
178 |
throw new InitializeException("Resource in use",this.description()); |
179 |
} |
180 |
this.url = url;
|
181 |
} |
182 |
|
183 |
/**
|
184 |
* @return the user
|
185 |
*/
|
186 |
public String getUser() { |
187 |
return user;
|
188 |
} |
189 |
|
190 |
/**
|
191 |
* @param user the user to set
|
192 |
* @throws InitializeException
|
193 |
*/
|
194 |
public void setUser(String user) throws InitializeException { |
195 |
if (this.getRefencesCount() > 0){ |
196 |
throw new InitializeException("Resource in use",this.description()); |
197 |
} |
198 |
this.user = user;
|
199 |
} |
200 |
|
201 |
protected Connection getConnection() throws ReadException{ |
202 |
this.checkOpen();
|
203 |
return this.connection; |
204 |
} |
205 |
|
206 |
protected Connection getWriterConnection() throws ReadException{ |
207 |
return this.createConnection(); |
208 |
} |
209 |
|
210 |
/* (non-Javadoc)
|
211 |
* @see org.gvsig.fmap.data.Resource#checkChanged()
|
212 |
*/
|
213 |
protected void checkChanged() throws ResourceChangedException { |
214 |
// NO Operation
|
215 |
|
216 |
} |
217 |
|
218 |
public boolean testConnection(){ |
219 |
try{
|
220 |
this.open();
|
221 |
} catch (OpenException e) {
|
222 |
return false; |
223 |
} |
224 |
return true; |
225 |
} |
226 |
} |
227 |
|