| 1 | | | |
| 2 | | | |
| 3 | | | |
| 4 | | | |
| 5 | | | |
| 6 | | | |
| 7 | | | |
| 8 | | | |
| 9 | | | |
| 10 | | | |
| 11 | | | |
| 12 | | | |
| 13 | | | |
| 14 | | | |
| 15 | | | |
| 16 | | | |
| 17 | | | |
| 18 | | | |
| 19 | | | |
| 20 | | | |
| 21 | | | |
| 22 | | | |
| 23 | | | |
| 24 | | | |
| 25 | | | |
| 26 | | | |
| 27 | | | |
| 28 | | | |
| 29 | | | |
| 30 | | | |
| 31 | | | |
| 32 | | | |
| 33 | | | package org.jcoderz.phoenix.jabber; |
| 34 | | | |
| 35 | | | import java.net.InetAddress; |
| 36 | | | import java.util.logging.Level; |
| 37 | | | import java.util.logging.Logger; |
| 38 | | | |
| 39 | | | import org.jivesoftware.smack.GroupChat; |
| 40 | | | import org.jivesoftware.smack.XMPPConnection; |
| 41 | | | import org.jivesoftware.smack.XMPPException; |
| 42 | | | |
| 43 | | | |
| 44 | | | |
| 45 | | | |
| 46 | | | <p> |
| 47 | | | <code></code> |
| 48 | | | </p> |
| 49 | | | |
| 50 | | | <p> |
| 51 | | | </p> |
| 52 | | | |
| 53 | | | <p></p> |
| 54 | | | |
| 55 | | | @author |
| 56 | | | |
| 57 | | | public final class Jabber |
| 58 | | | { |
| 59 | | | private static final int JABBER_DEFAULT_PORT = 5222; |
| 60 | 75 | | private static final String CLASSNAME = Jabber.class.getName(); |
| 61 | 100 | | private static final Logger logger = Logger.getLogger(CLASSNAME); |
| 62 | | | |
| 63 | | | private Jabber () |
| 64 | 0 | | { |
| 65 | | | |
| 66 | 0 | | } |
| 67 | | | |
| 68 | | | |
| 69 | | | |
| 70 | | (1) | |
| 71 | | | @param |
| 72 | | | |
| 73 | | | public static void main (String[] args) |
| 74 | | | { |
| 75 | 0 | | JabberConnection.getInstance().say(args[0]); |
| 76 | 0 | | } |
| 77 | | | |
| 78 | | | |
| 79 | | | |
| 80 | | | @param |
| 81 | | | @throws |
| 82 | | | |
| 83 | | | public static void say (String msg) |
| 84 | | | { |
| 85 | 0 | | JabberConnection.getInstance().say(msg); |
| 86 | 0 | | } |
| 87 | | | |
| 88 | | | private static class JabberConnection |
| 89 | | | { |
| 90 | 100 | | private static final JabberConnection INSTANCE = new JabberConnection(); |
| 91 | | | private static final int GRACEFUL_PERIOD = 1000; |
| 92 | | | |
| 93 | | | private final String mHostname; |
| 94 | 100 | | private String mJabberUserName = "cc"; |
| 95 | 100 | | private String mJabberUserPassword = "cc42"; |
| 96 | 100 | | private String mJabberServerName = "jabber.org"; |
| 97 | 100 | | private String mJabberHostAddress = "jabber.org"; |
| 98 | 100 | | private int mJabberHostPort = JABBER_DEFAULT_PORT; |
| 99 | 100 | | private String mJabberMucName = "jcoderz@conference.jabber.org"; |
| 100 | | | |
| 101 | | | private final String mJabberMucAlias; |
| 102 | | | |
| 103 | | | private XMPPConnection mConnection; |
| 104 | | | private GroupChat mGroupChat; |
| 105 | | | |
| 106 | | | JabberConnection () |
| 107 | 100 | | { |
| 108 | | | String localhost; |
| 109 | | | try |
| 110 | | | { |
| 111 | 100 | | localhost = InetAddress.getLocalHost().getHostName(); |
| 112 | | | } |
| 113 | 0 | | catch (Exception ex) |
| 114 | | | { |
| 115 | 0 | | localhost = "localhost"; |
| 116 | 100 | | } |
| 117 | 100 | | mHostname = localhost; |
| 118 | 100 | | mJabberMucAlias |
| 119 | | | = System.getProperty("user.name", "cruise.control") |
| 120 | | | + "@" + mHostname; |
| 121 | 100 | | } |
| 122 | | | |
| 123 | | | |
| 124 | | | |
| 125 | | | @return |
| 126 | | | |
| 127 | | | public static JabberConnection getInstance () |
| 128 | | | { |
| 129 | 100 | | return INSTANCE; |
| 130 | | | } |
| 131 | | | |
| 132 | | | |
| 133 | | | |
| 134 | | | |
| 135 | | | |
| 136 | | | |
| 137 | | | |
| 138 | | | @param |
| 139 | | | |
| 140 | | | @param |
| 141 | | | @param |
| 142 | | | |
| 143 | | | @param |
| 144 | | | |
| 145 | | | @param |
| 146 | | | @param |
| 147 | | | |
| 148 | | | |
| 149 | | | public synchronized void setup (String jabberHostName, |
| 150 | | | int jabberHostPort, String jabberServerName, String jabberUserName, |
| 151 | | | String jabberUserPassword, String jabberMucName) |
| 152 | | | { |
| 153 | 0 | | clear(); |
| 154 | 0 | | mJabberHostAddress = jabberHostName; |
| 155 | 0 | | mJabberHostPort = jabberHostPort; |
| 156 | 0 | | mJabberServerName = jabberServerName; |
| 157 | 0 | | mJabberUserName = jabberUserName; |
| 158 | 0 | | mJabberUserPassword = jabberUserPassword; |
| 159 | 0 | | mJabberMucName = jabberMucName; |
| 160 | 0 | | checkConnection(); |
| 161 | 0 | | } |
| 162 | | | |
| 163 | | | |
| 164 | | | |
| 165 | | | @param |
| 166 | | | @throws |
| 167 | | | |
| 168 | | | public synchronized void say (String message) |
| 169 | | | { |
| 170 | | | try |
| 171 | | | { |
| 172 | 0 | | checkConnection(); |
| 173 | 0 | | mGroupChat.sendMessage(message); |
| 174 | | | } |
| 175 | 0 | | catch (Exception ex) |
| 176 | | | { |
| 177 | 0 | | logger.log(Level.WARNING, "Exception sending message." |
| 178 | | | + " Will retry", ex); |
| 179 | 0 | | clear(); |
| 180 | | | try |
| 181 | | | { |
| 182 | 0 | | checkConnection(); |
| 183 | 0 | | mGroupChat.sendMessage(message); |
| 184 | | | } |
| 185 | 100 | | catch (Exception e) |
| 186 | | | { |
| 187 | 100 | | clear(); |
| 188 | 100 | (2) | throw new RuntimeException("Jabber send message failed fatal!", |
| 189 | | | e); |
| 190 | 0 | | } |
| 191 | 0 | | } |
| 192 | 0 | | } |
| 193 | | | |
| 194 | | | |
| 195 | | | |
| 196 | | | @throws |
| 197 | | | |
| 198 | | | public synchronized void checkConnection () |
| 199 | | | throws RuntimeException |
| 200 | | | { |
| 201 | | | try |
| 202 | | | { |
| 203 | 0 | | checkConnectionRaw(); |
| 204 | | | } |
| 205 | 100 | | catch (Exception ex) |
| 206 | | | { |
| 207 | 100 | | logger.log(Level.WARNING, "Exception while reconnecting (1).", ex); |
| 208 | 100 | | mGroupChat = null; |
| 209 | 0 | | } |
| 210 | | | |
| 211 | | | try |
| 212 | | | { |
| 213 | 0 | | checkConnectionRaw(); |
| 214 | | | } |
| 215 | 100 | | catch (Exception ex) |
| 216 | | | { |
| 217 | 100 | | logger.log(Level.WARNING, "Exception while reconnecting (2).", |
| 218 | | | ex); |
| 219 | 100 | | mConnection = null; |
| 220 | 100 | | mGroupChat = null; |
| 221 | 0 | | } |
| 222 | | | try |
| 223 | | | { |
| 224 | 0 | | checkConnectionRaw(); |
| 225 | | | } |
| 226 | 100 | | catch (Exception ex) |
| 227 | | | { |
| 228 | 100 | | mConnection = null; |
| 229 | 100 | | mGroupChat = null; |
| 230 | 100 | | throw new RuntimeException("Jabber connect failed fatal!", ex); |
| 231 | 0 | | } |
| 232 | 0 | | } |
| 233 | | | |
| 234 | | | protected void finalize () |
| 235 | | | throws Throwable |
| 236 | | | { |
| 237 | 0 | | clear(); |
| 238 | 0 | | super.finalize(); |
| 239 | 0 | | } |
| 240 | | | |
| 241 | | | private void checkConnectionRaw () |
| 242 | | (3) | throws XMPPException, InterruptedException |
| 243 | | | { |
| 244 | 100 | | if (mConnection == null || !mConnection.isConnected()) |
| 245 | | | { |
| 246 | 100 | | clear(); |
| 247 | 100 | (4) | mConnection = new XMPPConnection ( |
| 248 | | | mJabberHostAddress, mJabberHostPort, mJabberServerName); |
| 249 | 100 | | mGroupChat = null; |
| 250 | 100 | | logger.fine("New connection to jabber server. TLS: " |
| 251 | | | + mConnection.isUsingTLS() + " secure: " |
| 252 | | | + mConnection.isSecureConnection()); |
| 253 | 100 | | if (!mConnection.isConnected()) |
| 254 | | | { |
| 255 | 0 | | Thread.sleep(GRACEFUL_PERIOD); |
| 256 | | | } |
| 257 | | | } |
| 258 | | | |
| 259 | 100 | | if (!mConnection.isAuthenticated()) |
| 260 | | | { |
| 261 | 100 | | mConnection.login(mJabberUserName, mJabberUserPassword, mHostname); |
| 262 | 100 | | mGroupChat = null; |
| 263 | 100 | | logger.fine("Login to jabber server. (" |
| 264 | | | + mConnection.isAuthenticated() + ")."); |
| 265 | | | } |
| 266 | | | |
| 267 | 100 | | if (mGroupChat == null) |
| 268 | | | { |
| 269 | 100 | | mGroupChat = mConnection.createGroupChat(mJabberMucName); |
| 270 | 100 | | logger.fine("New group chat generated."); |
| 271 | | | } |
| 272 | | | |
| 273 | 100 | | if (!mGroupChat.isJoined()) |
| 274 | | | { |
| 275 | 0 | | mGroupChat.join(mJabberMucAlias); |
| 276 | 0 | | if (!mGroupChat.isJoined()) |
| 277 | | | { |
| 278 | 0 | | Thread.sleep(GRACEFUL_PERIOD); |
| 279 | | | } |
| 280 | 0 | | logger.fine("Joined to group chat. (" + mGroupChat.isJoined() |
| 281 | | | + ")."); |
| 282 | | | } |
| 283 | 0 | | } |
| 284 | | | |
| 285 | | | private void clear () |
| 286 | | | { |
| 287 | | | try |
| 288 | | | { |
| 289 | 100 | | if (mGroupChat != null) |
| 290 | | | { |
| 291 | 0 | | mGroupChat.leave(); |
| 292 | | | } |
| 293 | | | } |
| 294 | 0 | | catch (Exception ex) |
| 295 | | | { |
| 296 | 0 | | logger.log(Level.FINEST, "Exception while leaving groupchat.", ex); |
| 297 | 100 | | } |
| 298 | 100 | | mGroupChat = null; |
| 299 | | | try |
| 300 | | | { |
| 301 | 100 | | if (mConnection != null) |
| 302 | | | { |
| 303 | 0 | | mConnection.close(); |
| 304 | | | } |
| 305 | | | } |
| 306 | 0 | | catch (Exception ex) |
| 307 | | | { |
| 308 | 0 | | logger.log(Level.FINEST, |
| 309 | | | "Exception while closing jabber connection.", ex); |
| 310 | 100 | | } |
| 311 | 100 | | mConnection = null; |
| 312 | 100 | | } |
| 313 | | | |
| 314 | | | } |
| 315 | | | |
| 316 | | | |
| 317 | | | } |