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 / AbstractDBResourceBlocker.java @ 40559
History | View | Annotate | Download (5.12 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 |
/* gvSIG. Geographic Information System of the Valencian Government
|
25 |
*
|
26 |
* Copyright (C) 2007-2008 Infrastructures and Transports Department
|
27 |
* of the Valencian Government (CIT)
|
28 |
*
|
29 |
* This program is free software; you can redistribute it and/or
|
30 |
* modify it under the terms of the GNU General Public License
|
31 |
* as published by the Free Software Foundation; either version 2
|
32 |
* of the License, or (at your option) any later version.
|
33 |
*
|
34 |
* This program is distributed in the hope that it will be useful,
|
35 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
36 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
37 |
* GNU General Public License for more details.
|
38 |
*
|
39 |
* You should have received a copy of the GNU General Public License
|
40 |
* along with this program; if not, write to the Free Software
|
41 |
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
42 |
* MA 02110-1301, USA.
|
43 |
*
|
44 |
*/
|
45 |
|
46 |
/*
|
47 |
* AUTHORS (In addition to CIT):
|
48 |
* 2009 IVER T.I {{Task}}
|
49 |
*/
|
50 |
|
51 |
package org.gvsig.fmap.dal.resource.db; |
52 |
|
53 |
import org.gvsig.fmap.dal.exception.DataException; |
54 |
import org.gvsig.fmap.dal.exception.InitializeException; |
55 |
import org.gvsig.fmap.dal.resource.ResourceParameters; |
56 |
import org.gvsig.fmap.dal.resource.exception.AccessResourceException; |
57 |
import org.gvsig.fmap.dal.resource.exception.ResourceException; |
58 |
import org.gvsig.fmap.dal.resource.spi.AbstractResource; |
59 |
|
60 |
/**
|
61 |
* Abstract Data Base Resource implementation that prevents the concurrent
|
62 |
* access.
|
63 |
*
|
64 |
* @author jmvivo
|
65 |
*
|
66 |
*/
|
67 |
public abstract class AbstractDBResourceBlocker extends AbstractResource |
68 |
implements
|
69 |
DBResource { |
70 |
|
71 |
/**
|
72 |
* Default constructor
|
73 |
*
|
74 |
* @param parameters
|
75 |
* @throws InitializeException
|
76 |
*/
|
77 |
protected AbstractDBResourceBlocker(DBResourceParameters parameters)
|
78 |
throws InitializeException {
|
79 |
super(parameters);
|
80 |
} |
81 |
|
82 |
|
83 |
/**
|
84 |
* Return a connection to the data base.<br>
|
85 |
* Connect to the Data Base if is needed
|
86 |
*
|
87 |
* @return connection to the data base
|
88 |
*/
|
89 |
public Object get() throws AccessResourceException { |
90 |
if (!isConnected()) {
|
91 |
try {
|
92 |
this.connect();
|
93 |
} catch (DataException e) {
|
94 |
throw new AccessResourceException(this, e); |
95 |
} |
96 |
} |
97 |
try {
|
98 |
return getTheConnection();
|
99 |
} catch (DataException e) {
|
100 |
throw new AccessResourceException(this, e); |
101 |
} |
102 |
} |
103 |
|
104 |
/**
|
105 |
* Return a connection to the data base.<br>
|
106 |
* Connect to the Data Base if is needed
|
107 |
*
|
108 |
*
|
109 |
* @return connection to the data base
|
110 |
* @see #get()
|
111 |
*/
|
112 |
public Object getConnection() throws AccessResourceException { |
113 |
return get();
|
114 |
} |
115 |
|
116 |
/**
|
117 |
* Establish connection to data base
|
118 |
*
|
119 |
* @throws DataException
|
120 |
*/
|
121 |
public final void connect() throws DataException { |
122 |
if (this.isConnected()) { |
123 |
return;
|
124 |
} |
125 |
prepare(); |
126 |
connectToDB(); |
127 |
} |
128 |
|
129 |
/**
|
130 |
* Check if parameters is the same for this resource.<br>
|
131 |
* <br>
|
132 |
*
|
133 |
* <strong>Note:</strong> override this method to add checks for specify
|
134 |
* final implementation parameters
|
135 |
*
|
136 |
*
|
137 |
* @see AbstractResource#isThis(ResourceParameters)
|
138 |
*/
|
139 |
public boolean isThis(ResourceParameters parameters) |
140 |
throws ResourceException {
|
141 |
if (!(parameters instanceof DBResourceParameters)) { |
142 |
return false; |
143 |
} |
144 |
DBResourceParameters params = (DBResourceParameters) parameters |
145 |
.getCopy(); |
146 |
prepare(params); |
147 |
DBResourceParameters myParams = (DBResourceParameters) this
|
148 |
.getParameters(); |
149 |
|
150 |
if (!equals(myParams.getHost(), params.getHost())) {
|
151 |
return false; |
152 |
} |
153 |
if (!equals(myParams.getPort(), params.getPort())) {
|
154 |
return false; |
155 |
} |
156 |
if (!equals(myParams.getUser(), params.getUser())) {
|
157 |
return false; |
158 |
} |
159 |
|
160 |
return true; |
161 |
} |
162 |
|
163 |
@SuppressWarnings("unchecked") |
164 |
protected boolean equals(Comparable v1, Comparable v2) { |
165 |
if (v1 == v2) {
|
166 |
return true; |
167 |
} |
168 |
if ((v1 != null) && (v2 != null)) { |
169 |
return v1.compareTo(v2) == 0; |
170 |
} |
171 |
return false; |
172 |
} |
173 |
|
174 |
/**
|
175 |
* final implementation method to Establish connection to data base<br>
|
176 |
* Called from {@link #get()}<br>
|
177 |
* <br>
|
178 |
*
|
179 |
*
|
180 |
* @throws DataException
|
181 |
*/
|
182 |
protected abstract void connectToDB() throws DataException; |
183 |
|
184 |
/**
|
185 |
* final implementation method to get a connection to data base<br>
|
186 |
* Called from {@link #connect()}<br>
|
187 |
* <br>
|
188 |
* This method is called with the connection establish
|
189 |
*
|
190 |
* @throws DataException
|
191 |
*/
|
192 |
protected abstract Object getTheConnection() throws DataException; |
193 |
|
194 |
} |