1 23 package org.enhydra.kelp.common; 24 25 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 import org.enhydra.tool.common.ToolException; 34 35 import java.io.PrintWriter ; 37 import java.io.File ; 38 import java.util.StringTokenizer ; 39 40 abstract public class AbstractEchoBuilder extends ProgressBuilder { 42 private boolean echo = true; 43 private OtterProject project = null; 44 private PrintWriter echoWriter = null; 45 private Writer capture = null; 46 47 57 public AbstractEchoBuilder() { 58 super(); 59 capture = new Writer(); 60 capture.setLineBuffered(false); 61 echoWriter = new PrintWriter (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 (capture, false); 74 } 75 76 79 public OtterProject getProject() { 80 return project; 81 } 82 83 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 getEchoWriter() { 99 return echoWriter; 100 } 101 102 public void echo(String message) { 103 if (isEcho()) { 104 getEchoWriter().println(message); 105 } 106 } 107 108 public void echo(Exception e) { 109 StringTokenizer tokenizer = null; 110 String 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 (message, Constants.NEWLINE); 121 while (tokenizer.hasMoreTokens()) { 122 getEchoWriter().println(tokenizer.nextToken()); 123 } 124 } 125 } 126 127 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 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 171 abstract protected void buildImpl() throws ToolException; 172 173 180 public void sleepInSeconds(double seconds) { 181 try { 182 Thread.sleep((int) (seconds * 1000)); 183 } catch (InterruptedException e) {} 184 } 185 186 private class BuildRunner extends Thread { 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 e) { 196 echo(e); 197 } 198 } 199 200 } 201 } 202 | Popular Tags |