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 AbstractEchoGenerator 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 AbstractEchoGenerator() { 58 super(); 59 capture = new Writer(); 60 capture.setLineBuffered(false); 61 echoWriter = new PrintWriter (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 (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 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 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 170 abstract protected void generateImpl() throws ToolException; 171 172 179 public void sleepInSeconds(double seconds) { 180 try { 181 Thread.sleep((int) (seconds * 1000)); 182 } catch (InterruptedException e) {} 183 } 184 185 private class GenerateRunner extends Thread { 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 e) { 194 echo(e); 195 } 196 } 197 198 } 199 } 200 | Popular Tags |