1 17 package org.apache.servicemix.components.jabber; 18 19 import org.apache.commons.logging.Log; 20 import org.apache.commons.logging.LogFactory; 21 import org.apache.servicemix.components.util.OutBinding; 22 import org.jivesoftware.smack.AccountManager; 23 import org.jivesoftware.smack.PacketListener; 24 import org.jivesoftware.smack.XMPPConnection; 25 import org.jivesoftware.smack.XMPPException; 26 import org.jivesoftware.smack.packet.Packet; 27 import org.springframework.beans.factory.InitializingBean; 28 29 import javax.jbi.JBIException; 30 import javax.jbi.messaging.InOnly; 31 import javax.jbi.messaging.MessagingException; 32 import javax.jbi.messaging.NormalizedMessage; 33 34 37 public abstract class JabberComponentSupport extends OutBinding implements InitializingBean, PacketListener { 38 private static final transient Log log = LogFactory.getLog(JabberComponentSupport.class); 39 40 private JabberMarshaler marshaler = new JabberMarshaler(); 41 private XMPPConnection connection; 42 private String host; 43 private int port; 44 private String user; 45 private String password; 46 private String resource = "ServiceMix"; 47 private boolean login = true; 48 49 public void afterPropertiesSet() throws Exception { 50 if (connection == null) { 51 if (host == null) { 52 throw new IllegalArgumentException ("You must specify the connection or the host property"); 53 } 54 } 55 } 56 57 public void start() throws JBIException { 58 try { 59 if (connection == null) { 60 if (port > 0) { 61 connection = new XMPPConnection(host, port); 62 } 63 else { 64 connection = new XMPPConnection(host); 65 } 66 } 67 if (login && !connection.isAuthenticated()) { 68 if (user != null) { 69 AccountManager accountManager = new AccountManager(connection); 70 accountManager.createAccount(user, password); 71 72 log.info("Logging in to Jabber as user: " + user + " on connection: " + connection); 73 connection.login(user, password, resource); 74 } 75 else { 76 log.info("Logging in anonymously to Jabber on connection: " + connection); 77 connection.loginAnonymously(); 78 } 79 } 80 } 81 catch (XMPPException e) { 82 throw new JBIException("Failed to login to Jabber. Reason: " + e, e); 83 } 84 } 85 86 public void stop() throws JBIException { 87 if (connection != null) { 88 connection.close(); 89 connection = null; 90 } 91 } 92 93 public void processPacket(Packet packet) { 94 try { 95 InOnly exchange = getExchangeFactory().createInOnlyExchange(); 96 NormalizedMessage in = exchange.createMessage(); 97 exchange.setInMessage(in); 98 marshaler.toNMS(in, packet); 99 done(exchange); 100 } 101 catch (MessagingException e) { 102 throw new JabberListenerException(e, packet); 103 } 104 } 105 106 public XMPPConnection getConnection() { 109 return connection; 110 } 111 112 public void setConnection(XMPPConnection connection) { 113 this.connection = connection; 114 } 115 116 public String getHost() { 117 return host; 118 } 119 120 public void setHost(String host) { 121 this.host = host; 122 } 123 124 public int getPort() { 125 return port; 126 } 127 128 public void setPort(int port) { 129 this.port = port; 130 } 131 132 public JabberMarshaler getMarshaler() { 133 return marshaler; 134 } 135 136 public void setMarshaler(JabberMarshaler marshaler) { 137 this.marshaler = marshaler; 138 } 139 140 public String getUser() { 141 return user; 142 } 143 144 public void setUser(String user) { 145 this.user = user; 146 } 147 148 public String getPassword() { 149 return password; 150 } 151 152 public void setPassword(String password) { 153 this.password = password; 154 } 155 156 public String getResource() { 157 return resource; 158 } 159 160 public void setResource(String resource) { 161 this.resource = resource; 162 } 163 164 public boolean isLogin() { 165 return login; 166 } 167 168 public void setLogin(boolean login) { 169 this.login = login; 170 } 171 } 172 | Popular Tags |