1 19 20 package org.netbeans.api.project; 21 22 import java.io.File ; 23 import java.net.MalformedURLException ; 24 import java.net.URI ; 25 import java.net.URISyntaxException ; 26 import java.net.URL ; 27 import java.util.ArrayList ; 28 import java.util.List ; 29 import org.netbeans.modules.projectapi.SimpleFileOwnerQueryImplementation; 30 import org.netbeans.spi.project.FileOwnerQueryImplementation; 31 import org.openide.filesystems.FileObject; 32 import org.openide.filesystems.FileUtil; 33 import org.openide.util.Lookup; 34 import org.openide.util.LookupEvent; 35 import org.openide.util.LookupListener; 36 37 50 public class FileOwnerQuery { 51 52 54 private static Lookup.Result<FileOwnerQueryImplementation> implementations; 55 56 57 private static List <FileOwnerQueryImplementation> cache; 58 59 private FileOwnerQuery() {} 60 61 66 public static Project getOwner(FileObject file) { 67 if (file == null) { 68 throw new NullPointerException ("Passed null to FileOwnerQuery.getOwner(FileObject)"); } 70 FileObject archiveRoot = FileUtil.getArchiveFile(file); 71 if (archiveRoot != null) { 72 file = archiveRoot; 73 } 74 for (FileOwnerQueryImplementation q : getInstances()) { 75 Project p = q.getOwner(file); 76 if (p != null) { 77 return p; 78 } 79 } 80 return null; 81 } 82 83 89 public static Project getOwner(URI uri) { 90 if (uri.isOpaque() && "jar".equalsIgnoreCase(uri.getScheme())) { String schemaPart = uri.getSchemeSpecificPart(); 93 int index = schemaPart.lastIndexOf ('!'); if (index>0) { 95 schemaPart = schemaPart.substring(0,index); 96 } 97 try { 100 schemaPart = schemaPart.replace("#", "%23"); 102 uri = new URI (schemaPart); 103 } catch (URISyntaxException ex) { 104 try { 105 URL u = new URL (schemaPart); 106 uri = new File (u.getPath()).toURI(); 108 } catch (MalformedURLException ex2) { 109 ex2.printStackTrace(); 110 assert false : schemaPart; 111 return null; 112 } 113 } 114 } 115 else if (!uri.isAbsolute() || uri.isOpaque()) { 116 throw new IllegalArgumentException ("Bad URI: " + uri); } 118 for (FileOwnerQueryImplementation q : getInstances()) { 119 Project p = q.getOwner(uri); 120 if (p != null) { 121 return p; 122 } 123 } 124 return null; 125 } 126 127 131 static void reset() { 132 SimpleFileOwnerQueryImplementation.reset(); 133 } 134 135 145 public static final int EXTERNAL_ALGORITHM_TRANSIENT = 0; 146 147 172 public static void markExternalOwner(FileObject root, Project owner, int algorithm) throws IllegalArgumentException { 173 switch (algorithm) { 174 case EXTERNAL_ALGORITHM_TRANSIENT: 175 SimpleFileOwnerQueryImplementation.markExternalOwnerTransient(root, owner); 177 break; 178 default: 179 throw new IllegalArgumentException ("No such algorithm: " + algorithm); } 181 } 182 183 208 public static void markExternalOwner(URI root, Project owner, int algorithm) throws IllegalArgumentException { 209 switch (algorithm) { 210 case EXTERNAL_ALGORITHM_TRANSIENT: 211 SimpleFileOwnerQueryImplementation.markExternalOwnerTransient(root, owner); 213 break; 214 default: 215 throw new IllegalArgumentException ("No such algorithm: " + algorithm); } 217 } 218 219 222 223 private static synchronized List <FileOwnerQueryImplementation> getInstances() { 224 if (implementations == null) { 225 implementations = Lookup.getDefault().lookupResult(FileOwnerQueryImplementation.class); 226 implementations.addLookupListener(new LookupListener() { 227 public void resultChanged (LookupEvent ev) { 228 synchronized (FileOwnerQuery.class) { 229 cache = null; 230 } 231 }}); 232 } 233 if (cache == null) { 234 cache = new ArrayList <FileOwnerQueryImplementation>(implementations.allInstances()); 235 } 236 return cache; 237 } 238 239 } 240 | Popular Tags |