1 37 package net.sourceforge.cruisecontrol.publishers; 38 39 import net.sourceforge.cruisecontrol.CruiseControlException; 40 import net.sourceforge.cruisecontrol.Publisher; 41 import net.sourceforge.cruisecontrol.util.ValidationHelper; 42 import net.sourceforge.cruisecontrol.util.XMLLogHelper; 43 import org.jdom.Element; 44 45 import java.io.IOException ; 46 import java.io.PrintWriter ; 47 import java.net.Socket ; 48 import java.rmi.UnknownHostException ; 49 import org.apache.log4j.Logger; 50 51 54 public class SocketPublisher implements Publisher { 55 private static final Logger LOG = Logger.getLogger(SocketPublisher.class); 56 57 private final SocketFactory factory; 58 private String socketServer; 59 private int port = 0; 60 61 public SocketPublisher() { 62 factory = new SocketFactory() { 63 public Socket createSocket(String server, int port) throws IOException { 64 return new Socket (server, port); 65 } 66 }; 67 } 68 69 public SocketPublisher(SocketFactory sf) { 70 factory = sf; 71 } 72 73 public void validate() throws CruiseControlException { 74 75 ValidationHelper.assertIsSet(getSocketServer(), "socketServer", this.getClass()); 76 ValidationHelper.assertFalse(getPort() == 0, 77 "'port' not specified for SocketPublisher"); 78 } 79 80 public void publish(Element cruisecontrolLog) 81 throws CruiseControlException { 82 83 XMLLogHelper helper = new XMLLogHelper(cruisecontrolLog); 84 85 try { 86 if (helper.isBuildSuccessful()) { 87 writeToSocket("Success"); 88 } else { 89 writeToSocket("Failure"); 90 } 91 } catch (IOException e) { 92 throw new CruiseControlException(e); 93 } 94 } 95 96 protected void writeToSocket(String result) throws IOException { 97 Socket echoSocket = null; 98 PrintWriter out = null; 99 100 try { 101 echoSocket = factory.createSocket(socketServer, getPort()); 102 out = new PrintWriter (echoSocket.getOutputStream(), true); 103 } catch (UnknownHostException e) { 104 LOG.error("Don't know about host:" + socketServer); 105 } catch (IOException e) { 106 LOG.error("Couldn't get I/O for the connection to:" + socketServer); 107 } 108 109 if (out != null) { 110 out.write(result); 111 out.close(); 112 } 113 if (echoSocket != null) { 114 echoSocket.close(); 115 } 116 } 117 118 public String getSocketServer() { 119 return socketServer; 120 } 121 public void setSocketServer(String port) { 122 socketServer = port; 123 } 124 public int getPort() { 125 return port; 126 } 127 public void setPort(int port) { 128 this.port = port; 129 } 130 public void setPort(String port) { 131 this.port = Integer.parseInt(port); 132 } 133 } 134 | Popular Tags |