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