root / trunk / libraries / libFMap_data / src / org / gvsig / data / Resource.java @ 20898
History | View | Annotate | Download (4.86 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.data; |
32 |
|
33 |
import java.util.Date; |
34 |
|
35 |
import org.gvsig.data.exception.CloseException; |
36 |
import org.gvsig.data.exception.DataException; |
37 |
import org.gvsig.data.exception.OpenException; |
38 |
import org.gvsig.data.exception.ResourceChangedException; |
39 |
import org.gvsig.util.observer.Observable; |
40 |
import org.gvsig.util.observer.IObservable; |
41 |
import org.gvsig.util.observer.IObserver; |
42 |
|
43 |
/**
|
44 |
* @author jmvivo
|
45 |
*
|
46 |
*/
|
47 |
public abstract class Resource implements IObservable{ |
48 |
|
49 |
private int referenceCount; |
50 |
private Observable observable = new Observable(); |
51 |
|
52 |
protected Date lastTimeOpen = null; |
53 |
|
54 |
public abstract String description(); |
55 |
|
56 |
public abstract String getName(); |
57 |
|
58 |
protected abstract String generateKey(); |
59 |
public final String getKey(){ |
60 |
if (this.key == null){ |
61 |
this.key = this.generateKey(); |
62 |
} |
63 |
return this.key; |
64 |
|
65 |
} |
66 |
|
67 |
protected abstract void doChanged(); |
68 |
|
69 |
private String key=null; |
70 |
private boolean opened; |
71 |
|
72 |
public final void changed(IDataStore store) { |
73 |
doChanged(); |
74 |
observable.notifyObservers( |
75 |
this,
|
76 |
new ResourceNotification(this,IResourceNotification.CHANGED,store)); |
77 |
} |
78 |
|
79 |
public final void changed() { |
80 |
|
81 |
observable.notifyObservers( |
82 |
this,
|
83 |
new ResourceNotification(this,IResourceNotification.CHANGED)); |
84 |
} |
85 |
|
86 |
public synchronized final void close() throws CloseException{ |
87 |
if (!this.isOpen()) |
88 |
return;
|
89 |
doClose(); |
90 |
observable.notifyObservers( |
91 |
this,
|
92 |
new ResourceNotification(this,IResourceNotification.CLOSED)); |
93 |
|
94 |
} |
95 |
|
96 |
protected final void dispose() throws DataException{ |
97 |
observable.notifyObservers( |
98 |
this,
|
99 |
new ResourceNotification(this,IResourceNotification.BEGIN_DISPOSE)); |
100 |
doDispose(); |
101 |
} |
102 |
|
103 |
protected abstract void doDispose() throws DataException; |
104 |
|
105 |
public Date getLastTimeOpen() { |
106 |
return new Date(this.lastTimeOpen.getTime()); |
107 |
} |
108 |
|
109 |
/**
|
110 |
*
|
111 |
* @return ref count after operation
|
112 |
*/
|
113 |
void incref() {
|
114 |
this.referenceCount++;
|
115 |
} |
116 |
|
117 |
protected synchronized final void open() throws OpenException{ |
118 |
if (this.isOpen()){ |
119 |
return;
|
120 |
} |
121 |
if (doOpen()) {
|
122 |
this.updateLastTimeOpen();
|
123 |
} |
124 |
observable.notifyObservers( |
125 |
this,
|
126 |
new ResourceNotification(this,IResourceNotification.OPENED)); |
127 |
|
128 |
} |
129 |
|
130 |
protected void updateLastTimeOpen(){ |
131 |
if (this.lastTimeOpen == null){ |
132 |
this.lastTimeOpen = new Date(); |
133 |
} |
134 |
this.lastTimeOpen.setTime(System.currentTimeMillis()); |
135 |
} |
136 |
|
137 |
protected abstract boolean doOpen() throws OpenException; |
138 |
|
139 |
/**
|
140 |
*
|
141 |
* @return ref count after operation
|
142 |
*/
|
143 |
void unref() throws DataException{ |
144 |
if (this.referenceCount < 1){ |
145 |
throw new DataException("unref in refs < 1"); |
146 |
} |
147 |
this.referenceCount --;
|
148 |
if (this.referenceCount == 0){ |
149 |
this.close();
|
150 |
} |
151 |
} |
152 |
|
153 |
public final int getRefencesCount(){ |
154 |
return referenceCount;
|
155 |
} |
156 |
|
157 |
|
158 |
/* (non-Javadoc)
|
159 |
* @see org.gvsig.util.observer.IObservable#addObserver(org.gvsig.util.observer.IObserver)
|
160 |
*/
|
161 |
public void addObserver(IObserver o) { |
162 |
this.observable.addObserver(o);
|
163 |
|
164 |
} |
165 |
|
166 |
/* (non-Javadoc)
|
167 |
* @see org.gvsig.util.observer.IObservable#deleteObserver(org.gvsig.util.observer.IObserver)
|
168 |
*/
|
169 |
public void deleteObserver(IObserver o) { |
170 |
this.observable.deleteObserver(o);
|
171 |
} |
172 |
|
173 |
/* (non-Javadoc)
|
174 |
* @see org.gvsig.util.observer.IObservable#deleteObservers()
|
175 |
*/
|
176 |
public void deleteObservers() { |
177 |
this.deleteObservers();
|
178 |
|
179 |
} |
180 |
|
181 |
public void addObservers(Observable ob){ |
182 |
this.observable.addObservers(ob);
|
183 |
} |
184 |
|
185 |
/**
|
186 |
*
|
187 |
*/
|
188 |
void preapare() throws DataException{ |
189 |
try{
|
190 |
observable.notifyObservers( |
191 |
this,
|
192 |
new ResourceNotification(this,IResourceNotification.PREPARE)); |
193 |
} catch (Exception e){ |
194 |
throw new DataException("Perpare exception",e); |
195 |
} |
196 |
} |
197 |
|
198 |
protected synchronized void checkOpen() throws OpenException{ |
199 |
if (!this.isOpen()){ |
200 |
this.open();
|
201 |
} else {
|
202 |
this.checkChanged();
|
203 |
} |
204 |
} |
205 |
|
206 |
protected abstract void checkChanged() throws ResourceChangedException; |
207 |
|
208 |
public boolean isOpen() { |
209 |
return this.opened; |
210 |
} |
211 |
|
212 |
protected void setOpened() { |
213 |
this.opened=true; |
214 |
} |
215 |
|
216 |
protected boolean doClose() throws CloseException { |
217 |
this.opened= false; |
218 |
return true; |
219 |
} |
220 |
} |
221 |
|