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.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 59 60 public abstract class JabberPublisher implements Publisher { 61 62 private static final Logger LOG = Logger.getLogger(JabberPublisher.class); 63 64 private String host; 65 private int port = 5222; 66 private String username; 67 private String password; 68 private String recipient; 69 private String service; 70 71 private boolean chatroom = false; 72 private boolean ssl = false; 73 74 private static XMPPConnection connection; 76 private Chat chat; 77 private GroupChat groupchat; 78 79 public void setHost(String host) { 80 this.host = host; 81 } 82 83 public void setPort(int port) { 84 this.port = port; 85 } 86 87 public void setUsername(String username) { 88 this.username = username; 89 } 90 91 public void setPassword(String password) { 92 this.password = password; 93 } 94 95 public void setRecipient(String recipient) { 96 this.recipient = recipient; 97 } 98 99 public void setService(String 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 115 protected void init() { 116 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 160 private boolean requiresReconnect() { 161 return (!connection.isConnected() 162 && !connection.isSecureConnection()) 163 || !connection.isAuthenticated(); 164 } 165 166 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 username) { 189 return username.indexOf("@") != -1; 190 } 191 192 198 public void publish(Element cruisecontrolLog) throws CruiseControlException { 199 200 init(); 202 203 XMLLogHelper helper = new XMLLogHelper(cruisecontrolLog); 205 String message = createMessage(helper); 206 207 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 225 protected abstract String createMessage(XMLLogHelper logHelper) throws CruiseControlException; 226 227 } 228 | Popular Tags |