]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/java/org/opensrf/ClientSession.java
added session/request locale support
[OpenSRF.git] / src / java / org / opensrf / ClientSession.java
1 package org.opensrf;
2 import java.util.Date;
3 import java.util.List;
4 import java.util.Map;
5 import java.util.HashMap;
6 import java.util.ArrayList;
7 import java.util.Random;
8 import java.util.Arrays;
9
10 import org.opensrf.util.*;
11 import org.opensrf.net.xmpp.*;
12
13
14 /**
15  * Models an OpenSRF client session.
16  */
17 public class ClientSession extends Session {
18
19     /** The remote service to communicate with */
20     private String service;
21     /** OpenSRF domain */
22     private String domain;
23     /** Router name */
24     private String router;
25
26     /** 
27      * original remote node.  The current remote node will change based 
28      * on server responses.  This is used to reset the remote node to 
29      * its original state.
30      */
31     private String origRemoteNode;
32     /** The next request id */
33     private int nextId;
34     /** The requests this session has sent */
35     private Map<Integer, Request> requests;
36
37     /**
38      * Creates a new client session.  Initializes the 
39      * @param service The remote service.
40      */
41     public ClientSession(String service) throws ConfigException {
42         this(service, null);
43     }
44
45     /**
46      * Creates a new client session.  Initializes the 
47      * @param service The remote service.
48      * @param locale The locale for this session.
49      */
50     public ClientSession(String service, String locale) throws ConfigException {
51         this.service = service;
52         if(locale != null) 
53             setLocale(locale);
54
55         /** generate the remote node string */
56         domain = (String) Config.global().getFirst("/domain");
57         router = Config.global().getString("/router_name");
58         setRemoteNode(router + "@" + domain + "/" + service);
59         origRemoteNode = getRemoteNode();
60
61
62         /** create a random thread */
63         long time = new Date().getTime();
64         Random rand = new Random(time);
65         setThread(rand.nextInt()+""+rand.nextInt()+""+time+Thread.currentThread().getId());
66
67         nextId = 0;
68         requests = new HashMap<Integer, Request>();
69         cacheSession();
70     }
71
72     /**
73      * Creates a new request to send to our remote service.
74      * @param method The method API name
75      * @param params The list of method parameters
76      * @return The request object.
77      */
78     public Request request(String method, List<Object> params) throws SessionException {
79         return request(new Request(this, nextId++, method, params));
80     }
81
82     /**
83      * Creates a new request to send to our remote service.
84      * @param method The method API name
85      * @param params The list of method parameters
86      * @return The request object.
87      */
88     public Request request(String method, Object[] params) throws SessionException {
89         return request(new Request(this, nextId++, method, Arrays.asList(params)));
90     }
91
92
93     /**
94      * Creates a new request to send to our remote service.
95      * @param method The method API name
96      * @return The request object.
97      */
98     public Request request(String method) throws SessionException {
99         return request(new Request(this, nextId++, method));
100     }
101
102
103     private Request request(Request req) throws SessionException {
104         if(getConnectState() != ConnectState.CONNECTED)
105             resetRemoteId();
106         requests.put(new Integer(req.getId()), req);
107         req.send();
108         return req;
109     }
110
111
112     /**
113      * Resets the remoteNode to its original state.
114      */
115     public void resetRemoteId() {
116         setRemoteNode(origRemoteNode);
117     }
118
119
120     /**
121      * Pushes a response onto the result queue of the appropriate request.
122      * @param msg The received RESULT Message
123      */
124     public void pushResponse(Message msg) {
125
126         Request req = findRequest(msg.getId());
127         if(req == null) {
128             /** LOG that we've received a result to a non-existant request */
129             System.err.println(msg.getId() +" has no corresponding request");
130             return;
131         }
132         OSRFObject payload = (OSRFObject) msg.get("payload");
133
134         /** build a result and push it onto the request's result queue */
135         req.pushResponse(
136             new Result( 
137                 payload.getString("status"), 
138                 payload.getInt("statusCode"),
139                 payload.get("content")
140             )
141         );
142     }
143
144     public Request findRequest(int reqId) {
145         return requests.get(new Integer(reqId));
146     }
147
148     /**
149      * Removes a request for this session's request set
150      */
151     public void cleanupRequest(int reqId) {
152         requests.remove(new Integer(reqId));
153     }
154
155      public void setRequestComplete(int reqId) {
156         Request req = findRequest(reqId);
157         if(req == null) return;
158         req.setComplete();
159     }
160
161     public static Object atomicRequest(String service, String method, Object[] params) throws MethodException {
162         try {
163             ClientSession session = new ClientSession(service);
164             Request osrfRequest = session.request(method, params);
165             Result result = osrfRequest.recv(600000);
166             if(result.getStatusCode() != 200) 
167                 throw new MethodException( 
168                     "Request "+service+":"+method+":"+" failed with status code " + result.getStatusCode());
169             return result.getContent();
170         } catch(Exception e) {
171             throw new MethodException(e);
172         }
173     }
174 }
175