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