KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > javacore > jmiimpl > javamodel > JavaPackageImpl


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 package org.netbeans.modules.javacore.jmiimpl.javamodel;
20
21 import java.util.*;
22 import javax.jmi.model.MofClass;
23 import javax.jmi.model.NameNotFoundException;
24 import javax.jmi.reflect.ConstraintViolationException;
25 import javax.jmi.reflect.InvalidObjectException;
26 import javax.jmi.reflect.RefFeatured;
27 import javax.jmi.reflect.RefObject;
28 import org.netbeans.api.java.classpath.ClassPath;
29 import org.netbeans.jmi.javamodel.Element;
30 import org.netbeans.jmi.javamodel.ElementPartKind;
31 import org.netbeans.jmi.javamodel.JavaPackage;
32 import org.netbeans.jmi.javamodel.Resource;
33 import org.netbeans.mdr.handlers.InstanceHandler;
34 import org.netbeans.mdr.persistence.StorageException;
35 import org.netbeans.mdr.storagemodel.StorableObject;
36 import org.netbeans.modules.javacore.ClassIndex;
37 import org.netbeans.modules.javacore.internalapi.JavaMetamodel;
38 import org.openide.ErrorManager;
39 import org.openide.filesystems.FileObject;
40
41 /**
42  *
43  * @author Martin Matula
44  */

45 public abstract class JavaPackageImpl extends InstanceHandler implements JavaPackage {
46     String JavaDoc name = null;
47     private final boolean isProxy;
48     private final SubPackagesCollection subpackages;
49     private final ResourcesCollection resources;
50     private final HashSet representedPackages;
51     private boolean simpleDelete = false;
52     
53     private boolean isValid = true;
54
55     /** Creates a new instance of PackageImpl */
56     public JavaPackageImpl(StorableObject s) {
57         super(s);
58         if (s instanceof DeferredObject) {
59             subpackages = new SubPackagesCollection();
60             resources = new ResourcesCollection();
61             representedPackages = new HashSet();
62             isProxy = true;
63         } else {
64             subpackages = null;
65             resources = null;
66             representedPackages = null;
67             isProxy = false;
68         }
69     }
70
71     public String JavaDoc getName() {
72         if (isProxy) {
73             return name;
74         } else {
75             return super_getName();
76         }
77     }
78
79     protected abstract String JavaDoc super_getName();
80
81     public void setName(String JavaDoc name) {
82         RefObject nameAttr = null;
83         try {
84             nameAttr = ((MofClass) refMetaObject()).lookupElementExtended("name"); // NOI18N
85
} catch (NameNotFoundException e) {
86             // ignore
87
}
88         throw new ConstraintViolationException(this, nameAttr, "Name attribute is readonly."); // NOI18N
89
}
90
91     public Collection getSubPackages() {
92         return isProxy ? subpackages : super_getSubPackages();
93     }
94
95     public Collection getResources() {
96         return isProxy ? resources : super_getResources();
97     }
98     
99     public Resource getResource() {
100         return null;
101     }
102
103     protected abstract Collection super_getSubPackages();
104     protected abstract Collection super_getResources();
105
106     void addSubPackage(JavaPackageImpl pkg) {
107 // System.out.println("setting parent of: " + pkg.getName() + " to: " + getName());
108
if (isProxy) {
109             try {
110                 ((StorableObject) pkg._getDelegate()).setComposite(_getDelegate(), null, null);
111             } catch (StorageException e) {
112                 ErrorManager.getDefault().notify(e);
113             }
114             subpackages.addSubPackage(pkg);
115         } else {
116             super_getSubPackages().add(pkg);
117         }
118     }
119
120     void removeRealPackage(JavaPackageImpl real) {
121         if (!isProxy) throw new IllegalStateException JavaDoc();
122         representedPackages.remove(real);
123         if (representedPackages.isEmpty()) {
124             refDelete();
125         }
126     }
127
128     void addRealPackage(JavaPackageImpl real) {
129         if (!isProxy) throw new IllegalStateException JavaDoc();
130         if (!representedPackages.add(real)) {
131             throw new IllegalStateException JavaDoc();
132         }
133     }
134     
135     public boolean isValid() {
136         return isValid;
137     }
138
139     protected void _delete() {
140         if (!isProxy) {
141             if (!simpleDelete) ((JavaPackageClassImpl) refClass()).removeRealPackage(this);
142             super._delete();
143         } else {
144             JavaPackageClassImpl.removePackage(this);
145             for (Iterator it = representedPackages.iterator(); it.hasNext();) {
146                 JavaPackageImpl pck = (JavaPackageImpl) it.next();
147                 pck.delete();
148             }
149             representedPackages.clear();
150             JavaPackageImpl parent = (JavaPackageImpl) super._immediateComposite();
151             if (parent != null) {
152                 parent.subpackages.removeSubPackage(this);
153             }
154         }
155         isValid = false;
156     }
157     
158     private void delete() {
159         simpleDelete = true;
160         try {
161             refDelete();
162         } finally {
163             simpleDelete = false;
164         }
165     }
166
167     protected RefFeatured _immediateComposite() {
168         JavaPackage composite = (JavaPackage) super._immediateComposite();
169         return composite == null ? null : ((JavaPackageClassImpl) refClass()).resolvePackage(composite.getName(), true);
170     }
171
172     protected RefFeatured _outermostComposite() {
173         JavaPackage composite = (JavaPackage) super._outermostComposite();
174         return ((JavaPackageClassImpl) refClass()).resolvePackage(composite.getName(), true);
175     }
176
177     public Collection getReferences() {
178         Resource[] res = findReferencedResources();
179         UsageFinder finder = new UsageFinder(this);
180         return finder.getUsers(res);
181     }
182
183     private Resource[] findReferencedResources() {
184         StringTokenizer tokenizer = new StringTokenizer(getName(), "."); //NOI18N
185
String JavaDoc part = tokenizer.nextToken();
186         List result = new ArrayList(Arrays.asList((Object JavaDoc[]) ClassIndex.findResourcesForIdentifier(part)));
187
188         while (tokenizer.hasMoreTokens()) {
189             part = tokenizer.nextToken();
190             result.retainAll(Arrays.asList((Object JavaDoc[]) ClassIndex.findResourcesForIdentifier(part)));
191         }
192         return (Resource[]) result.toArray(new Resource[result.size()]);
193     }
194
195     boolean shouldInclude() {
196         return !(getResources().isEmpty() && getSubPackages().isEmpty());
197     }
198
199     private static class SubPackagesCollection extends AbstractCollection {
200         private HashSet subpackages = new HashSet();
201
202         private SubPackagesCollection() {
203         }
204         
205         private void _lock(boolean write) {
206             JavaMetamodel.getDefaultRepository().beginTrans(write);
207         }
208         
209         private void _unlock() {
210             JavaMetamodel.getDefaultRepository().endTrans(false);
211         }
212
213         public int size() {
214             _lock(false);
215             try {
216                 int size = 0;
217                 for (Iterator it = iterator(); it.hasNext(); size++, it.next());
218                 return size;
219             } finally {
220                 _unlock();
221             }
222         }
223
224         public boolean isEmpty() {
225             _lock(false);
226             try {
227                 return !iterator().hasNext();
228             } finally {
229                 _unlock();
230             }
231         }
232
233         public boolean contains(Object JavaDoc o) {
234             _lock(false);
235             try {
236                 return subpackages.contains(o) && ((JavaPackageImpl) o).shouldInclude();
237             } finally {
238                 _unlock();
239             }
240         }
241
242         public Iterator iterator() {
243             _lock(false);
244             try {
245                 return new Iterator() {
246                     private JavaPackage next = null;
247                     private Iterator iterator = subpackages.iterator();
248
249                     public boolean hasNext() {
250                         _lock(false);
251                         try {
252                             while (iterator.hasNext() && next == null) {
253                                 JavaPackageImpl temp = (JavaPackageImpl) iterator.next();
254                                 if (temp.subpackages.isReallyEmpty() && temp.resources.isReallyEmpty()) {
255                                     iterator.remove();
256                                     temp.refDelete();
257                                 } else {
258                                     if (temp.shouldInclude()) {
259                                         next = temp;
260                                     }
261                                 }
262                             }
263                             return next != null;
264                         } finally {
265                             _unlock();
266                         }
267                     }
268
269                     public Object JavaDoc next() {
270                         _lock(false);
271                         try {
272                             if (!hasNext()) {
273                                 throw new NoSuchElementException();
274                             }
275                             JavaPackage result = next;
276                             next = null;
277                             return result;
278                         } finally {
279                             _unlock();
280                         }
281                     }
282
283                     public void remove() {
284                         throw new UnsupportedOperationException JavaDoc();
285                     }
286                 };
287             } finally {
288                 _unlock();
289             }
290         }
291
292         void addSubPackage(JavaPackage pkg) {
293             subpackages.add(pkg);
294         }
295         
296         void removeSubPackage(JavaPackage pkg) {
297             subpackages.remove(pkg);
298         }
299
300         boolean isReallyEmpty() {
301             return subpackages.isEmpty();
302         }
303     }
304     
305     private void handleInvalid(InvalidObjectException e) {
306         System.err.println("Invalid represented package for: " + getName() + " toString: " + toString());
307         System.err.println("...number of represented packages: " + representedPackages.size());
308         System.err.println("...resolvePackage(getName()) returns " + ((JavaPackageClassImpl) refClass()).resolvePackage(getName(), true));
309         throw e;
310     }
311
312     private class ResourcesCollection extends AbstractCollection {
313         private ResourcesCollection() {
314         }
315
316         public int size() {
317             _lock(false);
318             try {
319                 int size = 0;
320                 for (Iterator it = iterator(); it.hasNext(); size++, it.next());
321                 return size;
322             } finally {
323                 _unlock();
324             }
325         }
326
327         public boolean isEmpty() {
328             _lock(false);
329             try {
330                 return !iterator().hasNext();
331             } finally {
332                 _unlock();
333             }
334         }
335
336         public boolean contains(Object JavaDoc o) {
337             _lock(false);
338             try {
339                 Set roots = createRootsSet();
340                 Iterator it = representedPackages.iterator();
341                 boolean contains = false;
342                 while (it.hasNext() && !contains) {
343                     JavaPackage pkg = (JavaPackage) it.next();
344                     try {
345                         if (roots.contains(pkg.refImmediatePackage())) {
346                             contains = pkg.getResources().contains(o);
347                         }
348                     } catch (InvalidObjectException e) {
349                         handleInvalid(e);
350                     }
351                 }
352                 return contains;
353             } finally {
354                 _unlock();
355             }
356         }
357
358         private Set createRootsSet() {
359             ClassPath cp = JavaMetamodel.getManager().getClassPath();
360             FileObject[] fo = cp.getRoots();
361             HashSet roots = new HashSet(fo.length);
362             for (int i = 0; i < fo.length; i++) {
363                 roots.add(org.netbeans.modules.javacore.internalapi.JavaMetamodel.getManager().getJavaExtent(fo[i]));
364             }
365             return roots;
366         }
367
368         public Iterator iterator() {
369             _lock(false);
370             try {
371                 return new Iterator() {
372                     private Iterator packages = representedPackages.iterator();
373                     private Set roots = createRootsSet();
374                     private Iterator resources = null;
375
376                     public boolean hasNext() {
377                         _lock(false);
378                         try {
379                             while ((resources == null || !resources.hasNext()) && packages.hasNext()) {
380                                 JavaPackage pkg = (JavaPackage) packages.next();
381                                 try {
382                                     if (roots.contains(pkg.refImmediatePackage())) {
383                                         resources = pkg.getResources().iterator();
384                                     }
385                                 } catch (InvalidObjectException e) {
386                                     handleInvalid(e);
387                                 }
388                             }
389                             return resources != null && resources.hasNext();
390                         } finally {
391                             _unlock();
392                         }
393                     }
394
395                     public Object JavaDoc next() {
396                         _lock(false);
397                         try {
398                             if (!hasNext()) {
399                                 throw new NoSuchElementException();
400                             }
401                             return resources.next();
402                         } finally {
403                             _unlock();
404                         }
405                     }
406
407                     public void remove() {
408                         throw new UnsupportedOperationException JavaDoc();
409                     }
410                 };
411             } finally {
412                 _unlock();
413             }
414         }
415
416         boolean isReallyEmpty() {
417             for (Iterator it = representedPackages.iterator(); it.hasNext();) {
418                 try {
419                     if (!((JavaPackage) it.next()).getResources().isEmpty()) {
420                         return false;
421                     }
422                 } catch (InvalidObjectException e) {
423                     handleInvalid(e);
424                 }
425             }
426             return true;
427         }
428     }
429     
430     public List getChildren() {
431         ArrayList result = new ArrayList(getSubPackages());
432         result.addAll(getResources());
433         return result;
434     }
435     
436     public void replaceChild(Element oldElement, Element newElement) {
437         throw new UnsupportedOperationException JavaDoc();
438     }
439     
440     public int getStartOffset() {
441         throw new UnsupportedOperationException JavaDoc();
442     }
443     
444     public int getEndOffset() {
445         throw new UnsupportedOperationException JavaDoc();
446     }
447     
448     public int getPartStartOffset(ElementPartKind part) {
449         throw new UnsupportedOperationException JavaDoc();
450     }
451
452     public int getPartEndOffset(ElementPartKind part) {
453         throw new UnsupportedOperationException JavaDoc();
454     }
455     
456     public Element duplicate() {
457         throw new UnsupportedOperationException JavaDoc("The operation is intentionally unsupported at this element."); // NOI18N
458
}
459 }
460
Popular Tags