1 7 8 package com.sun.corba.se.impl.oa.poa; 9 10 import java.util.Set ; 11 import java.util.HashSet ; 12 import java.util.Map ; 13 import java.util.HashMap ; 14 import java.util.Iterator ; 15 import java.util.Vector ; 16 17 import org.omg.PortableServer.Servant ; 18 import org.omg.PortableServer.POAPackage.WrongPolicy ; 19 import org.omg.CORBA.INTERNAL ; 20 21 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 toString() { 37 StringBuffer buffer = new StringBuffer (); 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 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 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 keyToEntry = new HashMap () ; private Map entryToServant = new HashMap () ; private Map servantToEntry = new HashMap () ; 86 public final boolean contains(Servant 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 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 getServant( AOMEntry entry ) 111 { 112 return (Servant )entryToServant.get( entry ) ; 113 } 114 115 public abstract Key getKey(AOMEntry value) throws WrongPolicy ; 116 117 public Key getKey(Servant value) throws WrongPolicy 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 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 servant = (Servant )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 keySet() 154 { 155 return keyToEntry.keySet() ; 156 } 157 } 158 159 class SingleObjectMap extends ActiveObjectMap 160 { 161 private Map entryToKey = new HashMap () ; 163 public SingleObjectMap( POAImpl poa ) 164 { 165 super( poa ) ; 166 } 167 168 public Key getKey(AOMEntry value) throws WrongPolicy 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 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 entryToKeys = new HashMap () ; 202 public MultipleObjectMap( POAImpl poa ) 203 { 204 super( poa ) ; 205 } 206 207 public Key getKey(AOMEntry value) throws WrongPolicy 208 { 209 throw new WrongPolicy () ; 210 } 211 212 protected void putEntry(Key key, AOMEntry value) 213 { 214 super.putEntry( key, value); 215 216 Set set = (Set )entryToKeys.get( value ) ; 217 if (set == null) { 218 set = new HashSet () ; 219 entryToKeys.put( value, set ) ; 220 } 221 set.add( key ) ; 222 } 223 224 public boolean hasMultipleIDs(AOMEntry value) 225 { 226 Set set = (Set )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 keys = (Set )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 |