001: /*
002: * Enhydra Java Application Server Project
003: *
004: * The contents of this file are subject to the Enhydra Public License
005: * Version 1.1 (the "License"); you may not use this file except in
006: * compliance with the License. You may obtain a copy of the License on
007: * the Enhydra web site ( http://www.enhydra.org/ ).
008: *
009: * Software distributed under the License is distributed on an "AS IS"
010: * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
011: * the License for the specific terms governing rights and limitations
012: * under the License.
013: *
014: * The Initial Developer of the Enhydra Application Server is Lutris
015: * Technologies, Inc. The Enhydra Application Server and portions created
016: * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
017: * All Rights Reserved.
018: *
019: * Contributor(s):
020: * Paul Mahar
021: *
022: */
023: package org.enhydra.kelp.common;
024:
025: // Kelp imports
026: import org.enhydra.kelp.common.Constants;
027: import org.enhydra.kelp.common.ProgressBuilder;
028: import org.enhydra.kelp.common.event.WriteListener;
029: import org.enhydra.kelp.common.node.OtterProject;
030:
031: // ToolBox imports
032: import org.enhydra.tool.common.ToolException;
033:
034: // Standard imports
035: import java.io.PrintWriter;
036: import java.util.StringTokenizer;
037:
038: //
039: abstract public class AbstractEchoBuilder extends ProgressBuilder {
040: private boolean echo = true;
041: private OtterProject project = null;
042: private PrintWriter echoWriter = null;
043: private Writer capture = null;
044:
045: /**
046: * Create a CompileThread with listeners ready to
047: * recieve output from build.
048: *
049: *
050: * @param listeners
051: * A listener implements an onWrite method that is called
052: * to write output. The writes are captured for display or
053: * logging.
054: */
055: public AbstractEchoBuilder() {
056: super ();
057: capture = new Writer();
058: capture.setLineBuffered(false);
059: echoWriter = new PrintWriter(capture, false);
060: }
061:
062: public AbstractEchoBuilder(WriteListener listener) {
063: this (listener, false);
064: }
065:
066: public AbstractEchoBuilder(WriteListener listener, boolean buffer) {
067: super ();
068: capture = new Writer();
069: capture.setLineBuffered(buffer);
070: addWriteListener(listener);
071: echoWriter = new PrintWriter(capture, false);
072: }
073:
074: /**
075: * Get the IDE project that contains the HTML source files to compile.
076: */
077: public OtterProject getProject() {
078: return project;
079: }
080:
081: /**
082: * Set the IDE project that contains the HTML source files to compile.
083: */
084: public void setProject(OtterProject bp) {
085: project = bp;
086: }
087:
088: public boolean isEcho() {
089: return echo;
090: }
091:
092: public void setEcho(boolean e) {
093: echo = e;
094: }
095:
096: public PrintWriter getEchoWriter() {
097: return echoWriter;
098: }
099:
100: public void echo(String message) {
101: if (isEcho()) {
102: getEchoWriter().println(message);
103: }
104: }
105:
106: public void echo(Exception e) {
107: StringTokenizer tokenizer = null;
108: String message = null;
109:
110: if (e != null) {
111: message = e.getMessage();
112: if (message == null) {
113: message = e.toString();
114: }
115: e.printStackTrace(System.err);
116: }
117: if (message != null) {
118: tokenizer = new StringTokenizer(message, Constants.NEWLINE);
119: while (tokenizer.hasMoreTokens()) {
120: getEchoWriter().println(tokenizer.nextToken());
121: }
122: }
123: }
124:
125: /**
126: * Run the XMLC for all selected HTML files.
127: */
128: public synchronized void build() {
129: setFresh(true);
130: BuildRunner runner = new BuildRunner();
131:
132: runner.start();
133: }
134:
135: public synchronized void buildInCurrentThread() {
136: setFresh(true);
137: try {
138: buildImpl();
139: } catch (Exception e) {
140: echo(e);
141: }
142: }
143:
144: protected WriteListener getWriteListener() {
145: WriteListener listener = null;
146:
147: if (capture.getWriteListeners().length > 0) {
148: listener = capture.getWriteListeners()[0];
149: }
150: return listener;
151: }
152:
153: protected void addWriteListener(WriteListener w) {
154: if (w != null) {
155: capture.addWriteListener(w);
156: }
157: }
158:
159: protected void removeWriteListener(WriteListener w) {
160: if (w != null) {
161: capture.removeWriteListener(w);
162: }
163: }
164:
165: /**
166: * Loop through all selected nodes files and build them.
167: * Write out exceptions to any registered listeners.
168: */
169: abstract protected void buildImpl() throws ToolException;
170:
171: /**
172: * Sleep the thread for a given number of seconds.
173: *
174: *
175: * @param seconds
176: * How many seconds to sleep.
177: */
178: public void sleepInSeconds(double seconds) {
179: try {
180: Thread.sleep((int) (seconds * 1000));
181: } catch (InterruptedException e) {
182: }
183: }
184:
185: private class BuildRunner extends Thread {
186: protected BuildRunner() {
187: setPriority((int) ((Thread.MIN_PRIORITY + Thread.NORM_PRIORITY) / 2));
188: }
189:
190: public void run() {
191: try {
192: buildImpl();
193: } catch (Exception e) {
194: echo(e);
195: }
196: }
197:
198: }
199: }
|