]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/java/org/opensrf/Session.java
15fd352d5fc783eca18471fbf05e529b80187ce4
[OpenSRF.git] / src / java / org / opensrf / Session.java
1 package org.opensrf;
2 import org.opensrf.util.JSONWriter;
3 import org.opensrf.net.xmpp.*;
4 import java.util.Map;
5 import java.util.HashMap;
6 import java.util.Arrays;
7
8 public abstract class Session {
9
10     /** Represents the different connection states for a session */
11     public enum ConnectState {
12         DISCONNECTED,
13         CONNECTING,
14         CONNECTED
15     };
16
17     /** local cache of existing sessions */
18     private static Map<String, Session> 
19         sessionCache = new HashMap<String, Session>();
20
21     /** the current connection state */
22     private ConnectState connectState;
23
24     /** The address of the remote party we are communicating with */
25     private String remoteNode;
26
27     /** Session locale */
28     protected String locale;
29     /** Default session locale */
30     protected static String defaultLocale = "en-US";
31
32     /** 
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.
36      */
37     private String thread;
38
39     public Session() {
40         connectState = ConnectState.DISCONNECTED;
41     }
42     
43     /**
44      * Sends a Message to our remoteNode.
45      */
46     public void send(Message omsg) throws SessionException {
47
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());
53
54         try {
55             XMPPSession.getThreadSession().send(xmsg);
56         } catch(XMPPException e) {
57             connectState = ConnectState.DISCONNECTED;
58             throw new SessionException("Error sending message to " + remoteNode, e);
59         }
60     }
61
62     /**
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
66      */
67     public static void waitForMessage(long millis) throws SessionException, MethodException {
68         try {
69             Stack.processXMPPMessage(
70                 XMPPSession.getThreadSession().recv(millis));
71         } catch(XMPPException e) {
72             throw new SessionException("Error waiting for message", e);
73         }
74     }
75
76     /**
77      * Removes this session from the session cache.
78      */
79     public void cleanup() {
80         sessionCache.remove(thread);
81     }
82
83     /**
84      * Searches for the cached session with the given thread.
85      * @param thread The session thread.
86      * @return The found session or null.
87      */
88     public static Session findCachedSession(String thread) {
89         return sessionCache.get(thread);
90     }
91
92     /**
93      * Puts this session into session cache.
94      */
95     protected void cacheSession() {
96         sessionCache.put(thread, this);
97     }
98
99     /**
100      * Sets the remote address
101      * @param nodeName The name of the remote node.
102      */
103     public void setRemoteNode(String nodeName) {
104         remoteNode = nodeName;
105     }
106     /**
107      * @return The remote node
108      */
109     public String getRemoteNode() {
110         return remoteNode;
111     }
112
113
114     /**
115      * Get thread.
116      * @return thread as String.
117      */
118     public String getThread() {
119         return thread;
120     }
121     
122     /**
123      * Set thread.
124      * @param thread the value to set.
125      */
126     public void setThread(String thread) {
127         this.thread = thread;
128     }
129
130     /**
131      * Get locale.
132      * @return locale as String.
133      */
134     public String getLocale() {
135         if(locale == null)
136             return defaultLocale;
137         return locale;
138     }
139     
140     /**
141      * Set locale.
142      * @param locale the value to set.
143      */
144     public void setLocale(String locale) {
145         this.locale = locale;
146     }
147
148     /**
149      * Get defaultLocale.
150      * @return defaultLocale as String.
151      */
152     public String getDefaultLocale() {
153         return defaultLocale;
154     }
155     
156     /**
157      * Set defaultLocale.
158      * @param defaultLocale the value to set.
159      */
160     public void setDefaultLocale(String defaultLocale) {
161         this.defaultLocale = defaultLocale;
162     }
163
164     
165     /**
166      * Get connectState.
167      * @return connectState as ConnectState.
168      */
169     public ConnectState getConnectState() {
170         return connectState;
171     }
172     
173     /**
174      * Set connectState.
175      * @param connectState the value to set.
176      */
177     public void setConnectState(ConnectState connectState) {
178         this.connectState = connectState;
179     }
180 }