| 1 package org.sapia.ubik.rmi.server; 2 3 import java.rmi.NoSuchObjectException ; 4 import java.rmi.server.Unreferenced ; 5 import java.util.Collections ; 6 import java.util.HashMap ; 7 import java.util.Map ; 8 9 import org.sapia.ubik.rmi.Consts; 10 11 22 public class ObjectTable { 23 24 static final float DEFAULT_LOAD_FACTOR = 0.75f; 25 static final int DEFAULT_INIT_CAPACITY = 2000; 26 27 Map _refs; 28 29 ObjectTable(){ 30 float loadFactor = DEFAULT_LOAD_FACTOR; 31 int initCapacity = DEFAULT_INIT_CAPACITY; 32 String loadFactorProp = System.getProperty(Consts.OBJECT_TABLE_LOAD_FACTOR); 33 String initCapacityProp = System.getProperty(Consts.OBJECT_TABLE_INITCAPACITY); 34 if(loadFactorProp != null){ 35 try{ 36 loadFactor = Float.parseFloat(loadFactorProp); 37 }catch(NumberFormatException e){ 38 Log.error(getClass(), "Invalid load factor: " + loadFactorProp + " - using: " + DEFAULT_LOAD_FACTOR); 39 } 40 } 41 if(initCapacityProp != null){ 42 try{ 43 initCapacity = Integer.parseInt(initCapacityProp); 44 }catch(NumberFormatException e){ 45 Log.error(getClass(), "Invalid initial capacity: " + loadFactorProp + " - using: " + DEFAULT_INIT_CAPACITY); 46 } 47 48 } 49 _refs = Collections.synchronizedMap(new HashMap (initCapacity, loadFactor)); 50 } 51 52 59 public synchronized void register(OID oid, Object o) { 60 if (Log.isDebug()) { 61 Log.debug(ObjectTable.class, "registering: " + oid); 62 } 63 64 Ref ref = (Ref) _refs.get(oid); 65 66 if (ref == null) { 67 ref = new Ref(oid, o); 68 _refs.put(oid, ref); 69 } 70 71 ref.inc(); 72 } 73 74 81 public synchronized void reference(OID oid) { 82 if (Log.isDebug()) { 83 Log.debug(ObjectTable.class, "referencing to: " + oid); 84 } 85 86 Ref ref = (Ref) _refs.get(oid); 87 88 if (ref == null) { 89 if(Log.isDebug()){ 90 Log.debug(getClass(), "No object reference for: " + oid); 91 Log.debug(getClass(), "Current objects: " + _refs); 92 } 93 throw new NullPointerException ("no object reference for: " + oid); 94 } 95 96 ref.inc(); 97 } 98 99 106 public synchronized void dereference(OID oid, int decrement) { 107 Ref ref = (Ref) _refs.get(oid); 108 109 if (ref != null) { 110 ref.dec(decrement); 111 112 if (ref.count() <= 0) { 113 if (Log.isDebug()) { 114 Log.debug(ObjectTable.class, 115 "dereferencing: " + oid + " - available for GC"); 116 } 117 118 _refs.remove(oid); 119 120 if (ref._obj instanceof Unreferenced ) { 121 ((Unreferenced ) ref._obj).unreferenced(); 122 } 123 } 124 } 125 } 126 127 133 public Object getObjectFor(OID oid) throws NoSuchObjectException { 134 Ref ref = (Ref) _refs.get(oid); 135 136 if ((ref != null) && (ref.count() > 0)) { 137 return ref.get(); 138 } else { 139 if(Log.isDebug()){ 140 Log.debug(getClass(), "No object reference for: " + oid); 141 Log.debug(getClass(), "Current objects: " + _refs); 142 } 143 throw new NullPointerException ("no object reference for: " + oid); 144 } 145 } 146 147 152 public boolean remove(Object o) { 153 Ref[] refs = (Ref[]) _refs.values().toArray(new Ref[_refs.size()]); 154 boolean removed = false; 155 156 for (int i = 0; i < refs.length; i++) { 157 if (refs[i]._obj.equals(o)) { 158 _refs.remove(refs[i]._oid); 159 removed = true; 160 } 161 } 162 163 return removed; 164 } 165 166 175 public boolean remove(ClassLoader loader) { 176 Ref[] refs = (Ref[]) _refs.values().toArray(new Ref[_refs.size()]); 177 boolean removed = false; 178 179 for (int i = 0; i < refs.length; i++) { 180 if (refs[i]._obj.getClass().getClassLoader().equals(loader)) { 181 _refs.remove(refs[i]._oid); 182 183 if (refs[i]._obj instanceof Unreferenced ) { 184 ((Unreferenced ) refs[i]._obj).unreferenced(); 185 } 186 187 removed = true; 188 } 189 } 190 191 return removed; 192 } 193 194 200 public int getRefCount(OID oid) { 201 Ref ref = (Ref) _refs.get(oid); 202 203 if (ref == null) { 204 return 0; 205 } else { 206 return ref.count(); 207 } 208 } 209 210 217 218 public synchronized void clear() { 241 _refs.clear(); 242 } 243 244 public Map getRefs() { 245 return _refs; 246 } 247 248 public synchronized void clear(OID oid) { 249 Ref ref = (Ref) _refs.get(oid); 250 251 if (ref != null) { 252 ref._count = 0; 253 } 254 } 255 256 259 protected static class Ref { 260 int _count; 261 Object _obj; 262 OID _oid; 263 264 Ref(OID oid, Object o) { 265 _obj = o; 266 _oid = oid; 267 } 268 269 void dec() { 270 _count--; 271 } 272 273 void dec(int count) { 274 _count = _count - count; 275 276 if (_count < 0) { 277 _count = 0; 278 } 279 } 280 281 void inc() { 282 _count++; 283 } 284 285 int count() { 286 return _count; 287 } 288 289 Object get() { 290 return _obj; 291 } 292 } 293 } 294 | Popular Tags |