1 19 20 package org.netbeans.api.java.classpath; 21 22 import java.beans.PropertyChangeListener ; 23 import java.net.URL ; 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 41 class ClassLoaderSupport extends NbClassLoader 42 implements FileChangeListener, PropertyChangeListener { 43 44 public static ClassLoader create(ClassPath cp) { 45 try { 46 return new ClassLoaderSupport(cp); 47 } catch (FileStateInvalidException e) { 48 throw new AssertionError (e); 50 } 51 } 52 53 54 private org.openide.filesystems.FileChangeListener listener; 55 56 57 private java.beans.PropertyChangeListener propListener; 58 59 60 private static java.security.PermissionCollection allPermission; 61 62 private static boolean firstTime = true; 63 64 67 private ClassPath classPath; 68 69 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 87 protected Class findClass (String name) throws ClassNotFoundException { 88 Class c = super.findClass (name); 89 if (c != null) { 90 org.openide.filesystems.FileObject fo; 91 String resName = name.replace('.', '/') + ".class"; fo = classPath.findResource(resName); 93 if (fo != null) { 94 fo.addFileChangeListener (listener); 97 } 98 } 99 return c; 100 } 101 102 107 public URL findResource (String name) { 108 URL url = super.findResource (name); 109 if (url != null) { 110 FileObject fo = classPath.findResource(name); 111 if (fo != null) { 112 fo.addFileChangeListener (listener); 115 } 116 } 117 return url; 118 } 119 120 124 private void test (org.openide.filesystems.FileObject fo) { 125 classPath.resetClassLoader(this); 126 fo.removeFileChangeListener (listener); 127 } 128 129 131 private void reset () { 132 classPath.resetClassLoader(this); 133 } 134 135 138 private void testRemove (org.openide.filesystems.FileObject fo) { 139 fo.removeFileChangeListener (listener); 140 } 141 142 148 public void fileFolderCreated (org.openide.filesystems.FileEvent fe) { 149 testRemove (fe.getFile ()); 150 } 151 152 158 public void fileDataCreated (org.openide.filesystems.FileEvent fe) { 159 testRemove (fe.getFile ()); 160 } 161 162 165 public void fileChanged (org.openide.filesystems.FileEvent fe) { 166 test (fe.getFile ()); 167 } 168 169 172 public void fileDeleted (org.openide.filesystems.FileEvent fe) { 173 test (fe.getFile ()); 174 } 175 176 180 public void fileRenamed (org.openide.filesystems.FileRenameEvent fe) { 181 test (fe.getFile ()); 182 } 183 184 188 public void fileAttributeChanged (org.openide.filesystems.FileAttributeEvent fe) { 189 testRemove (fe.getFile ()); 190 } 191 192 193 static synchronized java.security.PermissionCollection getAllPermissions() { 194 if (allPermission == null) { 195 allPermission = new java.security.Permissions (); 196 allPermission.add(new java.security.AllPermission ()); 197 } 198 return allPermission; 199 } 200 201 206 public void propertyChange(java.beans.PropertyChangeEvent evt) { 207 if (ClassPath.PROP_ROOTS.equals(evt.getPropertyName())) 208 reset(); 209 } 210 } 211 | Popular Tags |