]> git.evergreen-ils.org Git - OpenSRF.git/blob - src/java/org/opensrf/Message.java
added JSON reading ability, including serializable objects
[OpenSRF.git] / src / java / org / opensrf / Message.java
1 package org.opensrf;
2 import org.opensrf.util.*;
3
4
5 public class Message implements OSRFSerializable {
6
7     /** Message types */
8     public enum Type {
9         REQUEST,
10         STATUS,
11         RESULT,
12         CONNECT,
13         DISCONNECT,
14     };
15
16     /** Message ID.  This number is used to relate requests to responses */
17     private int id;
18     /** Type of message. */
19     private Type type;
20     /** message payload */
21     private Object payload;
22
23     /** Go ahead and register the Message object */
24     private static OSRFRegistry registry = 
25         OSRFRegistry.registerObject(
26             "osrfMessage", 
27             OSRFRegistry.WireProtocol.HASH, 
28             new String[] {"threadTrace", "type", "payload"});
29
30     /**
31      * @param id This message's ID
32      * @param type The type of message
33      */
34     public Message(int id, Type type) {
35         setId(id);
36         setType(type);
37     }
38     public Message(int id, Type type, Object payload) {
39         this(id, type);
40         setPayload(payload);
41     }
42
43
44     public int getId() {
45         return id;
46     }   
47     public Type getType() {
48         return type;
49     }
50     public Object getPayload() {
51         return payload;
52     }
53     public void setId(int id) {
54         this.id = id;
55     }
56     public void setType(Type type) {
57         this.type = type;
58     }
59     public void setPayload(Object p) {
60         payload = p;
61     }
62
63     /**
64      * Implements the generic get() API required by OSRFSerializable
65      */
66     public Object get(String field) {
67         if("threadTrace".equals(field))
68             return getId();
69         if("type".equals(field))
70             return getType().toString();
71         if("payload".equals(field))
72             return getPayload();
73         return null;
74     }
75
76     public OSRFRegistry getRegistry() {
77         return registry;
78     }
79 }
80
81