KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > mdr > test > ElementsCache


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.mdr.test;
21
22 import java.util.*;
23
24 import org.netbeans.lib.jmi.util.*;
25
26 import javax.jmi.reflect.*;
27 import javax.jmi.model.*;
28
29 public class ElementsCache extends Object JavaDoc {
30
31     // variables ................................................................
32

33     // cache storing structures' fields
34
private HashMap structureFields_cache = new HashMap ();
35
36     // caches storing instance-scoped and class-scoped attributes and references
37
private HashMap instanceAttributes_cache = new HashMap ();
38     private HashMap classAttributes_cache = new HashMap ();
39     private HashMap references_cache = new HashMap ();
40     private HashMap subtypes_cache = new HashMap ();
41     private HashMap associationEnds_cache = new HashMap ();
42     private HashMap classInstances_cache = new HashMap ();
43     private HashMap associationEndInstances_cache = new HashMap ();
44     private RefPackage extent;
45     
46     // storage of all outermost packages
47
private HashMap outermostPackages = new HashMap ();
48     // global cache used by @link #findOutermostPackages method
49
private HashMap trackedPackages;
50     // cache for resolved proxies enabling Enum and Struct creation and Association proxies
51
private HashMap proxies_cache = new HashMap ();
52     
53     // init .....................................................................
54

55     public ElementsCache (RefPackage extent) {
56         this.extent = extent;
57         trackedPackages = new HashMap (); // init global variable ...
58
findOutermostPackages (extent);
59     }
60     
61     // methods ..................................................................
62

63     /**
64      * Detects all outermost packages related to the input extents and stores them.
65      * Stores all related namespaces as well.
66      */

67     private void findOutermostPackages (RefPackage pkg) {
68         if (trackedPackages.get (pkg) != null)
69             return;
70         String JavaDoc name;
71         MofPackage metaObj = (MofPackage) pkg.refMetaObject ();
72         if (metaObj.getContainer() == null) {
73             Iterator iter = metaObj.getQualifiedName ().iterator ();
74             String JavaDoc fqName = (String JavaDoc) iter.next ();
75             while (iter.hasNext ())
76                 fqName = fqName.concat (".").concat ((String JavaDoc) iter.next ());
77             outermostPackages.put (fqName, pkg);
78         }
79         trackedPackages.put (pkg, pkg);
80         Iterator iter = pkg.refAllPackages ().iterator ();
81         while (iter.hasNext ()) {
82             findOutermostPackages ((RefPackage) iter.next ());
83         }
84     }
85     
86     /**
87      * Finds all attributes and references belonging to a mof class and caches them.
88      */

89     private void cacheContainedElements (Classifier classifier) {
90         List temp = new LinkedList ();
91         List superClasses = classifier.allSupertypes ();
92         Namespace namespace = null;
93         Iterator it = superClasses.iterator ();
94         while (it.hasNext ()) {
95             namespace = (Namespace) it.next ();
96             temp.addAll (namespace.getContents ());
97         }
98         temp.addAll (classifier.getContents ());
99         List instanceAttributes = new LinkedList ();
100         List classAttributes = new LinkedList ();
101         List references = new LinkedList ();
102         List assocEnds = new LinkedList ();
103         List assocEndInstances = new LinkedList();
104         
105         it = temp.iterator ();
106         while (it.hasNext ()) {
107             RefObject refObject = (RefObject) it.next ();
108             if (refObject instanceof Feature) {
109                 boolean instanceLevel = ((Feature) refObject).getScope ().equals (ScopeKindEnum.INSTANCE_LEVEL);
110                 if ((refObject instanceof Attribute) && (!((Attribute) refObject).isDerived ())) {
111                     if (instanceLevel) {
112                         instanceAttributes.add (refObject);
113                     } else {
114                         classAttributes.add (refObject);
115                     }
116                 } else if (refObject instanceof Reference) {
117                     Association assoc = (Association) ((Reference) refObject).
118                         getReferencedEnd ().getContainer ();
119                     if (!assoc.isDerived ())
120                         references.add (refObject);
121                 } // else
122
} // if (refObject instanceof Feature)
123
else if (refObject instanceof AssociationEnd) {
124                 assocEnds.add(refObject);
125                 Classifier type = ((AssociationEnd)refObject).getType ();
126                 while (type instanceof AliasType)
127                     type = ((AliasType) type).getType ();
128                 assocEndInstances.add(classInstances((MofClass)type));
129             }
130         } // while
131
instanceAttributes_cache.put (classifier, instanceAttributes);
132         classAttributes_cache.put (classifier, classAttributes);
133         references_cache.put (classifier, references);
134         associationEnds_cache.put (classifier, assocEnds);
135         associationEndInstances_cache.put (classifier, assocEndInstances);
136     }
137
138     /**
139      * For a given mof class, returns list of all instance-scoped attributes
140      * (references are not included).
141      *
142      * @param mofClass
143      * @return list of all non-derived instance-scoped attributes (including inherited ones)
144      */

145     public List instanceAttributes (MofClass mofClass) {
146         List list = (List) instanceAttributes_cache.get (mofClass);
147         if (list == null) {
148             cacheContainedElements (mofClass);
149             list = (List) instanceAttributes_cache.get (mofClass);
150         }
151         return list;
152     }
153     
154     /**
155      * For a given mof class, returns list of all class-scoped attributes.
156      *
157      * @param mofClass
158      * @return list of all non-derived class-scoped attributes (including inherited ones)
159      */

160     public List classAttributes (MofClass mofClass) {
161         List list = (List) classAttributes_cache.get (mofClass);
162         if (list == null) {
163             cacheContainedElements (mofClass);
164             list = (List) classAttributes_cache.get (mofClass);
165         }
166         return list;
167     }
168     
169     /**
170      * For a given mof class, returns list of all references.
171      *
172      * @param mofClass
173      * @return list of all non-derived references (including inherited ones)
174      */

175     private List references (MofClass mofClass) {
176         List list = (List) references_cache.get (mofClass);
177         if (list == null) {
178             cacheContainedElements (mofClass);
179             list = (List) references_cache.get (mofClass);
180         }
181         return list;
182     }
183     
184     /**
185      * Returns list of all fields belonging to the given StructureType.
186      */

187     public List structureFields (StructureType type) {
188         List fields = (List) structureFields_cache.get (type);
189         if (fields != null)
190             return fields;
191         // find fields and cache them
192
fields = new LinkedList ();
193         Iterator content = type.getContents ().iterator ();
194         while (content.hasNext ()) {
195             Object JavaDoc element = content.next ();
196             if (element instanceof StructureField)
197                 fields.add (element);
198         } // while
199
structureFields_cache.put (type, fields);
200         return fields;
201     }
202     
203     /**
204      * Finds proxy object related to the container of a given meta model element.
205      *
206      * @param element meta model element
207      * @return related proxy object or <code>null</code> if proxy cannot be found
208      */

209     public RefBaseObject findProxy (ModelElement element) {
210         RefBaseObject proxy = (RefBaseObject) proxies_cache.get (element);
211         if (proxy != null)
212             return proxy;
213         LinkedList path = new LinkedList ();
214         ModelElement container = element.getContainer ();
215         while (container != null) {
216             path.add (container);
217             container = container.getContainer ();
218         }
219         MofPackage mofPackage = (MofPackage) path.removeLast ();
220         RefPackage refPackage = (RefPackage) outermostPackages.get (mofPackage.getName ());
221         if (refPackage == null) {
222             Iterator iter = outermostPackages.entrySet ().iterator ();
223             while (iter.hasNext ()) {
224                 RefPackage ref = (RefPackage) ((Map.Entry) iter.next ()).getValue ();
225                 MofPackage meta = (MofPackage) ref.refMetaObject ();
226                 if (meta instanceof MofPackage) {
227                     refPackage = ref;
228                     break;
229                 }
230             } // while
231
} // if
232

233         if (refPackage == null)
234             return null;
235         if (path.size () == 0)
236             proxy = refPackage;
237         while (path.size () > 0) {
238             ModelElement elem = (ModelElement) path.removeLast ();
239             if (elem instanceof MofPackage) {
240                 refPackage = refPackage.refPackage (elem);
241                 if (path.size () == 0)
242                     proxy = refPackage;
243             } else {
244                 if ((elem instanceof MofClass) && (path.size () == 0)) {
245                     RefClass refClass = refPackage.refClass (elem);
246                     proxy = refClass;
247                 } else
248                     break;
249             } // else
250
} // while
251
if (proxy != null)
252             proxies_cache.put (element, proxy);
253         return proxy;
254     }
255     
256     private void cacheSubtypes (RefPackage pkg) {
257         Iterator iter = pkg.refAllClasses ().iterator ();
258         while (iter.hasNext ()) {
259             MofClass mofClass = (MofClass) ((RefClass) iter.next ()).refMetaObject ();
260             if (mofClass.isAbstract ())
261                 continue;
262             Iterator supertypes = mofClass.allSupertypes ().iterator ();
263             while (supertypes.hasNext ()) {
264                 MofClass superClass = (MofClass) supertypes.next ();
265                 List list = (List) subtypes_cache.get (superClass);
266                 if (list == null) {
267                     list = new LinkedList ();
268                     subtypes_cache.put (superClass, list);
269                 } // if
270
list.add (mofClass);
271             } // while
272
} // while
273
iter = pkg.refAllPackages ().iterator ();
274         while (iter.hasNext ())
275             cacheSubtypes ((RefPackage) iter.next ());
276     }
277     
278     public List nonAbstractSubtypes (MofClass mofClass) {
279         List subtypes = (List) subtypes_cache.get (mofClass);
280         if (subtypes != null)
281             return subtypes;
282         cacheSubtypes (extent);
283         return (List) subtypes_cache.get (mofClass);
284     }
285     
286     /**
287      * For a given association, returns its ends.
288      *
289      * @param mofAssoc
290      * @return both association ends.
291      */

292     public List associationEnds (Association mofAssoc) {
293         List list = (List) associationEnds_cache.get (mofAssoc);
294         if (list == null) {
295             cacheContainedElements (mofAssoc);
296             list = (List) associationEnds_cache.get (mofAssoc);
297         }
298         return list;
299     }
300
301     /**
302      * For a given association, returns instances that can be used as its ends.
303      *
304      * @param mofAssoc
305      * @return both association ends.
306      */

307     public List associationEndInstances (Association mofAssoc) {
308         List list = (List) associationEndInstances_cache.get (mofAssoc);
309         if (list == null) {
310             cacheContainedElements (mofAssoc);
311             list = (List) associationEndInstances_cache.get (mofAssoc);
312         }
313         return list;
314     }
315     
316     public RefObject[] classInstances(MofClass type) {
317         RefObject[] ret = (RefObject[])classInstances_cache.get(type);
318         if (ret == null) {
319             RefPackage refPackage = (RefPackage) findProxy (type);
320             RefClass proxy = refPackage.refClass (type);
321             Collection col = proxy.refAllOfType ();
322             ret = (RefObject[])col.toArray(new RefObject[col.size()]);
323             classInstances_cache.put(type, ret);
324         }
325         return ret;
326     }
327 }
328
Popular Tags