1 19 20 package org.netbeans.api.options; 21 22 import java.beans.PropertyVetoException ; 23 import java.io.File ; 24 import java.io.IOException ; 25 import java.net.URL ; 26 import java.net.URLStreamHandler ; 27 import java.net.URLStreamHandlerFactory ; 28 import java.util.Enumeration ; 29 import junit.framework.Assert; 30 import org.netbeans.junit.Manager; 31 import org.openide.filesystems.FileObject; 32 import org.openide.filesystems.FileSystem; 33 import org.openide.filesystems.FileUtil; 34 import org.openide.filesystems.LocalFileSystem; 35 import org.openide.filesystems.MultiFileSystem; 36 import org.openide.filesystems.Repository; 37 import org.openide.filesystems.XMLFileSystem; 38 import org.openide.util.Lookup; 39 import org.openide.util.lookup.Lookups; 40 import org.openide.util.lookup.ProxyLookup; 41 42 43 48 public class IDEInitializer extends ProxyLookup { 49 50 public static IDEInitializer DEFAULT_LOOKUP = null; 51 private static FileSystem lfs; 52 53 static { 54 IDEInitializer.class.getClassLoader ().setDefaultAssertionStatus (true); 55 System.setProperty ("org.openide.util.Lookup", IDEInitializer.class.getName ()); 56 Assert.assertEquals (IDEInitializer.class, Lookup.getDefault ().getClass ()); 57 } 58 59 public IDEInitializer () { 60 Assert.assertNull (DEFAULT_LOOKUP); 61 DEFAULT_LOOKUP = this; 62 URL.setURLStreamHandlerFactory (new MyURLHandlerFactory ()); 63 } 64 65 71 public static void setup ( 72 String [] layers, 73 Object [] instances 74 ) { 75 ClassLoader classLoader = IDEInitializer.class.getClassLoader (); 76 File workDir = new File (Manager.getWorkDirPath ()); 77 URL [] urls = new URL [layers.length]; 78 int i, k = urls.length; 79 for (i = 0; i < k; i++) 80 urls [i] = classLoader.getResource (layers [i]); 81 82 XMLFileSystem systemFS = new XMLFileSystem (); 84 lfs = FileUtil.createMemoryFileSystem(); 85 try { 86 systemFS.setXmlUrls (urls); 87 } catch (Exception ex) { 88 ex.printStackTrace (); 89 } 90 MyFileSystem myFileSystem = new MyFileSystem ( 91 new FileSystem [] {lfs, systemFS} 92 ); 93 Repository repository = new Repository (myFileSystem); 94 95 Object [] lookupContent = new Object [instances.length + 1]; 96 lookupContent [0] = repository; 97 System.arraycopy (instances, 0, lookupContent, 1, instances.length); 98 99 DEFAULT_LOOKUP.setLookups (new Lookup[] { 100 Lookups.fixed (lookupContent), 101 Lookups.metaInfServices (classLoader), 102 Lookups.singleton (classLoader), 103 }); 104 Assert.assertEquals (myFileSystem, Repository.getDefault ().getDefaultFileSystem ()); 105 } 106 107 public static void cleanWorkDir () { 108 try { 109 Enumeration en = lfs.getRoot ().getChildren (false); 110 while (en.hasMoreElements ()) 111 ((FileObject) en.nextElement ()).delete (); 112 } catch (IOException ex) { 113 ex.printStackTrace (); 114 } 115 } 116 117 private static class MyFileSystem extends MultiFileSystem { 118 public MyFileSystem (FileSystem[] fileSystems) { 119 super (fileSystems); 120 try { 121 setSystemName ("TestFS"); 122 } catch (PropertyVetoException ex) { 123 ex.printStackTrace(); 124 } 125 } 126 } 127 128 private static class MyURLHandlerFactory implements URLStreamHandlerFactory { 129 public URLStreamHandler createURLStreamHandler(String protocol) { 130 if (protocol.equals ("nbfs")) { 131 return FileUtil.nbfsURLStreamHandler (); 132 } 133 return null; 134 } 135 } 136 } 137 | Popular Tags |