KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > corba > se > impl > oa > poa > ActiveObjectMap


1 /*
2  * @(#)ActiveObjectMap.java 1.18 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package com.sun.corba.se.impl.oa.poa;
9
10 import java.util.Set JavaDoc ;
11 import java.util.HashSet JavaDoc ;
12 import java.util.Map JavaDoc ;
13 import java.util.HashMap JavaDoc ;
14 import java.util.Iterator JavaDoc ;
15 import java.util.Vector JavaDoc ;
16
17 import org.omg.PortableServer.Servant JavaDoc ;
18 import org.omg.PortableServer.POAPackage.WrongPolicy JavaDoc ;
19 import org.omg.CORBA.INTERNAL JavaDoc ;
20
21 /** The ActiveObjectMap maintains associations between servants and
22  * their keys. There are two variants, to support whether or not
23  * multiple IDs per servant are allowed. This class suppots bidirectional
24  * traversal of the key-servant association. Access to an instance of this
25  * class is serialized by the POA mutex.
26  */

27 public abstract class ActiveObjectMap
28 {
29     public static class Key {
30     public byte[] id;
31
32     Key(byte[] id) {
33         this.id = id;
34     }
35
36     public String JavaDoc toString() {
37         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
38         for(int i = 0; i < id.length; i++) {
39         buffer.append(Integer.toString((int) id[i], 16));
40         if (i != id.length-1)
41             buffer.append(":");
42         }
43         return buffer.toString();
44     }
45
46     public boolean equals(java.lang.Object JavaDoc key) {
47         if (!(key instanceof Key))
48         return false;
49         Key k = (Key) key;
50         if (k.id.length != this.id.length)
51         return false;
52         for(int i = 0; i < this.id.length; i++)
53         if (this.id[i] != k.id[i])
54             return false;
55         return true;
56     }
57
58     // Use the same hash function as for String
59
public int hashCode() {
60         int h = 0;
61         for (int i = 0; i < id.length; i++)
62         h = 31*h + id[i];
63         return h;
64     }
65     }
66    
67     protected POAImpl poa ;
68
69     protected ActiveObjectMap( POAImpl poa )
70     {
71     this.poa = poa ;
72     }
73
74     public static ActiveObjectMap create( POAImpl poa, boolean multipleIDsAllowed )
75     {
76     if (multipleIDsAllowed)
77         return new MultipleObjectMap( poa ) ;
78     else
79         return new SingleObjectMap(poa ) ;
80     }
81
82     private Map JavaDoc keyToEntry = new HashMap JavaDoc() ; // Map< Key, AOMEntry >
83
private Map JavaDoc entryToServant = new HashMap JavaDoc() ; // Map< AOMEntry, Servant >
84
private Map JavaDoc servantToEntry = new HashMap JavaDoc() ; // Map< Servant, AOMEntry >
85

86     public final boolean contains(Servant JavaDoc value)
87     {
88     return servantToEntry.containsKey( value ) ;
89     }
90
91     public final boolean containsKey(Key key)
92     {
93     return keyToEntry.containsKey(key);
94     }
95
96     /** get Returbs the entry assigned to the key, or creates a new
97     * entry in state INVALID if none is present.
98     */

99     public final AOMEntry get(Key key)
100     {
101     AOMEntry result = (AOMEntry)keyToEntry.get(key);
102     if (result == null) {
103         result = new AOMEntry( poa ) ;
104         putEntry( key, result ) ;
105     }
106
107     return result ;
108     }
109
110     public final Servant JavaDoc getServant( AOMEntry entry )
111     {
112     return (Servant JavaDoc)entryToServant.get( entry ) ;
113     }
114
115     public abstract Key getKey(AOMEntry value) throws WrongPolicy JavaDoc ;
116
117     public Key getKey(Servant JavaDoc value) throws WrongPolicy JavaDoc
118     {
119     AOMEntry entry = (AOMEntry)servantToEntry.get( value ) ;
120     return getKey( entry ) ;
121     }
122
123     protected void putEntry( Key key, AOMEntry value )
124     {
125     keyToEntry.put( key, value ) ;
126     }
127
128     public final void putServant( Servant JavaDoc servant, AOMEntry value )
129     {
130     entryToServant.put( value, servant ) ;
131     servantToEntry.put( servant, value ) ;
132     }
133
134     protected abstract void removeEntry( AOMEntry entry, Key key ) ;
135
136     public final void remove( Key key )
137     {
138     AOMEntry entry = (AOMEntry)keyToEntry.remove( key ) ;
139     Servant JavaDoc servant = (Servant JavaDoc)entryToServant.remove( entry ) ;
140     if (servant != null)
141         servantToEntry.remove( servant ) ;
142
143     removeEntry( entry, key ) ;
144     }
145
146     public abstract boolean hasMultipleIDs(AOMEntry value) ;
147
148     protected void clear()
149     {
150         keyToEntry.clear();
151     }
152
153     public final Set JavaDoc keySet()
154     {
155     return keyToEntry.keySet() ;
156     }
157 }
158
159 class SingleObjectMap extends ActiveObjectMap
160 {
161     private Map JavaDoc entryToKey = new HashMap JavaDoc() ; // Map< AOMEntry, Key >
162

163     public SingleObjectMap( POAImpl poa )
164     {
165     super( poa ) ;
166     }
167
168     public Key getKey(AOMEntry value) throws WrongPolicy JavaDoc
169     {
170     return (Key)entryToKey.get( value ) ;
171     }
172
173     protected void putEntry(Key key, AOMEntry value)
174     {
175     super.putEntry( key, value);
176
177     entryToKey.put( value, key ) ;
178     }
179
180     public boolean hasMultipleIDs(AOMEntry value)
181     {
182     return false;
183     }
184
185     // This case does not need the key.
186
protected void removeEntry(AOMEntry entry, Key key)
187     {
188     entryToKey.remove( entry ) ;
189     }
190
191     public void clear()
192     {
193     super.clear() ;
194     entryToKey.clear() ;
195     }
196 }
197
198 class MultipleObjectMap extends ActiveObjectMap
199 {
200     private Map JavaDoc entryToKeys = new HashMap JavaDoc() ; // Map< AOMEntry, Set< Key > >
201

202     public MultipleObjectMap( POAImpl poa )
203     {
204     super( poa ) ;
205     }
206
207     public Key getKey(AOMEntry value) throws WrongPolicy JavaDoc
208     {
209     throw new WrongPolicy JavaDoc() ;
210     }
211
212     protected void putEntry(Key key, AOMEntry value)
213     {
214     super.putEntry( key, value);
215
216     Set JavaDoc set = (Set JavaDoc)entryToKeys.get( value ) ;
217     if (set == null) {
218         set = new HashSet JavaDoc() ;
219         entryToKeys.put( value, set ) ;
220     }
221     set.add( key ) ;
222     }
223
224     public boolean hasMultipleIDs(AOMEntry value)
225     {
226     Set JavaDoc set = (Set JavaDoc)entryToKeys.get( value ) ;
227     if (set == null)
228         return false ;
229     return set.size() > 1 ;
230     }
231
232     protected void removeEntry(AOMEntry entry, Key key)
233     {
234     Set JavaDoc keys = (Set JavaDoc)entryToKeys.get( entry ) ;
235     if (keys != null) {
236         keys.remove( key ) ;
237         if (keys.isEmpty())
238         entryToKeys.remove( entry ) ;
239     }
240     }
241
242     public void clear()
243     {
244     super.clear() ;
245     entryToKeys.clear() ;
246     }
247 }
248
249
Popular Tags