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 @ 40596
History | View | Annotate | Download (4.37 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 org.gvsig.fmap.dal.exception.DataException; |
28 |
import org.gvsig.fmap.dal.exception.InitializeException; |
29 |
import org.gvsig.fmap.dal.resource.ResourceParameters; |
30 |
import org.gvsig.fmap.dal.resource.exception.AccessResourceException; |
31 |
import org.gvsig.fmap.dal.resource.exception.ResourceException; |
32 |
import org.gvsig.fmap.dal.resource.spi.AbstractNonBlockingResource; |
33 |
|
34 |
/**
|
35 |
* <p>
|
36 |
* Abstract Data Base Resource implementation that allow the concurrent access.
|
37 |
* </p>
|
38 |
*
|
39 |
* <p>
|
40 |
* Useful for Pooled Data Base Access.
|
41 |
* </p>
|
42 |
*
|
43 |
* @author jmvivo
|
44 |
*
|
45 |
*/
|
46 |
public abstract class AbstractDBResourceNoBlocker extends AbstractNonBlockingResource { |
47 |
|
48 |
/**
|
49 |
* Default constructor
|
50 |
*
|
51 |
* @param parameters
|
52 |
* @throws InitializeException
|
53 |
*/
|
54 |
protected AbstractDBResourceNoBlocker(DBResourceParameters parameters)
|
55 |
throws InitializeException {
|
56 |
super(parameters);
|
57 |
} |
58 |
|
59 |
/**
|
60 |
* Return a connection to the data base.<br>
|
61 |
* Connect to the Data Base if is needed
|
62 |
*
|
63 |
* @return connection to the data base
|
64 |
*/
|
65 |
public Object get() throws AccessResourceException { |
66 |
if (!isConnected()) {
|
67 |
try {
|
68 |
this.connect();
|
69 |
} catch (DataException e) {
|
70 |
throw new AccessResourceException(this, e); |
71 |
} |
72 |
} |
73 |
try {
|
74 |
return getTheConnection();
|
75 |
} catch (DataException e) {
|
76 |
throw new AccessResourceException(this, e); |
77 |
} |
78 |
} |
79 |
|
80 |
/**
|
81 |
* Return a connection to the data base.<br>
|
82 |
* Connect to the Data Base if is needed
|
83 |
*
|
84 |
*
|
85 |
* @return connection to the data base
|
86 |
* @see #get()
|
87 |
*/
|
88 |
public Object getConnection() throws AccessResourceException { |
89 |
return get();
|
90 |
} |
91 |
|
92 |
/**
|
93 |
* inform if connection to the data base is established
|
94 |
*
|
95 |
* @return
|
96 |
*/
|
97 |
public abstract boolean isConnected(); |
98 |
|
99 |
/**
|
100 |
* Establish connection to data base
|
101 |
*
|
102 |
* @throws DataException
|
103 |
*/
|
104 |
public final void connect() throws DataException { |
105 |
if (this.isConnected()) { |
106 |
return;
|
107 |
} |
108 |
prepare(); |
109 |
connectToDB(); |
110 |
} |
111 |
|
112 |
/**
|
113 |
* final implementation method to Establish connection to data base<br>
|
114 |
* Called from {@link #get()}<br>
|
115 |
* <br>
|
116 |
*
|
117 |
*
|
118 |
* @throws DataException
|
119 |
*/
|
120 |
protected abstract void connectToDB() throws DataException; |
121 |
|
122 |
/**
|
123 |
* final implementation method to get a connection to data base<br>
|
124 |
* Called from {@link #connect()}<br>
|
125 |
* <br>
|
126 |
* This method is called with the connection establish
|
127 |
*
|
128 |
* @throws DataException
|
129 |
*/
|
130 |
protected abstract Object getTheConnection() throws DataException; |
131 |
|
132 |
/**
|
133 |
* Check if parameters is the same for this resource.<br>
|
134 |
* <br>
|
135 |
*
|
136 |
* <strong>Note:</strong> override this method to add checks for specify
|
137 |
* final implementation parameters
|
138 |
*
|
139 |
*
|
140 |
* @see AbstractResource#isThis(ResourceParameters)
|
141 |
*/
|
142 |
public boolean isThis(ResourceParameters parameters) |
143 |
throws ResourceException {
|
144 |
if (!(parameters instanceof DBResourceParameters)) { |
145 |
return false; |
146 |
} |
147 |
DBResourceParameters params = (DBResourceParameters) parameters |
148 |
.getCopy(); |
149 |
prepare(params); |
150 |
DBResourceParameters myParams = (DBResourceParameters) this
|
151 |
.getParameters(); |
152 |
|
153 |
if (!equals(myParams.getHost(), params.getHost())) {
|
154 |
return false; |
155 |
} |
156 |
if (!equals(myParams.getPort(), params.getPort())) {
|
157 |
return false; |
158 |
} |
159 |
if (!equals(myParams.getUser(), params.getUser())) {
|
160 |
return false; |
161 |
} |
162 |
|
163 |
return true; |
164 |
} |
165 |
|
166 |
@SuppressWarnings("unchecked") |
167 |
protected boolean equals(Comparable v1, Comparable v2) { |
168 |
if (v1 == v2) {
|
169 |
return true; |
170 |
} |
171 |
if ((v1 != null) && (v2 != null)) { |
172 |
return v1.compareTo(v2) == 0; |
173 |
} |
174 |
return false; |
175 |
} |
176 |
|
177 |
|
178 |
} |