KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > java > classpath > ClassLoaderSupport


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.api.java.classpath;
21
22 import java.beans.PropertyChangeListener JavaDoc;
23 import java.net.URL JavaDoc;
24 import java.util.*;
25
26 import org.openide.execution.NbClassLoader;
27 import org.openide.filesystems.*;
28 import org.openide.util.WeakListeners;
29 import org.openide.ErrorManager;
30 import org.openide.util.Lookup;
31 import org.openide.util.LookupEvent;
32 import org.openide.util.LookupListener;
33
34 /** Classloader for the filesystem pool. Attaches itself as a listener to
35  * each file a class has been loaded from. If such a file is deleted, modified
36  * or renamed clears the global variable that holds "current" classloader, so
37  * on next request for current one new is created.
38  *
39  * @author Jaroslav Tulach
40  */

41 class ClassLoaderSupport extends NbClassLoader
42     implements FileChangeListener, PropertyChangeListener JavaDoc {
43     
44     public static ClassLoader JavaDoc create(ClassPath cp) {
45         try {
46             return new ClassLoaderSupport(cp);
47         } catch (FileStateInvalidException e) {
48             // Should not happen, we already trimmed unused roots:
49
throw new AssertionError JavaDoc(e);
50         }
51     }
52
53     /** change listener */
54     private org.openide.filesystems.FileChangeListener listener;
55
56     /** PropertyChangeListener */
57     private java.beans.PropertyChangeListener JavaDoc propListener;
58
59     /** contains AllPermission */
60     private static java.security.PermissionCollection JavaDoc allPermission;
61
62     private static boolean firstTime = true;
63
64     /**
65      * The ClassPath to load classes from.
66      */

67     private ClassPath classPath;
68
69     /** Constructor that attaches itself to the filesystem pool.
70     */

71     private ClassLoaderSupport (ClassPath cp) throws FileStateInvalidException {
72         super(cp.getRoots(), ClassLoader.getSystemClassLoader(), null);
73         this.classPath = cp;
74
75         setDefaultPermissions(getAllPermissions());
76         listener = FileUtil.weakFileChangeListener(this, null);
77         propListener = WeakListeners.propertyChange (this, null);
78         cp.addPropertyChangeListener(propListener);
79     }
80
81     /**
82      * Tries to locate the .class file on the ClassPath
83      * @param name
84      * @return
85      * @throws ClassNotFoundException
86      */

87     protected Class JavaDoc findClass (String JavaDoc name) throws ClassNotFoundException JavaDoc {
88         Class JavaDoc c = super.findClass (name);
89         if (c != null) {
90             org.openide.filesystems.FileObject fo;
91             String JavaDoc resName = name.replace('.', '/') + ".class"; // NOI18N
92
fo = classPath.findResource(resName);
93             if (fo != null) {
94                 // if the file is from the file system pool,
95
// register to catch its changes
96
fo.addFileChangeListener (listener);
97             }
98         }
99         return c;
100     }
101     
102     /**
103      * Tries to locate the resource on the ClassPath
104      * @param name
105      * @return URL of the resource
106      */

107     public URL JavaDoc findResource (String JavaDoc name) {
108         URL JavaDoc url = super.findResource (name);
109         if (url != null) {
110             FileObject fo = classPath.findResource(name);
111             if (fo != null) {
112                 // if the file is from the file system pool,
113
// register to catch its changes
114
fo.addFileChangeListener (listener);
115             }
116         }
117         return url;
118     }
119
120     /** Tests whether this object is current loader and if so,
121     * clears the loader.
122     * @param fo file object that initiated the action
123     */

124     private void test (org.openide.filesystems.FileObject fo) {
125         classPath.resetClassLoader(this);
126         fo.removeFileChangeListener (listener);
127     }
128
129     /** Resets the loader, removes it from listneing on all known objects.
130     */

131     private void reset () {
132         classPath.resetClassLoader(this);
133     }
134
135     /** If this object is not current classloader, removes it from
136     * listening on given file object.
137     */

138     private void testRemove (org.openide.filesystems.FileObject fo) {
139         fo.removeFileChangeListener (listener);
140     }
141
142     /** Fired when a new folder has been created. This action can only be
143     * listened in folders containing the created file up to the root of
144     * file system.
145     *
146     * @param fe the event describing context where action has taken place
147     */

148     public void fileFolderCreated (org.openide.filesystems.FileEvent fe) {
149         testRemove (fe.getFile ());
150     }
151
152     /** Fired when a new file has been created. This action can only be
153     * listened in folders containing the created file up to the root of
154     * file system.
155     *
156     * @param fe the event describing context where action has taken place
157     */

158     public void fileDataCreated (org.openide.filesystems.FileEvent fe) {
159         testRemove (fe.getFile ());
160     }
161
162     /** Fired when a file has been changed.
163     * @param fe the event describing context where action has taken place
164     */

165     public void fileChanged (org.openide.filesystems.FileEvent fe) {
166         test (fe.getFile ());
167     }
168
169     /** Fired when a file has been deleted.
170     * @param fe the event describing context where action has taken place
171     */

172     public void fileDeleted (org.openide.filesystems.FileEvent fe) {
173         test (fe.getFile ());
174     }
175
176     /** Fired when a file has been renamed.
177     * @param fe the event describing context where action has taken place
178     * and the original name and extension.
179     */

180     public void fileRenamed (org.openide.filesystems.FileRenameEvent fe) {
181         test (fe.getFile ());
182     }
183
184     /** Fired when a file attribute has been changed.
185     * @param fe the event describing context where action has taken place,
186     * the name of attribute and old and new value.
187     */

188     public void fileAttributeChanged (org.openide.filesystems.FileAttributeEvent fe) {
189         testRemove (fe.getFile ());
190     }
191     
192     /** Getter for allPermissions */
193     static synchronized java.security.PermissionCollection JavaDoc getAllPermissions() {
194         if (allPermission == null) {
195             allPermission = new java.security.Permissions JavaDoc();
196             allPermission.add(new java.security.AllPermission JavaDoc());
197         }
198         return allPermission;
199     }
200
201     /**
202      * This method gets called when a bound property is changed.
203      * @param evt A PropertyChangeEvent object describing the event source
204      * and the property that has changed.
205      */

206     public void propertyChange(java.beans.PropertyChangeEvent JavaDoc evt) {
207         if (ClassPath.PROP_ROOTS.equals(evt.getPropertyName()))
208             reset();
209     }
210 }
211
Popular Tags