KickJava   Java API By Example, From Geeks To Geeks.

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


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.reflect.ConstraintViolationException;
23 import org.netbeans.jmi.javamodel.JavaModelPackage;
24 import org.netbeans.jmi.javamodel.JavaPackage;
25 import org.netbeans.jmi.javamodel.JavaPackageClass;
26 import org.netbeans.mdr.handlers.ClassProxyHandler;
27 import org.netbeans.mdr.persistence.MOFID;
28 import org.netbeans.mdr.persistence.StorageException;
29 import org.netbeans.mdr.storagemodel.StorableBaseObject;
30 import org.netbeans.mdr.storagemodel.StorableClass;
31 import org.netbeans.mdr.storagemodel.StorableObject;
32 import org.netbeans.mdr.util.DebugException;
33 import org.netbeans.modules.javacore.JMManager;
34 import org.openide.ErrorManager;
35
36 /**
37  *
38  * @author Martin Matula
39  */

40 public abstract class JavaPackageClassImpl extends ClassProxyHandler implements JavaPackageClass {
41     private static final String JavaDoc MOFID_PREFIX = "package:"; // NOI18N
42
private static final HashMap allInstances = new HashMap();
43     
44     private static JavaPackageClassImpl mainProxy = null;
45     
46     private static JavaPackageClassImpl getMainProxy() {
47         // initialize the "main" class proxy - the class proxy that the "proxy" packages will be associated with
48
// (it will be the class proxy for the JavaPackage class for the default filesystem
49
// codebase)
50
if (mainProxy == null) {
51             JavaModelPackage pkg = org.netbeans.modules.javacore.internalapi.JavaMetamodel.getManager().getDefaultExtent();
52             mainProxy = (JavaPackageClassImpl) pkg.getJavaPackage();
53         }
54         return mainProxy;
55     }
56
57     static void removePackage(JavaPackage pkg) {
58         allInstances.remove(pkg.getName());
59     }
60     
61     /** Creates a new instance of PackageClassImpl */
62     public JavaPackageClassImpl(StorableClass s) {
63         super(s);
64     }
65     
66     public JavaPackage createJavaPackage(String JavaDoc name, List annotations) {
67         throw new UnsupportedOperationException JavaDoc("Cannot explicitly create a package."); // NOI18N
68
}
69     
70     protected abstract JavaPackage super_createJavaPackage(String JavaDoc name, List annotations);
71     
72     public JavaPackage createJavaPackage() {
73         throw new ConstraintViolationException(this, refMetaObject(), "Cannot create package without a name."); // NOI18N
74
}
75     
76     public JavaPackage resolvePackage(String JavaDoc name, boolean forceInclude) {
77         // if this is not the main proxy, delegate the call to the main proxy
78
if (getMainProxy() != this) {
79             return getMainProxy().resolvePackage(name, forceInclude);
80         }
81         JavaPackageImpl result = (JavaPackageImpl) allInstances.get(name);
82         return (forceInclude || (result != null && result.shouldInclude())) ? result : null;
83     }
84
85     public JavaPackage resolvePackage(String JavaDoc name) {
86         repository().beginTrans(false);
87         try {
88             boolean force = false;
89             if (getMainProxy() != this) {
90                 force = (findRealPackage(name) != null);
91             }
92             return resolvePackage(name, force);
93         } finally {
94             repository().endTrans();
95         }
96     }
97
98     JavaPackageImpl resolveOrCreatePackage(String JavaDoc name) {
99         JavaPackageImpl result = (JavaPackageImpl) allInstances.get(name);
100         if (result == null) {
101             try {
102                 JavaPackageImpl parent = null;
103                 if (name.length() > 0) {
104                     int index = name.lastIndexOf('.');
105                     String JavaDoc parentName = index > 0 ? name.substring(0, index) : "";
106                     parent = resolveOrCreatePackage(parentName);
107                 }
108                 StorableBaseObject s = _getDelegate();
109                 MOFID mofId = new MOFID(name.hashCode(), MOFID_PREFIX + name);
110                 DeferredObject o = new DeferredObject(mofId, s.getMdrStorage(), s.getImmediatePackageId(), s.getOutermostPackageId(), s.getMetaObject(), (StorableClass) s, parent == null ? null : (StorableObject) parent._getDelegate());
111                 result = (JavaPackageImpl) _getRepository().getHandler(o);
112                 result.name = name;
113                 allInstances.put(name, result);
114                 if (parent != null) {
115                     parent.addSubPackage(result);
116                 }
117 // System.out.println("adding package: " + name);
118
// add to the parent package
119
} catch (StorageException e) {
120                 throw new DebugException();
121             }
122         }
123         return result;
124     }
125     
126     JavaPackage resolveRealPackage(String JavaDoc name) {
127         if (name == null) return null;
128         JavaPackageImpl result = findRealPackage(name);
129         if (result == null) {
130             result = (JavaPackageImpl) super_createJavaPackage(name, null);
131             if (name.length() > 0) {
132                 int index = name.lastIndexOf('.');
133                 String JavaDoc parentName = index > 0 ? name.substring(0, index) : "";
134                 JavaPackageImpl parent = (JavaPackageImpl) resolveRealPackage(parentName);
135                 if (parent == null) {
136                     throw new IllegalStateException JavaDoc("parent package for " + name + " not found."); // NOI18N
137
}
138                 parent.addSubPackage(result);
139             }
140             addRealPackage(result);
141         }
142         return result;
143     }
144
145     public JavaPackageImpl findRealPackage(String JavaDoc name) {
146         StorableBaseObject storable = _getDelegate();
147         Collection col = storable.getMdrStorage().getObjectsFromAdditionalIndex(storable.getImmediatePackageId(), "Package.name", name); // NOI18N
148
return col == null || col.isEmpty() ? null : (JavaPackageImpl) _getRepository().getHandler((StorableBaseObject) col.iterator().next());
149     }
150
151     void removeRealPackage(JavaPackageImpl realPackage) {
152         JavaPackageImpl result = (JavaPackageImpl) resolvePackage(realPackage.getName(), true);
153         if (result != null) {
154             result.removeRealPackage(realPackage);
155         } else {
156             JMManager.getLog().log(ErrorManager.WARNING, "proxy package for package " + realPackage.getName() + " not found.");
157         }
158     }
159     
160     void addRealPackage(JavaPackageImpl realPackage) {
161         if (getMainProxy() != this) {
162             getMainProxy().addRealPackage(realPackage);
163             return;
164         }
165         String JavaDoc name = realPackage.getName();
166         JavaPackageImpl result = resolveOrCreatePackage(name);
167         result.addRealPackage(realPackage);
168     }
169     
170     public void addAllRealPackages() {
171         for (Iterator it = super._allOfClass(false).iterator(); it.hasNext();) {
172             addRealPackage((JavaPackageImpl) it.next());
173         }
174     }
175     
176     public void removeAllRealPackages() {
177         for (Iterator it = super._allOfClass(false).iterator(); it.hasNext();) {
178             removeRealPackage((JavaPackageImpl) it.next());
179         }
180     }
181     
182     protected Collection _allOfClass(boolean recursive) {
183         return Collections.unmodifiableCollection(allInstances.values());
184     }
185 }
186
Popular Tags