1 17 package org.eclipse.emf.ecore.impl; 18 19 20 import java.util.Collection ; 21 import java.util.HashMap ; 22 import java.util.Iterator ; 23 import java.util.Map ; 24 import java.util.Set ; 25 import java.util.WeakHashMap ; 26 27 import org.eclipse.emf.common.EMFPlugin; 28 import org.eclipse.emf.ecore.EPackage; 29 30 import org.eclipse.emf.ecore.plugin.EcorePlugin; 31 32 33 36 public class EPackageRegistryImpl extends HashMap implements EPackage.Registry 37 { 38 41 public static EPackage.Registry createGlobalRegistry() 42 { 43 try 44 { 45 String className = System.getProperty("org.eclipse.emf.ecore.EPackage.Registry.INSTANCE"); 46 if (className == null) 47 { 48 if (!EMFPlugin.IS_ECLIPSE_RUNNING) 49 { 50 return new Delegator(); 51 } 52 else 53 { 54 return new EPackageRegistryImpl(); 55 } 56 } 57 else 58 { 59 return (EPackage.Registry)Class.forName(className).newInstance(); 60 } 61 } 62 catch (Exception exception) 63 { 64 EcorePlugin.INSTANCE.log(exception); 65 return new EPackageRegistryImpl(); 66 } 67 } 68 69 72 protected EPackage.Registry delegateRegistry; 73 74 77 public EPackageRegistryImpl() 78 { 79 } 80 81 84 public EPackageRegistryImpl(EPackage.Registry delegateRegistry) 85 { 86 this.delegateRegistry = delegateRegistry; 87 } 88 89 92 public EPackage getEPackage(String nsURI) 93 { 94 Object ePackage = get(nsURI); 95 if (ePackage instanceof EPackage) 96 { 97 EPackage result = (EPackage)ePackage; 98 if (result.getNsURI() == null) 99 { 100 initialize(result); 101 } 102 return result; 103 } 104 else if (ePackage instanceof EPackage.Descriptor) 105 { 106 EPackage.Descriptor ePackageDescriptor = (EPackage.Descriptor)ePackage; 107 EPackage result = ePackageDescriptor.getEPackage(); 108 if (result != null) 109 { 110 put(nsURI, result); 111 initialize(result); 112 } 113 return result; 114 } 115 else 116 { 117 return delegatedGetEPackage(nsURI); 118 } 119 } 120 121 124 protected void initialize(EPackage ePackage) 125 { 126 } 127 128 132 protected EPackage delegatedGetEPackage(String nsURI) 133 { 134 if (delegateRegistry != null) 135 { 136 return delegateRegistry.getEPackage(nsURI); 137 } 138 139 return null; 140 } 141 142 149 public boolean containsKey(Object key) 150 { 151 return super.containsKey(key) || delegateRegistry != null && delegateRegistry.containsKey(key); 152 } 153 154 157 protected static Map classLoaderToRegistryMap = new WeakHashMap (); 158 159 164 public static synchronized EPackage.Registry getRegistry(ClassLoader classLoader) 165 { 166 EPackage.Registry result = (EPackage.Registry)classLoaderToRegistryMap.get(classLoader); 167 if (result == null) 168 { 169 if (classLoader == null) 170 { 171 result = null; 172 } 173 else 174 { 175 result = new EPackageRegistryImpl(getRegistry(classLoader.getParent())); 176 classLoaderToRegistryMap.put(classLoader, result); 177 } 178 } 179 return result; 180 } 181 182 185 public static class Delegator implements EPackage.Registry 186 { 187 protected EPackage.Registry delegateRegistry(ClassLoader classLoader) 188 { 189 return getRegistry(classLoader); 190 } 191 192 protected EPackage.Registry delegateRegistry() 193 { 194 return delegateRegistry(Thread.currentThread().getContextClassLoader()); 195 } 196 197 public EPackage getEPackage(String key) 198 { 199 return delegateRegistry().getEPackage(key); 200 } 201 202 public int size() 203 { 204 return delegateRegistry().size(); 205 } 206 207 public boolean isEmpty() 208 { 209 return delegateRegistry().isEmpty(); 210 } 211 212 public boolean containsKey(Object key) 213 { 214 return delegateRegistry().containsKey(key); 215 } 216 217 public boolean containsValue(Object value) 218 { 219 return delegateRegistry().containsValue(value); 220 } 221 222 public Object get(Object key) 223 { 224 return delegateRegistry().get(key); 225 } 226 227 public Object put(Object key, Object value) 228 { 229 Class valueClass = value.getClass(); 230 if (valueClass == EPackageImpl.class) 231 { 232 return delegateRegistry().put(key, value); 233 } 234 else 235 { 236 String valueClassName = valueClass.getName(); 237 238 ClassLoader result = Thread.currentThread().getContextClassLoader(); 241 for (ClassLoader classLoader = result.getParent(); classLoader != null; classLoader = classLoader.getParent()) 242 { 243 try 244 { 245 Class loadedClass = classLoader.loadClass(valueClassName); 246 if (loadedClass == valueClass) 247 { 248 result = classLoader; 249 } 250 else 251 { 252 break; 256 } 257 } 258 catch (ClassNotFoundException exception) 259 { 260 break; 264 } 265 } 266 267 return delegateRegistry(result).put(key, value); 270 } 271 } 272 273 public Object remove(Object key) 274 { 275 return delegateRegistry().remove(key); 276 } 277 278 public void putAll(Map map) 279 { 280 for (Iterator i = map.entrySet().iterator(); i.hasNext(); ) 281 { 282 Map.Entry entry = (Map.Entry )i.next(); 283 put(entry.getKey(), entry.getValue()); 284 } 285 } 286 287 public void clear() 288 { 289 delegateRegistry().clear(); 290 } 291 292 public Set keySet() 293 { 294 return delegateRegistry().keySet(); 295 } 296 297 public Collection values() 298 { 299 return delegateRegistry().values(); 300 } 301 302 public Set entrySet() 303 { 304 return delegateRegistry().entrySet(); 305 } 306 } 307 } 308 | Popular Tags |