1 /*------------------------------------------------------------------------------
2 Name: UpdateQos.java
3 Project: xmlBlaster.org
4 Copyright: xmlBlaster.org, see xmlBlaster-LICENSE file
5 ------------------------------------------------------------------------------*/
6 package org.xmlBlaster.client.qos;
7
8 import java.util.Map;
9 import java.util.Properties;
10
11 import org.xmlBlaster.util.Global;
12 import org.xmlBlaster.util.SessionName;
13 import org.xmlBlaster.util.Timestamp;
14 import org.xmlBlaster.util.XmlBlasterException;
15 import org.xmlBlaster.util.cluster.RouteInfo;
16 import org.xmlBlaster.util.def.Constants;
17 import org.xmlBlaster.util.def.MethodName;
18 import org.xmlBlaster.util.def.PriorityEnum;
19 import org.xmlBlaster.util.qos.ClientProperty;
20 import org.xmlBlaster.util.qos.MsgQosData;
21
22 /**
23 * QoS (quality of service) informations sent from server to client<br />
24 * via the update() method from the I_Callback interface.
25 * <p />
26 * If you are a Java client you may use this class to parse the QoS argument.
27 * <p />
28 * Example:
29 * <pre>
30 * <qos> <!-- UpdateQos -->
31 * <state id='OK'/>
32 * <sender>Tim</sender>
33 * <priority>5</priority>
34 * <subscribe id='__subId:1/>
35 * <rcvTimestamp nanos='1007764305862000002'> <!-- UTC time when message was created in xmlBlaster server with a publish() call, in nanoseconds since 1970 -->
36 * 2001-12-07 23:31:45.862000002 <!-- The nanos from above but human readable -->
37 * </rcvTimestamp>
38 * <expiration lifeTime='1200'/> <!-- The overall life time of the message [milliseconds] -->
39 * <queue index='0' of='1'/> <!-- If queued messages are flushed on login -->
40 * <redeliver>4</redeliver>
41 * <route>
42 * <node id='heron'/>
43 * </route>
44 * <clientProperty name='transactionId' type='int'>120001</clientProperty>
45 * <clientProperty name='transactionId' type='String' encoding='base64'>OKFKAL==</clientProperty>
46 * </qos>
47 * </pre>
48 * The receive timestamp can be delivered in human readable form as well
49 * by setting on server command line:
50 * <pre>
51 * -cb.receiveTimestampHumanReadable true
52 *
53 * <rcvTimestamp nanos='1015959656372000000'>
54 * 2002-03-12 20:00:56.372
55 * </rcvTimestamp>
56 * </pre>
57 * @see org.xmlBlaster.util.qos.MsgQosData
58 * @see org.xmlBlaster.util.qos.MsgQosSaxFactory
59 * @see <a href="http://www.xmlblaster.org/xmlBlaster/doc/requirements/interface.update.html">update interface</a>
60 */
61 public final class UpdateQos
62 {
63 private final Global glob;
64 private final MsgQosData msgQosData;
65
66 /**
67 * Default constructor for transient messages.
68 */
69 public UpdateQos(Global glob, MsgQosData msgQosData) {
70 this.glob = (glob==null) ? Global.instance() : glob;
71
72 this.msgQosData = msgQosData;
73 this.msgQosData.setMethod(MethodName.UPDATE);
74 }
75
76 /**
77 * Constructs the specialized quality of service object for a update() call.
78 */
79 public UpdateQos(Global glob, String xmlQos) throws XmlBlasterException {
80 this(glob, glob.getMsgQosFactory().readObject(xmlQos));
81 }
82
83 /**
84 * Convenience method to get the raw content as a string, the encoding is UTF-8 if not specified by clientProperty {@link Constants#CLIENTPROPERTY_CONTENT_CHARSET}
85 * @return never null
86 */
87 public String getContentStr(byte[] msgContent) throws XmlBlasterException {
88 if (getData() == null)
89 return Constants.toUtf8String(msgContent);
90 return getData().getContentStr(msgContent);
91 }
92
93 /**
94 * Convenience method to get the raw content as a string, the encoding is UTF-8 if not specified by clientProperty {@link Constants#CLIENTPROPERTY_CONTENT_CHARSET}
95 * @return never null
96 */
97 public String getContentStrNoEx(byte[] msgContent) {
98 if (getData() == null)
99 return Constants.toUtf8String(msgContent);
100 return getData().getContentStrNoEx(msgContent);
101 }
102
103 /**
104 * Get the QoS data object which i'm hiding
105 */
106 public MsgQosData getData() {
107 return this.msgQosData;
108 }
109
110 public Global getGlobal() {
111 return this.glob;
112 }
113
114 /**
115 * Access sender name.
116 * @return loginName and session of sender
117 */
118 public SessionName getSender() {
119 return this.msgQosData.getSender();
120 }
121
122 /**
123 * Message priority.
124 * @return priority 0-9
125 * @see org.xmlBlaster.util.def.Constants
126 */
127 public PriorityEnum getPriority() {
128 return this.msgQosData.getPriority();
129 }
130
131 /**
132 * Returns > 0 if the message probably is redelivered.
133 * @return == 0 The message is guaranteed to be delivered only once.
134 */
135 public int getRedeliver() {
136 return this.msgQosData.getRedeliver();
137 }
138
139 /**
140 * Access state of message.
141 * @return OK (Other values are not yet supported)
142 */
143 public String getState() {
144 return this.msgQosData.getState();
145 }
146
147 /**
148 * True if the message is OK
149 */
150 public boolean isOk() {
151 return this.msgQosData.isOk();
152 }
153
154 /**
155 * True if the message was erased by timer or by a
156 * client invoking erase().
157 */
158 public boolean isErased() {
159 return this.msgQosData.isErased();
160 }
161
162 /**
163 * True if a timeout on this message occurred.
164 * <p />
165 * Timeouts are spanned by the publisher and thrown by xmlBlaster
166 * on timeout to indicate for example
167 * STALE messages or any other user problem domain specific event.
168 */
169 public boolean isTimeout() {
170 return this.msgQosData.isTimeout();
171 }
172
173 /**
174 * True on cluster forward problems
175 */
176 public boolean isForwardError() {
177 return this.msgQosData.isForwardError();
178 }
179
180 /**
181 * Is this a volatile message?
182 */
183 public boolean isVolatile() {
184 return this.msgQosData.isVolatile();
185 }
186
187 /**
188 * Was this message subscribed?
189 */
190 public boolean isSubscribable() {
191 return this.msgQosData.isSubscribable();
192 }
193
194 /**
195 * Did i reveive the message in PtP mode?
196 */
197 public boolean isPtp() {
198 return this.msgQosData.isPtp();
199 }
200
201 /**
202 * Is this a persistent message?
203 */
204 public boolean isPersistent() {
205 return this.msgQosData.isPersistent();
206 }
207
208 /**
209 * Is this a readonly message?
210 */
211 public boolean isReadonly() {
212 return this.msgQosData.isReadonly();
213 }
214
215 /**
216 * If Pub/Sub style update: contains the subscribe ID which caused this update
217 * @return subscribeId or null if PtP message
218 */
219 public String getSubscriptionId() {
220 return this.msgQosData.getSubscriptionId();
221 }
222
223 /**
224 * If persistent messages where in queue, this is flushed on login.
225 * @return The number of queued messages
226 */
227 public long getQueueSize() {
228 return this.msgQosData.getQueueSize();
229 }
230
231 /**
232 * If persistent messages where in queue, this is flushed on login.
233 * @return The index of the message of the queue
234 */
235 public long getQueueIndex() {
236 return this.msgQosData.getQueueIndex();
237 }
238
239 /**
240 * The approximate receive timestamp (UTC time),
241 * when message was created - arrived at xmlBlaster server.<br />
242 * In nanoseconds elapsed since midnight, January 1, 1970 UTC
243 */
244 public Timestamp getRcvTimestamp() {
245 return this.msgQosData.getRcvTimestamp();
246 }
247
248 /*
249 * The local xmlBlaster node which delivered the message (does not need to be the master).
250 * @return The xmlBlaster cluster node which delivered the message
251 public String getLocalNodeId() {
252 return nodeId;
253 }
254 */
255
256 /**
257 * Human readable form of message receive time in xmlBlaster server,
258 * in SQL representation e.g.:<br />
259 * 2001-12-07 23:31:45.862000004
260 */
261 public String getRcvTime() {
262 return this.msgQosData.getRcvTimestamp().toString();
263 }
264
265 /**
266 * Approxiamte millis counted from now when message will be discarded
267 * by xmlBlaster.
268 * Calculated by xmlBlaster just before sending the update, so there
269 * will be an offset (the time sending the message to us).
270 * @return The time to live for this message or -1 (unlimited) if not known
271 */
272 public long getRemainingLifeStatic() {
273 return this.msgQosData.getRemainingLifeStatic();
274 }
275
276 /**
277 * @return never null, but may have length==0
278 */
279 public RouteInfo[] getRouteNodes() {
280 return this.msgQosData.getRouteNodes();
281 }
282
283 /**
284 * Access all client properties.
285 * @return a map The return is unordered and the map values are of type ClientProperty.
286 * @see org.xmlBlaster.util.qos.ClientProperty
287 */
288 public final Map getClientProperties() {
289 return this.msgQosData.getClientProperties();
290 }
291
292 /**
293 * Read back a property.
294 * @return The client property or null if not found
295 */
296 public ClientProperty getClientProperty(String key) {
297 return this.msgQosData.getClientProperty(key);
298 }
299
300 /**
301 * Access the String client property.
302 * @param name The property key
303 * @param defaultValue The value to return if the property is not known
304 */
305 public final String getClientProperty(String name, String defaultValue) {
306 return this.msgQosData.getClientProperty(name, defaultValue);
307 }
308
309 /**
310 * Access the integer client property.
311 * @param name The property key
312 * @param defaultValue The value to return if the property is not known
313 */
314 public final int getClientProperty(String name, int defaultValue) {
315 return this.msgQosData.getClientProperty(name, defaultValue);
316 }
317
318 /**
319 * Access the boolean client property.
320 * @param name The property key
321 * @param defaultValue The value to return if the property is not known
322 */
323 public final boolean getClientProperty(String name, boolean defaultValue) {
324 return this.msgQosData.getClientProperty(name, defaultValue);
325 }
326
327 /**
328 * Access the double client property.
329 * @param name The property key
330 * @param defaultValue The value to return if the property is not known
331 */
332 public final double getClientProperty(String name, double defaultValue) {
333 return this.msgQosData.getClientProperty(name, defaultValue);
334 }
335
336 /**
337 * Access the float client property.
338 * @param name The property key
339 * @param defaultValue The value to return if the property is not known
340 */
341 public final float getClientProperty(String name, float defaultValue) {
342 return this.msgQosData.getClientProperty(name, defaultValue);
343 }
344
345 /**
346 * Access the byte client property.
347 * @param name The property key
348 * @param defaultValue The value to return if the property is not known
349 */
350 public final byte getClientProperty(String name, byte defaultValue) {
351 return this.msgQosData.getClientProperty(name, defaultValue);
352 }
353
354 /**
355 * Access the byte[] client property.
356 * @param name The property key
357 * @param defaultValue The value to return if the property is not known
358 */
359 public final byte[] getClientProperty(String name, byte[] defaultValue) {
360 return this.msgQosData.getClientProperty(name, defaultValue);
361 }
362
363 /**
364 * Access the long client property.
365 * @param name The property key
366 * @param defaultValue The value to return if the property is not known
367 */
368 public final long getClientProperty(String name, long defaultValue) {
369 return this.msgQosData.getClientProperty(name, defaultValue);
370 }
371
372 /**
373 * Access the short client property.
374 * @param name The property key
375 * @param defaultValue The value to return if the property is not known
376 */
377 public final short getClientProperty(String name, short defaultValue) {
378 return this.msgQosData.getClientProperty(name, defaultValue);
379 }
380
381 /**
382 * Dump state of this object into a XML ASCII string.
383 * <br>
384 * @return internal state of the RequestBroker as a XML ASCII string
385 */
386 public String toXml() {
387 return toXml((String)null, (Properties)null);
388 }
389
390 /**
391 * Dump state of this object into a XML ASCII string.
392 * <br>
393 * @param extraOffset indenting of tags for nice output
394 * @return internal state of the RequestBroker as a XML ASCII string
395 */
396 public String toXml(String extraOffset) {
397 return this.msgQosData.toXml(extraOffset);
398 }
399
400 /**
401 * Overwrite qosData.toXml
402 * @param extraOffset
403 * @param forceReadable
404 * @return
405 */
406 public String toXml(String extraOffset, Properties props) {
407 return this.msgQosData.toXml(extraOffset, props);
408 }
409
410 public String toString() {
411 return toXml(null);
412 }
413 }
syntax highlighted by Code2HTML, v. 0.9.1