svn-gvsig-desktop / trunk / org.gvsig.desktop / org.gvsig.desktop.compat.cdc / org.gvsig.fmap.dal / org.gvsig.fmap.dal.spi / src / main / java / org / gvsig / fmap / dal / resource / spi / AbstractResource.java @ 40435
History | View | Annotate | Download (8.71 KB)
1 | 40435 | jjdelcerro | /* 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 | /**
|
||
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.Iterator; |
||
36 | import java.util.List; |
||
37 | |||
38 | import org.gvsig.fmap.dal.exception.CopyParametersException; |
||
39 | import org.gvsig.fmap.dal.exception.InitializeException; |
||
40 | import org.gvsig.fmap.dal.resource.Resource; |
||
41 | import org.gvsig.fmap.dal.resource.ResourceAction; |
||
42 | import org.gvsig.fmap.dal.resource.ResourceNotification; |
||
43 | import org.gvsig.fmap.dal.resource.ResourceParameters; |
||
44 | import org.gvsig.fmap.dal.resource.exception.AccessResourceException; |
||
45 | import org.gvsig.fmap.dal.resource.exception.PrepareResourceException; |
||
46 | import org.gvsig.fmap.dal.resource.exception.ResourceException; |
||
47 | import org.gvsig.fmap.dal.resource.exception.ResourceExecuteException; |
||
48 | import org.gvsig.fmap.dal.resource.exception.ResourceNotifyChangesException; |
||
49 | import org.gvsig.fmap.dal.resource.exception.ResourceNotifyCloseException; |
||
50 | import org.gvsig.fmap.dal.resource.exception.ResourceNotifyDisposeException; |
||
51 | import org.gvsig.fmap.dal.resource.exception.ResourceNotifyOpenException; |
||
52 | import org.gvsig.fmap.dal.resource.impl.DefaultResourceNotification; |
||
53 | import org.gvsig.tools.observer.Observer; |
||
54 | import org.gvsig.tools.observer.WeakReferencingObservable; |
||
55 | import org.gvsig.tools.observer.impl.BaseWeakReferencingObservable; |
||
56 | import org.gvsig.tools.observer.impl.DelegateWeakReferencingObservable; |
||
57 | |||
58 | /**
|
||
59 | * <p>
|
||
60 | * Base implementation for Resource
|
||
61 | * </p>
|
||
62 | *
|
||
63 | * <p>
|
||
64 | * This implementation not define the {@link Resource#begin()} and
|
||
65 | * {@link Resource#end()}
|
||
66 | * </p>
|
||
67 | *
|
||
68 | * @author jmvivo
|
||
69 | *
|
||
70 | */
|
||
71 | public abstract class AbstractResource implements ResourceProvider, |
||
72 | WeakReferencingObservable { |
||
73 | |||
74 | private DelegateWeakReferencingObservable delegateObservable;
|
||
75 | |||
76 | private List consumers; |
||
77 | |||
78 | private long lastTimeOpen; |
||
79 | private long lastTimeUsed; |
||
80 | |||
81 | private ResourceParameters parameters;
|
||
82 | private ResourceParameters preparedParameters;
|
||
83 | |||
84 | private Object data; |
||
85 | |||
86 | private int openCount; |
||
87 | |||
88 | /**
|
||
89 | * Number of times an execute is being simultaneously performed with this
|
||
90 | * resource.
|
||
91 | */
|
||
92 | private int executeCount = 0; |
||
93 | |||
94 | protected final Object lock; |
||
95 | |||
96 | protected AbstractResource(ResourceParameters parameters)
|
||
97 | throws InitializeException {
|
||
98 | consumers = new ArrayList(); |
||
99 | lastTimeOpen = System.currentTimeMillis();
|
||
100 | lastTimeUsed = lastTimeOpen; |
||
101 | |||
102 | openCount = 0;
|
||
103 | |||
104 | preparedParameters = null;
|
||
105 | delegateObservable = new DelegateWeakReferencingObservable(this); |
||
106 | lock = new Object(); |
||
107 | try {
|
||
108 | this.parameters = (ResourceParameters) parameters.getCopy();
|
||
109 | } catch (CopyParametersException e) {
|
||
110 | throw new InitializeException(e); |
||
111 | } |
||
112 | |||
113 | } |
||
114 | |||
115 | protected final void updateLastTimeUsed() { |
||
116 | lastTimeUsed = System.currentTimeMillis();
|
||
117 | } |
||
118 | |||
119 | protected final void updateLastTimeOpen() { |
||
120 | lastTimeOpen = System.currentTimeMillis();
|
||
121 | lastTimeUsed = lastTimeOpen; |
||
122 | } |
||
123 | |||
124 | public final long getLastTimeOpen() { |
||
125 | return lastTimeOpen;
|
||
126 | } |
||
127 | |||
128 | public final long getLastTimeUsed() { |
||
129 | return lastTimeUsed;
|
||
130 | } |
||
131 | |||
132 | public final ResourceParameters getParameters() { |
||
133 | if (preparedParameters != null) { |
||
134 | return preparedParameters;
|
||
135 | } |
||
136 | return this.parameters; |
||
137 | } |
||
138 | |||
139 | public void prepare(ResourceParameters params) |
||
140 | throws PrepareResourceException {
|
||
141 | ResourceNotification notification = |
||
142 | new DefaultResourceNotification(this, |
||
143 | ResourceNotification.PREPARE, params); |
||
144 | this.delegateObservable.notifyObservers(notification);
|
||
145 | } |
||
146 | |||
147 | public void prepare() throws PrepareResourceException { |
||
148 | if (preparedParameters == null) { |
||
149 | try {
|
||
150 | ResourceParameters params = |
||
151 | (ResourceParameters) parameters.getCopy(); |
||
152 | prepare(params); |
||
153 | preparedParameters = params; |
||
154 | } catch (CopyParametersException e) {
|
||
155 | throw new PrepareResourceException(this, e); |
||
156 | } |
||
157 | } |
||
158 | |||
159 | } |
||
160 | |||
161 | public void notifyOpen() throws ResourceNotifyOpenException { |
||
162 | this.notifyObserver(ResourceNotification.OPEN);
|
||
163 | updateLastTimeOpen(); |
||
164 | openCount++; |
||
165 | } |
||
166 | |||
167 | public void notifyClose() throws ResourceNotifyCloseException { |
||
168 | if (openCount <= 0) { |
||
169 | throw new IllegalStateException(); |
||
170 | } |
||
171 | this.notifyObserver(ResourceNotification.CLOSE);
|
||
172 | openCount--; |
||
173 | } |
||
174 | |||
175 | public void notifyChanges() throws ResourceNotifyChangesException { |
||
176 | this.notifyObserver(ResourceNotification.CHANGED);
|
||
177 | |||
178 | Iterator it = consumers.iterator();
|
||
179 | while (it.hasNext()) {
|
||
180 | ResourceConsumer consumer = |
||
181 | (ResourceConsumer) ((WeakReference) it.next()).get();
|
||
182 | if (consumer != null) { |
||
183 | consumer.resourceChanged(this);
|
||
184 | } else {
|
||
185 | it.remove(); |
||
186 | } |
||
187 | } |
||
188 | } |
||
189 | |||
190 | public boolean isOpen() { |
||
191 | return this.openCount > 0; |
||
192 | } |
||
193 | |||
194 | public int openCount() { |
||
195 | return this.openCount; |
||
196 | } |
||
197 | |||
198 | public void addObserver(Observer o) { |
||
199 | this.delegateObservable.addObserver(o);
|
||
200 | } |
||
201 | |||
202 | public void deleteObserver(Observer o) { |
||
203 | this.delegateObservable.deleteObserver(o);
|
||
204 | } |
||
205 | |||
206 | public void deleteObservers() { |
||
207 | this.delegateObservable.deleteObservers();
|
||
208 | |||
209 | } |
||
210 | |||
211 | public final void addObservers(BaseWeakReferencingObservable observers) { |
||
212 | this.delegateObservable.addObservers(observers);
|
||
213 | } |
||
214 | |||
215 | public final void addConsumer(ResourceConsumer consumer) { |
||
216 | this.updateConsumersList();
|
||
217 | consumers.add(new WeakReference(consumer)); |
||
218 | } |
||
219 | |||
220 | public final void removeConsumer(ResourceConsumer consumer) { |
||
221 | ResourceConsumer cur; |
||
222 | Iterator it = consumers.iterator();
|
||
223 | while (it.hasNext()) {
|
||
224 | cur = (ResourceConsumer) ((WeakReference) it.next()).get();
|
||
225 | if (cur == null || (cur == consumer)) { |
||
226 | it.remove(); |
||
227 | } |
||
228 | } |
||
229 | } |
||
230 | |||
231 | public int getConsumersCount() { |
||
232 | this.updateConsumersList();
|
||
233 | return consumers.size();
|
||
234 | } |
||
235 | |||
236 | private synchronized void updateConsumersList() { |
||
237 | Iterator it = consumers.iterator();
|
||
238 | WeakReference ref;
|
||
239 | while (it.hasNext()) {
|
||
240 | ref = (WeakReference) it.next();
|
||
241 | if (ref.get() == null) { |
||
242 | it.remove(); |
||
243 | } |
||
244 | } |
||
245 | } |
||
246 | |||
247 | public void closeRequest() throws ResourceException { |
||
248 | if (inUse()) {
|
||
249 | return;
|
||
250 | } |
||
251 | if (consumers != null) { |
||
252 | for (int i = 0; i < consumers.size(); i++) { |
||
253 | |||
254 | } |
||
255 | } |
||
256 | if (consumers != null) { |
||
257 | Iterator it = consumers.iterator();
|
||
258 | while (it.hasNext()) {
|
||
259 | ResourceConsumer consumer = |
||
260 | (ResourceConsumer) ((WeakReference) it.next()).get();
|
||
261 | if (consumer != null) { |
||
262 | consumer.closeResourceRequested(this);
|
||
263 | } else {
|
||
264 | it.remove(); |
||
265 | } |
||
266 | } |
||
267 | } |
||
268 | } |
||
269 | |||
270 | public void setData(Object data) { |
||
271 | this.data = data;
|
||
272 | } |
||
273 | |||
274 | public Object getData() { |
||
275 | return this.data; |
||
276 | } |
||
277 | |||
278 | protected void notifyObserver(String type) { |
||
279 | if (delegateObservable != null) { |
||
280 | this.delegateObservable.notifyObservers(new DefaultResourceNotification( |
||
281 | this, type));
|
||
282 | } |
||
283 | } |
||
284 | |||
285 | public void notifyDispose() throws ResourceNotifyDisposeException { |
||
286 | this.notifyObserver(ResourceNotification.DISPOSE);
|
||
287 | |||
288 | if (consumers != null) { |
||
289 | consumers.clear(); |
||
290 | } |
||
291 | |||
292 | lastTimeOpen = 0l;
|
||
293 | lastTimeUsed = 0l;
|
||
294 | |||
295 | data = null;
|
||
296 | |||
297 | if (delegateObservable != null) { |
||
298 | delegateObservable.deleteObservers(); |
||
299 | } |
||
300 | } |
||
301 | |||
302 | public final boolean inUse() { |
||
303 | return executeCount > 0; |
||
304 | } |
||
305 | |||
306 | public Object execute(ResourceAction action) |
||
307 | throws ResourceExecuteException {
|
||
308 | Object value = null; |
||
309 | synchronized (lock) {
|
||
310 | executeBegins(); |
||
311 | try {
|
||
312 | value = performExecution(action); |
||
313 | } catch (Exception e) { |
||
314 | throw new ResourceExecuteException(this, e); |
||
315 | } finally {
|
||
316 | executeEnds(); |
||
317 | } |
||
318 | } |
||
319 | return value;
|
||
320 | } |
||
321 | |||
322 | protected Object performExecution(ResourceAction action) throws Exception { |
||
323 | return action.run();
|
||
324 | } |
||
325 | |||
326 | protected final void executeBegins() { |
||
327 | executeCount++; |
||
328 | } |
||
329 | |||
330 | protected final void executeEnds() { |
||
331 | updateLastTimeUsed(); |
||
332 | executeCount--; |
||
333 | } |
||
334 | |||
335 | /**
|
||
336 | * Returns the name of the {@link Resource}.
|
||
337 | *
|
||
338 | * @throws AccessResourceException
|
||
339 | * if there is an error while accessing the resource
|
||
340 | */
|
||
341 | public abstract String getName() throws AccessResourceException; |
||
342 | |||
343 | /**
|
||
344 | * Returns the real resource represented by this object.
|
||
345 | *
|
||
346 | * @throws AccessResourceException
|
||
347 | * if there is an error while accessing the resource
|
||
348 | */
|
||
349 | public abstract Object get() throws AccessResourceException; |
||
350 | |||
351 | } |