KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > edu > rice > cs > drjava > model > repl > RMIInteractionsModel


1 /*BEGIN_COPYRIGHT_BLOCK
2  *
3  * This file is part of DrJava. Download the current version of this project from http://www.drjava.org/
4  * or http://sourceforge.net/projects/drjava/
5  *
6  * DrJava Open Source License
7  *
8  * Copyright (C) 2001-2005 JavaPLT group at Rice University (javaplt@rice.edu). All rights reserved.
9  *
10  * Developed by: Java Programming Languages Team, Rice University, http://www.cs.rice.edu/~javaplt/
11  *
12  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
13  * documentation files (the "Software"), to deal with the Software without restriction, including without limitation
14  * the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
15  * to permit persons to whom the Software is furnished to do so, subject to the following conditions:
16  *
17  * - Redistributions of source code must retain the above copyright notice, this list of conditions and the
18  * following disclaimers.
19  * - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
20  * following disclaimers in the documentation and/or other materials provided with the distribution.
21  * - Neither the names of DrJava, the JavaPLT, Rice University, nor the names of its contributors may be used to
22  * endorse or promote products derived from this Software without specific prior written permission.
23  * - Products derived from this software may not be called "DrJava" nor use the term "DrJava" as part of their
24  * names without prior written permission from the JavaPLT group. For permission, write to javaplt@rice.edu.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
27  * THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28  * CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
29  * CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
30  * WITH THE SOFTWARE.
31  *
32  *END_COPYRIGHT_BLOCK*/

33
34 package edu.rice.cs.drjava.model.repl;
35
36 import edu.rice.cs.drjava.model.repl.newjvm.*;
37 import edu.rice.cs.util.ClassPathVector;
38 import edu.rice.cs.util.text.EditDocumentInterface;
39
40 import java.net.URL JavaDoc;
41 import java.io.File JavaDoc;
42
43 /**
44  * An InteractionsModel which can serve as the glue between a local
45  * InteractionsDocument and a remote JavaInterpreter in another JVM.
46  * @version $Id: RMIInteractionsModel.java 4031 2006-11-15 22:09:06Z rcartwright $
47  */

48 public abstract class RMIInteractionsModel extends InteractionsModel {
49
50   /** RMI interface to the remote Java interpreter.*/
51   protected final MainJVM _jvm;
52
53   /** Constructs an InteractionsModel which can communicate with another JVM.
54    * @param jvm RMI interface to the slave JVM
55    * @param adapter InteractionsDJDocument to use in the InteractionsDocument
56    * @param historySize Number of lines to store in the history
57    * @param writeDelay Number of milliseconds to wait after each println
58    */

59   public RMIInteractionsModel(MainJVM jvm, EditDocumentInterface adapter, File JavaDoc wd, int historySize, int writeDelay) {
60     super(adapter, wd, historySize, writeDelay);
61     _jvm = jvm;
62   }
63
64   /** Interprets the given command.
65    * @param toEval command to be evaluated
66    */

67   protected void _interpret(String JavaDoc toEval) { _jvm.interpret(toEval); }
68
69   /** Gets the string representation of the value of a variable in the current interpreter.
70    * @param var the name of the variable
71    */

72   public String JavaDoc getVariableToString(String JavaDoc var) { return _jvm.getVariableToString(var); }
73
74   /**
75    * Gets the class name of a variable in the current interpreter.
76    * @param var the name of the variable
77    */

78   public String JavaDoc getVariableClassName(String JavaDoc var) {
79     return _jvm.getVariableClassName(var);
80   }
81
82   /**
83    * Adds the given path to the interpreter's classpath.
84    * @param path Path to add
85    */

86 // public void addToClassPath(String path) {
87
// _interpreterControl.addClassPath(path);
88
// }
89
//
90
public void addProjectClassPath(URL JavaDoc path) { _jvm.addProjectClassPath(path); }
91
92   public void addBuildDirectoryClassPath(URL JavaDoc path) { _jvm.addBuildDirectoryClassPath(path); }
93   
94   public void addProjectFilesClassPath(URL JavaDoc path) {
95 // System.err.println("Adding " + path + " to projectFilesClassPath in the slave JVM");
96
_jvm.addProjectFilesClassPath(path);
97   }
98   
99   public void addExternalFilesClassPath(URL JavaDoc path) { _jvm.addExternalFilesClassPath(path); }
100   
101   public void addExtraClassPath(URL JavaDoc path) { _jvm.addExtraClassPath(path); }
102   
103   /** Resets the Java interpreter. */
104   protected void _resetInterpreter(File JavaDoc wd) { _jvm.killInterpreter(wd); }
105
106   /** Adds a named DynamicJavaAdapter to the list of interpreters.
107    * @param name the unique name for the interpreter
108    * @throws IllegalArgumentException if the name is not unique
109    */

110   public void addJavaInterpreter(String JavaDoc name) { _jvm.addJavaInterpreter(name); }
111
112   /** Adds a named JavaDebugInterpreter to the list of interpreters.
113    * @param name the unique name for the debug interpreter
114    * @param className the fully qualified class name of the class the debug interpreter is in
115    * @throws IllegalArgumentException if the name is not unique
116    */

117   public void addDebugInterpreter(String JavaDoc name, String JavaDoc className) {
118     _jvm.addDebugInterpreter(name, className);
119   }
120
121   /** Removes the interpreter with the given name, if it exists.
122    * @param name Name of the interpreter to remove
123    */

124   public void removeInterpreter(String JavaDoc name) {
125     _jvm.removeInterpreter(name);
126   }
127
128   /** Sets the active interpreter.
129    * @param name the (unique) name of the interpreter.
130    * @param prompt the prompt the interpreter should have.
131    */

132   public void setActiveInterpreter(String JavaDoc name, String JavaDoc prompt) {
133     String JavaDoc currName = _jvm.getCurrentInterpreterName();
134     boolean inProgress = _jvm.setActiveInterpreter(name);
135     _updateDocument(prompt, inProgress, !currName.equals(name));
136     _notifyInterpreterChanged(inProgress);
137   }
138
139   /** Sets the default interpreter to be the current one. */
140   public void setToDefaultInterpreter() {
141     // Only print prompt if we're not already the default
142
String JavaDoc currName = _jvm.getCurrentInterpreterName();
143     boolean printPrompt = !MainJVM.DEFAULT_INTERPRETER_NAME.equals(currName);
144
145     boolean inProgress = _jvm.setToDefaultInterpreter();
146
147     _updateDocument(InteractionsDocument.DEFAULT_PROMPT, inProgress, printPrompt);
148     _notifyInterpreterChanged(inProgress);
149   }
150
151   /** Updates the prompt and status of the document after an interpreter change. Assumes write lock is already held.
152    * @param prompt New prompt to display
153    * @param inProgress whether the interpreter is currently in progress
154    * @param updatePrompt whether or not the interpreter has changed
155    */

156   private void _updateDocument(String JavaDoc prompt, boolean inProgress, boolean updatePrompt) {
157     if (updatePrompt) {
158       _document.acquireWriteLock();
159       try {
160         _document.setPrompt(prompt);
161         _document.insertNewLine(_document.getLength());
162         _document.insertPrompt();
163       }
164       finally { _document.releaseWriteLock(); }
165     }
166     _document.setInProgress(inProgress);
167   }
168
169   /** Notifies listeners that the interpreter has changed. (Subclasses must maintain listeners.)
170    * @param inProgress Whether the new interpreter is currently in progress with an interaction, i.e., whether
171    * an interactionEnded event will be fired)
172    */

173   protected abstract void _notifyInterpreterChanged(boolean inProgress);
174
175   /** Sets whether or not the interpreter should allow access to private members. */
176   public void setPrivateAccessible(boolean allow) {
177     _jvm.setPrivateAccessible(allow);
178   }
179
180   /** Gets the interpreter classpath from the interpreter jvm.
181    * @return a vector of classpath elements
182    */

183   public ClassPathVector getClassPath() { return _jvm.getClassPath(); }
184 }
Popular Tags