001: /*
002: * MessageQueueClient: The message queue client library
003: * Copyright (C) 2006 Rift IT Contracting
004: *
005: * This library is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU Lesser General Public
007: * License as published by the Free Software Foundation; either
008: * version 2.1 of the License, or (at your option) any later version.
009: *
010: * This library is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
013: * Lesser General Public License for more details.
014: *
015: * You should have received a copy of the GNU Lesser General Public
016: * License along with this library; if not, write to the Free Software
017: * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
018: *
019: * MessageImpl.java
020: */
021:
022: // package path
023: package com.rift.coad.daemon.messageservice.message;
024:
025: // java imports
026: import java.util.ArrayList;
027: import java.util.Date;
028: import java.util.Enumeration;
029: import java.util.List;
030: import java.util.Properties;
031:
032: // coadunation imports
033: import com.rift.coad.daemon.messageservice.InvalidMessageType;
034: import com.rift.coad.daemon.messageservice.InvalidProperty;
035: import com.rift.coad.daemon.messageservice.Message;
036: import com.rift.coad.daemon.messageservice.MessageError;
037: import com.rift.coad.daemon.messageservice.MessageServiceException;
038: import com.rift.coad.lib.common.ObjectSerializer;
039:
040: /**
041: * The implementation of the message object.
042: *
043: * @author Brett Chaldecott
044: */
045: public abstract class MessageImpl implements Message, Cloneable {
046:
047: // private member variables
048: private String messageId = null;
049: private Date created = null;
050: private int retries = 0;
051: private Date processedDate = null;
052: private Date nextProcessDate = null;
053: private String sessionId = null;
054: private String user = null;
055: private List principals = null;
056: private String from = null;
057: private int messageType = 0;
058: private String target = null;
059: private String[] services = null;
060: private String replyTo = null;
061: private String targetNamedQueue = null;
062: private String replyNamedQueue = null;
063: private boolean reply = false;
064: private int priority = 0;
065: private String correlationId = null;
066: private Properties properties = new Properties();
067: private boolean acknowledged = false;
068: private int status = 0;
069: private List errors = new ArrayList();
070:
071: /**
072: * Creates a new instance of MessageImpl.
073: *
074: * @param messageId The unique id of this message.
075: * @param user The name of the user.
076: * @param sessionId The id of the session.
077: * @param principals The list of principals.
078: * @param status The status of the message.
079: */
080: public MessageImpl(String messageId, String user, String sessionId,
081: List principals, int status) {
082: this .messageId = messageId;
083: this .created = new Date();
084: this .processedDate = new Date();
085: this .user = user;
086: this .sessionId = sessionId;
087: this .principals = principals;
088: this .status = status;
089: }
090:
091: /**
092: * Creates a new instance of MessageImpl.
093: *
094: * @param messageId The unique id of this message.
095: * @param create The date the message was created.
096: * @param retries The number of retries of this message.
097: * @param processedDate The processed date.
098: * @param user The user responsible for this message.
099: * @param principals The list of principals.
100: * @param from The from url of the message.
101: * @param messageType The message type.
102: * @param status The status of the message.
103: */
104: public MessageImpl(String messageId, Date created, int retries,
105: Date processedDate, String user, String sessionId,
106: List principals, String from, int messageType, int status) {
107: this .messageId = messageId;
108: this .created = created;
109: this .retries = retries;
110: this .processedDate = processedDate;
111: this .user = user;
112: this .sessionId = sessionId;
113: this .principals = principals;
114: this .from = from;
115: this .messageType = messageType;
116: this .status = status;
117: }
118:
119: /**
120: * This method returns the id of this message object.
121: *
122: * @return The id of the message object.
123: */
124: public String getMessageId() {
125: return messageId;
126: }
127:
128: /**
129: * This method sets the new id for this message
130: *
131: * @param messageId The new id for this message.
132: */
133: public void setMessageId(String messageId) {
134: this .messageId = messageId;
135: }
136:
137: /**
138: * This method returns the date this message was created.
139: *
140: * @return The date the message was created
141: */
142: public Date getCreated() {
143: return created;
144: }
145:
146: /**
147: * This is the number of retries this message has had.
148: *
149: * @return The number of retries performed by this message.
150: */
151: public int getRetries() {
152: return retries;
153: }
154:
155: /**
156: * This method increments the retry count on this object.
157: */
158: public void incrementRetries() {
159: retries++;
160: }
161:
162: /**
163: * This method will return the last processed date.
164: */
165: public Date getProcessedDate() {
166: return processedDate;
167: }
168:
169: /**
170: * This method sets the processed date.
171: *
172: * @param processedDate The processed date of the message.
173: */
174: public void setProcessedDate(Date processedDate) {
175: this .processedDate = processedDate;
176: }
177:
178: /**
179: * This method returns the next process date of the object.
180: */
181: public Date getNextProcessDate() {
182: return this .nextProcessDate;
183: }
184:
185: /**
186: * This method returns the next process date of the object.
187: *
188: * @param nextProcessDate The next processed date of the message.
189: */
190: public void setNextProcessDate(Date nextProcessDate) {
191: this .nextProcessDate = nextProcessDate;
192: }
193:
194: /**
195: * This method returns the creator of this message.
196: */
197: public String getMessageCreater() {
198: return user;
199: }
200:
201: /**
202: * This method returns the ID of the session that created this message.
203: *
204: * @return The string containing the id of the session that created this
205: * message.
206: */
207: public String getSessionId() {
208: return sessionId;
209: }
210:
211: /**
212: * This method returns the list of user principals assigned to this message.
213: *
214: * @return The list of user principals assigned to this message.
215: */
216: public List getMessagePrincipals() {
217: return principals;
218: }
219:
220: /**
221: * This method sets the list of user principals assigned to this message.
222: *
223: * @param principals The new list of principals
224: */
225: public void setMessagePrincipals(List principals) {
226: this .principals = principals;
227: }
228:
229: /**
230: * This method returns the message type for this object.
231: *
232: * @return The int containing the message type.
233: */
234: public int getMessageType() {
235: return messageType;
236: }
237:
238: /**
239: * This method sets the type of message that this object represents.
240: *
241: * @param messageType The type of object being wrapped.
242: */
243: public void setMessageType(int messageType) {
244: this .messageType = messageType;
245: }
246:
247: /**
248: * This method returns the target of this message.
249: *
250: * @return The string containing the target information.
251: * @exception MessageServiceException
252: * @exception InvalidMessageType
253: */
254: public String getTarget() throws MessageServiceException,
255: InvalidMessageType {
256: return target;
257: }
258:
259: /**
260: * This method returns the target of this message.
261: *
262: * @param target The string containing the Target URL.
263: * @exception MessageServiceException
264: * @exception InvalidMessageType
265: */
266: public void setTarget(String target)
267: throws MessageServiceException, InvalidMessageType {
268: this .target = target;
269: }
270:
271: /**
272: * This method returns the list of services this message targeted at.
273: *
274: * @return This method returns the list of services.
275: * @exception MessageServiceException
276: * @exception InvalidMessageType
277: */
278: public String[] getServices() throws MessageServiceException,
279: InvalidMessageType {
280: return services;
281: }
282:
283: /**
284: * This method returns the list of services this message targeted at.
285: *
286: * @param services The list of services.
287: * @exception MessageServiceException
288: * @exception InvalidMessageType
289: */
290: public void setServices(String[] services)
291: throws MessageServiceException, InvalidMessageType {
292: this .services = services;
293: }
294:
295: /**
296: * This method returns the from address of the message.
297: *
298: * @return The from address of the message.
299: * @exception MessageServiceException
300: */
301: public String getFrom() throws MessageServiceException {
302: return from;
303: }
304:
305: /**
306: * This method returns the from address of the message.
307: *
308: * @return The from address of the message.
309: * @exception MessageServiceException
310: */
311: public void setFrom(String from) throws MessageServiceException {
312: this .from = from;
313: }
314:
315: /**
316: * This method returns the reply to URL, it can be different from the from
317: * URL.
318: *
319: * @return The string containing the from URL.
320: * @exception MessageServiceException
321: */
322: public String getReplyTo() throws MessageServiceException {
323: return replyTo;
324: }
325:
326: /**
327: * This method returns the reply to URL, it can be different from the from
328: * URL.
329: *
330: * @return The string containing the from URL.
331: * @exception MessageServiceException
332: */
333: public void setReplyTo(String replyTo)
334: throws MessageServiceException {
335: this .replyTo = replyTo;
336: }
337:
338: /**
339: * This will only be set if the reply service is not a daemon, but an
340: * external process.
341: *
342: * @return The string containing the Queue name.
343: * @exception MessageServiceException
344: */
345: public String getTargetNamedQueue() throws MessageServiceException {
346: return targetNamedQueue;
347: }
348:
349: /**
350: * This will only be set if the reply service is not a daemon, but an
351: * external process.
352: *
353: * @param name The string containing the Queue name.
354: * @exception MessageServiceException
355: */
356: public void setTargetNamedQueue(String name)
357: throws MessageServiceException {
358: this .targetNamedQueue = name;
359: }
360:
361: /**
362: * This will only be set if the reply service is not a daemon, but an
363: * external process.
364: *
365: * @return The string containing the Queue name.
366: * @exception MessageServiceException
367: */
368: public String getReplyNamedQueue() throws MessageServiceException {
369: return replyNamedQueue;
370: }
371:
372: /**
373: * This will only be set if the reply service is not a daemon, but an
374: * external process.
375: *
376: * @param name The string containing the Queue name.
377: * @exception MessageServiceException
378: */
379: public void setReplyNamedQueue(String name)
380: throws MessageServiceException {
381: this .replyNamedQueue = name;
382: }
383:
384: /**
385: * This method returns the value of the reply flag. TRUE if it should reply
386: * FALSE if it should not.
387: *
388: * @return TRUE if this message must reply, FALSE if not.
389: * @exception MessageServiceException
390: */
391: public boolean getReply() throws MessageServiceException {
392: return reply;
393: }
394:
395: /**
396: * This method sets the reply flag.
397: *
398: * @param value TRUE if a reply is required, FALSE if not.
399: * @exception MessageServiceException
400: */
401: public void setReply(boolean value) throws MessageServiceException {
402: this .reply = value;
403: }
404:
405: /**
406: * This message returns the priority of this message.
407: *
408: * @return The int indicating the priority of this message.
409: * @exception MessageServiceException
410: */
411: public int getPriority() {
412: return priority;
413: }
414:
415: /**
416: * This method sets the priority of the message.
417: *
418: * @param priority The priority of the message.
419: * @exception MessageServiceException
420: */
421: public void setPriority(int priority)
422: throws MessageServiceException {
423: this .priority = priority;
424: }
425:
426: /**
427: * This method sets the correlation id for this message. It is the external
428: * identifier for this message.
429: *
430: * @param id The id that will be used as the correlation id.
431: * @exception MessageServiceException
432: */
433: public void setCorrelationId(String id)
434: throws MessageServiceException {
435: this .correlationId = id;
436: }
437:
438: /**
439: * The external correlation id for this message.
440: *
441: * @return The string containing the correllation ID.
442: * @exception MessageServiceException
443: */
444: public String getCorrelationId() throws MessageServiceException {
445: return correlationId;
446: }
447:
448: /**
449: * This clears the body of the message.
450: *
451: * @exception MessageServiceException
452: */
453: public abstract void clearBody() throws MessageServiceException;
454:
455: /**
456: * This method clears the properties assigned to this message.
457: *
458: * @exception MessageServiceException
459: */
460: public void clearProperties() throws MessageServiceException {
461: properties.clear();
462: }
463:
464: /**
465: * This method returns true if the property is found.
466: *
467: * @return TRUE if the property is found, FALSE if not.
468: * @param name The name of the property.
469: * @exception MessageServiceException
470: */
471: public boolean containsProperty(String name)
472: throws MessageServiceException {
473: return properties.containsKey(name);
474: }
475:
476: /**
477: * This method returns the boolean property value for the requested name.
478: *
479: * @return The value of the boolean property.
480: * @param name The name of the property.
481: * @exception MessageServiceException
482: * @exception InvalidProperty
483: */
484: public boolean getBooleanProperty(String name)
485: throws MessageServiceException, InvalidProperty {
486: if (!properties.containsKey(name)) {
487: throw new InvalidProperty("The property [" + name
488: + "] could not" + "be found.");
489: }
490: Boolean bool = (Boolean) properties.get(name);
491: return bool.booleanValue();
492: }
493:
494: /**
495: * This method returns the byte property value for the requested name.
496: *
497: * @return The value of the byte property.
498: * @param name The name of the property.
499: * @exception MessageServiceException
500: * @exception InvalidProperty
501: */
502: public byte getByteProperty(String name)
503: throws MessageServiceException, InvalidProperty {
504: if (!properties.containsKey(name)) {
505: throw new InvalidProperty("The property [" + name
506: + "] could not" + "be found.");
507: }
508: Byte byteValue = (Byte) properties.get(name);
509: return byteValue.byteValue();
510: }
511:
512: /**
513: * This method returns the double property value for the requested name.
514: *
515: * @return The value of the byte property.
516: * @param name The name of the property.
517: * @exception MessageServiceException
518: * @exception InvalidProperty
519: */
520: public double getDoubleProperty(String name)
521: throws MessageServiceException, InvalidProperty {
522: if (!properties.containsKey(name)) {
523: throw new InvalidProperty("The property [" + name
524: + "] could not" + "be found.");
525: }
526: Double doubleValue = (Double) properties.get(name);
527: return doubleValue.doubleValue();
528: }
529:
530: /**
531: * This method returns the float property value for the requested name.
532: *
533: * @return The value of the float property.
534: * @param name The name of the property.
535: * @exception MessageServiceException
536: * @exception InvalidProperty
537: */
538: public float getFloatProperty(String name)
539: throws MessageServiceException, InvalidProperty {
540: if (!properties.containsKey(name)) {
541: throw new InvalidProperty("The property [" + name
542: + "] could not" + "be found.");
543: }
544: Float floatValue = (Float) properties.get(name);
545: return floatValue.floatValue();
546: }
547:
548: /**
549: * This method returns the int property value for the requested name.
550: *
551: * @return The value of the int property.
552: * @param name The name of the property.
553: * @exception MessageServiceException
554: * @exception InvalidProperty
555: */
556: public int getIntProperty(String name)
557: throws MessageServiceException, InvalidProperty {
558: if (!properties.containsKey(name)) {
559: throw new InvalidProperty("The property [" + name
560: + "] could not" + "be found.");
561: }
562: Integer intValue = (Integer) properties.get(name);
563: return intValue.intValue();
564: }
565:
566: /**
567: * This method returns the long property value for the requested name.
568: *
569: * @return The value of the long property.
570: * @param name The name of the property.
571: * @exception MessageServiceException
572: * @exception InvalidProperty
573: */
574: public long getLongProperty(String name)
575: throws MessageServiceException, InvalidProperty {
576: if (!properties.containsKey(name)) {
577: throw new InvalidProperty("The property [" + name
578: + "] could not" + "be found.");
579: }
580: Long longValue = (Long) properties.get(name);
581: return longValue.longValue();
582: }
583:
584: /**
585: * This method returns the object property value for the requested name.
586: *
587: * @return The value of the object property.
588: * @param name The name of the property.
589: * @exception MessageServiceException
590: * @exception InvalidProperty
591: */
592: public Object getObjectProperty(String name)
593: throws MessageServiceException, InvalidProperty {
594: if (!properties.containsKey(name)) {
595: throw new InvalidProperty("The property [" + name
596: + "] could not" + "be found.");
597: }
598: try {
599: return ObjectSerializer.deserialize((byte[]) properties
600: .get(name));
601: } catch (Exception ex) {
602: throw new MessageServiceException(
603: "Failed to retrieve the object [" + name
604: + "] value :" + ex.getMessage(), ex);
605: }
606: }
607:
608: /**
609: * This method returns the string property value for the requested name.
610: *
611: * @return The value of the string property.
612: * @param name The name of the property.
613: * @exception MessageServiceException
614: * @exception InvalidProperty
615: */
616: public String getStringProperty(String name)
617: throws MessageServiceException, InvalidProperty {
618: if (!properties.containsKey(name)) {
619: throw new InvalidProperty("The property [" + name
620: + "] could not" + "be found.");
621: }
622: return (String) properties.get(name);
623: }
624:
625: /**
626: * This method returns the value of the property.
627: *
628: * @return The value of the property.
629: * @param name The name of the property.
630: * @exception MessageServiceException
631: * @exception InvalidProperty
632: */
633: public Object getPropertyValue(String name)
634: throws MessageServiceException, InvalidProperty {
635: if (!properties.containsKey(name)) {
636: throw new InvalidProperty("The property [" + name
637: + "] could not" + "be found.");
638: }
639: return properties.get(name);
640: }
641:
642: /**
643: * This method returns the string property value for the requested name.
644: *
645: * @return The list of property names
646: * @exception MessageServiceException
647: */
648: public Enumeration getPropertyNames()
649: throws MessageServiceException {
650: return properties.propertyNames();
651: }
652:
653: /**
654: * This method returns true if the specified property exits.
655: *
656: * @return TRUE if the property exists, FALSE if not.
657: * @param name The name of the property to look for.
658: * @exception MessageServiceException
659: */
660: public boolean propertyExists(String name)
661: throws MessageServiceException {
662: return properties.containsKey(name);
663: }
664:
665: /**
666: * This method sets the boolean property value for the name.
667: *
668: * @param name The name of the property.
669: * @param value The value of the boolean property.
670: * @exception MessageServiceException
671: */
672: public void setBooleanProperty(String name, boolean value)
673: throws MessageServiceException {
674: properties.put(name, new Boolean(value));
675: }
676:
677: /**
678: * This method sets the byte property value for the name.
679: *
680: * @param name The name of the property.
681: * @param value The value of the byte property.
682: * @exception MessageServiceException
683: */
684: public void setByteProperty(String name, byte value)
685: throws MessageServiceException {
686: properties.put(name, new Byte(value));
687: }
688:
689: /**
690: * This method sets the double property value for the name.
691: *
692: * @param name The name of the property.
693: * @param value The value of the double property.
694: * @exception MessageServiceException
695: */
696: public void setDoubleProperty(String name, double value)
697: throws MessageServiceException {
698: properties.put(name, new Double(value));
699: }
700:
701: /**
702: * This method sets the float property value for the name.
703: *
704: * @param name The name of the property.
705: * @param value The value of the float property.
706: * @exception MessageServiceException
707: */
708: public void setFloatProperty(String name, float value)
709: throws MessageServiceException {
710: properties.put(name, new Float(value));
711: }
712:
713: /**
714: * This method set the int property value for the name.
715: *
716: * @param name The name of the property.
717: * @param value The value of the int property.
718: * @exception MessageServiceException
719: */
720: public void setIntProperty(String name, int value)
721: throws MessageServiceException {
722: properties.put(name, new Integer(value));
723: }
724:
725: /**
726: * This method sets the long property value for the name.
727: *
728: * @param name The name of the property.
729: * @return value The new long value.
730: * @exception MessageServiceException
731: */
732: public void setLongProperty(String name, long value)
733: throws MessageServiceException {
734: properties.put(name, new Long(value));
735: }
736:
737: /**
738: * This method returns the object property value for the name.
739: *
740: * @param name The name of the property.
741: * @param value The new object value to set.
742: * @exception MessageServiceException
743: * @exception InvalidProperty
744: */
745: public void setObjectProperty(String name, Object value)
746: throws MessageServiceException {
747: try {
748: properties.put(name, ObjectSerializer.serialize(value));
749: } catch (Exception ex) {
750: throw new MessageServiceException(
751: "Failed to store the object value :"
752: + ex.getMessage(), ex);
753: }
754: }
755:
756: /**
757: * This method sets the string property value for the name.
758: *
759: * @param name The name of the property.
760: * @return value The new string value to set.
761: * @exception MessageServiceException
762: */
763: public void setStringProperty(String name, String value)
764: throws MessageServiceException {
765: properties.put(name, value);
766: }
767:
768: /**
769: * This method sets the value of the property.
770: *
771: * @param name The name of the property.
772: * @param value The property value.
773: * @exception MessageServiceException
774: * @exception InvalidProperty
775: */
776: public void setPropertyValue(String name, Object value)
777: throws MessageServiceException, InvalidProperty {
778: properties.put(name, value);
779: }
780:
781: /**
782: * This method acknowledges that this message has been successfully
783: * processed by a target.
784: *
785: * @exception MessageServiceException
786: */
787: public void acknowledge() throws MessageServiceException {
788: acknowledged = true;
789: }
790:
791: /**
792: * This method returns the value of the acknowledged flag for this message.
793: *
794: * @return TRUE if acknowleded, FALSE if not.
795: * @exception MessageServiceException
796: */
797: public boolean isAcknowledged() throws MessageServiceException {
798: return acknowledged;
799: }
800:
801: /**
802: * This method returns the current state of this message.
803: *
804: * @return The current state of this message.
805: * @exception MessageServiceException
806: */
807: public int getState() throws MessageServiceException {
808: return status;
809: }
810:
811: /**
812: * This method sets the current state of this message.
813: *
814: * @param status The new status of this message.
815: * @exception MessageServiceException
816: */
817: public void setState(int status) throws MessageServiceException {
818: this .status = status;
819: }
820:
821: /**
822: * This method returns list of errors
823: */
824: public List getErrors() throws MessageServiceException {
825: return errors;
826: }
827:
828: /**
829: * This method adds an error to the list of errors for this message.
830: *
831: * @param level The level of the error.
832: * @param msg The message associated with the error.
833: */
834: public void addError(int level, String msg)
835: throws MessageServiceException {
836: errors.add(new MessageError(new Date(), level, msg));
837: }
838:
839: /**
840: * This method adds the error to the list of errors.
841: *
842: * @param error The error to add to the list.
843: */
844: public void addError(MessageError error) {
845: errors.add(error);
846: }
847:
848: /**
849: * This method returns a clone of this object.
850: *
851: * @return A cloned instance of this object.
852: * @exception CloneNotSupportedException
853: */
854: public Object clone() throws CloneNotSupportedException {
855: return super.clone();
856: }
857: }
|