KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > kelp > common > AbstractEchoBuilder


1 /*
2  * Enhydra Java Application Server Project
3  *
4  * The contents of this file are subject to the Enhydra Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License on
7  * the Enhydra web site ( http://www.enhydra.org/ ).
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11  * the License for the specific terms governing rights and limitations
12  * under the License.
13  *
14  * The Initial Developer of the Enhydra Application Server is Lutris
15  * Technologies, Inc. The Enhydra Application Server and portions created
16  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17  * All Rights Reserved.
18  *
19  * Contributor(s):
20  * Paul Mahar
21  *
22  */

23 package org.enhydra.kelp.common;
24
25 // Kelp imports
26
import org.enhydra.kelp.common.Constants;
27 import org.enhydra.kelp.common.ProgressBuilder;
28 import org.enhydra.kelp.common.event.WriteListener;
29 import org.enhydra.kelp.common.node.OtterNode;
30 import org.enhydra.kelp.common.node.OtterProject;
31
32 // ToolBox imports
33
import org.enhydra.tool.common.ToolException;
34
35 // Standard imports
36
import java.io.PrintWriter JavaDoc;
37 import java.io.File JavaDoc;
38 import java.util.StringTokenizer JavaDoc;
39
40 //
41
abstract public class AbstractEchoBuilder extends ProgressBuilder {
42     private boolean echo = true;
43     private OtterProject project = null;
44     private PrintWriter JavaDoc echoWriter = null;
45     private Writer capture = null;
46
47     /**
48      * Create a CompileThread with listeners ready to
49      * recieve output from build.
50      *
51      *
52      * @param listeners
53      * A listener implements an onWrite method that is called
54      * to write output. The writes are captured for display or
55      * logging.
56      */

57     public AbstractEchoBuilder() {
58         super();
59         capture = new Writer();
60         capture.setLineBuffered(false);
61         echoWriter = new PrintWriter JavaDoc(capture, false);
62     }
63
64     public AbstractEchoBuilder(WriteListener listener) {
65         this(listener, false);
66     }
67
68     public AbstractEchoBuilder(WriteListener listener, boolean buffer) {
69         super();
70         capture = new Writer();
71         capture.setLineBuffered(buffer);
72         addWriteListener(listener);
73         echoWriter = new PrintWriter JavaDoc(capture, false);
74     }
75
76     /**
77      * Get the IDE project that contains the HTML source files to compile.
78      */

79     public OtterProject getProject() {
80         return project;
81     }
82
83     /**
84      * Set the IDE project that contains the HTML source files to compile.
85      */

86     public void setProject(OtterProject bp) {
87         project = bp;
88     }
89
90     public boolean isEcho() {
91         return echo;
92     }
93
94     public void setEcho(boolean e) {
95         echo = e;
96     }
97
98     public PrintWriter JavaDoc getEchoWriter() {
99         return echoWriter;
100     }
101
102     public void echo(String JavaDoc message) {
103         if (isEcho()) {
104             getEchoWriter().println(message);
105         }
106     }
107
108     public void echo(Exception JavaDoc e) {
109         StringTokenizer JavaDoc tokenizer = null;
110         String JavaDoc message = null;
111
112         if (e != null) {
113             message = e.getMessage();
114             if (message == null) {
115               message = e.toString();
116             }
117             e.printStackTrace(System.err);
118         }
119         if (message != null) {
120             tokenizer = new StringTokenizer JavaDoc(message, Constants.NEWLINE);
121             while (tokenizer.hasMoreTokens()) {
122                 getEchoWriter().println(tokenizer.nextToken());
123             }
124         }
125     }
126
127     /**
128      * Run the XMLC for all selected HTML files.
129      */

130     public synchronized void build() {
131         setFresh(true);
132         BuildRunner runner = new BuildRunner();
133
134         runner.start();
135     }
136
137     public synchronized void buildInCurrentThread() {
138         setFresh(true);
139         try {
140             buildImpl();
141         } catch (Exception JavaDoc e) {
142             echo(e);
143         }
144     }
145
146     protected WriteListener getWriteListener() {
147         WriteListener listener = null;
148
149         if (capture.getWriteListeners().length > 0) {
150             listener = capture.getWriteListeners()[0];
151         }
152         return listener;
153     }
154
155     protected void addWriteListener(WriteListener w) {
156         if (w != null) {
157             capture.addWriteListener(w);
158         }
159     }
160
161     protected void removeWriteListener(WriteListener w) {
162         if (w != null) {
163             capture.removeWriteListener(w);
164         }
165     }
166
167     /**
168      * Loop through all selected nodes files and build them.
169      * Write out exceptions to any registered listeners.
170      */

171     abstract protected void buildImpl() throws ToolException;
172
173     /**
174      * Sleep the thread for a given number of seconds.
175      *
176      *
177      * @param seconds
178      * How many seconds to sleep.
179      */

180     public void sleepInSeconds(double seconds) {
181         try {
182             Thread.sleep((int) (seconds * 1000));
183         } catch (InterruptedException JavaDoc e) {}
184     }
185
186     private class BuildRunner extends Thread JavaDoc {
187         protected BuildRunner() {
188             setPriority((int) ((Thread.MIN_PRIORITY + Thread.NORM_PRIORITY)
189                                / 2));
190         }
191
192         public void run() {
193             try {
194                 buildImpl();
195             } catch (Exception JavaDoc e) {
196                 echo(e);
197             }
198         }
199
200     }
201 }
202
Popular Tags