1 17 18 package org.objectweb.jac.util; 19 20 import java.util.*; 21 import org.apache.log4j.Logger; 22 23 26 27 35 public class Repository { 36 static Logger logger = Logger.getLogger("repository"); 37 38 44 public static Repository get () { 45 if (repository == null) 46 repository = new Repository(); 47 return repository; 48 } 49 50 57 protected static Repository repository = null; 58 59 61 public Map objects; 62 63 65 public Map names; 66 67 public Repository() { 68 init(); 69 } 70 71 protected void init() { 72 objects = new Hashtable(); 73 names = new Hashtable(); 74 } 75 76 86 public boolean register(String logicalName, Object object) { 87 logger.debug("register("+logicalName+","+object.getClass().getName()+")"); 88 Object old = objects.put(logicalName, object); 89 if (old!=null) { 90 logger.warn("overriding "+logicalName+" -> "+old+" with "+object); 91 names.remove(old); 92 } 93 names.put(object, logicalName); 94 return true; 95 } 96 97 105 public void unregister(String logicalName) { 106 logger.debug("unregister("+logicalName+")"); 107 Object object = objects.remove(logicalName); 108 if (object != null) { 109 names.remove(object); 110 } 111 } 112 113 121 public void unregisterObject(Object object) { 122 logger.debug("unregisterObject("+object+")"); 123 Object logicalName = names.remove(object); 124 if (logicalName != null) { 125 objects.remove(logicalName); 126 } 127 } 128 129 135 136 public boolean isRegistered(String logicalName) { 137 if (objects.containsKey(logicalName)) { 138 return true; 139 } 140 return false; 141 } 142 143 153 public Object [] getObjects() { 154 return objects.values().toArray(); 155 } 156 157 167 public String [] getNames() { 168 return (String [])names.values().toArray(ExtArrays.emptyStringArray); 169 } 170 171 185 public Object getObject(String logicalName) { 186 if (logicalName == null) 187 return null; 188 Object ret = objects.get(logicalName); 189 logger.debug("getObject("+logicalName+") -> "+ 190 (ret==null?"null":ret.getClass().getName())); 191 return ret; 192 } 193 194 206 public String getName(Object object) { 207 if (object == null) { 208 return null; 209 } 210 return (String )names.get(object); 211 } 212 213 216 public void dump() { 217 System.out.println(this + " dump:"); 218 System.out.println(getPrintableString()); 219 } 220 221 public String getPrintableString() { 222 return ""+objects; 223 } 224 225 } 226 | Popular Tags |