KickJava   Java API By Example, From Geeks To Geeks.

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


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.jivesoftware.smack.Chat;
20 import org.jivesoftware.smack.packet.Message;
21
22 import javax.jbi.JBIException;
23 import javax.jbi.messaging.MessageExchange;
24 import javax.jbi.messaging.NormalizedMessage;
25
26 /**
27  * Sends one way messages to a private Jabber {@link Chat}
28  *
29  * @version $Revision: 426415 $
30  */

31 public class JabberChatSender extends JabberComponentSupport {
32
33     private Chat chat;
34     private String JavaDoc participant;
35
36     public void afterPropertiesSet() throws Exception JavaDoc {
37         super.afterPropertiesSet();
38         if (chat == null) {
39             if (participant == null) {
40                 throw new IllegalArgumentException JavaDoc("You must specify the participant property");
41             }
42         }
43     }
44
45     public void start() throws JBIException {
46         super.start();
47         if (chat == null) {
48             chat = getConnection().createChat(participant);
49         }
50     }
51
52     public void stop() throws JBIException {
53         chat = null;
54         super.stop();
55     }
56
57     // Properties
58
//-------------------------------------------------------------------------
59
public Chat getChat() {
60         return chat;
61     }
62
63     public void setChat(Chat chat) {
64         this.chat = chat;
65     }
66
67     public String JavaDoc getParticipant() {
68         return participant;
69     }
70
71     public void setParticipant(String JavaDoc participant) {
72         this.participant = participant;
73     }
74
75     // Implementation methods
76
//-------------------------------------------------------------------------
77
protected void process(MessageExchange messageExchange, NormalizedMessage normalizedMessage) throws Exception JavaDoc {
78         Message message = chat.createMessage();
79         getMarshaler().fromNMS(message, normalizedMessage);
80         chat.sendMessage(message);
81         done(messageExchange);
82     }
83 }
84
Popular Tags