alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

What this is

This file is included in the DevDaily.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Java by Example" TM.

Other links

The source code

/*
 *                 Sun Public License Notice
 * 
 * The contents of this file are subject to the Sun Public License
 * Version 1.0 (the "License"). You may not use this file except in
 * compliance with the License. A copy of the License is available at
 * http://www.sun.com/
 * 
 * The Original Code is NetBeans. The Initial Developer of the Original
 * Code is Sun Microsystems, Inc. Portions Copyright 1997-2000 Sun
 * Microsystems, Inc. All Rights Reserved.
 */

package org.netbeans.core.deprecated;

import java.util.Enumeration;
import java.security.PermissionCollection;
import java.security.Permissions;
import java.security.AllPermission;
import java.beans.*;

import org.openide.execution.NbClassLoader;
import org.openide.filesystems.*;
import org.openide.util.WeakListener;
import org.openide.ErrorManager;
import org.openide.util.Lookup;
import org.openide.util.LookupEvent;
import org.openide.util.LookupListener;

/** Classloader for the filesystem pool. Attaches itself as a listener to
 * each file a class has been loaded from. If such a file is deleted, modified
 * or renamed clears the global variable that holds "current" classloader, so
 * on next request for current one new is created.
 *
 * @author Jaroslav Tulach
 */
class ClassLoaderSupport extends NbClassLoader
    implements FileChangeListener, RepositoryListener, PropertyChangeListener {
    /** change listener */
    private FileChangeListener listener;
    /** PropertyChangeListener */
    private PropertyChangeListener propListener;

    
    /** the pool */
    private static final Repository POOL = Repository.getDefault ();
    /** holds current classloader (or null if not created yet) */
    private static ClassLoaderSupport current;
    /** contains AllPermission */
    private static PermissionCollection allPermission;
    private static boolean firstTime = true;

    /** @return the current classloader for the system */
    synchronized static ClassLoader currentClassLoader () {
        /*
        ClassLoader c = current;
        if (c == null) {
            c = createClassLoader ();
        }
        return c;
    }

    /** Creates new classloader. Synchronized to allow only
    * one access the current at time.
    * /
    synchronized static ClassLoader createClassLoader () {
        */
        if (current == null) {
            current = new ClassLoaderSupport ();
            if (firstTime) {
                firstTime = false;
                Lookup.getDefault().lookup(new Lookup.Template(ClassLoader.class)).addLookupListener(new LookupListener() {
                    public void resultChanged(LookupEvent e) {
                        if (current != null) {
                            current.reset();
                        }
                    }
                });
            }
        }
        return current;
    }

    /** Constructor that attaches itself to the filesystem pool.
    */
    public ClassLoaderSupport () {
        POOL.addRepositoryListener ((RepositoryListener)WeakListener.create (RepositoryListener.class, this, POOL));
        listener = org.openide.filesystems.FileUtil.weakFileChangeListener (this, null);
        Enumeration en = POOL.fileSystems();
        propListener = WeakListener.propertyChange (this, null);
        while (en.hasMoreElements()) {
            FileSystem fs = (FileSystem)en.nextElement();
            fs.addPropertyChangeListener (propListener);        
        }        
        setDefaultPermissions(getAllPermissions());
    }

    protected void finalize () {
        ErrorManager.getDefault().getInstance("org.netbeans.core").log ("Collected currentClassLoader");
    }

    /** When looking for a file, register listener on its changes.
    * @param name of the class
    * @return the Class or null
    */
    protected Class findClass (String name) throws ClassNotFoundException {
        Class c = super.findClass (name);
        if (c != null) {
            FileObject fo;
            int lastDot = name.lastIndexOf ('.');
            if (lastDot == -1) {
                fo = POOL.find ("", name, "class"); // NOI18N
            } else {
                fo = POOL.find (name.substring (0, lastDot), name.substring (lastDot + 1), "class"); // NOI18N
            }
            if (fo != null) {
                // if the file is from the file system pool,
                // register to catch its changes
                fo.addFileChangeListener (listener);
            }
        }
        return c;
    }


    /** Tests whether this object is current loader and if so,
    * clears the loader.
    * @param fo file object that initiated the action
    */
    private void test (FileObject fo) {
        if (current == this) {
            reset ();
        }
        fo.removeFileChangeListener (listener);
    }

    /** Resets the loader, removes it from listneing on all known objects.
    */
    private synchronized void reset () {
        if (current == this) {
            current = null;
        }
    }

    /** If this object is not current classloader, removes it from
    * listening on given file object.
    */
    private void testRemove (FileObject fo) {
        if (current != this) {
            fo.removeFileChangeListener (listener);
        }
    }



    /** Called when new file system is added to the pool.
    * @param ev event describing the action
    */
    public void fileSystemAdded (RepositoryEvent ev) {
        FileSystem fs = ev.getFileSystem();
        fs.addPropertyChangeListener (propListener);                
        reset ();
    }

    /** Called when a file system is deleted from the pool.
    * @param ev event describing the action
    */
    public void fileSystemRemoved (RepositoryEvent ev) {
        FileSystem fs = ev.getFileSystem();        
        fs.removePropertyChangeListener (propListener);                        
        reset ();
    }

    /** Resets the loader.
    */
    public void fileSystemPoolReordered (RepositoryReorderedEvent ev) {
        reset ();
    }

    /** Fired when a new folder has been created. This action can only be
    * listened in folders containing the created file up to the root of
    * file system.
    *
    * @param fe the event describing context where action has taken place
    */
    public void fileFolderCreated (FileEvent fe) {
        testRemove (fe.getFile ());
    }

    /** Fired when a new file has been created. This action can only be
    * listened in folders containing the created file up to the root of
    * file system.
    *
    * @param fe the event describing context where action has taken place
    */
    public void fileDataCreated (FileEvent fe) {
        testRemove (fe.getFile ());
    }

    /** Fired when a file has been changed.
    * @param fe the event describing context where action has taken place
    */
    public void fileChanged (FileEvent fe) {
        test (fe.getFile ());
    }

    /** Fired when a file has been deleted.
    * @param fe the event describing context where action has taken place
    */
    public void fileDeleted (FileEvent fe) {
        test (fe.getFile ());
    }

    /** Fired when a file has been renamed.
    * @param fe the event describing context where action has taken place
    *           and the original name and extension.
    */
    public void fileRenamed (FileRenameEvent fe) {
        test (fe.getFile ());
    }

    /** Fired when a file attribute has been changed.
    * @param fe the event describing context where action has taken place,
    *           the name of attribute and old and new value.
    */
    public void fileAttributeChanged (FileAttributeEvent fe) {
        testRemove (fe.getFile ());
    }
    
    /** Getter for allPermissions */
    static synchronized PermissionCollection getAllPermissions() {
        if (allPermission == null) {
            allPermission = new Permissions();
            allPermission.add(new AllPermission());
        }
        return allPermission;
    }

    /**
     * This method gets called when a bound property is changed.
     * @param evt A PropertyChangeEvent object describing the event source 
     *  	and the property that has changed.
     */
    public void propertyChange(PropertyChangeEvent evt) {
        if (evt.getPropertyName().equals("root"))  // NOI18N
            reset ();                
    }

}
... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.