5 import java.util.HashMap;
6 import java.util.ArrayList;
7 import java.util.Random;
8 import java.util.Arrays;
10 import org.opensrf.util.*;
11 import org.opensrf.net.xmpp.*;
15 * Models an OpenSRF client session.
17 public class ClientSession extends Session {
19 /** The remote service to communicate with */
20 private String service;
22 private String domain;
24 private String router;
27 * original remote node. The current remote node will change based
28 * on server responses. This is used to reset the remote node to
31 private String origRemoteNode;
32 /** The next request id */
34 /** The requests this session has sent */
35 private Map<Integer, Request> requests;
38 * Creates a new client session. Initializes the
39 * @param service The remove service.
41 public ClientSession(String service) throws ConfigException {
42 this.service = service;
44 /** generate the remote node string */
45 domain = (String) Config.getFirst("/domains/domain");
46 router = Config.getString("/router_name");
47 setRemoteNode(router + "@" + domain + "/" + service);
48 origRemoteNode = getRemoteNode();
51 /** create a random thread */
52 long time = new Date().getTime();
53 Random rand = new Random(time);
54 setThread(rand.nextInt()+""+rand.nextInt()+""+time);
57 requests = new HashMap<Integer, Request>();
62 * Creates a new request to send to our remote service.
63 * @param method The method API name
64 * @param params The list of method parameters
65 * @return The request object.
67 public Request request(String method, List<Object> params) throws SessionException {
68 return request(new Request(this, nextId++, method, params));
72 * Creates a new request to send to our remote service.
73 * @param method The method API name
74 * @param params The list of method parameters
75 * @return The request object.
77 public Request request(String method, Object[] params) throws SessionException {
78 return request(new Request(this, nextId++, method, Arrays.asList(params)));
83 * Creates a new request to send to our remote service.
84 * @param method The method API name
85 * @return The request object.
87 public Request request(String method) throws SessionException {
88 return request(new Request(this, nextId++, method));
92 private Request request(Request req) throws SessionException {
93 if(getConnectState() != ConnectState.CONNECTED)
95 requests.put(new Integer(req.getId()), req);
102 * Resets the remoteNode to its original state.
104 public void resetRemoteId() {
105 setRemoteNode(origRemoteNode);
110 * Pushes a response onto the result queue of the appropriate request.
111 * @param msg The received RESULT Message
113 public void pushResponse(Message msg) {
115 Request req = findRequest(msg.getId());
117 /** LOG that we've received a result to a non-existant request */
120 OSRFObject payload = (OSRFObject) msg.get("payload");
122 /** build a result and push it onto the request's result queue */
125 payload.getString("status"),
126 payload.getInt("statusCode"),
127 payload.get("content")
132 public Request findRequest(int reqId) {
133 return requests.get(new Integer(reqId));
137 * Removes a request for this session's request set
139 public void cleanupRequest(int reqId) {
140 requests.remove(new Integer(reqId));
143 public void setRequestComplete(int reqId) {
144 Request req = findRequest(reqId);
145 if(req == null) return;
151 * Connects to the OpenSRF network so that client sessions may communicate.
152 * @param configFile The OpenSRF config file
153 * @param configContext Where in the XML document the config chunk lives. This
154 * allows an OpenSRF client config chunk to live in XML files where other config
158 public static void bootstrap(String configFile, String configContext)
159 throws ConfigException, SessionException {
161 Config config = new Config(configContext);
162 config.parse(configFile);
163 Config.setConfig(config);
165 String username = Config.getString("/username");
166 String passwd = Config.getString("/passwd");
167 String host = (String) Config.getFirst("/domains/domain");
168 int port = Config.getInt("/port");
171 XMPPSession xses = new XMPPSession(host, port);
172 xses.connect(username, passwd, "test-java");
173 XMPPSession.setGlobalSession(xses);
174 } catch(XMPPException e) {
175 throw new SessionException("Unable to bootstrap client", e);