View Javadoc

1   /*
2    * Copyright (c) 2002 Peter Antman, Teknik i Media  <peter.antman@tim.se>
3    *
4    * $Id: JabberAliveTest.java,v 1.1.1.1 2004/05/19 12:33:41 pra Exp $
5    *
6    * This library is free software; you can redistribute it and/or
7    * modify it under the terms of the GNU Lesser General Public
8    * License as published by the Free Software Foundation; either
9    * version 2 of the License, or (at your option) any later version
10   * 
11   * This library is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   * Lesser General Public License for more details.
15   * 
16   * You should have received a copy of the GNU Lesser General Public
17   * License along with this library; if not, write to the Free Software
18   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19   */
20  package org.backsource.alert.agent;
21  import org.jabber.webb.*;
22  import org.jabber.webb.packet.*;
23  
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  
27  import org.backsource.alert.PropertyConstants;
28  import org.backsource.alert.AlertError;
29  /***
30   * Check that is possible to log into jabber.
31   *
32   * <p>.It uses thre properties {@link org.backsource.alert.PropertyConstants#JABBER_SERVER},{@link org.backsource.alert.PropertyConstants#JABBER_USER} and {@link org.backsource.alert.PropertyConstants#JABBER_PWD}.</p>
33   *
34   *
35   *
36   * @author <a href="mailto:pra@tim.se">Peter Antman</a>
37   * @version $Revision: 1.1.1.1 $
38   */
39  
40  public class JabberAliveTest extends BaseCase{
41     static final Log log = LogFactory.getLog(JabberAliveTest.class);
42     String server;
43     String user;
44     String pwd;
45     String resource ="JabberAliveTest";
46     boolean error = false;
47     int prio = 6;
48  
49  
50     public JabberAliveTest (String name){
51        super(name);
52     }
53     protected void setUp() throws Exception{
54        super.setUp();
55        server = prop.getProperty(PropertyConstants.JABBER_SERVER);
56        user =prop.getProperty(PropertyConstants.JABBER_USER);
57        pwd = prop.getProperty(PropertyConstants.JABBER_PWD);
58        
59     }
60     
61     /***
62      * Agent/test method.
63      *
64      * <p>Check if its possible to log into the jabber server given in {@link org.backsource.alert.PropertyConstants#JABBER_SERVER}.</p>
65      */
66     public void testLogin() {
67        JabberConnection con = null;
68        try {
69           
70           con = new JabberConnection();
71           con.addListener( new JabberEventHandler(con) );
72           con.connect(server);
73  	
74           //Login
75           AuthenticationPacket auth = new AuthenticationPacket(user, pwd, resource);
76           try {
77              log.debug( "Loggin in with auth packet: " + auth.formatAsXML() );
78           } catch(Exception ex) {
79               
80           }
81           con.sendPacket(auth);
82  	
83           // Add precence
84           PresencePacket pre = new PresencePacket(null, null, null);
85           try {
86              log.debug( "Sending presence: " + pre.formatAsXML() );
87           } catch(Exception ex) {
88   
89           }
90  	
91           con.sendPacket(pre);
92  
93  
94           Thread.sleep(4000);
95  
96           // Check if listener got an error
97           if ( error) {
98              throw new AlertError("Error in contacting jabber server " + server + ": ");
99           } // end of if ()
100          
101          
102 
103       }catch (Exception e) {
104          throw new AlertError("Error in contacting jabber server " + server + ": " + e);
105       } // end of catch
106       finally {
107          try {
108             
109             //OK we are finnished
110             if ( con != null) {
111                con.disconnect();
112             } // end of if ()
113             
114          } catch (Throwable e) {
115             // we do not care
116          } // end of try-catch
117          
118       } // end of finally
119       
120       
121 
122    }
123    
124    
125    public static void main(String[] args){
126       try {
127          System.out.println("Staring jabber");
128          
129          JabberAliveTest t = new JabberAliveTest("main");
130          t.server ="linutv3.tim.se";
131          t.user = "mf";
132          t.pwd = "mypass";
133          t.testLogin();
134       } catch (Exception e) {
135          e.printStackTrace();
136       } // end of try-catch
137       
138    }
139 
140    public class JabberEventHandler extends JabberConnectionEventAdapter {
141       private JabberConnection con = null;
142       private JabberID[] presence;
143       private String name;
144       // private PresenceListener presenceListener = null;
145       public JabberEventHandler(JabberConnection con) {
146          this.con = con;
147       }
148 
149       public JabberEventHandler(JabberConnection con, JabberID[] presence) {
150          this.con = con;
151          this.presence = presence;
152       }
153       public JabberEventHandler(JabberConnection con, String name) {
154          this.con = con;
155          this.name = name;
156       }
157       /*
158         public void addPresenceListener(PresenceListener listener) {
159 	presenceListener = listener;
160         }
161       */
162       public void connected(JabberConnectionEvent evt){
163          log.debug( "Connected");
164       }
165       public void connectionError(ConnectionErrorEvent cee){
166          log.error( "Connect ERROR " + cee.getMessage());
167          error = true;
168 
169       }
170       public void disconnected(JabberConnectionEvent evt) {
171          log.debug( "Disconnected");
172 
173       }
174       public void endOfData(JabberConnectionEvent evt) {
175 
176       } 
177       public void newPacket(PacketEvent pe) {
178          try {
179 	    log.debug( "Packet: " + pe.getPacket().formatAsXML());
180             Packet pkt = pe.getPacket();
181             if( pkt instanceof AuthenticationPacket) {
182                AuthenticationPacket msg = (AuthenticationPacket)pkt;
183 
184                 if (msg.isFailure() ) {
185                    log.debug("AUTH ERROR");
186                    error = true;
187                 } // end of if ()
188                 
189 		
190 	    }
191          } catch(Exception ex) {
192 	    
193          }
194       }
195  
196    }
197 } // JabberAliveTest