KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > emf > ecore > impl > EPackageRegistryImpl


1 /**
2  * <copyright>
3  *
4  * Copyright (c) 2002-2005 IBM Corporation and others.
5  * All rights reserved. This program and the accompanying materials
6  * are made available under the terms of the Eclipse Public License v1.0
7  * which accompanies this distribution, and is available at
8  * http://www.eclipse.org/legal/epl-v10.html
9  *
10  * Contributors:
11  * IBM - Initial API and implementation
12  *
13  * </copyright>
14  *
15  * $Id: EPackageRegistryImpl.java,v 1.8 2005/06/20 21:05:41 khussey Exp $
16  */

17 package org.eclipse.emf.ecore.impl;
18
19
20 import java.util.Collection JavaDoc;
21 import java.util.HashMap JavaDoc;
22 import java.util.Iterator JavaDoc;
23 import java.util.Map JavaDoc;
24 import java.util.Set JavaDoc;
25 import java.util.WeakHashMap JavaDoc;
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 /**
34  * An implementation of a package registry that can delegate failed lookup to another registry.
35  */

36 public class EPackageRegistryImpl extends HashMap JavaDoc implements EPackage.Registry
37 {
38   /**
39    * Creates the {@link EPackage.Registry#INSTANCE instance} of the global registry.
40    */

41   public static EPackage.Registry createGlobalRegistry()
42   {
43     try
44     {
45       String JavaDoc 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 JavaDoc exception)
63     {
64       EcorePlugin.INSTANCE.log(exception);
65       return new EPackageRegistryImpl();
66     }
67   }
68
69   /**
70    * The delegate registry.
71    */

72   protected EPackage.Registry delegateRegistry;
73
74   /**
75    * Creates a non-delegating instance.
76    */

77   public EPackageRegistryImpl()
78   {
79   }
80
81   /**
82    * Creates a delegating instance.
83    */

84   public EPackageRegistryImpl(EPackage.Registry delegateRegistry)
85   {
86     this.delegateRegistry = delegateRegistry;
87   }
88
89   /*
90    * Javadoc copied from interface.
91    */

92   public EPackage getEPackage(String JavaDoc nsURI)
93   {
94     Object JavaDoc 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   /**
122    * Creates a delegating instance.
123    */

124   protected void initialize(EPackage ePackage)
125   {
126   }
127
128   /**
129    * Returns the package from the delegate registry, if there is one.
130    * @return the package from the delegate registry.
131    */

132   protected EPackage delegatedGetEPackage(String JavaDoc nsURI)
133   {
134     if (delegateRegistry != null)
135     {
136       return delegateRegistry.getEPackage(nsURI);
137     }
138
139     return null;
140   }
141
142   /**
143    * Returns whether this map or the delegate map contains this key. Note that
144    * if there is a delegate map, the result of this method may
145    * <em><b>not</b></em> be the same as <code>keySet().contains(key)</code>.
146    * @param key the key whose presence in this map is to be tested.
147    * @return whether this map or the delegate map contains this key.
148    */

149   public boolean containsKey(Object JavaDoc key)
150   {
151     return super.containsKey(key) || delegateRegistry != null && delegateRegistry.containsKey(key);
152   }
153
154   /**
155    * A map from class loader to its associated registry.
156    */

157   protected static Map JavaDoc classLoaderToRegistryMap = new WeakHashMap JavaDoc();
158
159   /**
160    * Returns the package registry associated with the given class loader.
161    * @param classLoader the class loader.
162    * @return the package registry associated with the given class loader.
163    */

164   public static synchronized EPackage.Registry getRegistry(ClassLoader JavaDoc 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   /**
183    * A package registry implementation that delegates to a class loader specific registry.
184    */

185   public static class Delegator implements EPackage.Registry
186   {
187     protected EPackage.Registry delegateRegistry(ClassLoader JavaDoc 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 JavaDoc 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 JavaDoc key)
213     {
214       return delegateRegistry().containsKey(key);
215     }
216
217     public boolean containsValue(Object JavaDoc value)
218     {
219       return delegateRegistry().containsValue(value);
220     }
221
222     public Object JavaDoc get(Object JavaDoc key)
223     {
224       return delegateRegistry().get(key);
225     }
226
227     public Object JavaDoc put(Object JavaDoc key, Object JavaDoc value)
228     {
229       Class JavaDoc valueClass = value.getClass();
230       if (valueClass == EPackageImpl.class)
231       {
232         return delegateRegistry().put(key, value);
233       }
234       else
235       {
236         String JavaDoc valueClassName = valueClass.getName();
237
238         // Find the uppermost classloader in the hierarchy that can load the class.
239
//
240
ClassLoader JavaDoc result = Thread.currentThread().getContextClassLoader();
241         for (ClassLoader JavaDoc classLoader = result.getParent(); classLoader != null; classLoader = classLoader.getParent())
242         {
243           try
244           {
245             Class JavaDoc loadedClass = classLoader.loadClass(valueClassName);
246             if (loadedClass == valueClass)
247             {
248               result = classLoader;
249             }
250             else
251             {
252               // The class address was not equal, so we don't want this classloader,
253
// but instead want the last result that was able to load the class.
254
//
255
break;
256             }
257           }
258           catch (ClassNotFoundException JavaDoc exception)
259           {
260             // We can't find the class, so we don't want this classloader,
261
// but instead want the last result that was able to load the class.
262
//
263
break;
264           }
265         }
266
267         // Register with the upper most classloader that's able to load the class.
268
//
269
return delegateRegistry(result).put(key, value);
270       }
271     }
272
273     public Object JavaDoc remove(Object JavaDoc key)
274     {
275       return delegateRegistry().remove(key);
276     }
277
278     public void putAll(Map JavaDoc map)
279     {
280       for (Iterator JavaDoc i = map.entrySet().iterator(); i.hasNext(); )
281       {
282         Map.Entry JavaDoc entry = (Map.Entry JavaDoc)i.next();
283         put(entry.getKey(), entry.getValue());
284       }
285     }
286
287     public void clear()
288     {
289       delegateRegistry().clear();
290     }
291
292     public Set JavaDoc keySet()
293     {
294       return delegateRegistry().keySet();
295     }
296
297     public Collection JavaDoc values()
298     {
299       return delegateRegistry().values();
300     }
301
302     public Set JavaDoc entrySet()
303     {
304       return delegateRegistry().entrySet();
305     }
306   }
307 }
308
Popular Tags