KickJava   Java API By Example, From Geeks To Geeks.

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


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.apache.log4j.Logger;
44 import org.jdom.Element;
45 import org.jivesoftware.smack.Chat;
46 import org.jivesoftware.smack.GroupChat;
47 import org.jivesoftware.smack.SSLXMPPConnection;
48 import org.jivesoftware.smack.XMPPConnection;
49 import org.jivesoftware.smack.XMPPException;
50
51 /**
52  * Abstract publisher which establishes this transport to publish
53  * build results via Jabber Instant Messaging framework.
54  *
55  * @author <a HREF="mailto:jonas_edgeworth@cal.berkeley.edu">Jonas Edgeworth</a>
56  * @version 1.0
57  * @see LinkJabberPublisher
58  */

59
60 public abstract class JabberPublisher implements Publisher {
61
62     private static final Logger LOG = Logger.getLogger(JabberPublisher.class);
63
64     private String JavaDoc host;
65     private int port = 5222;
66     private String JavaDoc username;
67     private String JavaDoc password;
68     private String JavaDoc recipient;
69     private String JavaDoc service;
70
71     private boolean chatroom = false;
72     private boolean ssl = false;
73
74     // one connection for the CC session
75
private static XMPPConnection connection;
76     private Chat chat;
77     private GroupChat groupchat;
78
79     public void setHost(String JavaDoc host) {
80         this.host = host;
81     }
82
83     public void setPort(int port) {
84         this.port = port;
85     }
86
87     public void setUsername(String JavaDoc username) {
88         this.username = username;
89     }
90
91     public void setPassword(String JavaDoc password) {
92         this.password = password;
93     }
94
95     public void setRecipient(String JavaDoc recipient) {
96         this.recipient = recipient;
97     }
98
99     public void setService(String JavaDoc service) {
100         this.service = service;
101     }
102
103     public void setChatroom(boolean chatroom) {
104         this.chatroom = chatroom;
105     }
106
107     public void setSsl(boolean ssl) {
108         this.ssl = ssl;
109     }
110
111     /**
112      * Initialize the XMPPConnection to the Jabber server and
113      * create the necessary chat session to send a message.
114      */

115     protected void init() {
116         // Uncommenting will execute Smack XML-RPC trace GUI
117
//XMPPConnection.DEBUG_ENABLED = true;
118
if (null == connection || requiresReconnect()) {
119             try {
120                 if (ssl) {
121                     if (service != null) {
122                         connection = new SSLXMPPConnection(host, port, service);
123                     } else {
124                         connection = new SSLXMPPConnection(host, port);
125                     }
126                 } else {
127                     if (service != null) {
128                         connection = new XMPPConnection(host, port, service);
129                     } else {
130                         connection = new XMPPConnection(host, port);
131                     }
132                 }
133             } catch (XMPPException e) {
134                 LOG.error("Error initializing jabber connection", e);
135             }
136             try {
137                 connection.login(username, password);
138             } catch (XMPPException e) {
139                 LOG.error("Authentication error on login", e);
140             }
141         }
142
143         try {
144             if (chatroom) {
145                 groupchat = connection.createGroupChat(recipient);
146                 groupchat.join(username);
147             } else {
148                 chat = connection.createChat(recipient);
149             }
150         } catch (XMPPException e) {
151             LOG.error("Could not send message to recipient or chat room", e);
152         }
153     }
154
155     /**
156      * Checks for changes to params or connection failure
157      *
158      * @return true if a reconnect is required
159      */

160     private boolean requiresReconnect() {
161         return (!connection.isConnected()
162                 && !connection.isSecureConnection())
163                 || !connection.isAuthenticated();
164     }
165
166     /**
167      * Validate that all the mandatory parameters were specified in order
168      * to properly initial the Jabber client service. Note that this is called
169      * after the configuration file is read.
170      *
171      * @throws CruiseControlException if there was a configuration error.
172      */

173     public void validate() throws CruiseControlException {
174
175         ValidationHelper.assertIsSet(host, "host", this.getClass());
176         ValidationHelper.assertIsSet(username, "username", this.getClass());
177         ValidationHelper.assertFalse(isEmail(username),
178                 "'username' is not in correct format. "
179                         + "'username' should not be of the form user@domain.com");
180
181         ValidationHelper.assertIsSet(password, "password", this.getClass());
182         ValidationHelper.assertIsSet(recipient, "recipient", this.getClass());
183         ValidationHelper.assertTrue(isEmail(recipient),
184                 "'recipient' is not in correct format. "
185                         + "'recipient' should be of the form user@domain.com");
186     }
187
188     private boolean isEmail(final String JavaDoc username) {
189         return username.indexOf("@") != -1;
190     }
191
192     /**
193      * Publish the results to the Jabber transport via an instant message.
194      *
195      * @param cruisecontrolLog
196      * @throws CruiseControlException
197      */

198     public void publish(Element cruisecontrolLog) throws CruiseControlException {
199
200         // Initialize the XMPP connection
201
init();
202
203         // Generate the message to be sent to the recipient
204
XMLLogHelper helper = new XMLLogHelper(cruisecontrolLog);
205         String JavaDoc message = createMessage(helper);
206
207         // Send the message to the recipient
208
try {
209             if (chatroom) {
210                 groupchat.sendMessage(message);
211             } else {
212                 chat.sendMessage(message);
213             }
214         } catch (XMPPException e) {
215             LOG.error("Unable to send message via Jabber", e);
216         }
217     }
218
219     /**
220      * Creates the IM message body to be sent to the recipient.
221      *
222      * @return <code>String</code> that makes up the body of the IM message
223      * @throws CruiseControlException
224      */

225     protected abstract String JavaDoc createMessage(XMLLogHelper logHelper) throws CruiseControlException;
226
227 }
228
Popular Tags