Statistics
| Revision:

svn-gvsig-desktop / branches / v2_0_0_prep / libraries / libFMap_dataDB / src / org / gvsig / fmap / data / feature / db / jdbc / JDBCResource.java @ 22373

History | View | Annotate | Download (4.78 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
* 2008 IVER T.I   {{Task}}
26
*/
27

    
28
/**
29
 *
30
 */
31
package org.gvsig.fmap.data.feature.db.jdbc;
32

    
33
import java.sql.Connection;
34

    
35
import org.gvsig.fmap.data.CloseException;
36
import org.gvsig.fmap.data.DataException;
37
import org.gvsig.fmap.data.InitializeException;
38
import org.gvsig.fmap.data.OpenException;
39
import org.gvsig.fmap.data.ReadException;
40
import org.gvsig.fmap.data.Resource;
41
import org.gvsig.fmap.data.ResourceChangedException;
42
import org.gvsig.fmap.data.feature.file.FileStoreParameters;
43

    
44
/**
45
 * @author jmvivo
46
 *
47
 */
48
public abstract class JDBCResource extends Resource {
49
        protected Connection connection = null;
50

    
51
        private String user;
52
        private String url;
53
        private String password;
54

    
55

    
56

    
57
        /* (non-Javadoc)
58
         * @see org.gvsig.fmap.data.Resource#doDispose()
59
         */
60
        protected void doDispose() throws DataException {
61
                this.connection = this.createConnection();
62
                this.setOpened();
63

    
64
        }
65

    
66
        /* (non-Javadoc)
67
         * @see org.gvsig.fmap.data.Resource#doOpen()
68
         */
69
        protected boolean doOpen() throws OpenException {
70
                try {
71
                        this.connection = this.createConnection();
72
                } catch (ReadException e) {
73
                        throw new OpenException(this.getName(),e);
74
                }
75
                this.setOpened();
76
                return true;
77
        }
78

    
79
        public JDBCResource(JDBCStoreParameters params){
80
                this.loadParamsData(params);
81
        }
82

    
83
        public JDBCResource(JDBCExplorerParameter params){
84
                this.loadParamsData(params);
85
        }
86

    
87
        /* (non-Javadoc)
88
         * @see org.gvsig.fmap.data.Resource#doClose()
89
         */
90
        protected boolean doClose() throws CloseException {
91
                try {
92
                        connection.close();
93
                } catch (java.sql.SQLException e) {
94
                        throw new CloseException(this.getName(),e);
95
                }
96
                return super.doClose();
97
        }
98

    
99
        /* (non-Javadoc)
100
         * @see org.gvsig.fmap.data.Resource#getName()
101
         */
102
        public String getName() {
103
                // TODO Auto-generated method stub
104
                return null;
105
        }
106

    
107
        protected void loadParamsData(JDBCStoreParameters params){
108
                this.url = params.getUrl();
109
                this.user = params.getUser();
110
                this.password = params.getPassw();
111
        }
112

    
113
        protected void loadParamsData(JDBCExplorerParameter params){
114
                this.url = params.getUrl();
115
                this.user = params.getUser();
116
                this.password = params.getPassw();
117
        }
118

    
119

    
120
        protected abstract Connection createConnection() throws ReadException;
121

    
122

    
123
        /* (non-Javadoc)
124
         * @see org.gvsig.fmap.data.Resource#description()
125
         */
126
        public String description() {
127
                return this.getName()+" JDBC Connection Resource: url='"+this.getUrl()+"' user='"+this.getUser()+"'";
128
        }
129

    
130
        /* (non-Javadoc)
131
         * @see org.gvsig.fmap.data.Resource#doChanged()
132
         */
133
        protected void doChanged() {
134
                // No Operation
135
        }
136

    
137
        /**
138
         * @return the password
139
         */
140
        public String getPassword() {
141
                return password;
142
        }
143

    
144
        /**
145
         * @param password the password to set
146
         * @throws InitializeException
147
         */
148
        public void setPassword(String password) throws InitializeException {
149
                if (this.getRefencesCount() > 0){
150
                        throw new InitializeException("Resource in use",this.description());
151
                }
152
                this.password = password;
153
        }
154

    
155
        /**
156
         * @return the url
157
         */
158
        public String getUrl() {
159
                return url;
160
        }
161

    
162
        /**
163
         * @param url the url to set
164
         * @throws InitializeException
165
         */
166
        public void setUrl(String url) throws InitializeException {
167
                if (this.getRefencesCount() > 0){
168
                        throw new InitializeException("Resource in use",this.description());
169
                }
170
                this.url = url;
171
        }
172

    
173
        /**
174
         * @return the user
175
         */
176
        public String getUser() {
177
                return user;
178
        }
179

    
180
        /**
181
         * @param user the user to set
182
         * @throws InitializeException
183
         */
184
        public void setUser(String user) throws InitializeException {
185
                if (this.getRefencesCount() > 0){
186
                        throw new InitializeException("Resource in use",this.description());
187
                }
188
                this.user = user;
189
        }
190

    
191
        protected Connection getConnection() throws ReadException{
192
                this.checkOpen();
193
                return this.connection;
194
        }
195

    
196
        protected Connection getWriterConnection() throws ReadException{
197
                return this.createConnection();
198
        }
199

    
200
        /* (non-Javadoc)
201
         * @see org.gvsig.fmap.data.Resource#checkChanged()
202
         */
203
        protected void checkChanged() throws ResourceChangedException {
204
                // NO Operation
205

    
206
        }
207

    
208
}
209