]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/java/org/opensrf/Sys.java
updated to match new opensrf_core.xml domain layout
[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     private static void initLogger(Config config) {
13         if(Logger.instance() == null) {
14             try {
15                 String logFile = config.getString("/logfile");
16                 int logLevel = config.getInt("/loglevel");
17                 Logger.init( (short) config.getInt("/loglevel"), new FileLogger(logFile));
18                 /** add syslog support... */
19             } catch(Exception e) {
20                 /* by default, log to stderr at WARN level */
21                 Logger.init(Logger.WARN, new Logger()); 
22             }
23         }
24     }
25
26     /**
27      * Connects to the OpenSRF network so that client sessions may communicate.
28      * @param configFile The OpenSRF config file 
29      * @param configContext Where in the XML document the config chunk lives.  This
30      * allows an OpenSRF client config chunk to live in XML files where other config
31      * information lives.
32      */
33     public static void bootstrapClient(String configFile, String configContext) 
34             throws ConfigException, SessionException  {
35
36
37         /** see if the current thread already has a connection */
38         XMPPSession existing = XMPPSession.getThreadSession();
39         if(existing != null && existing.connected())
40             return;
41
42         /** create the config parser */
43         Config config = new Config(configContext);
44         config.parse(configFile);
45         Config.setConfig(config); /* set this as the global config */
46
47         initLogger(config);
48
49         /** Collect the network connection info from the config */
50         String username = config.getString("/username");
51         String passwd = config.getString("/passwd");
52         String host = (String) config.getFirst("/domain");
53         int port = config.getInt("/port");
54
55
56         /** Create a random login resource string */
57         String res = "java_";
58         try {
59             res += InetAddress.getLocalHost().getHostAddress();
60         } catch(java.net.UnknownHostException e) {}
61         res += "_"+Math.abs(new Random(new Date().getTime()).nextInt()) 
62             + "_t"+ Thread.currentThread().getId();
63
64
65
66         try {
67
68             /** Connect to the Jabber network */
69             Logger.info("attempting to create XMPP session "+username+"@"+host+"/"+res);
70             XMPPSession xses = new XMPPSession(host, port);
71             xses.connect(username, passwd, res);
72             XMPPSession.setThreadSession(xses);
73
74         } catch(XMPPException e) {
75             throw new SessionException("Unable to bootstrap client", e);
76         }
77     }
78
79     /**
80      * Shuts down the connection to the opensrf network
81      */
82     public static void shutdown() {
83         XMPPSession.getThreadSession().disconnect();
84     }
85 }
86