]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/java/org/opensrf/Sys.java
added some more rigorous checking of session connected-ness and some logging
[OpenSRF.git] / src / java / org / opensrf / Sys.java
1 package org.opensrf;
2
3 import org.opensrf.util.*;
4 import org.opensrf.net.xmpp.*;
5 import java.util.Random;
6 import java.util.Date;
7 import java.net.InetAddress;
8
9
10 public class Sys {
11
12     /**
13      * Connects to the OpenSRF network so that client sessions may communicate.
14      * @param configFile The OpenSRF config file 
15      * @param configContext Where in the XML document the config chunk lives.  This
16      * allows an OpenSRF client config chunk to live in XML files where other config
17      * information lives.
18      */
19     public static void bootstrapClient(String configFile, String configContext) 
20             throws ConfigException, SessionException  {
21
22         /** see if the current thread already has a connection */
23         XMPPSession existing = XMPPSession.getThreadSession();
24         if(existing != null && existing.connected())
25             return;
26
27         /** create the config parser */
28         Config config = new Config(configContext);
29         config.parse(configFile);
30         Config.setConfig(config); /* set this as the global config */
31
32         /** Collect the network connection info from the config */
33         String username = config.getString("/username");
34         String passwd = config.getString("/passwd");
35         String host = (String) config.getFirst("/domains/domain");
36         int port = config.getInt("/port");
37
38
39         /** Create a random login resource string */
40         String res = "java_";
41         try {
42             res += InetAddress.getLocalHost().getHostAddress();
43         } catch(java.net.UnknownHostException e) {}
44         res += "_"+Math.abs(new Random(new Date().getTime()).nextInt()) 
45             + "_t"+ Thread.currentThread().getId();
46
47
48         try {
49
50             /** Connect to the Jabber network */
51             Logger.info("attempting to create XMPP session "+username+"@"+host+"/"+res);
52             XMPPSession xses = new XMPPSession(host, port);
53             xses.connect(username, passwd, res);
54             XMPPSession.setThreadSession(xses);
55
56         } catch(XMPPException e) {
57             throw new SessionException("Unable to bootstrap client", e);
58         }
59     }
60
61     /**
62      * Shuts down the connection to the opensrf network
63      */
64     public static void shutdown() {
65         XMPPSession.getThreadSession().disconnect();
66     }
67 }
68