Statistics
| Revision:

root / branches / v2_0_0_prep / libraries / libFMap_data / src / org / gvsig / fmap / data / spi / AbstractResource.java @ 23871

History | View | Annotate | Download (8.22 KB)

1 20580 jmvivo
/* 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 23772 jjdelcerro
package org.gvsig.fmap.data.spi;
32 20580 jmvivo
33 23507 jmvivo
import java.lang.ref.WeakReference;
34
import java.util.ArrayList;
35 20624 jmvivo
import java.util.Date;
36 23507 jmvivo
import java.util.List;
37 20624 jmvivo
38 23754 jjdelcerro
import org.gvsig.fmap.data.DataParameters;
39
import org.gvsig.fmap.data.DataStore;
40
import org.gvsig.fmap.data.Resource;
41
import org.gvsig.fmap.data.ResourceNotification;
42
import org.gvsig.fmap.data.exceptions.CloseException;
43
import org.gvsig.fmap.data.exceptions.DataException;
44
import org.gvsig.fmap.data.exceptions.OpenException;
45 23772 jjdelcerro
import org.gvsig.fmap.data.exceptions.PrepareResourceException;
46 23754 jjdelcerro
import org.gvsig.fmap.data.exceptions.ResourceChangedException;
47 23772 jjdelcerro
import org.gvsig.fmap.data.exceptions.ResourceConsumerNotFoundException;
48 23774 jjdelcerro
import org.gvsig.fmap.data.exceptions.UnreferenceResourceException;
49 23772 jjdelcerro
import org.gvsig.fmap.data.impl.DefaultResourceNotification;
50 23066 jmvivo
import org.gvsig.tools.observer.DefaultObservable;
51
import org.gvsig.tools.observer.Observable;
52
import org.gvsig.tools.observer.Observer;
53 20580 jmvivo
54
/**
55
 * @author jmvivo
56
 *
57
 */
58 23754 jjdelcerro
public abstract class AbstractResource implements Observable, Resource {
59 20580 jmvivo
60 23507 jmvivo
        private List references = new ArrayList();
61 21045 jmvivo
        private DefaultObservable observable = new DefaultObservable();
62 20580 jmvivo
63 22956 jmvivo
        protected Date lastTimeOpen = new Date();
64
        protected Date lastTimeUsed = new Date();
65 20624 jmvivo
66 23616 jmvivo
        protected DataParameters params = null;
67
68 23754 jjdelcerro
        protected AbstractResource(DataParameters params) {
69 23616 jmvivo
                this.params = params;
70
        }
71
72 23754 jjdelcerro
        /* (non-Javadoc)
73
         * @see org.gvsig.fmap.data.Resource#getParameters()
74 23616 jmvivo
         */
75
        public DataParameters getParameters() throws DataException {
76 23754 jjdelcerro
                return this.params.getCopy();
77 23616 jmvivo
        }
78
79 23754 jjdelcerro
        /* (non-Javadoc)
80
         * @see org.gvsig.fmap.data.Resource#description()
81
         */
82
        public abstract String getDescription();
83 20580 jmvivo
84 23754 jjdelcerro
        /* (non-Javadoc)
85
         * @see org.gvsig.fmap.data.Resource#getName()
86
         */
87 20731 jmvivo
        public abstract String getName();
88 20580 jmvivo
89 20731 jmvivo
        protected abstract String generateKey();
90 23754 jjdelcerro
        /* (non-Javadoc)
91
         * @see org.gvsig.fmap.data.Resource#getKey()
92
         */
93 20731 jmvivo
        public final String getKey(){
94
                if (this.key == null){
95
                        this.key = this.generateKey();
96
                }
97
                return this.key;
98
99
        }
100
101 20624 jmvivo
        protected abstract void doChanged();
102 20580 jmvivo
103 20731 jmvivo
        private String key=null;
104 20742 jmvivo
        private boolean opened;
105 20731 jmvivo
106 21045 jmvivo
        public final void changed(DataStore store) {
107 20624 jmvivo
                doChanged();
108
                observable.notifyObservers(
109
                                this,
110 21045 jmvivo
                                new DefaultResourceNotification(this,ResourceNotification.CHANGED,store));
111 20624 jmvivo
        }
112
113 20658 jmvivo
        public final void changed() {
114 20624 jmvivo
115 20580 jmvivo
                observable.notifyObservers(
116
                                this,
117 21045 jmvivo
                                new DefaultResourceNotification(this,ResourceNotification.CHANGED));
118 20580 jmvivo
        }
119
120 23754 jjdelcerro
        /* (non-Javadoc)
121
         * @see org.gvsig.fmap.data.Resource#close()
122
         */
123 20658 jmvivo
        public synchronized final void close() throws CloseException{
124 22415 jmvivo
                if (!this.isOpen()) {
125 20658 jmvivo
                        return;
126 22415 jmvivo
                }
127 23178 vcaballero
                observable.notifyObservers(
128
                                this,
129
                                new DefaultResourceNotification(this,ResourceNotification.BEGIN_CLOSE));
130 20580 jmvivo
                doClose();
131
                observable.notifyObservers(
132
                                this,
133 21045 jmvivo
                                new DefaultResourceNotification(this,ResourceNotification.CLOSED));
134 20580 jmvivo
135
        }
136
137 23772 jjdelcerro
        public final void dispose() throws DataException {
138 20580 jmvivo
                observable.notifyObservers(
139
                                this,
140 21045 jmvivo
                                new DefaultResourceNotification(this,ResourceNotification.BEGIN_DISPOSE));
141 20580 jmvivo
                doDispose();
142 23507 jmvivo
                observable.deleteObservers();
143
                this.references.clear();
144 20580 jmvivo
        }
145
146 20624 jmvivo
        protected abstract void doDispose() throws DataException;
147 20580 jmvivo
148 23754 jjdelcerro
        /* (non-Javadoc)
149
         * @see org.gvsig.fmap.data.Resource#getLastTimeOpen()
150
         */
151 20624 jmvivo
        public Date getLastTimeOpen() {
152
                return new Date(this.lastTimeOpen.getTime());
153 20580 jmvivo
        }
154
155 23754 jjdelcerro
        /* (non-Javadoc)
156
         * @see org.gvsig.fmap.data.Resource#getLastTimeUsed()
157
         */
158 22956 jmvivo
        public Date getLastTimeUsed() {
159
                return new Date(this.lastTimeOpen.getTime());
160
        }
161
162 23772 jjdelcerro
        public void incref(Object consumer) {
163 22956 jmvivo
                this.updateLastTimeUsed();
164 23507 jmvivo
                if (consumer != null) {
165
                        this.references.add(new WeakReference(consumer));
166
                } else {
167
                        this.references.add(null);
168
                }
169
                //                this.referenceCount++;
170 20580 jmvivo
        }
171
172 23820 jjdelcerro
        protected synchronized final void open() throws OpenException,
173
                        ResourceChangedException {
174 22956 jmvivo
                this.updateLastTimeUsed();
175 20624 jmvivo
                if (this.isOpen()){
176
                        return;
177
                }
178 23178 vcaballero
                observable.notifyObservers(
179
                                this,
180
                                new DefaultResourceNotification(this,ResourceNotification.BEGIN_OPEN));
181
182 20624 jmvivo
                if (doOpen()) {
183
                        this.updateLastTimeOpen();
184
                }
185 20580 jmvivo
                observable.notifyObservers(
186
                                this,
187 21045 jmvivo
                                new DefaultResourceNotification(this,ResourceNotification.OPENED));
188 20580 jmvivo
189
        }
190
191 22956 jmvivo
        protected void updateLastTimeUsed() {
192
                if (this.lastTimeUsed == null) {
193
                        this.lastTimeUsed = new Date();
194
                }
195
                this.lastTimeUsed.setTime(System.currentTimeMillis());
196
        }
197
198 20624 jmvivo
        protected void updateLastTimeOpen(){
199
                if (this.lastTimeOpen == null){
200
                        this.lastTimeOpen = new Date();
201
                }
202
                this.lastTimeOpen.setTime(System.currentTimeMillis());
203
        }
204 20580 jmvivo
205 23820 jjdelcerro
        protected abstract boolean doOpen() throws OpenException,
206
                        ResourceChangedException;
207 20624 jmvivo
208 20580 jmvivo
        /**
209 23600 jmvivo
         *
210 23507 jmvivo
         * @param consumer
211 20580 jmvivo
         * @return ref count after operation
212
         */
213 23772 jjdelcerro
        public void unref(Object consumer) throws DataException {
214 23507 jmvivo
                if (this.references.size() < 1) {
215 23774 jjdelcerro
                        throw new UnreferenceResourceException(this.getKey());
216 20624 jmvivo
                }
217 22956 jmvivo
                this.updateLastTimeUsed();
218 23507 jmvivo
219
                this.updateReferences();
220
221
                int index = -1;
222
                int i;
223
                WeakReference ref;
224
                for (i=0;i<this.references.size();i++){
225
                        ref = (WeakReference) this.references.get(i);
226
                        if (ref != null) {
227
                                if (ref.get().equals(consumer)) {
228
                                        index = i;
229
                                        break;
230
                                }
231
232
                        } else if (consumer == null) {
233
                                index = i;
234
                                break;
235
                        }
236
                }
237
                if (index < 0) {
238 23772 jjdelcerro
                        throw new ResourceConsumerNotFoundException(this, consumer);
239 23507 jmvivo
                }
240
                this.references.remove(index);
241
242
243
                if (this.references.size() == 0) {
244 20624 jmvivo
                        this.close();
245
                }
246 20580 jmvivo
        }
247
248
        public final int getRefencesCount(){
249 23507 jmvivo
                this.updateReferences();
250
                return this.references.size();
251 20580 jmvivo
        }
252
253
254
        /* (non-Javadoc)
255 23178 vcaballero
         * @see org.gvsig.util.observer.Observable#addObserver(org.gvsig.tools.observer.Observer)
256 20580 jmvivo
         */
257 21045 jmvivo
        public void addObserver(Observer o) {
258 20580 jmvivo
                this.observable.addObserver(o);
259
260
        }
261
262
        /* (non-Javadoc)
263 23178 vcaballero
         * @see org.gvsig.util.observer.Observable#deleteObserver(org.gvsig.tools.observer.Observer)
264 20580 jmvivo
         */
265 21045 jmvivo
        public void deleteObserver(Observer o) {
266 20580 jmvivo
                this.observable.deleteObserver(o);
267
        }
268
269
        /* (non-Javadoc)
270 21045 jmvivo
         * @see org.gvsig.util.observer.Observable#deleteObservers()
271 20580 jmvivo
         */
272
        public void deleteObservers() {
273
                this.deleteObservers();
274
275
        }
276
277 21045 jmvivo
        public void addObservers(DefaultObservable ob){
278 20580 jmvivo
                this.observable.addObservers(ob);
279
        }
280
281
        /**
282
         *
283
         */
284 23772 jjdelcerro
        public void prepare() throws DataException {
285 20624 jmvivo
                try{
286
                        observable.notifyObservers(
287
                                        this,
288 21045 jmvivo
                                        new DefaultResourceNotification(this,ResourceNotification.PREPARE));
289 20624 jmvivo
                } catch (Exception e){
290 23772 jjdelcerro
                        throw new PrepareResourceException(this, e);
291 20624 jmvivo
                }
292 22956 jmvivo
                this.updateLastTimeUsed();
293 20580 jmvivo
        }
294
295 23774 jjdelcerro
        protected synchronized void checkOpen() throws OpenException,
296
                        ResourceChangedException {
297 22956 jmvivo
                this.updateLastTimeUsed();
298 20742 jmvivo
                if (!this.isOpen()){
299
                        this.open();
300
                } else {
301
                        this.checkChanged();
302
                }
303
        }
304 20580 jmvivo
305 20742 jmvivo
        protected abstract void checkChanged() throws ResourceChangedException;
306
307 23754 jjdelcerro
        /* (non-Javadoc)
308
         * @see org.gvsig.fmap.data.Resource#isOpen()
309
         */
310 20742 jmvivo
        public boolean isOpen() {
311
                return this.opened;
312
        }
313
314
        protected void setOpened() {
315 22956 jmvivo
                this.updateLastTimeUsed();
316 20742 jmvivo
                this.opened=true;
317
        }
318
319
        protected boolean doClose() throws CloseException {
320 22415 jmvivo
                if (!this.isOpen()) {
321
                        return false;
322
                }
323 20742 jmvivo
                this.opened= false;
324
                return true;
325
        }
326 23507 jmvivo
327
        public synchronized void updateReferences() {
328
                List deadRefs = new ArrayList();
329
                WeakReference ref;
330
                int i;
331
                for (i = 0; i < this.references.size(); i++) {
332
                        ref = (WeakReference) this.references.get(i);
333
                        if (ref == null) {
334
                                continue;
335
                        }
336
                        if (ref.get() == null) {
337
                                deadRefs.add(new Integer(i));
338
                        }
339
340
                }
341
                for (i = deadRefs.size(); i > 0; i--) {
342
                        this.references.remove(((Integer) deadRefs.get(i - 1)).intValue());
343
                }
344
        }
345 20580 jmvivo
}