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 AbstractEchoGenerator 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 AbstractEchoGenerator() {
056: super ();
057: capture = new Writer();
058: capture.setLineBuffered(false);
059: echoWriter = new PrintWriter(capture, false);
060: }
061:
062: public AbstractEchoGenerator(WriteListener listener) {
063: this (listener, false);
064: }
065:
066: public AbstractEchoGenerator(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 generate() {
129: setFresh(true);
130: GenerateRunner runner = new GenerateRunner();
131: runner.start();
132: }
133:
134: public synchronized void buildInCurrentThread() {
135: setFresh(true);
136: try {
137: generateImpl();
138: } catch (Exception e) {
139: echo(e);
140: }
141: }
142:
143: protected WriteListener getWriteListener() {
144: WriteListener listener = null;
145:
146: if (capture.getWriteListeners().length > 0) {
147: listener = capture.getWriteListeners()[0];
148: }
149: return listener;
150: }
151:
152: protected void addWriteListener(WriteListener w) {
153: if (w != null) {
154: capture.addWriteListener(w);
155: }
156: }
157:
158: protected void removeWriteListener(WriteListener w) {
159: if (w != null) {
160: capture.removeWriteListener(w);
161: }
162: }
163:
164: /**
165: * Loop through all selected nodes files and build them.
166: * Write out exceptions to any registered listeners.
167: */
168: abstract protected void generateImpl() throws ToolException;
169:
170: /**
171: * Sleep the thread for a given number of seconds.
172: *
173: *
174: * @param seconds
175: * How many seconds to sleep.
176: */
177: public void sleepInSeconds(double seconds) {
178: try {
179: Thread.sleep((int) (seconds * 1000));
180: } catch (InterruptedException e) {
181: }
182: }
183:
184: private class GenerateRunner extends Thread {
185: protected GenerateRunner() {
186: setPriority((int) ((Thread.MIN_PRIORITY + Thread.NORM_PRIORITY) / 2));
187: }
188:
189: public void run() {
190: try {
191: generateImpl();
192: } catch (Exception e) {
193: echo(e);
194: }
195: }
196:
197: }
198: }
|