1 19 20 package org.netbeans.modules.options.editor; 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 LocalFileSystem 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 = new LocalFileSystem (); 85 try { 86 systemFS.setXmlUrls (urls); 87 lfs.setRootDirectory (workDir); 88 } catch (Exception ex) { 89 ex.printStackTrace (); 90 } 91 MyFileSystem myFileSystem = new MyFileSystem ( 92 new FileSystem [] {lfs, systemFS} 93 ); 94 Repository repository = new Repository (myFileSystem); 95 96 Object [] lookupContent = new Object [instances.length + 1]; 97 lookupContent [0] = repository; 98 System.arraycopy (instances, 0, lookupContent, 1, instances.length); 99 100 DEFAULT_LOOKUP.setLookups (new Lookup[] { 101 Lookups.fixed (lookupContent), 102 Lookups.metaInfServices (classLoader), 103 Lookups.singleton (classLoader), 104 }); 105 Assert.assertEquals (myFileSystem, Repository.getDefault ().getDefaultFileSystem ()); 106 } 107 108 public static void cleanWorkDir () { 109 try { 110 Enumeration en = lfs.getRoot ().getChildren (false); 111 while (en.hasMoreElements ()) 112 ((FileObject) en.nextElement ()).delete (); 113 } catch (IOException ex) { 114 ex.printStackTrace (); 115 } 116 } 117 118 private static class MyFileSystem extends MultiFileSystem { 119 public MyFileSystem (FileSystem[] fileSystems) { 120 super (fileSystems); 121 try { 122 setSystemName ("Yarda"); 123 } catch (PropertyVetoException ex) { 124 ex.printStackTrace(); 125 } 126 } 127 } 128 129 private static class MyURLHandlerFactory implements URLStreamHandlerFactory { 130 public URLStreamHandler createURLStreamHandler(String protocol) { 131 if (protocol.equals ("nbfs")) { 132 return FileUtil.nbfsURLStreamHandler (); 133 } 134 return null; 135 } 136 } 137 } 138 | Popular Tags |