001: // Copyright 2000 Samuele Pedroni
002:
003: package org.python.core;
004:
005: import java.lang.ref.*;
006: import java.util.*;
007:
008: public abstract class AutoInternalTables extends InternalTables2 {
009:
010: protected ReferenceQueue queue = new ReferenceQueue();
011:
012: protected abstract Reference newAutoRef(short type, Object key,
013: Object obj);
014:
015: protected abstract short getAutoRefType(Reference ref);
016:
017: protected abstract Object getAutoRefKey(Reference ref);
018:
019: private synchronized void cleanup() {
020: if (this .keepstable >= this .GSTABLE)
021: return;
022: this .adapters.remove(null); // trick
023: Reference ref;
024: while ((ref = this .queue.poll()) != null) {
025: Object key = getAutoRefKey(ref);
026: switch (getAutoRefType(ref)) {
027: case JCLASS:
028: Class cl = (Class) key;
029: this .classes.remove(cl);
030: classesDec(cl.getName());
031: break;
032: case LAZY_JCLASS:
033: this .lazyClasses.remove(key);
034: break;
035: case ADAPTER_CLASS:
036: this .adapterClasses.remove(key);
037: }
038: }
039: }
040:
041: protected boolean queryCanonical(String name) {
042: cleanup();
043: return super .queryCanonical(name);
044: }
045:
046: protected PyJavaClass getCanonical(Class c) {
047: cleanup();
048: Reference ref = (Reference) classesGet(c);
049: if (ref == null)
050: return null;
051: return (PyJavaClass) ref.get();
052: }
053:
054: protected PyJavaClass getLazyCanonical(String name) {
055: cleanup();
056: Reference ref = (Reference) this .lazyClasses.get(name);
057: if (ref == null)
058: return null;
059: return (PyJavaClass) ref.get();
060: }
061:
062: protected void putCanonical(Class c, PyJavaClass canonical) {
063: cleanup();
064: classesPut(c, newAutoRef(JCLASS, c, canonical));
065: }
066:
067: protected void putLazyCanonical(String name, PyJavaClass canonical) {
068: cleanup();
069: this .lazyClasses.put(name, newAutoRef(LAZY_JCLASS, name,
070: canonical));
071: }
072:
073: protected Class getAdapterClass(Class c) {
074: cleanup();
075: Reference ref = (Reference) this .adapterClasses.get(c);
076: if (ref == null)
077: return null;
078: return (Class) ref.get();
079: }
080:
081: protected void putAdapterClass(Class c, Class ac) {
082: cleanup();
083: this .adapterClasses.put(c, newAutoRef(ADAPTER_CLASS, c, ac));
084: }
085:
086: protected Object getAdapter(Object o, String evc) {
087: cleanup();
088: return super .getAdapter(o, evc);
089: }
090:
091: protected void putAdapter(Object o, String evc, Object ad) {
092: cleanup();
093: super .putAdapter(o, evc, ad);
094: }
095:
096: public boolean _doesSomeAutoUnload() {
097: return true;
098: }
099:
100: public void _forceCleanup() {
101: cleanup();
102: }
103:
104: public void _beginCanonical() {
105: cleanup();
106: super ._beginCanonical();
107: }
108:
109: public void _beginLazyCanonical() {
110: cleanup();
111: super ._beginLazyCanonical();
112: }
113:
114: public void _beginOverAdapterClasses() {
115: cleanup();
116: super ._beginOverAdapterClasses();
117:
118: }
119:
120: public void _beginOverAdapters() {
121: cleanup();
122: super ._beginOverAdapters();
123: }
124:
125: public Object _next() {
126: if (this .iterType == ADAPTER) {
127: Object ret = super ._next();
128: if (ret != null)
129: return ret;
130: } else {
131: while (this .iter.hasNext()) {
132: this .cur = this .iter.next();
133: switch (this .iterType) {
134: case JCLASS:
135: PyJavaClass jc = (PyJavaClass) ((Reference) this .cur)
136: .get();
137: if (jc == null)
138: continue;
139: this .cur = jc;
140: return jc;
141: case LAZY_JCLASS:
142: PyJavaClass lazy = (PyJavaClass) ((Reference) this .cur)
143: .get();
144: if (lazy == null)
145: continue;
146: return new _LazyRep(lazy.__name__, lazy.__mgr__);
147: case ADAPTER_CLASS:
148: Map.Entry entry = (Map.Entry) this .cur;
149: if (((Reference) entry.getValue()).get() == null)
150: continue;
151: return entry.getKey();
152: }
153: }
154: this .cur = null;
155: this .iter = null;
156: endStable();
157: }
158: cleanup();
159: return null;
160: }
161:
162: public void _flush(PyJavaClass jc) {
163: cleanup();
164: super._flush(jc);
165: }
166:
167: }
|