1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springframework.ws.transport.xmpp.support;
18
19 import org.springframework.beans.factory.DisposableBean;
20 import org.springframework.beans.factory.FactoryBean;
21 import org.springframework.beans.factory.InitializingBean;
22 import org.springframework.util.Assert;
23 import org.springframework.util.StringUtils;
24
25 import org.jivesoftware.smack.ConnectionConfiguration;
26 import org.jivesoftware.smack.XMPPConnection;
27 import org.jivesoftware.smack.XMPPException;
28
29
30
31
32
33
34
35
36 public class XmppConnectionFactoryBean implements FactoryBean<XMPPConnection>, InitializingBean, DisposableBean {
37
38 private static final int DEFAULT_PORT = 5222;
39
40 private XMPPConnection connection;
41
42 private String host;
43
44 private int port = DEFAULT_PORT;
45
46 private String serviceName;
47
48 private String username;
49
50 private String password;
51
52 private String resource;
53
54
55 public void setHost(String host) {
56 this.host = host;
57 }
58
59
60
61
62
63
64 public void setPort(int port) {
65 Assert.isTrue(port > 0, "'port' must be larger than 0");
66 this.port = port;
67 }
68
69
70 public void setServiceName(String serviceName) {
71 this.serviceName = serviceName;
72 }
73
74 public void setUsername(String username) {
75 this.username = username;
76 }
77
78 public void setPassword(String password) {
79 this.password = password;
80 }
81
82 public void setResource(String resource) {
83 this.resource = resource;
84 }
85
86 public void afterPropertiesSet() throws XMPPException {
87 ConnectionConfiguration configuration = createConnectionConfiguration(host, port, serviceName);
88 Assert.notNull(configuration, "'configuration' must not be null");
89 Assert.hasText(username, "'username' must not be empty");
90 Assert.hasText(password, "'password' must not be empty");
91
92 connection = new XMPPConnection(configuration);
93 connection.connect();
94 if (StringUtils.hasText(resource)) {
95 connection.login(username, password, resource);
96 }
97 else {
98 connection.login(username, password);
99 }
100 }
101
102 public void destroy() {
103 connection.disconnect();
104 }
105
106 public XMPPConnection getObject() {
107 return connection;
108 }
109
110 public Class<XMPPConnection> getObjectType() {
111 return XMPPConnection.class;
112 }
113
114 public boolean isSingleton() {
115 return true;
116 }
117
118
119
120
121
122
123
124
125 protected ConnectionConfiguration createConnectionConfiguration(String host, int port, String serviceName) {
126 Assert.hasText(host, "'host' must not be empty");
127 if (StringUtils.hasText(serviceName)) {
128 return new ConnectionConfiguration(host, port, serviceName);
129 }
130 else {
131 return new ConnectionConfiguration(host, port);
132 }
133 }
134
135
136 }