KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cruisecontrol > publishers > SocketPublisher


1 /********************************************************************************
2  * CruiseControl, a Continuous Integration Toolkit
3  * Copyright (c) 2001-2003, ThoughtWorks, Inc.
4  * 651 W Washington Ave. Suite 600
5  * Chicago, IL 60661 USA
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * + Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * + Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  *
20  * + Neither the name of ThoughtWorks, Inc., CruiseControl, nor the
21  * names of its contributors may be used to endorse or promote
22  * products derived from this software without specific prior
23  * written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
29  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  ********************************************************************************/

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 JavaDoc;
46 import java.io.PrintWriter JavaDoc;
47 import java.net.Socket JavaDoc;
48 import java.rmi.UnknownHostException JavaDoc;
49 import org.apache.log4j.Logger;
50
51 /**
52  * @author <a HREF="mailto:dcotterill@thoughtworks.com">Darren Cotterill</a>
53  */

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 JavaDoc socketServer;
59     private int port = 0;
60
61     public SocketPublisher() {
62         factory = new SocketFactory() {
63             public Socket JavaDoc createSocket(String JavaDoc server, int port) throws IOException JavaDoc {
64                 return new Socket JavaDoc(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 JavaDoc e) {
92             throw new CruiseControlException(e);
93         }
94     }
95
96     protected void writeToSocket(String JavaDoc result) throws IOException JavaDoc {
97         Socket JavaDoc echoSocket = null;
98         PrintWriter JavaDoc out = null;
99
100         try {
101             echoSocket = factory.createSocket(socketServer, getPort());
102             out = new PrintWriter JavaDoc(echoSocket.getOutputStream(), true);
103         } catch (UnknownHostException JavaDoc e) {
104             LOG.error("Don't know about host:" + socketServer);
105         } catch (IOException JavaDoc 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 JavaDoc getSocketServer() {
119         return socketServer;
120     }
121     public void setSocketServer(String JavaDoc 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 JavaDoc port) {
131         this.port = Integer.parseInt(port);
132     }
133 }
134
Popular Tags