KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > components > jabber > JabberComponentSupport


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

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 /**
35  * @version $Revision: 426415 $
36  */

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 JavaDoc host;
43     private int port;
44     private String JavaDoc user;
45     private String JavaDoc password;
46     private String JavaDoc resource = "ServiceMix";
47     private boolean login = true;
48
49     public void afterPropertiesSet() throws Exception JavaDoc {
50         if (connection == null) {
51             if (host == null) {
52                 throw new IllegalArgumentException JavaDoc("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     // Properties
107
//-------------------------------------------------------------------------
108
public XMPPConnection getConnection() {
109         return connection;
110     }
111
112     public void setConnection(XMPPConnection connection) {
113         this.connection = connection;
114     }
115
116     public String JavaDoc getHost() {
117         return host;
118     }
119
120     public void setHost(String JavaDoc 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 JavaDoc getUser() {
141         return user;
142     }
143
144     public void setUser(String JavaDoc user) {
145         this.user = user;
146     }
147
148     public String JavaDoc getPassword() {
149         return password;
150     }
151
152     public void setPassword(String JavaDoc password) {
153         this.password = password;
154     }
155
156     public String JavaDoc getResource() {
157         return resource;
158     }
159
160     public void setResource(String JavaDoc 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