1 /*------------------------------------------------------------------------------
2 Name: MsgQueueSubscribeEntry.java
3 Project: xmlBlaster.org
4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
5 ------------------------------------------------------------------------------*/
6 package org.xmlBlaster.client.queuemsg;
7
8 import org.xmlBlaster.util.Global;
9 import org.xmlBlaster.util.MsgUnit;
10 import org.xmlBlaster.util.SessionName;
11 import org.xmlBlaster.util.Timestamp;
12 import org.xmlBlaster.util.XmlBlasterException;
13 import org.xmlBlaster.util.def.MethodName;
14 import org.xmlBlaster.util.def.PriorityEnum;
15 import org.xmlBlaster.util.key.QueryKeyData;
16 import org.xmlBlaster.util.qos.QueryQosData;
17 import org.xmlBlaster.util.queue.StorageId;
18 import org.xmlBlaster.util.queuemsg.MsgQueueEntry;
19
20
21 /**
22 * Wraps an subscribe() message into an entry for a sorted queue.
23 * @author xmlBlaster@marcelruff.info
24 */
25 public final class MsgQueueSubscribeEntry extends MsgQueueEntry
26 {
27 private static final long serialVersionUID = 1L;
28 private final QueryQosData subscribeQosData;
29 private final QueryKeyData subscribeKeyData;
30 private SessionName receiver;
31 private final long immutableSizeInBytes;
32
33 /**
34 * Use this constructor if a new message object is fed by method subscribe().
35 * <p />
36 */
37 public MsgQueueSubscribeEntry(Global glob, StorageId storageId,
38 QueryKeyData subscribeKeyData, QueryQosData subscribeQosData)
39 throws XmlBlasterException {
40 super(glob, MethodName.SUBSCRIBE, PriorityEnum.NORM_PRIORITY, storageId,
41 (subscribeQosData == null) ? false : subscribeQosData.isPersistent());
42 this.subscribeQosData = (subscribeQosData == null) ? new QueryQosData(glob, MethodName.SUBSCRIBE) : subscribeQosData;
43 this.subscribeKeyData = subscribeKeyData;
44 this.immutableSizeInBytes = 567 + this.subscribeQosData.size() + this.subscribeKeyData.size();
45 }
46
47 /**
48 * For persistence recovery
49 */
50 public MsgQueueSubscribeEntry(Global glob, PriorityEnum priority, StorageId storageId,
51 Timestamp timestamp, long sizeInBytes,
52 QueryKeyData subscribeKeyData, QueryQosData subscribeQosData) {
53 super(glob, MethodName.SUBSCRIBE.toString(), priority,
54 timestamp, storageId, subscribeQosData.isPersistent());
55 this.subscribeQosData = subscribeQosData;
56 this.subscribeKeyData = subscribeKeyData;
57 this.immutableSizeInBytes = sizeInBytes;
58 }
59
60 /**
61 * @see MsgQueueEntry#isExpired
62 */
63 public boolean isExpired() {
64 return false;
65 }
66
67 /**
68 * @see MsgQueueEntry#isDestroyed
69 */
70 public boolean isDestroyed() {
71 return false;
72 }
73
74 public QueryQosData getSubscribeQosData() {
75 return this.subscribeQosData;
76 }
77
78 public QueryKeyData getSubscribeKeyData() {
79 return this.subscribeKeyData;
80 }
81
82 public MsgUnit getMsgUnit() {
83 return new MsgUnit(getSubscribeKeyData(), null, getSubscribeQosData());
84 }
85
86 /**
87 * Access the unique login name of the subscriber.
88 * @return loginName of the source
89 * @see MsgQueueEntry#getSender()
90 */
91 public SessionName getSender() {
92 return (this.subscribeQosData==null) ? null : this.subscribeQosData.getSender();
93 }
94
95 /**
96 * @return The name of the receiver (data sink) or null
97 * @see MsgQueueEntry#getReceiver()
98 */
99 public void setReceiver(SessionName receiver) {
100 this.receiver = receiver;
101 }
102
103 /**
104 * @return The name of the receiver (data sink) or null
105 * @see MsgQueueEntry#getReceiver()
106 */
107 public SessionName getReceiver() {
108 return this.receiver;
109 }
110
111 /**
112 * @see MsgQueueEntry#getKeyOid()
113 */
114 public String getKeyOid() {
115 return (this.subscribeKeyData==null) ? null: this.subscribeKeyData.getOid();
116 }
117
118 /**
119 * return null
120 */
121 public Timestamp getRcvTimestamp() {
122 return null;
123 }
124
125 /**
126 * The embeddded object for this implementing class is an Object[2] where
127 * Object[0] = qos.toXml()
128 * Object[1] = key.toXml()
129 */
130 public Object getEmbeddedObject() {
131 Object[] obj = { this.subscribeQosData.toXml(), this.subscribeKeyData.toXml() };
132 return obj;
133 }
134
135 public long getSizeInBytes() {
136 return this.immutableSizeInBytes;
137 }
138
139 /**
140 * @return true
141 */
142 public boolean isInternal() {
143 return true;
144 }
145
146 public final void embeddedObjectToXml(java.io.OutputStream out, java.util.Properties props) throws java.io.IOException {
147 out.write(this.subscribeKeyData.toXml().getBytes());
148 out.write(this.subscribeQosData.toXml((String)null, props).getBytes());
149 }
150 /**
151 * Returns a shallow clone
152 */
153 public Object clone() {
154 MsgQueueSubscribeEntry entry = null;
155 entry = (MsgQueueSubscribeEntry)super.clone();
156 return entry;
157 }
158 }
syntax highlighted by Code2HTML, v. 0.9.1