KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > java > source > engine > EngineFactory


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.modules.java.source.engine;
21
22 import java.io.File JavaDoc;
23 import java.io.FileNotFoundException JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.lang.reflect.Constructor JavaDoc;
26 import org.openide.modules.InstalledFileLocator;
27
28 /**
29  * Creates new instances of a JackpotEngine. This should only be called by the
30  * NetBeans Jackpot module.
31  */

32 public final class EngineFactory {
33     /**
34      * Create a new Jackpot instance.
35      *
36      * @param appContext the application context the engine runs within.
37      */

38     public static JackpotEngine createEngine(ApplicationContext appContext) throws Exception JavaDoc {
39         String JavaDoc jarPath = jackpotJarsPath();
40         // separate classloader to isolate memory leaks
41
ClassLoader JavaDoc loader = new ClassLoader JavaDoc(EngineFactory.class.getClassLoader()) {
42             protected Class JavaDoc<?> findClass(String JavaDoc name) throws ClassNotFoundException JavaDoc {
43                 return super.findClass(name);
44             }
45         };
46         Class JavaDoc c = Class.forName("org.netbeans.modules.java.source.builder.Engine", true, loader); // NOI18N
47
Constructor JavaDoc con = c.getConstructor(ApplicationContext.class, String JavaDoc.class);
48         con.setAccessible(true);
49         return (JackpotEngine)con.newInstance(appContext, jarPath);
50     }
51     
52     private static String JavaDoc jackpotJarsPath() throws IOException JavaDoc {
53         InstalledFileLocator locator = InstalledFileLocator.getDefault();
54         File JavaDoc jar = locator.locate("modules/org-netbeans-modules-java-source.jar", // NOI18N
55
"org.netbeans.modules.java.source", false); // NOI18N
56
if (jar == null)
57             return(""); // InstalledFileLocator not initialized, so rely on classpath instead.
58
StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
59         sb.append(jar.getCanonicalPath());
60         sb.append(File.pathSeparator);
61         jar = locator.locate("modules/ext/javac-api.jar", // NOI18N
62
"org.netbeans.libs.javacapi", false); // NOI18N
63
if (jar == null)
64             throw new FileNotFoundException JavaDoc("modules/ext/javac-api.jar");
65         sb.append(jar.getCanonicalPath());
66         sb.append(File.pathSeparator);
67         jar = locator.locate("modules/ext/javac-impl.jar", // NOI18N
68
"org.netbeans.libs.javacimpl", false); // NOI18N
69
if (jar == null)
70             throw new FileNotFoundException JavaDoc("modules/ext/javac-impl.jar");
71         sb.append(jar.getCanonicalPath());
72         return sb.toString();
73     }
74 }
75
Popular Tags