]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/java/org/opensrf/Sys.java
added multi-threaded client support to the opensrf network/xmpp layer
[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         if(XMPPSession.getThreadSession() != null)
24             return;
25
26         /** create the config parser */
27         Config config = new Config(configContext);
28         config.parse(configFile);
29         Config.setConfig(config); /* set this as the global config */
30
31         /** Collect the network connection info from the config */
32         String username = config.getString("/username");
33         String passwd = config.getString("/passwd");
34         String host = (String) config.getFirst("/domains/domain");
35         int port = config.getInt("/port");
36
37
38         /** Create a random login resource string */
39         String res = "java_";
40         try {
41             res += InetAddress.getLocalHost().getHostAddress();
42         } catch(java.net.UnknownHostException e) {}
43         res += "_"+Math.abs(new Random(new Date().getTime()).nextInt()) 
44             + "_t"+ Thread.currentThread().getId();
45
46
47         try {
48
49             /** Connect to the Jabber network */
50             XMPPSession xses = new XMPPSession(host, port);
51             System.out.println("resource = " + res);
52             xses.connect(username, passwd, res);
53             XMPPSession.setThreadSession(xses);
54
55         } catch(XMPPException e) {
56             throw new SessionException("Unable to bootstrap client", e);
57         }
58     }
59
60     /**
61      * Shuts down the connection to the opensrf network
62      */
63     public static void shutdown() {
64         XMPPSession.getThreadSession().disconnect();
65     }
66 }
67