KickJava   Java API By Example, From Geeks To Geeks.

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


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 AbstractEchoGenerator 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 AbstractEchoGenerator() {
58         super();
59         capture = new Writer();
60         capture.setLineBuffered(false);
61         echoWriter = new PrintWriter JavaDoc(capture, false);
62     }
63
64     public AbstractEchoGenerator(WriteListener listener) {
65         this(listener, false);
66     }
67
68     public AbstractEchoGenerator(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 generate() {
131         setFresh(true);
132         GenerateRunner runner = new GenerateRunner();
133         runner.start();
134     }
135
136     public synchronized void buildInCurrentThread() {
137         setFresh(true);
138         try {
139             generateImpl();
140         } catch (Exception JavaDoc e) {
141             echo(e);
142         }
143     }
144
145     protected WriteListener getWriteListener() {
146         WriteListener listener = null;
147
148         if (capture.getWriteListeners().length > 0) {
149             listener = capture.getWriteListeners()[0];
150         }
151         return listener;
152     }
153
154     protected void addWriteListener(WriteListener w) {
155         if (w != null) {
156             capture.addWriteListener(w);
157         }
158     }
159
160     protected void removeWriteListener(WriteListener w) {
161         if (w != null) {
162             capture.removeWriteListener(w);
163         }
164     }
165
166     /**
167      * Loop through all selected nodes files and build them.
168      * Write out exceptions to any registered listeners.
169      */

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

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