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 / AbstractDBResourceBlocker.java @ 40435

History | View | Annotate | Download (4.18 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.AbstractResource;
36

    
37
/**
38
 * Abstract Data Base Resource implementation that prevents the concurrent
39
 * access.
40
 *
41
 * @author jmvivo
42
 * 
43
 */
44
public abstract class AbstractDBResourceBlocker extends AbstractResource
45
                implements
46
                DBResource {
47

    
48
        /**
49
         * Default constructor
50
         *
51
         * @param parameters
52
         * @throws InitializeException
53
         */
54
        protected AbstractDBResourceBlocker(DBResourceParameters parameters)
55
                        throws InitializeException {
56
                super(parameters);
57
        }
58

    
59

    
60
        /**
61
         * Return a connection to the data base.<br>
62
         * Connect to the Data Base if is needed
63
         *
64
         * @return connection to the data base
65
         */
66
        public Object get() throws AccessResourceException {
67
                if (!isConnected()) {
68
                        try {
69
                                this.connect();
70
                        } catch (DataException e) {
71
                                throw new AccessResourceException(this, e);
72
                        }
73
                }
74
                try {
75
                        return getTheConnection();
76
                } catch (DataException e) {
77
                        throw new AccessResourceException(this, e);
78
                }
79
        }
80

    
81
        /**
82
         * Return a connection to the data base.<br>
83
         * Connect to the Data Base if is needed
84
         *
85
         *
86
         * @return connection to the data base
87
         * @see #get()
88
         */
89
        public Object getConnection() throws AccessResourceException {
90
                return get();
91
        }
92

    
93
        /**
94
         * Establish connection to data base
95
         *
96
         * @throws DataException
97
         */
98
        public final void connect() throws DataException {
99
                if (this.isConnected()) {
100
                        return;
101
                }
102
                prepare();
103
                connectToDB();
104
        }
105

    
106
        /**
107
         * Check if parameters is the same for this resource.<br>
108
         * <br>
109
         *
110
         * <strong>Note:</strong> override this method to add checks for specify
111
         * final implementation parameters
112
         *
113
         *
114
         * @see AbstractResource#isThis(ResourceParameters)
115
         */
116
        public boolean isThis(ResourceParameters parameters)
117
                        throws ResourceException {
118
                if (!(parameters instanceof DBResourceParameters)) {
119
                        return false;
120
                }
121
                DBResourceParameters params = (DBResourceParameters) parameters
122
                                .getCopy();
123
                prepare(params);
124
                DBResourceParameters myParams = (DBResourceParameters) this
125
                                .getParameters();
126

    
127
                if (!equals(myParams.getHost(), params.getHost())) {
128
                        return false;
129
                }
130
                if (!equals(myParams.getPort(), params.getPort())) {
131
                        return false;
132
                }
133
                if (!equals(myParams.getUser(), params.getUser())) {
134
                        return false;
135
                }
136

    
137
                return true;
138
        }
139

    
140
        @SuppressWarnings("unchecked")
141
        protected boolean equals(Comparable v1, Comparable v2) {
142
                if (v1 == v2) {
143
                        return true;
144
                }
145
                if ((v1 != null) && (v2 != null)) {
146
                        return v1.compareTo(v2) == 0;
147
                }
148
                return false;
149
        }
150

    
151
        /**
152
         * final implementation method to Establish connection to data base<br>
153
         * Called from {@link #get()}<br>
154
         * <br>
155
         *
156
         *
157
         * @throws DataException
158
         */
159
        protected abstract void connectToDB() throws DataException;
160

    
161
        /**
162
         * final implementation method to get a connection to data base<br>
163
         * Called from {@link #connect()}<br>
164
         * <br>
165
         * This method is called with the connection establish
166
         *
167
         * @throws DataException
168
         */
169
        protected abstract Object getTheConnection() throws DataException;
170

    
171
}