001: package hero.ra.jabber;
002:
003: /**
004: *
005: * Bonita
006: * Copyright (C) 1999 Bull S.A.
007: * Bull 68 route de versailles 78434 Louveciennes Cedex France
008: * Further information: bonita@objectweb.org
009: *
010: * This library is free software; you can redistribute it and/or
011: * modify it under the terms of the GNU Lesser General Public
012: * License as published by the Free Software Foundation; either
013: * version 2.1 of the License, or any later version.
014: *
015: * This library is distributed in the hope that it will be useful,
016: * but WITHOUT ANY WARRANTY; without even the implied warranty of
017: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
018: * Lesser General Public License for more details.
019: *
020: * You should have received a copy of the GNU Lesser General Public
021: * License along with this library; if not, write to the Free Software
022: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
023: * USA
024: *
025: *
026: --------------------------------------------------------------------------
027: * $Id$
028: *
029: --------------------------------------------------------------------------
030: */
031:
032: import com.echomine.jabber.*;
033: import com.echomine.net.*;
034:
035: import java.util.HashMap;
036: import java.util.List;
037:
038: import javax.management.MBeanServer;
039: import javax.management.MBeanServerFactory;
040: import javax.management.ObjectName;
041:
042: import hero.ra.BonitaService;
043:
044: public class Jabber implements BonitaService, JabberMBean {
045: /** Password used for the Jabber session */
046: private String password = null;
047:
048: /** Port used for the Jabber session */
049: private int port = 0;
050:
051: /** Host used for the Jabber session */
052: private String host = null;
053:
054: /** Jabber Session */
055: private JabberSession jabberSession;
056:
057: /** Jabber Context */
058: private JabberContext jabberContext;
059:
060: /** User name used for the Jabber session */
061: private String user = null;
062:
063: /**
064: * Connects to the Jabber server based on User, Password, Host and Port
065: * class attributes.
066: */
067: private void connect() {
068: // Initialize the Jabber context
069: jabberContext = new JabberContext(user, password, host);
070:
071: // Create an instance of the Jabber session factory
072: com.echomine.jabber.Jabber jabber = new com.echomine.jabber.Jabber();
073:
074: // Create the new session
075: jabberSession = jabber.createSession(jabberContext);
076:
077: // Connect to the server
078: try {
079: jabberSession.connect(host, port);
080: } catch (Exception e) {
081: System.err.println(" Jabber service -> Connection Failed:"
082: + e.getMessage());
083: }
084:
085: // Connect to the Jabber server
086: try {
087: jabberSession.getUserService().login();
088: } catch (Exception e) {
089: System.err.println(" Jabber service -> Login Failed:"
090: + e.getMessage());
091: }
092:
093: }
094:
095: /**
096: * Invoked when the service is stopped
097: */
098: public void stopService() {
099: /*
100: * The session may is null when the Jabber feature is turned off or if
101: * a problem occured during initialization
102: */
103: if (jabberSession != null) {
104: try {
105: jabberSession.disconnect();
106: } catch (Exception e) {
107: System.err
108: .println(" Jabber service -> Disconnect Failed:"
109: + e.getMessage());
110: }
111:
112: jabberSession = null;
113: }
114: }
115:
116: /**
117: * Invoked when the service is started
118: */
119: public void startService() throws Exception {
120: // Retrieve the MBean Server
121: ObjectName confObjName = new ObjectName(
122: "bonita:type=bonitaservice,name=Config");
123: List list = MBeanServerFactory.findMBeanServer(null);
124: MBeanServer mServer = (MBeanServer) list.iterator().next();
125:
126: /*
127: * MBean Attributes are retrieved by name so that Configuration classes
128: * do not need to be packaged in the RAR.
129: */
130: boolean enabled = ((Boolean) mServer.getAttribute(confObjName,
131: "JabberEnabled")).booleanValue();
132:
133: // If the configuration indicates that the Jabber feature is on
134: if (enabled) {
135: // Retrieve configuration attributes.
136: password = (String) mServer.getAttribute(confObjName,
137: "JabberPassword");
138: port = ((Integer) mServer.getAttribute(confObjName,
139: "JabberPort")).intValue();
140: host = (String) mServer.getAttribute(confObjName,
141: "JabberHost");
142: user = (String) mServer.getAttribute(confObjName,
143: "JabberUser");
144:
145: // Connect to the Jabber service
146: connect();
147: }
148: }
149:
150: /**
151: * Sends a Jabber message to the specified JID
152: */
153: public synchronized boolean sendMessage(String JID,
154: String resource, String subject, String messageBody) {
155: try {
156: JabberChatMessage msg = new JabberChatMessage(
157: JabberChatMessage.TYPE_NORMAL);
158: msg.setSubject(subject);
159: msg.setBody(messageBody);
160: msg.setTo(JID);
161: jabberSession.sendMessage(msg);
162: return true;
163: } catch (Exception e) {
164: System.err
165: .println(" Jabber service -> Send Message Failed:"
166: + e.getMessage());
167: }
168:
169: return false;
170: }
171:
172: /**
173: * Registers a new user to Jabber server
174: */
175: public synchronized boolean registerUser(String login,
176: String password, String email, String name) {
177: boolean status = true;
178: jabberSession.disconnect();
179: com.echomine.jabber.Jabber jabber = new com.echomine.jabber.Jabber();
180: JabberSession session = jabber.createSession(jabberContext);
181:
182: try {
183: session.connect(host, port);
184: HashMap fields = new HashMap();
185: fields.put("username", login);
186: fields.put("password", password);
187: fields.put("email", email);
188: fields.put("name", name);
189:
190: try {
191: session.getUserService().register(
192: jabberContext.getServerName(), fields);
193: } catch (JabberMessageException ex) {
194: status = false;
195: System.err.println(" Jabber service ->" + ex);
196: }
197:
198: session.disconnect();
199: connect();
200: return status;
201: } catch (Exception e) {
202: System.err.println(" Jabber service ->" + e);
203: }
204:
205: connect();
206:
207: return false;
208: }
209:
210: /**
211: * Add to Roster
212: */
213: public synchronized boolean addToRoster(String login,
214: String password, String contactJID, String contactName,
215: String contactGroup) {
216: boolean status = true;
217: jabberSession.disconnect();
218: com.echomine.jabber.Jabber jabber = new com.echomine.jabber.Jabber();
219: JabberContext context = new JabberContext(login, password, host);
220: JabberSession session = jabber.createSession(context);
221:
222: try {
223: session.connect(host, port);
224: session.getUserService().login();
225:
226: try {
227: session.getRosterService().addToRoster(contactJID,
228: contactName, contactGroup, true);
229: session.getPresenceService().subscribe(contactJID);
230: } catch (Exception ex) {
231: status = false;
232: System.err.println(" Jabber service ->" + ex);
233: }
234:
235: session.disconnect();
236: connect();
237:
238: return status;
239: } catch (Exception e) {
240: System.err.println(" Jabber service ->" + e);
241: }
242:
243: connect();
244: return false;
245: }
246: }
|