KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > mdrshell > DJava


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 package org.netbeans.mdrshell;
20
21 import java.util.*;
22 import java.io.*;
23
24 import koala.dynamicjava.interpreter.*;
25 import koala.dynamicjava.parser.wrapper.*;
26
27 import org.netbeans.api.mdr.MDRManager;
28 import org.netbeans.mdr.handlers.*;
29 import org.netbeans.mdr.persistence.StorageException;
30 import org.netbeans.mdr.persistence.memoryimpl.StorageFactoryImpl;
31 import org.openide.ErrorManager;
32
33 /**
34  *
35  * @author Petr Hrebejk
36  * @version
37  */

38 public class DJava extends Object JavaDoc {
39
40     /** Creates new DJava */
41     public DJava() {
42     }
43
44     static koala.dynamicjava.interpreter.Interpreter interpreter;
45     /**
46      * Initialize the engine.
47      */

48     public static void initialize ( ) {
49         // create an interpreter
50
interpreter = new TreeInterpreter(new JavaCCParserFactory());
51         BaseObjectHandler.setClassLoaderProvider(new CLProviderImpl());
52
53         // this is here just to make MDR boot when starting up the shell (not lazily)
54
MDRManager.getDefault().getDefaultRepository().getExtentNames();
55         
56         String JavaDoc storageName = System.getProperty("MDRStorageProperty." + StorageFactoryImpl.STORAGE_ID);
57         if (storageName != null) {
58             try {
59                 StorageFactoryImpl.serialize(storageName);
60             } catch (StorageException e) {
61             }
62         }
63     }
64
65     public static Object JavaDoc eval ( String JavaDoc line ) throws Exception JavaDoc {
66         try {
67             Reader r = new StringReader(line + ";");
68             return interpreter.interpret(r, line );
69         } catch (Exception JavaDoc e) {
70             ErrorManager.getDefault().notify(e);
71             throw e;
72         }
73     }
74
75     /**
76      * Declare a bean
77      */

78     public static void declareVariable ( String JavaDoc name, Object JavaDoc object ) {
79         interpreter.defineVariable( name, object );
80     }
81
82     private static class CLProviderImpl implements ClassLoaderProvider {
83         public ClassLoader JavaDoc getClassLoader() {
84             return interpreter.getClassLoader();
85         }
86         
87         /** Implementation of this method can define a given class.
88          * The defined class should be then visible from the ClassLoader returned
89          * from {@link #getClassLoader} method.
90          * @param className name of the class to define.
91          * @param
92          * @return Defined class or null (if null is return, MDR will define the
93          * class in its own classloader. This class will then not be accessible from outside
94          * of MDR
95          */

96         public Class JavaDoc defineClass(String JavaDoc className, byte[] classFile) {
97             return interpreter.defineClass(className, classFile);
98         }
99     }
100 }
101
Popular Tags