KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > api > options > IDEInitializer


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.options;
21
22 import java.beans.PropertyVetoException JavaDoc;
23 import java.io.File JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.net.URL JavaDoc;
26 import java.net.URLStreamHandler JavaDoc;
27 import java.net.URLStreamHandlerFactory JavaDoc;
28 import java.util.Enumeration JavaDoc;
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 /**
44  * Inspired by org.netbeans.api.project.TestUtil.
45  *
46  * @author Miloslav Metelka, Jan Lahoda
47  */

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     /**
66      * Set the global default lookup with the specified content.
67      *
68      * @param layers xml-layer URLs to be present in the system filesystem.
69      * @param instances object instances to be present in the default lookup.
70      */

71     public static void setup (
72         String JavaDoc[] layers,
73         Object JavaDoc[] instances
74     ) {
75         ClassLoader JavaDoc classLoader = IDEInitializer.class.getClassLoader ();
76         File JavaDoc workDir = new File JavaDoc (Manager.getWorkDirPath ());
77         URL JavaDoc[] urls = new URL JavaDoc [layers.length];
78         int i, k = urls.length;
79         for (i = 0; i < k; i++)
80             urls [i] = classLoader.getResource (layers [i]);
81
82         // 1) create repository
83
XMLFileSystem systemFS = new XMLFileSystem ();
84         lfs = FileUtil.createMemoryFileSystem();
85         try {
86             systemFS.setXmlUrls (urls);
87         } catch (Exception JavaDoc 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 JavaDoc[] lookupContent = new Object JavaDoc [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 JavaDoc en = lfs.getRoot ().getChildren (false);
110             while (en.hasMoreElements ())
111                 ((FileObject) en.nextElement ()).delete ();
112         } catch (IOException JavaDoc 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 JavaDoc ex) {
123                 ex.printStackTrace();
124             }
125         }
126     }
127     
128     private static class MyURLHandlerFactory implements URLStreamHandlerFactory JavaDoc {
129         public URLStreamHandler JavaDoc createURLStreamHandler(String JavaDoc protocol) {
130             if (protocol.equals ("nbfs")) {
131                 return FileUtil.nbfsURLStreamHandler ();
132             }
133             return null;
134         }
135     }
136 }
137
Popular Tags