forked from micw/php-java-bridge
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContextManager.java
More file actions
93 lines (82 loc) · 2.19 KB
/
Copy pathContextManager.java
File metadata and controls
93 lines (82 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/*-*- mode: Java; tab-width:8 -*-*/
package php.java.bridge;
import java.util.Hashtable;
import java.util.Iterator;
import php.java.bridge.Context;
public class ContextManager extends SessionFactory {
protected boolean removed=false;
protected Object context;
static final Hashtable contexts = new Hashtable();
private JavaBridge bridge;
private static short count=0; // If a context survives more than 65535
// context creations, that context will be
// destroyed.
protected String id;
protected static synchronized int getNext() {
if(++count==0) ++count;
return count;
}
protected ContextManager() {
id=String.valueOf(0xFFFF&getNext());
}
protected void add() {
Object old = contexts.put(this.getId(), this);
if(old!=null) {
((ContextManager)old).remove();
if(Util.logLevel>2) Util.logError("Removed stale context: " +old);
}
}
public static ContextManager addNew() {
ContextManager ctx = new ContextManager();
ctx.add();
ctx.setContext(new Context());
return ctx;
}
public static ContextManager get(String id) {
return (ContextManager)contexts.get(id);
}
public synchronized void remove() {
contexts.remove(getId());
bridge=null;
removed=true;
notify();
}
public static void removeAll() {
for(Iterator ii=contexts.values().iterator(); ii.hasNext();) {
ContextManager ctx = ((ContextManager)ii.next());
synchronized(ctx) {
ctx.removed=true;
ctx.notify();
}
ii.remove();
}
}
public synchronized void waitFor() throws InterruptedException {
if(!removed) wait();
}
public String getId() {
return id;
}
public String toString() {
return "Context# " +id;
}
public Object getContext() {
return context;
}
public void setContext(Object context) {
this.context = context;
}
/**
* @param bridge The bridge to set.
*/
public void setBridge(JavaBridge bridge) {
if(this.bridge!=null) throw new IllegalStateException("Context already has a bridge");
this.bridge = bridge;
}
/**
* @return Returns the bridge.
*/
public JavaBridge getBridge() {
return bridge;
}
}