Statistics
| Revision:

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 @ 40559

History | View | Annotate | Download (5.31 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.AbstractNonBlockingResource;
59

    
60
/**
61
 * <p>
62
 * Abstract Data Base Resource implementation that allow the concurrent access.
63
 * </p>
64
 * 
65
 * <p>
66
 * Useful for Pooled Data Base Access.
67
 * </p>
68
 * 
69
 * @author jmvivo
70
 *
71
 */
72
public abstract class AbstractDBResourceNoBlocker extends AbstractNonBlockingResource {
73

    
74
        /**
75
         * Default constructor
76
         *
77
         * @param parameters
78
         * @throws InitializeException
79
         */
80
        protected AbstractDBResourceNoBlocker(DBResourceParameters parameters)
81
                        throws InitializeException {
82
                super(parameters);
83
        }
84

    
85
        /**
86
         * Return a connection to the data base.<br>
87
         * Connect to the Data Base if is needed
88
         *
89
         * @return connection to the data base
90
         */
91
        public Object get() throws AccessResourceException {
92
                if (!isConnected()) {
93
                        try {
94
                                this.connect();
95
                        } catch (DataException e) {
96
                                throw new AccessResourceException(this, e);
97
                        }
98
                }
99
                try {
100
                        return getTheConnection();
101
                } catch (DataException e) {
102
                        throw new AccessResourceException(this, e);
103
                }
104
        }
105

    
106
        /**
107
         * Return a connection to the data base.<br>
108
         * Connect to the Data Base if is needed
109
         *
110
         *
111
         * @return connection to the data base
112
         * @see #get()
113
         */
114
        public Object getConnection() throws AccessResourceException {
115
                return get();
116
        }
117

    
118
        /**
119
         * inform if connection to the data base is established
120
         *
121
         * @return
122
         */
123
        public abstract boolean isConnected();
124

    
125
        /**
126
         * Establish connection to data base
127
         *
128
         * @throws DataException
129
         */
130
        public final void connect() throws DataException {
131
                if (this.isConnected()) {
132
                        return;
133
                }
134
                prepare();
135
                connectToDB();
136
        }
137

    
138
        /**
139
         * final implementation method to Establish connection to data base<br>
140
         * Called from {@link #get()}<br>
141
         * <br>
142
         *
143
         *
144
         * @throws DataException
145
         */
146
        protected abstract void connectToDB() throws DataException;
147

    
148
        /**
149
         * final implementation method to get a connection to data base<br>
150
         * Called from {@link #connect()}<br>
151
         * <br>
152
         * This method is called with the connection establish
153
         *
154
         * @throws DataException
155
         */
156
        protected abstract Object getTheConnection() throws DataException;
157

    
158
        /**
159
         * Check if parameters is the same for this resource.<br>
160
         * <br>
161
         *
162
         * <strong>Note:</strong> override this method to add checks for specify
163
         * final implementation parameters
164
         *
165
         *
166
         * @see AbstractResource#isThis(ResourceParameters)
167
         */
168
        public boolean isThis(ResourceParameters parameters)
169
                        throws ResourceException {
170
                if (!(parameters instanceof DBResourceParameters)) {
171
                        return false;
172
                }
173
                DBResourceParameters params = (DBResourceParameters) parameters
174
                                .getCopy();
175
                prepare(params);
176
                DBResourceParameters myParams = (DBResourceParameters) this
177
                                .getParameters();
178

    
179
                if (!equals(myParams.getHost(), params.getHost())) {
180
                        return false;
181
                }
182
                if (!equals(myParams.getPort(), params.getPort())) {
183
                        return false;
184
                }
185
                if (!equals(myParams.getUser(), params.getUser())) {
186
                        return false;
187
                }
188

    
189
                return true;
190
        }
191

    
192
        @SuppressWarnings("unchecked")
193
        protected boolean equals(Comparable v1, Comparable v2) {
194
                if (v1 == v2) {
195
                        return true;
196
                }
197
                if ((v1 != null) && (v2 != null)) {
198
                        return v1.compareTo(v2) == 0;
199
                }
200
                return false;
201
        }
202

    
203

    
204
}