1 18 19 package org.objectweb.jac.core; 20 21 import java.util.Arrays ; 22 import java.util.Collection ; 23 import java.util.HashSet ; 24 import java.util.Iterator ; 25 import java.util.List ; 26 import java.util.Vector ; 27 import org.apache.log4j.Logger; 28 import org.objectweb.jac.core.rtti.ClassItem; 29 import org.objectweb.jac.core.rtti.ClassRepository; 30 import org.objectweb.jac.core.rtti.CollectionItem; 31 import org.objectweb.jac.core.rtti.FieldItem; 32 import org.objectweb.jac.core.rtti.RttiAC; 33 import org.objectweb.jac.util.ExtArrays; 34 import org.objectweb.jac.util.Predicate; 35 import org.objectweb.jac.util.Strings; 36 37 65 66 public class ObjectRepository { 67 static Logger logger = Logger.getLogger("repository"); 68 static Logger loggerGet = Logger.getLogger("repository.getobjects"); 69 70 71 transient static org.objectweb.jac.util.WeakHashMap instances = new org.objectweb.jac.util.WeakHashMap(); 72 73 74 transient static java.util.WeakHashMap reverseInstances = new java.util.WeakHashMap (); 75 76 77 transient static int instancecount = 0; 78 79 public static void register(Object wrappee) { 80 if (wrappee instanceof org.objectweb.jac.lib.java.util.Vector || 85 wrappee instanceof org.objectweb.jac.lib.java.util.HashSet || 86 wrappee instanceof org.objectweb.jac.lib.java.util.Hashtable || 87 wrappee instanceof org.objectweb.jac.lib.java.util.HashMap) { 88 return; 89 } 90 Integer inst = new Integer (instancecount++); 92 instances.put(inst,wrappee); 93 logger.debug(wrappee+"->"+inst); 94 reverseInstances.put(wrappee,inst); 95 } 96 97 105 public static void delete(Wrappee object) { 106 logger.debug("delete "+object); 107 instances.remove(reverseInstances.get(object)); 109 reverseInstances.remove(object); 110 NameRepository.get().unregisterObject(object); 112 ACManager.getACM().whenDeleted(object); 113 } 114 115 public static void free(Wrappee object) { 116 instances.remove(object); 118 reverseInstances.remove(object); 119 NameRepository.get().unregisterObject(object); 121 ACManager.getACM().whenFree(object); 122 } 123 124 131 public static Object getMemoryObject(int nth) { 132 return instances.get(new Integer (nth)); 133 } 134 135 136 143 public static int getMemoryObjectIndex(Object obj) { 144 return ((Integer )reverseInstances.get(obj)).intValue(); 145 } 146 147 148 154 public static int memoryObjectCount() { 155 return instances.size(); } 157 158 159 171 public static Collection getMemoryObjects() { 172 return instances.values(); 173 } 174 175 176 187 public static Object [] getMemoryObjects(String type) { 188 ClassItem cl = null; 189 try { 190 cl = ClassRepository.get().getClass(type); 191 } catch( Exception e ) { 192 e.printStackTrace(); 193 return ExtArrays.emptyObjectArray; 194 } 195 return getMemoryObjects(cl); 196 } 197 198 199 210 public static Object [] getMemoryObjects(ClassItem cl) { 211 Vector objects = new Vector (); 212 213 Class type = cl.getActualClass(); 214 Iterator it = getMemoryObjects().iterator(); 215 while(it.hasNext()) { 216 Object cur = it.next(); 217 if (type.isAssignableFrom(cur.getClass())) { 218 objects.add(cur); 219 } 220 } 221 222 logger.debug("getMemoryObjects("+cl+") -> "+objects); 223 return objects.toArray(); 224 } 225 226 233 public static Object [] getMemoryObjects(String [] types) { 234 Vector objects = new Vector (); 235 for (int i=0; i<types.length; i++) { 236 try { 237 objects.addAll( 238 Arrays.asList( 239 getMemoryObjects(ClassRepository.get().getClass(types[i]))) ); 240 } catch( Exception e ) { 241 e.printStackTrace(); 242 } 243 } 244 return objects.toArray(); 245 } 246 247 254 public static Object [] getMemoryObjects(ClassItem[] types) { 255 Vector objects = new Vector (); 256 for (int i=0; i<types.length; i++) { 257 objects.addAll( Arrays.asList(getMemoryObjects(types[i])) ); 258 } 259 return objects.toArray(); 260 } 261 262 272 public static Collection getObjects() { 273 HashSet objects = new HashSet (instances.values()); 274 ((ACManager)ACManager.get()).whenGetObjects(objects,null); 275 return objects; 276 } 277 278 287 public static Collection getObjects(ClassItem cl) { 288 loggerGet.debug("getObjects "+cl); 289 String repName = (String )cl.getAttribute(RttiAC.REPOSITORY_NAME); 290 CollectionItem repCollection = 291 (CollectionItem)cl.getAttribute(RttiAC.REPOSITORY_COLLECTION); 292 Object repository = null; 293 if (repName!=null) { 294 repository = NameRepository.get().getObject(repName); 295 if (repository==null) 296 loggerGet.error(cl+": no such repository object "+repName); 297 } 298 if (repository!=null && repCollection!=null) { 299 loggerGet.debug("Using repository "+repName+"."+repCollection.getName()); 300 return FieldItem.getPathLeaves(repCollection,repository); 301 } else { 302 List objects = new Vector (Arrays.asList(getMemoryObjects(cl))); 303 ((ACManager)ACManager.get()).whenGetObjects(objects,cl); 304 return objects; 305 } 306 } 307 308 315 public static Collection getObjects(Class cl) { 316 return getObjects(ClassRepository.get().getClass(cl)); 317 } 318 319 330 public static Collection getObjectsWhere(ClassItem cl, 331 FieldItem relation, Object value) { 332 loggerGet.debug("getObjectsWhere "+cl+","+relation+","+value); 333 Collection objects = getObjects(cl); 334 Vector result = new Vector (); 335 FieldItem field = relation.getField(); 336 Iterator it = objects.iterator(); 337 while (it.hasNext()) { 338 Object object = it.next(); 339 for (Iterator j=relation.getSubstances(object).iterator(); 340 j.hasNext();) { 341 Object substance = j.next(); 342 if (field instanceof CollectionItem) { 343 if (((CollectionItem)field). 344 getActualCollectionThroughAccessor(substance).contains(value)) { 345 result.add(object); 346 break; 347 } 348 } else if (field.isReference()) { 349 if (field.getThroughAccessor(substance)==value) { 350 result.add(object); 351 break; 352 } 353 } else { 354 Object testedValue = field.getThroughAccessor(substance); 355 if ((value==null && testedValue==null) || (value!=null && value.equals(testedValue))) { 356 result.add(object); 357 break; 358 } 359 } 360 } 361 } 362 return result; 363 } 364 365 366 372 public static Collection getObjectsWhere(ClassItem cl, Predicate filter) { 373 loggerGet.debug("getObjectsWhere "+cl+","+filter.getClass().getName()); 374 Collection objects = getObjects(cl); 375 Vector result = new Vector (); 376 Iterator it = objects.iterator(); 377 while (it.hasNext()) { 378 Object object = it.next(); 379 if (filter.apply(object)) { 380 result.add(object); 381 } 382 } 383 return result; 384 } 385 386 public void dump() { 387 } 388 389 } 390 | Popular Tags |