2 import org.opensrf.util.JSONWriter;
3 import org.opensrf.net.xmpp.*;
5 import java.util.HashMap;
6 import java.util.Arrays;
8 public abstract class Session {
10 /** Represents the different connection states for a session */
11 public enum ConnectState {
17 /** local cache of existing sessions */
18 private static Map<String, Session>
19 sessionCache = new HashMap<String, Session>();
21 /** the current connection state */
22 private ConnectState connectState;
24 /** The address of the remote party we are communicating with */
25 private String remoteNode;
28 protected String locale;
29 /** Default session locale */
30 protected static String defaultLocale = "en_US";
33 * The thread is used to link messages to a given session.
34 * In other words, each session has a unique thread, and all messages
35 * in that session will carry this thread around as an indicator.
37 private String thread;
40 connectState = ConnectState.DISCONNECTED;
44 * Sends a Message to our remoteNode.
46 public void send(Message omsg) throws SessionException {
48 /** construct the XMPP message */
49 XMPPMessage xmsg = new XMPPMessage();
50 xmsg.setTo(remoteNode);
51 xmsg.setThread(thread);
52 xmsg.setBody(new JSONWriter(Arrays.asList(new Message[] {omsg})).write());
55 XMPPSession.getThreadSession().send(xmsg);
56 } catch(XMPPException e) {
57 connectState = ConnectState.DISCONNECTED;
58 throw new SessionException("Error sending message to " + remoteNode, e);
63 * Waits for a message to arrive over the network and passes
64 * all received messages to the stack for processing
65 * @param millis The number of milliseconds to wait for a message to arrive
67 public static void waitForMessage(long millis) throws SessionException, MethodException {
69 Stack.processXMPPMessage(
70 XMPPSession.getThreadSession().recv(millis));
71 } catch(XMPPException e) {
72 throw new SessionException("Error waiting for message", e);
77 * Removes this session from the session cache.
79 public void cleanup() {
80 sessionCache.remove(thread);
84 * Searches for the cached session with the given thread.
85 * @param thread The session thread.
86 * @return The found session or null.
88 public static Session findCachedSession(String thread) {
89 return sessionCache.get(thread);
93 * Puts this session into session cache.
95 protected void cacheSession() {
96 sessionCache.put(thread, this);
100 * Sets the remote address
101 * @param nodeName The name of the remote node.
103 public void setRemoteNode(String nodeName) {
104 remoteNode = nodeName;
107 * @return The remote node
109 public String getRemoteNode() {
116 * @return thread as String.
118 public String getThread() {
124 * @param thread the value to set.
126 public void setThread(String thread) {
127 this.thread = thread;
132 * @return locale as String.
134 public String getLocale() {
136 return defaultLocale;
142 * @param locale the value to set.
144 public void setLocale(String locale) {
145 this.locale = locale;
150 * @return defaultLocale as String.
152 public String getDefaultLocale() {
153 return defaultLocale;
158 * @param defaultLocale the value to set.
160 public void setDefaultLocale(String defaultLocale) {
161 this.defaultLocale = defaultLocale;
167 * @return connectState as ConnectState.
169 public ConnectState getConnectState() {
175 * @param connectState the value to set.
177 public void setConnectState(ConnectState connectState) {
178 this.connectState = connectState;