3 import org.opensrf.util.*;
4 import org.opensrf.net.xmpp.*;
5 import java.util.Random;
7 import java.net.InetAddress;
12 private static void initLogger(Config config) {
13 if(Logger.instance() == null) {
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());
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
33 public static void bootstrapClient(String configFile, String configContext)
34 throws ConfigException, SessionException {
37 /** see if the current thread already has a connection */
38 XMPPSession existing = XMPPSession.getThreadSession();
39 if(existing != null && existing.connected())
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 */
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");
56 /** Create a random login resource string */
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();
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);
74 } catch(XMPPException e) {
75 throw new SessionException("Unable to bootstrap client", e);
80 * Shuts down the connection to the opensrf network
82 public static void shutdown() {
83 XMPPSession.getThreadSession().disconnect();