View Javadoc

1   /*
2    * Copyright 2005-2010 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *      http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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   * Factory to make {@link org.jivesoftware.smack.XMPPConnection} and perform connection and login on the XMPP server
31   *
32   * @author Gildas Cuisinier
33   * @author Arjen Poutsma
34   * @since 2.0
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      /** Sets the server host to connect to. */
55      public void setHost(String host) {
56          this.host = host;
57      }
58  
59      /**
60       * Sets the the server port to connect to.
61       * <p/>
62       * Defaults to {@code 5222}.
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      /** Sets the service name to connect to. */
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      * Creates the {@code ConnectionConfiguration} from the given parameters.
120      *
121      * @param host        the host to connect to
122      * @param port        the port to connect to
123      * @param serviceName the name of the service to connect to. May be {@code null}
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 }