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