/Users/lyon/j4p/src/javagroup/util/Resource.java
|
1 /*
2 * Copyright (C) 1997 Luke Gorrie
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
13 *
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 675 Mass Ave, Cambridge,
17 * MA 02139, USA.
18 */
19
20 package javagroup.util;
21
22 import java.util.Vector;
23
24 /**
25 * Class to represent a reference-counted resource. The resource is
26 * dispose()d when all "hard" references are released.
27 *
28 * @author Luke Gorrie
29 */
30 public class Resource implements Disposable {
31
32 // Vector used to reference resources to prevent garbage collection
33 private static Vector __resources;
34
35 /**
36 * The number of registered references to this Resource.
37 */
38 protected int _references;
39
40 /**
41 * True if this resource is not to be disposed of.
42 */
43 protected boolean _static;
44
45 protected Vector _disposalListeners;
46
47 protected Disposable _wrappedResource;
48
49 protected boolean _disposed = false;
50
51 public Resource() {
52 _disposalListeners = new Vector();
53 }
54
55 public Resource(Disposable disposable) {
56 this();
57 _wrappedResource = disposable;
58 }
59
60 /**
61 * Lock add a lock. *
62 */
63 public synchronized void addLock() {
64 _references++;
65 }
66
67 /**
68 * Release a lock. *
69 */
70 public synchronized void releaseLock() {
71 if (!_static) {
72 _references--;
73
74 if (_references <= 0)
75 this.dispose();
76 }
77 }
78
79 /**
80 * Mark resource as 'static', so it will never be destroyed. *
81 */
82 public synchronized void setStatic() {
83 if (!_static)
84 __resources.addElement(this);
85 _static = true;
86 }
87
88 /**
89 * Release all locks and free resource. *
90 */
91 public void unregister() {
92
93 if (__resources.contains(this))
94 __resources.removeElement(this);
95 }
96
97 public void dispose() {
98 if (_disposed)
99 return;
100
101 if (_wrappedResource != null) {
102 _wrappedResource.dispose();
103 }
104 fireResourceDisposalEvent();
105 _disposalListeners.removeAllElements();
106 _disposed = true;
107 }
108
109 public boolean isDisposed() {
110 return _disposed;
111 }
112
113 public void addDisposalListener(ResourceDisposalListener listener) {
114 _disposalListeners.addElement(listener);
115 }
116
117 public void removeDisposalListener(ResourceDisposalListener listener) {
118 _disposalListeners.removeElement(listener);
119 }
120
121 public void fireResourceDisposalEvent() {
122 synchronized (_disposalListeners) {
123 for (int i = 0; i < _disposalListeners.size(); i++) {
124 ResourceDisposalListener listener = (ResourceDisposalListener)
125 _disposalListeners.elementAt(i);
126 listener.resourceDisposed(this);
127 }
128 }
129 }
130
131 }
132
133