]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/java/org/opensrf/Sys.java
added shutdown call to cleanly disconnect from the opensrf network
[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
6
7 public class Sys {
8
9     /**
10      * Connects to the OpenSRF network so that client sessions may communicate.
11      * @param configFile The OpenSRF config file 
12      * @param configContext Where in the XML document the config chunk lives.  This
13      * allows an OpenSRF client config chunk to live in XML files where other config
14      * information lives.
15      */
16     public static void bootstrapClient(String configFile, String configContext) 
17             throws ConfigException, SessionException  {
18
19         /** create the config parser */
20         Config config = new Config(configContext);
21         config.parse(configFile);
22         Config.setConfig(config); /* set this as the global config */
23
24         /** Collect the network connection info from the config */
25         String username = config.getString("/username");
26         String passwd = config.getString("/passwd");
27         String host = (String) config.getFirst("/domains/domain");
28         int port = config.getInt("/port");
29
30         try {
31             /** Connect to the Jabber network */
32             XMPPSession xses = new XMPPSession(host, port);
33             xses.connect(username, passwd, "test-java"); /* XXX */
34             XMPPSession.setGlobalSession(xses);
35         } catch(XMPPException e) {
36             throw new SessionException("Unable to bootstrap client", e);
37         }
38     }
39
40     /**
41      * Shuts down the connection to the opensrf network
42      */
43     public static void shutdown() {
44         XMPPSession.getGlobalSession().disconnect();
45     }
46 }
47