svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.db / org.gvsig.fmap.dal.db.lib / src / main / java / org / gvsig / fmap / dal / resource / db / AbstractDBResourceNoBlocker.java @ 41437
History | View | Annotate | Download (5.22 KB)
1 |
/**
|
---|---|
2 |
* gvSIG. Desktop Geographic Information System.
|
3 |
*
|
4 |
* Copyright (C) 2007-2013 gvSIG Association.
|
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 3
|
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 |
* For any additional information, do not hesitate to contact us
|
22 |
* at info AT gvsig.com, or visit our website www.gvsig.com.
|
23 |
*/
|
24 |
|
25 |
package org.gvsig.fmap.dal.resource.db; |
26 |
|
27 |
import java.sql.SQLException; |
28 |
import org.gvsig.fmap.dal.exception.DataException; |
29 |
import org.gvsig.fmap.dal.exception.InitializeException; |
30 |
import org.gvsig.fmap.dal.resource.ResourceParameters; |
31 |
import org.gvsig.fmap.dal.resource.exception.AccessResourceException; |
32 |
import org.gvsig.fmap.dal.resource.exception.ResourceException; |
33 |
import org.gvsig.fmap.dal.resource.spi.AbstractNonBlockingResource; |
34 |
|
35 |
/**
|
36 |
* <p>
|
37 |
* Abstract Data Base Resource implementation that allow the concurrent access.
|
38 |
* </p>
|
39 |
*
|
40 |
* <p>
|
41 |
* Useful for Pooled Data Base Access.
|
42 |
* </p>
|
43 |
*
|
44 |
* @author jmvivo
|
45 |
*
|
46 |
*/
|
47 |
public abstract class AbstractDBResourceNoBlocker extends AbstractNonBlockingResource { |
48 |
|
49 |
/**
|
50 |
* Default constructor
|
51 |
*
|
52 |
* @param parameters
|
53 |
* @throws InitializeException
|
54 |
*/
|
55 |
protected AbstractDBResourceNoBlocker(DBResourceParameters parameters)
|
56 |
throws InitializeException {
|
57 |
super(parameters);
|
58 |
} |
59 |
|
60 |
private static class CanConnectException extends AccessResourceException { |
61 |
public CanConnectException(String resource, DataException cause) { |
62 |
super("Can't get a connection to the resource (%(resource))", cause, "_CanConnectException", 0); |
63 |
setValue("resource", resource);
|
64 |
} |
65 |
} |
66 |
private static class CanGetConnectException extends AccessResourceException { |
67 |
public CanGetConnectException(String datasource, DataException cause) { |
68 |
super("Can't get the connection of the resource (%(resource))", cause, "_CanGetConnectionException", 0); |
69 |
setValue("resource", datasource);
|
70 |
} |
71 |
} |
72 |
|
73 |
/**
|
74 |
* Return a connection to the data base.<br>
|
75 |
* Connect to the Data Base if is needed
|
76 |
*
|
77 |
* @return connection to the data base
|
78 |
*/
|
79 |
public Object get() throws AccessResourceException { |
80 |
if (!isConnected()) {
|
81 |
try {
|
82 |
this.connect();
|
83 |
} catch (DataException e) {
|
84 |
throw new CanConnectException(this.toString(), e); |
85 |
} |
86 |
} |
87 |
try {
|
88 |
return getTheConnection();
|
89 |
} catch (DataException e) {
|
90 |
throw new CanGetConnectException(this.toString(), e); |
91 |
} |
92 |
} |
93 |
|
94 |
/**
|
95 |
* Return a connection to the data base.<br>
|
96 |
* Connect to the Data Base if is needed
|
97 |
*
|
98 |
*
|
99 |
* @return connection to the data base
|
100 |
* @see #get()
|
101 |
*/
|
102 |
public Object getConnection() throws AccessResourceException { |
103 |
return get();
|
104 |
} |
105 |
|
106 |
/**
|
107 |
* inform if connection to the data base is established
|
108 |
*
|
109 |
* @return
|
110 |
*/
|
111 |
public abstract boolean isConnected(); |
112 |
|
113 |
/**
|
114 |
* Establish connection to data base
|
115 |
*
|
116 |
* @throws DataException
|
117 |
*/
|
118 |
public final void connect() throws DataException { |
119 |
if (this.isConnected()) { |
120 |
return;
|
121 |
} |
122 |
prepare(); |
123 |
connectToDB(); |
124 |
} |
125 |
|
126 |
/**
|
127 |
* final implementation method to Establish connection to data base<br>
|
128 |
* Called from {@link #get()}<br>
|
129 |
* <br>
|
130 |
*
|
131 |
*
|
132 |
* @throws DataException
|
133 |
*/
|
134 |
protected abstract void connectToDB() throws DataException; |
135 |
|
136 |
/**
|
137 |
* final implementation method to get a connection to data base<br>
|
138 |
* Called from {@link #connect()}<br>
|
139 |
* <br>
|
140 |
* This method is called with the connection establish
|
141 |
*
|
142 |
* @throws DataException
|
143 |
*/
|
144 |
protected abstract Object getTheConnection() throws DataException; |
145 |
|
146 |
/**
|
147 |
* Check if parameters is the same for this resource.<br>
|
148 |
* <br>
|
149 |
*
|
150 |
* <strong>Note:</strong> override this method to add checks for specify
|
151 |
* final implementation parameters
|
152 |
*
|
153 |
*
|
154 |
* @see AbstractResource#isThis(ResourceParameters)
|
155 |
*/
|
156 |
public boolean isThis(ResourceParameters parameters) |
157 |
throws ResourceException {
|
158 |
if (!(parameters instanceof DBResourceParameters)) { |
159 |
return false; |
160 |
} |
161 |
DBResourceParameters params = (DBResourceParameters) parameters |
162 |
.getCopy(); |
163 |
prepare(params); |
164 |
DBResourceParameters myParams = (DBResourceParameters) this
|
165 |
.getParameters(); |
166 |
|
167 |
if (!equals(myParams.getHost(), params.getHost())) {
|
168 |
return false; |
169 |
} |
170 |
if (!equals(myParams.getPort(), params.getPort())) {
|
171 |
return false; |
172 |
} |
173 |
if (!equals(myParams.getUser(), params.getUser())) {
|
174 |
return false; |
175 |
} |
176 |
if (!equals(myParams.getPassword(), params.getPassword())) {
|
177 |
return false; |
178 |
} |
179 |
return true; |
180 |
} |
181 |
|
182 |
@SuppressWarnings("unchecked") |
183 |
protected boolean equals(Comparable v1, Comparable v2) { |
184 |
if (v1 == v2) {
|
185 |
return true; |
186 |
} |
187 |
if ((v1 != null) && (v2 != null)) { |
188 |
return v1.compareTo(v2) == 0; |
189 |
} |
190 |
return false; |
191 |
} |
192 |
|
193 |
|
194 |
} |