001: /*
002: * PnutsConsole.java
003: *
004: * Copyright (c) 2004-2006 Sun Microsystems, Inc. All Rights Reserved.
005: *
006: * See the file "LICENSE.txt" for information on usage and redistribution of
007: * this file, and for a DISCLAIMER OF ALL WARRANTIES.
008: */
009: package pnuts.tools;
010:
011: import java.io.IOException;
012: import java.io.Reader;
013: import java.io.Writer;
014: import pnuts.lang.Context;
015: import pnuts.lang.Pnuts;
016:
017: public class PnutsConsole extends Console {
018: final static String THREAD_NAME = "Pnuts Console";
019: public final static String GREETING = "pnuts.tools.PnutsConsole.greeting";
020: public final static String INPUTLOG = "pnuts.tools.PnutsConsole.inputlog";
021:
022: boolean greeting = true;
023: Thread th;
024: String inputlog;
025: Reader reader;
026: Context context;
027: ClassLoader classLoader;
028: Runnable terminationCallback;
029: String[] modules;
030: int priority = Thread.NORM_PRIORITY;
031:
032: /**
033: * Constructor
034: */
035: public PnutsConsole() {
036: }
037:
038: public void setModules(String[] modules) {
039: this .modules = modules;
040: }
041:
042: public String[] getModules() {
043: return this .modules;
044: }
045:
046: public void setGreeting(boolean flag) {
047: this .greeting = flag;
048: }
049:
050: public boolean getGreeting() {
051: return this .greeting;
052: }
053:
054: public void setContext(Context context) {
055: this .context = context;
056: }
057:
058: public Context getContext() {
059: return this .context;
060: }
061:
062: public void setClassLoader(ClassLoader cl) {
063: this .classLoader = cl;
064: }
065:
066: public ClassLoader getClassLoader() {
067: return this .classLoader;
068: }
069:
070: public void setInputLog(String name) {
071: this .inputlog = name;
072: }
073:
074: public String getInputLog() {
075: return this .inputlog;
076: }
077:
078: public void setPriority(int priority) {
079: this .priority = priority;
080: }
081:
082: public int getPriority() {
083: return this .priority;
084: }
085:
086: public void setTerminationCallback(Runnable terminationCallback) {
087: this .terminationCallback = terminationCallback;
088: }
089:
090: public Runnable getTerminationCallback() {
091: return this .terminationCallback;
092: }
093:
094: public void start() {
095: if (this .ui == null) {
096: throw new IllegalStateException("no UI ");
097: }
098: final Writer w = getWriter();
099: final Context c;
100: if (context == null) {
101: c = new CancelableContext();
102: } else {
103: c = context;
104: }
105: if (classLoader != null) {
106: c.setClassLoader(classLoader);
107: }
108: if (modules != null) {
109: for (int i = 0; i < modules.length; i++) {
110: c.usePackage(modules[i]);
111: }
112: }
113: c.setWriter(w);
114: c.setErrorWriter(w);
115: c.setTerminalWriter(w);
116: final Reader r = getReader();
117:
118: th = new Thread(new Runnable() {
119: public void run() {
120: if (greeting) {
121: Main.greeting(c);
122: }
123: Pnuts.load(r, true, c);
124: close();
125: try {
126: w.close();
127: } catch (IOException e) {
128: /* ignore */
129: }
130: try {
131: r.close();
132: } catch (IOException e) {
133: /* ignore */
134: }
135: }
136: }, THREAD_NAME);
137: th.setDaemon(true);
138: th.setPriority(priority);
139: if (classLoader != null) {
140: th.setContextClassLoader(classLoader);
141: }
142:
143: th.start();
144: }
145:
146: protected void close() {
147: if (terminationCallback != null) {
148: terminationCallback.run();
149: }
150: ui.close();
151: }
152:
153: public void dispose() {
154: try {
155: if (context instanceof CancelableContext) {
156: ((CancelableContext) context).cancel();
157: }
158: enter("quit()\n");
159: getReader().close();
160: getWriter().close();
161: if (th != null) {
162: th.interrupt();
163: }
164: } catch (Exception ex) {
165: //ex.printStackTrace();
166: }
167: }
168:
169: public synchronized Reader getReader() {
170: if (this .reader == null) {
171: if (inputlog != null) {
172: try {
173: this .reader = new LogReader(super .getReader(),
174: inputlog);
175: } catch (IOException e) {
176: this.reader = super.getReader();
177: }
178: } else {
179: this.reader = super.getReader();
180: }
181: }
182: return reader;
183: }
184: }
|