~ubuntu-branches/ubuntu/oneiric/icedtea-web/oneiric

« back to all changes in this revision

Viewing changes to plugin/icedteanp/java/sun/applet/PluginObjectStore.java

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2011-04-06 13:10:44 UTC
  • mfrom: (1.1.6 upstream)
  • Revision ID: james.westby@ubuntu.com-20110406131044-6jky8obchc43cf02
Tags: 1.1~20110406-0ubuntu1
Fix typo in icedtea-netx postinst to install the javaws alternative.

Show diffs side-by-side

added added

removed removed

Lines of Context:
37
37
 
38
38
package sun.applet;
39
39
 
40
 
import java.util.*;
41
 
 
42
 
public class PluginObjectStore {
43
 
    private static HashMap<Integer, Object> objects = new HashMap<Integer, Object>();
44
 
    private static HashMap<Integer, Integer> counts = new HashMap<Integer, Integer>();
45
 
    private static HashMap<Object, Integer> identifiers = new HashMap<Object, Integer>();
46
 
    // FIXME:
47
 
    //
48
 
    // IF uniqueID == MAX_LONG, uniqueID =
49
 
    // 0 && wrapped = true
50
 
    //
51
 
    // if (wrapped), check if
52
 
    // objects.get(uniqueID) returns null
53
 
    //
54
 
    // if yes, use uniqueID, if no,
55
 
    // uniqueID++ and keep checking
56
 
    // or:
57
 
    // stack of available ids:
58
 
    // derefed id -> derefed id -> nextUniqueIdentifier
59
 
    private static int nextUniqueIdentifier = 1;
 
40
import java.util.HashMap;
 
41
import java.util.Map;
 
42
 
 
43
// Enums are the best way to implement singletons:
 
44
// Bloch, Joshua. Effective Java, 2nd Edition. Item 3, Chapter 2. ISBN: 0-321-35668-3.
 
45
enum PluginObjectStore {
 
46
    INSTANCE;
 
47
 
 
48
    private HashMap<Integer, Object> objects = new HashMap<Integer, Object>();
 
49
    private HashMap<Integer, Integer> counts = new HashMap<Integer, Integer>();
 
50
    private HashMap<Object, Integer> identifiers = new HashMap<Object, Integer>();
 
51
    private final Object lock = new Object();
 
52
 
 
53
    private boolean wrapped = false;
 
54
    private int nextUniqueIdentifier = 1;
 
55
 
 
56
    public static PluginObjectStore getInstance() {
 
57
        return INSTANCE;
 
58
    }
60
59
 
61
60
    public Object getObject(Integer identifier) {
62
 
        return objects.get(identifier);
 
61
        synchronized(lock) {
 
62
            return objects.get(identifier);
 
63
        }
63
64
    }
64
65
 
65
66
    public Integer getIdentifier(Object object) {
66
 
        if (object == null)
67
 
            return 0;
68
 
        return identifiers.get(object);
 
67
        synchronized(lock) {
 
68
            if (object == null)
 
69
                return 0;
 
70
            return identifiers.get(object);
 
71
        }
69
72
    }
70
73
 
71
74
    public boolean contains(Object object) {
72
 
        if (object == null)
73
 
            return identifiers.containsKey(object);
 
75
        synchronized(lock) {
 
76
            if (object == null)
 
77
                return identifiers.containsKey(object);
74
78
 
75
 
        return false;
 
79
            return false;
 
80
        }
76
81
    }
77
82
 
78
83
    public boolean contains(int identifier) {
79
 
        return objects.containsKey(identifier);
 
84
        synchronized(lock) {
 
85
            return objects.containsKey(identifier);
 
86
        }
 
87
    }
 
88
 
 
89
    private boolean checkNeg() {
 
90
        if (nextUniqueIdentifier < 1) {
 
91
            wrapped = true;
 
92
            nextUniqueIdentifier = 1;
 
93
        }
 
94
        return wrapped;
 
95
    }
 
96
 
 
97
    private int getNextID() {
 
98
        while (checkNeg() && objects.containsKey(nextUniqueIdentifier))
 
99
            nextUniqueIdentifier++;
 
100
        return nextUniqueIdentifier++;
80
101
    }
81
102
 
82
103
    public void reference(Object object) {
83
 
        Integer identifier = identifiers.get(object);
84
 
        if (identifier == null) {
85
 
            objects.put(nextUniqueIdentifier, object);
86
 
            counts.put(nextUniqueIdentifier, 1);
87
 
            identifiers.put(object, nextUniqueIdentifier);
88
 
            nextUniqueIdentifier++;
89
 
        } else {
90
 
            counts.put(identifier, counts.get(identifier) + 1);
 
104
        synchronized(lock) {
 
105
            Integer identifier = identifiers.get(object);
 
106
            if (identifier == null) {
 
107
                int next = getNextID();
 
108
                objects.put(next, object);
 
109
                counts.put(next, 1);
 
110
                identifiers.put(object, next);
 
111
            } else {
 
112
                counts.put(identifier, counts.get(identifier) + 1);
 
113
            }
91
114
        }
92
115
    }
93
116
 
94
117
    public void unreference(int identifier) {
95
 
        Integer currentCount = counts.get(identifier);
96
 
        if (currentCount == null) {
97
 
            return;
98
 
        }
99
 
        if (currentCount == 1) {
100
 
            Object object = objects.get(identifier);
101
 
            objects.remove(identifier);
102
 
            counts.remove(identifier);
103
 
            identifiers.remove(object);
104
 
        } else {
105
 
            counts.put(identifier, currentCount - 1);
 
118
        synchronized(lock) {
 
119
            Integer currentCount = counts.get(identifier);
 
120
            if (currentCount == null) {
 
121
                return;
 
122
            }
 
123
            if (currentCount == 1) {
 
124
                Object object = objects.get(identifier);
 
125
                objects.remove(identifier);
 
126
                counts.remove(identifier);
 
127
                identifiers.remove(object);
 
128
            } else {
 
129
                counts.put(identifier, currentCount - 1);
 
130
            }
106
131
        }
107
132
    }
108
133
 
109
134
    public void dump() {
110
 
        Iterator i = objects.keySet().iterator();
111
 
        while (i.hasNext()) {
112
 
            Object key = i.next();
113
 
            PluginDebug.debug(key + "::" + objects.get(key));
 
135
        synchronized(lock) {
 
136
            if (PluginDebug.DEBUG) {
 
137
                for (Map.Entry<Integer, Object> e : objects.entrySet()) {
 
138
                    PluginDebug.debug(e.getKey(), "::", e.getValue());
 
139
                }
 
140
            }
114
141
        }
115
142
    }
116
143
}