| 1 | package biz.xsoftware.buildtemplate; |
| 2 | import java.io.IOException; |
| 3 | import java.io.OutputStream; |
| 4 | import java.io.PrintStream; |
| 5 | import java.io.PrintWriter; |
| 6 | import java.net.InetAddress; |
| 7 | import java.net.Socket; |
| 8 | |
| 9 | import org.apache.tools.ant.BuildEvent; |
| 10 | import org.apache.tools.ant.DefaultLogger; |
| 11 | /* |
| 12 | * Created on Sep 6, 2004 |
| 13 | * |
| 14 | * TODO To change the template for this generated file go to |
| 15 | * Window - Preferences - Java - Code Style - Code Templates |
| 16 | */ |
| 17 | |
| 18 | /** |
| 19 | * @author Dean Hiller |
| 20 | * |
| 21 | * TODO To change the template for this generated type comment go to |
| 22 | * Window - Preferences - Java - Code Style - Code Templates |
| 23 | */ |
| 24 | public class AntSocketLogger extends DefaultLogger { |
| 25 | |
| 26 | public static final String PORT_PROPERTY = "biz.xsoftware.buildtemplate.AntSocketLogger.port"; |
| 27 | private boolean socketFailure = false; |
| 28 | private Socket socket; |
| 29 | private PrintWriter writer; |
| 30 | private StringBuffer cache = new StringBuffer(); |
| 31 | public AntSocketLogger() { |
| 32 | } |
| 33 | |
| 34 | |
| 35 | /* (non-Javadoc) |
| 36 | * @see org.apache.tools.ant.DefaultLogger#printMessage(java.lang.String, java.io.PrintStream, int) |
| 37 | */ |
| 38 | protected void printMessage(String message, PrintStream stream, int priority) { |
| 39 | if(socketFailure) |
| 40 | stream.print(message+"\n"); |
| 41 | else if(writer != null) { |
| 42 | if(cache != null) { |
| 43 | writer.print(cache.toString()); |
| 44 | cache = null; |
| 45 | } |
| 46 | writer.print(message+"\n"); |
| 47 | writer.flush(); |
| 48 | } |
| 49 | else { |
| 50 | //unfortunately, sometimes, the socket does not open at all, like |
| 51 | //when there is some kind fo failure on a taskdef, and so we |
| 52 | //must print it out to stream so it is still printed to the screen |
| 53 | //at some point. |
| 54 | stream.print(message+"\n"); |
| 55 | cache.append(message).append("\n"); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | /* (non-Javadoc) |
| 60 | * @see org.apache.tools.ant.BuildListener#buildStarted(org.apache.tools.ant.BuildEvent) |
| 61 | */ |
| 62 | public void buildStarted(BuildEvent evt) { |
| 63 | super.buildStarted(evt); |
| 64 | } |
| 65 | |
| 66 | /* (non-Javadoc) |
| 67 | * @see org.apache.tools.ant.BuildListener#targetStarted(org.apache.tools.ant.BuildEvent) |
| 68 | */ |
| 69 | public void targetStarted(BuildEvent evt) { |
| 70 | if(socket == null && !socketFailure) { |
| 71 | String portString = evt.getProject().getProperty(PORT_PROPERTY); |
| 72 | int port = Integer.parseInt(portString); |
| 73 | InetAddress localHost; |
| 74 | try { |
| 75 | localHost = InetAddress.getByName("127.0.0.1"); |
| 76 | socket = new Socket(localHost, port); |
| 77 | OutputStream socketOut = socket.getOutputStream(); |
| 78 | writer = new PrintWriter(socketOut); |
| 79 | writer.flush(); |
| 80 | } catch (Exception e) { |
| 81 | socketFailure = true; |
| 82 | e.printStackTrace(out); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | super.targetStarted(evt); |
| 87 | } |
| 88 | /* (non-Javadoc) |
| 89 | * @see org.apache.tools.ant.BuildListener#buildFinished(org.apache.tools.ant.BuildEvent) |
| 90 | */ |
| 91 | public void buildFinished(BuildEvent evt) { |
| 92 | super.buildFinished(evt); |
| 93 | |
| 94 | try { |
| 95 | if(socket != null) |
| 96 | socket.close(); |
| 97 | } catch (IOException e) { |
| 98 | e.printStackTrace(out); |
| 99 | } |
| 100 | } |
| 101 | } |