root / branches / v2_0_0_prep / libraries / libFMap_dal / src / org / gvsig / fmap / dal / resource / spi / AbstractResource.java @ 24496
History | View | Annotate | Download (7.33 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.dal.resource.spi; |
32 |
|
33 |
import java.lang.ref.WeakReference; |
34 |
import java.util.ArrayList; |
35 |
import java.util.Date; |
36 |
import java.util.Iterator; |
37 |
import java.util.List; |
38 |
|
39 |
import org.gvsig.fmap.dal.exceptions.CopyParametersException; |
40 |
import org.gvsig.fmap.dal.exceptions.InitializeException; |
41 |
import org.gvsig.fmap.dal.resource.ResourceNotification; |
42 |
import org.gvsig.fmap.dal.resource.ResourceParameters; |
43 |
import org.gvsig.fmap.dal.resource.exception.AccessResourceException; |
44 |
import org.gvsig.fmap.dal.resource.exception.PrepareResourceException; |
45 |
import org.gvsig.fmap.dal.resource.exception.ResourceBeginException; |
46 |
import org.gvsig.fmap.dal.resource.exception.ResourceException; |
47 |
import org.gvsig.fmap.dal.resource.exception.ResourceNotifyChangesException; |
48 |
import org.gvsig.fmap.dal.resource.exception.ResourceNotifyCloseException; |
49 |
import org.gvsig.fmap.dal.resource.exception.ResourceNotifyDisposeException; |
50 |
import org.gvsig.fmap.dal.resource.exception.ResourceNotifyOpenException; |
51 |
import org.gvsig.fmap.dal.resource.impl.DefaultResourceNotification; |
52 |
import org.gvsig.tools.observer.Observer; |
53 |
import org.gvsig.tools.observer.WeakReferencingObservable; |
54 |
import org.gvsig.tools.observer.impl.BaseWeakReferencingObservable; |
55 |
import org.gvsig.tools.observer.impl.DelegateWeakReferencingObservable; |
56 |
|
57 |
public abstract class AbstractResource implements ResourceProvider, WeakReferencingObservable { |
58 |
|
59 |
private DelegateWeakReferencingObservable delegateObservable;
|
60 |
|
61 |
private List consumers; |
62 |
|
63 |
private Date lastTimeOpen; |
64 |
private Date lastTimeUsed; |
65 |
|
66 |
private boolean inUse; |
67 |
private Object data; |
68 |
|
69 |
private int openCount; |
70 |
|
71 |
private Object semaforo; |
72 |
private ResourceParameters parameters;
|
73 |
private ResourceParameters preparedParameters;
|
74 |
|
75 |
protected AbstractResource(ResourceParameters parameters)
|
76 |
throws InitializeException {
|
77 |
try {
|
78 |
delegateObservable = new DelegateWeakReferencingObservable(this); |
79 |
consumers = new ArrayList(); |
80 |
lastTimeOpen = new Date(); |
81 |
lastTimeUsed = new Date(); |
82 |
inUse = false;
|
83 |
openCount = 0;
|
84 |
semaforo = new Object(); |
85 |
preparedParameters = null;
|
86 |
this.parameters = (ResourceParameters) parameters.getCopy();
|
87 |
} catch (CopyParametersException e) {
|
88 |
throw new InitializeException(e); |
89 |
} |
90 |
} |
91 |
|
92 |
final private void updateLastTimeUsed() { |
93 |
this.lastTimeUsed.setTime(System.currentTimeMillis()); |
94 |
} |
95 |
|
96 |
final private void updateLastTimeOpen() { |
97 |
this.lastTimeOpen.setTime(System.currentTimeMillis()); |
98 |
this.updateLastTimeUsed();
|
99 |
} |
100 |
|
101 |
final public Date getLastTimeOpen() { |
102 |
return new Date(this.lastTimeOpen.getTime()); |
103 |
} |
104 |
|
105 |
final public Date getLastTimeUsed() { |
106 |
return new Date(this.lastTimeOpen.getTime()); |
107 |
} |
108 |
|
109 |
public final ResourceParameters getParameters() { |
110 |
if (preparedParameters != null) { |
111 |
return preparedParameters;
|
112 |
} |
113 |
return this.parameters; |
114 |
} |
115 |
|
116 |
public void prepare(ResourceParameters params) |
117 |
throws PrepareResourceException {
|
118 |
ResourceNotification notification = new DefaultResourceNotification(
|
119 |
this, ResourceNotification.PREPARE, params);
|
120 |
this.delegateObservable.notifyObservers(notification);
|
121 |
} |
122 |
|
123 |
public void prepare() throws PrepareResourceException { |
124 |
if (preparedParameters == null) { |
125 |
try {
|
126 |
ResourceParameters params = (ResourceParameters) parameters |
127 |
.getCopy(); |
128 |
prepare(params); |
129 |
preparedParameters = params; |
130 |
} catch (CopyParametersException e) {
|
131 |
throw new PrepareResourceException(this, e); |
132 |
} |
133 |
} |
134 |
|
135 |
} |
136 |
|
137 |
public final void begin() throws ResourceBeginException { |
138 |
synchronized (semaforo) {
|
139 |
if (inUse) {
|
140 |
try {
|
141 |
semaforo.wait(); |
142 |
} catch (InterruptedException e) { |
143 |
throw new ResourceBeginException(this, e); |
144 |
} |
145 |
} |
146 |
inUse = true;
|
147 |
updateLastTimeUsed(); |
148 |
|
149 |
} |
150 |
} |
151 |
|
152 |
public final void end() { |
153 |
synchronized (semaforo) {
|
154 |
if (!inUse) {
|
155 |
return;
|
156 |
} |
157 |
updateLastTimeUsed(); |
158 |
inUse = false;
|
159 |
semaforo.notifyAll(); |
160 |
} |
161 |
} |
162 |
|
163 |
public final boolean inUse() { |
164 |
return inUse;
|
165 |
} |
166 |
|
167 |
public void notifyOpen() throws ResourceNotifyOpenException { |
168 |
this.notifyObserver(ResourceNotification.OPEN);
|
169 |
updateLastTimeOpen(); |
170 |
openCount++; |
171 |
} |
172 |
|
173 |
public void notifyClose() throws ResourceNotifyCloseException { |
174 |
if (openCount <= 0) { |
175 |
throw new IllegalStateException(); |
176 |
} |
177 |
this.notifyObserver(ResourceNotification.CLOSE);
|
178 |
openCount--; |
179 |
} |
180 |
|
181 |
public void notifyChanges() throws ResourceNotifyChangesException { |
182 |
this.notifyObserver(ResourceNotification.CHANGED);
|
183 |
} |
184 |
|
185 |
public void notifyDispose() throws ResourceNotifyDisposeException { |
186 |
this.notifyObserver(ResourceNotification.DISPOSE);
|
187 |
|
188 |
consumers.clear(); |
189 |
consumers = null;
|
190 |
|
191 |
lastTimeOpen = null;
|
192 |
lastTimeUsed = null;
|
193 |
|
194 |
data = null;
|
195 |
|
196 |
semaforo = null;
|
197 |
|
198 |
delegateObservable.deleteObservers(); |
199 |
delegateObservable = null;
|
200 |
} |
201 |
|
202 |
public boolean isOpen() { |
203 |
return this.openCount > 0; |
204 |
} |
205 |
|
206 |
public int openCount() { |
207 |
return this.openCount; |
208 |
} |
209 |
|
210 |
public void addObserver(Observer o) { |
211 |
this.delegateObservable.addObserver(o);
|
212 |
} |
213 |
|
214 |
public void deleteObserver(Observer o) { |
215 |
this.delegateObservable.deleteObserver(o);
|
216 |
} |
217 |
|
218 |
public void deleteObservers() { |
219 |
this.delegateObservable.deleteObservers();
|
220 |
|
221 |
} |
222 |
|
223 |
public final void addObservers(BaseWeakReferencingObservable observers) { |
224 |
this.delegateObservable.addObservers(observers);
|
225 |
} |
226 |
|
227 |
public final void addConsumer(ResourceConsumer consumer) { |
228 |
this.updateConsumersList();
|
229 |
consumers.add(new WeakReference(consumer)); |
230 |
} |
231 |
|
232 |
public final void removeConsumer(ResourceConsumer consumer) { |
233 |
this.updateConsumersList();
|
234 |
consumers.remove(consumer); |
235 |
} |
236 |
|
237 |
public int getConsumersCount() { |
238 |
this.updateConsumersList();
|
239 |
return consumers.size();
|
240 |
} |
241 |
|
242 |
private synchronized void updateConsumersList() { |
243 |
Iterator it = consumers.iterator();
|
244 |
WeakReference ref;
|
245 |
while (it.hasNext()) {
|
246 |
ref = (WeakReference) it.next();
|
247 |
if (ref.get() == null) { |
248 |
it.remove(); |
249 |
} |
250 |
} |
251 |
} |
252 |
|
253 |
public void closeRequest() throws ResourceException { |
254 |
if (inUse) {
|
255 |
return;
|
256 |
} |
257 |
Iterator it = consumers.iterator();
|
258 |
while (it.hasNext()) {
|
259 |
ResourceConsumer consumer = (ResourceConsumer) ((WeakReference) it
|
260 |
.next()).get(); |
261 |
if (consumer != null) { |
262 |
consumer.closeResourceRequested(this);
|
263 |
} |
264 |
} |
265 |
} |
266 |
|
267 |
public void setData(Object data) { |
268 |
this.data = data;
|
269 |
} |
270 |
|
271 |
public Object getData() { |
272 |
return this.data; |
273 |
} |
274 |
|
275 |
protected void notifyObserver(String type) { |
276 |
this.delegateObservable
|
277 |
.notifyObservers(new DefaultResourceNotification(
|
278 |
this, type));
|
279 |
} |
280 |
|
281 |
public abstract String getName() throws AccessResourceException; |
282 |
|
283 |
public abstract Object get() throws AccessResourceException; |
284 |
|
285 |
|
286 |
} |
287 |
|