This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

View | Details | Raw Unified | Return to bug 19717
Collapse All | Expand All

(-)ProxyClassLoader.java (-31 / +19 lines)
Lines 16-21 Link Here
16
import java.util.*;
16
import java.util.*;
17
import java.net.URL;
17
import java.net.URL;
18
import java.io.IOException;
18
import java.io.IOException;
19
import org.openide.*;
19
import org.openide.util.WeakSet;
20
import org.openide.util.WeakSet;
20
import org.openide.util.enum.RemoveDuplicatesEnumeration;
21
import org.openide.util.enum.RemoveDuplicatesEnumeration;
21
import org.openide.util.enum.SequenceEnumeration;
22
import org.openide.util.enum.SequenceEnumeration;
Lines 32-42 Link Here
32
 */
33
 */
33
public class ProxyClassLoader extends ClassLoader {
34
public class ProxyClassLoader extends ClassLoader {
34
    
35
    
35
    /** <code>Set&lt;ProxyClassLoader&gt;</code> of all live instancef of all
36
     * ProxyClassLoaders, usefull when we need to destroy a ProxyClassLoader
37
     * that is in other ProxyClassLoader's list of parents.
38
     */
39
    private static final Set instances = new WeakSet();
40
    // Map<String,ClassLoader>
36
    // Map<String,ClassLoader>
41
    private final Map domainsByPackage = new HashMap(); 
37
    private final Map domainsByPackage = new HashMap(); 
42
    // Map<String,Package>
38
    // Map<String,Package>
Lines 45-50 Link Here
45
    // All parentf of this classloader, including their parents recursively
41
    // All parentf of this classloader, including their parents recursively
46
    private ClassLoader[] parents;
42
    private ClassLoader[] parents;
47
43
44
    /** if true, we have been destroyed */
45
    private boolean dead = false;
48
    
46
    
49
    /** Create a multi-parented classloader.
47
    /** Create a multi-parented classloader.
50
     * @param parents list of parent classloaders. 
48
     * @param parents list of parent classloaders. 
Lines 61-70 Link Here
61
        if (check.contains(null)) throw new IllegalArgumentException("null parent"); // NOI18N
59
        if (check.contains(null)) throw new IllegalArgumentException("null parent"); // NOI18N
62
60
63
        this.parents = coalesceParents(parents);
61
        this.parents = coalesceParents(parents);
64
65
        synchronized (instances) {
66
            instances.add(this);
67
        }
68
    }
62
    }
69
    
63
    
70
    // this is used only by system classloader, maybe we can redesign it a bit
64
    // this is used only by system classloader, maybe we can redesign it a bit
Lines 85-115 Link Here
85
79
86
    
80
    
87
    
81
    
88
    // Is this needed? Before this classloader can go bye-bey, every childer
89
    // of it should be bye-bye too? Except SystemClassLoader
90
    /** Try to destroy this classloader.
82
    /** Try to destroy this classloader.
91
     * It will be removed from the parent lists of all existing ProxyClassLoaders.
83
     * Subsequent attempts to use it will log an error (at most one though).
92
     */
84
     */
93
    public void destroy() {
85
    public void destroy() {
94
        synchronized (instances) {
86
        dead = true;
95
            Iterator it = instances.iterator();
87
    }
96
            while (it.hasNext()) {
88
97
                ProxyClassLoader pcl = (ProxyClassLoader)it.next();
89
    private void zombieCheck(String hint) {
98
                synchronized (pcl) {
90
        if (dead) {
99
                    ClassLoader[] p = pcl.parents;
91
            IllegalStateException ise = new IllegalStateException("Attempting to use a zombie classloader " + this + " on " + hint); // NOI18N
100
                    for (int i = 0; i < p.length; i++) {
92
            TopManager.getDefault().getErrorManager().notify(ErrorManager.INFORMATIONAL, ise);
101
                        if (p[i] == this) {
93
            // don't warn again for same loader... this was enough
102
                            ClassLoader[] p2 = new ClassLoader[p.length - 1];
94
            dead = false;
103
                            if (i > 0) System.arraycopy(p, 0, p2, 0, i);
104
                            if (i < p.length - 1) System.arraycopy(p, i + 1, p2, i, p.length - i - 1);
105
                            pcl.parents = p2;
106
                            break;
107
                        }
108
                    }
109
                    Collection c = pcl.domainsByPackage.values();
110
                    while (c.remove(this)) ;
111
                }
112
            }
113
        }
95
        }
114
    }
96
    }
115
97
Lines 133-138 Link Here
133
     */
115
     */
134
    protected synchronized final Class loadClass(String name, boolean resolve)
116
    protected synchronized final Class loadClass(String name, boolean resolve)
135
                                            throws ClassNotFoundException {
117
                                            throws ClassNotFoundException {
118
        zombieCheck(name);
136
        final int dotIdx = name.lastIndexOf('.');
119
        final int dotIdx = name.lastIndexOf('.');
137
        if (dotIdx == -1) throw new ClassNotFoundException("Will not load classes from default package"); // NOI18N
120
        if (dotIdx == -1) throw new ClassNotFoundException("Will not load classes from default package"); // NOI18N
138
        Class c = smartLoadClass(name, dotIdx);
121
        Class c = smartLoadClass(name, dotIdx);
Lines 175-180 Link Here
175
     * @see #findResource(String)
158
     * @see #findResource(String)
176
     */
159
     */
177
    public final URL getResource(final String name) {
160
    public final URL getResource(final String name) {
161
        zombieCheck(name);
162
        
178
        final int slashIdx = name.lastIndexOf('/');
163
        final int slashIdx = name.lastIndexOf('/');
179
        if (slashIdx == -1) return null;    // won't load from the default package
164
        if (slashIdx == -1) return null;    // won't load from the default package
180
        final String pkg = name.substring(0, slashIdx);
165
        final String pkg = name.substring(0, slashIdx);
Lines 235-240 Link Here
235
     * @throws IOException if I/O errors occur
220
     * @throws IOException if I/O errors occur
236
     */    
221
     */    
237
    protected final synchronized Enumeration findResources(String name) throws IOException {
222
    protected final synchronized Enumeration findResources(String name) throws IOException {
223
        zombieCheck(name);
238
        // Don't bother optimizing this call by domains--it is practically unused.
224
        // Don't bother optimizing this call by domains--it is practically unused.
239
        Enumeration e = simpleFindResources(name);
225
        Enumeration e = simpleFindResources(name);
240
        for (int i = parents.length - 1; i >= 0; i--) {
226
        for (int i = parents.length - 1; i >= 0; i--) {
Lines 265-270 Link Here
265
     * @return the Package corresponding to the given name, or null if not found
251
     * @return the Package corresponding to the given name, or null if not found
266
     */
252
     */
267
    protected Package getPackage(String name) {
253
    protected Package getPackage(String name) {
254
        zombieCheck(name);
268
	synchronized (packages) {
255
	synchronized (packages) {
269
	    Package pkg = (Package)packages.get(name);
256
	    Package pkg = (Package)packages.get(name);
270
            if (pkg != null) return pkg;
257
            if (pkg != null) return pkg;
Lines 305-310 Link Here
305
     * <code>ClassLoader</code>
292
     * <code>ClassLoader</code>
306
     */
293
     */
307
    protected synchronized Package[] getPackages() {
294
    protected synchronized Package[] getPackages() {
295
        zombieCheck(null);
308
        Map all = new HashMap(); // Map<String,Package>
296
        Map all = new HashMap(); // Map<String,Package>
309
        addPackages(all, super.getPackages());
297
        addPackages(all, super.getPackages());
310
        for (int i = 0; i < parents.length; i++) {
298
        for (int i = 0; i < parents.length; i++) {

Return to bug 19717