KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > rift > coad > daemon > messageservice > message > MessageImpl


1 /*
2  * MessageQueueClient: The message queue client library
3  * Copyright (C) 2006 Rift IT Contracting
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  * MessageImpl.java
20  */

21
22 // package path
23
package com.rift.coad.daemon.messageservice.message;
24
25 // java imports
26
import java.util.ArrayList JavaDoc;
27 import java.util.Date JavaDoc;
28 import java.util.Enumeration JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.Properties JavaDoc;
31
32 // coadunation imports
33
import com.rift.coad.daemon.messageservice.InvalidMessageType;
34 import com.rift.coad.daemon.messageservice.InvalidProperty;
35 import com.rift.coad.daemon.messageservice.Message;
36 import com.rift.coad.daemon.messageservice.MessageError;
37 import com.rift.coad.daemon.messageservice.MessageServiceException;
38 import com.rift.coad.lib.common.ObjectSerializer;
39
40 /**
41  * The implementation of the message object.
42  *
43  * @author Brett Chaldecott
44  */

45 public abstract class MessageImpl implements Message, Cloneable JavaDoc {
46     
47     // private member variables
48
private String JavaDoc messageId = null;
49     private Date JavaDoc created = null;
50     private int retries = 0;
51     private Date JavaDoc processedDate = null;
52     private Date JavaDoc nextProcessDate = null;
53     private String JavaDoc sessionId = null;
54     private String JavaDoc user = null;
55     private List JavaDoc principals = null;
56     private String JavaDoc from = null;
57     private int messageType = 0;
58     private String JavaDoc target = null;
59     private String JavaDoc[] services = null;
60     private String JavaDoc replyTo = null;
61     private String JavaDoc targetNamedQueue = null;
62     private String JavaDoc replyNamedQueue = null;
63     private boolean reply = false;
64     private int priority = 0;
65     private String JavaDoc correlationId = null;
66     private Properties JavaDoc properties = new Properties JavaDoc();
67     private boolean acknowledged = false;
68     private int status = 0;
69     private List JavaDoc errors = new ArrayList JavaDoc();
70     
71     /**
72      * Creates a new instance of MessageImpl.
73      *
74      * @param messageId The unique id of this message.
75      * @param user The name of the user.
76      * @param sessionId The id of the session.
77      * @param principals The list of principals.
78      * @param status The status of the message.
79      */

80     public MessageImpl(String JavaDoc messageId, String JavaDoc user, String JavaDoc sessionId,
81             List JavaDoc principals, int status) {
82         this.messageId = messageId;
83         this.created = new Date JavaDoc();
84         this.processedDate = new Date JavaDoc();
85         this.user = user;
86         this.sessionId = sessionId;
87         this.principals = principals;
88         this.status = status;
89     }
90     
91     
92     /**
93      * Creates a new instance of MessageImpl.
94      *
95      * @param messageId The unique id of this message.
96      * @param create The date the message was created.
97      * @param retries The number of retries of this message.
98      * @param processedDate The processed date.
99      * @param user The user responsible for this message.
100      * @param principals The list of principals.
101      * @param from The from url of the message.
102      * @param messageType The message type.
103      * @param status The status of the message.
104      */

105     public MessageImpl(String JavaDoc messageId, Date JavaDoc created, int retries,
106             Date JavaDoc processedDate,String JavaDoc user,String JavaDoc sessionId, List JavaDoc principals,
107             String JavaDoc from, int messageType, int status) {
108         this.messageId = messageId;
109         this.created = created;
110         this.retries = retries;
111         this.processedDate = processedDate;
112         this.user = user;
113         this.sessionId = sessionId;
114         this.principals = principals;
115         this.from = from;
116         this.messageType = messageType;
117         this.status = status;
118     }
119     
120     
121     /**
122      * This method returns the id of this message object.
123      *
124      * @return The id of the message object.
125      */

126     public String JavaDoc getMessageId() {
127         return messageId;
128     }
129     
130     /**
131      * This method sets the new id for this message
132      *
133      * @param messageId The new id for this message.
134      */

135     public void setMessageId(String JavaDoc messageId) {
136         this.messageId = messageId;
137     }
138     
139     
140     /**
141      * This method returns the date this message was created.
142      *
143      * @return The date the message was created
144      */

145     public Date JavaDoc getCreated() {
146         return created;
147     }
148     
149     
150     /**
151      * This is the number of retries this message has had.
152      *
153      * @return The number of retries performed by this message.
154      */

155     public int getRetries() {
156         return retries;
157     }
158     
159     
160     /**
161      * This method increments the retry count on this object.
162      */

163     public void incrementRetries() {
164         retries++;
165     }
166     
167     
168     /**
169      * This method will return the last processed date.
170      */

171     public Date JavaDoc getProcessedDate() {
172         return processedDate;
173     }
174     
175     
176     /**
177      * This method sets the processed date.
178      *
179      * @param processedDate The processed date of the message.
180      */

181     public void setProcessedDate(Date JavaDoc processedDate) {
182         this.processedDate = processedDate;
183     }
184     
185     
186     /**
187      * This method returns the next process date of the object.
188      */

189     public Date JavaDoc getNextProcessDate() {
190         return this.nextProcessDate;
191     }
192     
193     
194     /**
195      * This method returns the next process date of the object.
196      *
197      * @param nextProcessDate The next processed date of the message.
198      */

199     public void setNextProcessDate(Date JavaDoc nextProcessDate) {
200         this.nextProcessDate = nextProcessDate;
201     }
202     
203     
204     /**
205      * This method returns the creator of this message.
206      */

207     public String JavaDoc getMessageCreater() {
208         return user;
209     }
210     
211     
212     /**
213      * This method returns the ID of the session that created this message.
214      *
215      * @return The string containing the id of the session that created this
216      * message.
217      */

218     public String JavaDoc getSessionId() {
219         return sessionId;
220     }
221     
222     
223     /**
224      * This method returns the list of user principals assigned to this message.
225      *
226      * @return The list of user principals assigned to this message.
227      */

228     public List JavaDoc getMessagePrincipals() {
229         return principals;
230     }
231     
232     
233     /**
234      * This method sets the list of user principals assigned to this message.
235      *
236      * @param principals The new list of principals
237      */

238     public void setMessagePrincipals(List JavaDoc principals) {
239         this.principals = principals;
240     }
241     
242     
243     /**
244      * This method returns the message type for this object.
245      *
246      * @return The int containing the message type.
247      */

248     public int getMessageType() {
249         return messageType;
250     }
251     
252     
253     /**
254      * This method sets the type of message that this object represents.
255      *
256      * @param messageType The type of object being wrapped.
257      */

258     public void setMessageType(int messageType) {
259         this.messageType = messageType;
260     }
261     
262     /**
263      * This method returns the target of this message.
264      *
265      * @return The string containing the target information.
266      * @exception MessageServiceException
267      * @exception InvalidMessageType
268      */

269     public String JavaDoc getTarget() throws MessageServiceException,
270             InvalidMessageType {
271         return target;
272     }
273     
274     /**
275      * This method returns the target of this message.
276      *
277      * @param target The string containing the Target URL.
278      * @exception MessageServiceException
279      * @exception InvalidMessageType
280      */

281     public void setTarget(String JavaDoc target) throws MessageServiceException,
282             InvalidMessageType {
283         this.target = target;
284     }
285     
286     
287     /**
288      * This method returns the list of services this message targeted at.
289      *
290      * @return This method returns the list of services.
291      * @exception MessageServiceException
292      * @exception InvalidMessageType
293      */

294     public String JavaDoc[] getServices() throws MessageServiceException,
295             InvalidMessageType {
296         return services;
297     }
298     
299     
300     /**
301      * This method returns the list of services this message targeted at.
302      *
303      * @param services The list of services.
304      * @exception MessageServiceException
305      * @exception InvalidMessageType
306      */

307     public void setServices(String JavaDoc[] services) throws MessageServiceException,
308             InvalidMessageType {
309         this.services = services;
310     }
311     
312     
313     /**
314      * This method returns the from address of the message.
315      *
316      * @return The from address of the message.
317      * @exception MessageServiceException
318      */

319     public String JavaDoc getFrom() throws MessageServiceException {
320         return from;
321     }
322     
323     
324     /**
325      * This method returns the from address of the message.
326      *
327      * @return The from address of the message.
328      * @exception MessageServiceException
329      */

330     public void setFrom(String JavaDoc from) throws MessageServiceException {
331         this.from = from;
332     }
333     
334     
335     /**
336      * This method returns the reply to URL, it can be different from the from
337      * URL.
338      *
339      * @return The string containing the from URL.
340      * @exception MessageServiceException
341      */

342     public String JavaDoc getReplyTo() throws MessageServiceException {
343         return replyTo;
344     }
345     
346     
347     /**
348      * This method returns the reply to URL, it can be different from the from
349      * URL.
350      *
351      * @return The string containing the from URL.
352      * @exception MessageServiceException
353      */

354     public void setReplyTo(String JavaDoc replyTo) throws MessageServiceException {
355         this.replyTo = replyTo;
356     }
357     
358     
359     /**
360      * This will only be set if the reply service is not a daemon, but an
361      * external process.
362      *
363      * @return The string containing the Queue name.
364      * @exception MessageServiceException
365      */

366     public String JavaDoc getTargetNamedQueue() throws MessageServiceException {
367         return targetNamedQueue;
368     }
369     
370     
371     /**
372      * This will only be set if the reply service is not a daemon, but an
373      * external process.
374      *
375      * @param name The string containing the Queue name.
376      * @exception MessageServiceException
377      */

378     public void setTargetNamedQueue(String JavaDoc name) throws MessageServiceException {
379         this.targetNamedQueue = name;
380     }
381     
382     
383     /**
384      * This will only be set if the reply service is not a daemon, but an
385      * external process.
386      *
387      * @return The string containing the Queue name.
388      * @exception MessageServiceException
389      */

390     public String JavaDoc getReplyNamedQueue() throws MessageServiceException {
391         return replyNamedQueue;
392     }
393     
394     
395     /**
396      * This will only be set if the reply service is not a daemon, but an
397      * external process.
398      *
399      * @param name The string containing the Queue name.
400      * @exception MessageServiceException
401      */

402     public void setReplyNamedQueue(String JavaDoc name) throws MessageServiceException {
403         this.replyNamedQueue = name;
404     }
405     
406     
407     /**
408      * This method returns the value of the reply flag. TRUE if it should reply
409      * FALSE if it should not.
410      *
411      * @return TRUE if this message must reply, FALSE if not.
412      * @exception MessageServiceException
413      */

414     public boolean getReply() throws MessageServiceException {
415         return reply;
416     }
417     
418     
419     /**
420      * This method sets the reply flag.
421      *
422      * @param value TRUE if a reply is required, FALSE if not.
423      * @exception MessageServiceException
424      */

425     public void setReply(boolean value) throws MessageServiceException {
426         this.reply = value;
427     }
428     
429     
430     /**
431      * This message returns the priority of this message.
432      *
433      * @return The int indicating the priority of this message.
434      * @exception MessageServiceException
435      */

436     public int getPriority() {
437         return priority;
438     }
439     
440     
441     /**
442      * This method sets the priority of the message.
443      *
444      * @param priority The priority of the message.
445      * @exception MessageServiceException
446      */

447     public void setPriority(int priority) throws MessageServiceException {
448         this.priority = priority;
449     }
450     
451     
452     /**
453      * This method sets the correlation id for this message. It is the external
454      * identifier for this message.
455      *
456      * @param id The id that will be used as the correlation id.
457      * @exception MessageServiceException
458      */

459     public void setCorrelationId(String JavaDoc id) throws MessageServiceException {
460         this.correlationId = id;
461     }
462     
463     
464     /**
465      * The external correlation id for this message.
466      *
467      * @return The string containing the correllation ID.
468      * @exception MessageServiceException
469      */

470     public String JavaDoc getCorrelationId() throws MessageServiceException {
471         return correlationId;
472     }
473     
474     
475     /**
476      * This clears the body of the message.
477      *
478      * @exception MessageServiceException
479      */

480     public abstract void clearBody() throws MessageServiceException;
481     
482     
483     /**
484      * This method clears the properties assigned to this message.
485      *
486      * @exception MessageServiceException
487      */

488     public void clearProperties() throws MessageServiceException {
489         properties.clear();
490     }
491     
492     
493     /**
494      * This method returns true if the property is found.
495      *
496      * @return TRUE if the property is found, FALSE if not.
497      * @param name The name of the property.
498      * @exception MessageServiceException
499      */

500     public boolean containsProperty(String JavaDoc name) throws MessageServiceException {
501         return properties.containsKey(name);
502     }
503     
504     
505     /**
506      * This method returns the boolean property value for the requested name.
507      *
508      * @return The value of the boolean property.
509      * @param name The name of the property.
510      * @exception MessageServiceException
511      * @exception InvalidProperty
512      */

513     public boolean getBooleanProperty(String JavaDoc name) throws
514             MessageServiceException, InvalidProperty {
515         if (properties.contains(name)) {
516             throw new InvalidProperty("The property [" + name + "] could not" +
517                     "be found.");
518         }
519         Boolean JavaDoc bool = (Boolean JavaDoc)properties.get(name);
520         return bool.booleanValue();
521     }
522     
523     
524     /**
525      * This method returns the byte property value for the requested name.
526      *
527      * @return The value of the byte property.
528      * @param name The name of the property.
529      * @exception MessageServiceException
530      * @exception InvalidProperty
531      */

532     public byte getByteProperty(String JavaDoc name) throws
533             MessageServiceException, InvalidProperty {
534         if (properties.contains(name)) {
535             throw new InvalidProperty("The property [" + name + "] could not" +
536                     "be found.");
537         }
538         Byte JavaDoc byteValue = (Byte JavaDoc)properties.get(name);
539         return byteValue.byteValue();
540     }
541     
542     
543     /**
544      * This method returns the double property value for the requested name.
545      *
546      * @return The value of the byte property.
547      * @param name The name of the property.
548      * @exception MessageServiceException
549      * @exception InvalidProperty
550      */

551     public double getDoubleProperty(String JavaDoc name) throws
552             MessageServiceException, InvalidProperty {
553         if (properties.contains(name)) {
554             throw new InvalidProperty("The property [" + name + "] could not" +
555                     "be found.");
556         }
557         Double JavaDoc doubleValue = (Double JavaDoc)properties.get(name);
558         return doubleValue.doubleValue();
559     }
560     
561     
562     /**
563      * This method returns the float property value for the requested name.
564      *
565      * @return The value of the float property.
566      * @param name The name of the property.
567      * @exception MessageServiceException
568      * @exception InvalidProperty
569      */

570     public float getFloatProperty(String JavaDoc name) throws
571             MessageServiceException, InvalidProperty {
572         if (properties.contains(name)) {
573             throw new InvalidProperty("The property [" + name + "] could not" +
574                     "be found.");
575         }
576         Float JavaDoc floatValue = (Float JavaDoc)properties.get(name);
577         return floatValue.floatValue();
578     }
579     
580     
581     /**
582      * This method returns the int property value for the requested name.
583      *
584      * @return The value of the int property.
585      * @param name The name of the property.
586      * @exception MessageServiceException
587      * @exception InvalidProperty
588      */

589     public int getIntProperty(String JavaDoc name) throws
590             MessageServiceException, InvalidProperty {
591         if (properties.contains(name)) {
592             throw new InvalidProperty("The property [" + name + "] could not" +
593                     "be found.");
594         }
595         Integer JavaDoc intValue = (Integer JavaDoc)properties.get(name);
596         return intValue.intValue();
597     }
598     
599     
600     /**
601      * This method returns the long property value for the requested name.
602      *
603      * @return The value of the long property.
604      * @param name The name of the property.
605      * @exception MessageServiceException
606      * @exception InvalidProperty
607      */

608     public long getLongProperty(String JavaDoc name) throws
609             MessageServiceException, InvalidProperty {
610         if (properties.contains(name)) {
611             throw new InvalidProperty("The property [" + name + "] could not" +
612                     "be found.");
613         }
614         Long JavaDoc longValue = (Long JavaDoc)properties.get(name);
615         return longValue.longValue();
616     }
617     
618     
619     /**
620      * This method returns the object property value for the requested name.
621      *
622      * @return The value of the object property.
623      * @param name The name of the property.
624      * @exception MessageServiceException
625      * @exception InvalidProperty
626      */

627     public Object JavaDoc getObjectProperty(String JavaDoc name) throws
628             MessageServiceException, InvalidProperty {
629         if (properties.contains(name)) {
630             throw new InvalidProperty("The property [" + name + "] could not" +
631                     "be found.");
632         }
633         try {
634             return ObjectSerializer.deserialize((byte[])properties.get(name));
635         } catch (Exception JavaDoc ex) {
636             throw new MessageServiceException("Failed to retrieve the object " +
637                     "value :" + ex.getMessage(),ex);
638         }
639     }
640     
641     
642     /**
643      * This method returns the string property value for the requested name.
644      *
645      * @return The value of the string property.
646      * @param name The name of the property.
647      * @exception MessageServiceException
648      * @exception InvalidProperty
649      */

650     public String JavaDoc getStringProperty(String JavaDoc name) throws
651             MessageServiceException, InvalidProperty {
652         if (properties.contains(name)) {
653             throw new InvalidProperty("The property [" + name + "] could not" +
654                     "be found.");
655         }
656         return (String JavaDoc)properties.get(name);
657     }
658     
659     
660     /**
661      * This method returns the value of the property.
662      *
663      * @return The value of the property.
664      * @param name The name of the property.
665      * @exception MessageServiceException
666      * @exception InvalidProperty
667      */

668     public Object JavaDoc getPropertyValue(String JavaDoc name) throws
669             MessageServiceException, InvalidProperty {
670         if (properties.contains(name)) {
671             throw new InvalidProperty("The property [" + name + "] could not" +
672                     "be found.");
673         }
674         return properties.get(name);
675     }
676     
677     
678     /**
679      * This method returns the string property value for the requested name.
680      *
681      * @return The list of property names
682      * @exception MessageServiceException
683      */

684     public Enumeration JavaDoc getPropertyNames() throws MessageServiceException {
685         return properties.propertyNames();
686     }
687     
688     
689     /**
690      * This method returns true if the specified property exits.
691      *
692      * @return TRUE if the property exists, FALSE if not.
693      * @param name The name of the property to look for.
694      * @exception MessageServiceException
695      */

696     public boolean propertyExists(String JavaDoc name) throws MessageServiceException {
697         return properties.containsKey(name);
698     }
699     
700     
701     /**
702      * This method sets the boolean property value for the name.
703      *
704      * @param name The name of the property.
705      * @param value The value of the boolean property.
706      * @exception MessageServiceException
707      */

708     public void setBooleanProperty(String JavaDoc name, boolean value) throws
709             MessageServiceException {
710         properties.put(name, new Boolean JavaDoc(value));
711     }
712     
713     
714     /**
715      * This method sets the byte property value for the name.
716      *
717      * @param name The name of the property.
718      * @param value The value of the byte property.
719      * @exception MessageServiceException
720      */

721     public void setByteProperty(String JavaDoc name, byte value) throws
722             MessageServiceException {
723         properties.put(name, new Byte JavaDoc(value));
724     }
725     
726     
727     /**
728      * This method sets the double property value for the name.
729      *
730      * @param name The name of the property.
731      * @param value The value of the double property.
732      * @exception MessageServiceException
733      */

734     public void setDoubleProperty(String JavaDoc name, double value) throws
735             MessageServiceException{
736         properties.put(name, new Double JavaDoc(value));
737     }
738     
739     
740     /**
741      * This method sets the float property value for the name.
742      *
743      * @param name The name of the property.
744      * @param value The value of the float property.
745      * @exception MessageServiceException
746      */

747     public void setFloatProperty(String JavaDoc name, float value) throws
748             MessageServiceException {
749         properties.put(name, new Float JavaDoc(value));
750     }
751     
752     
753     /**
754      * This method set the int property value for the name.
755      *
756      * @param name The name of the property.
757      * @param value The value of the int property.
758      * @exception MessageServiceException
759      */

760     public void setIntProperty(String JavaDoc name, int value) throws
761             MessageServiceException {
762         properties.put(name, new Integer JavaDoc(value));
763     }
764     
765     
766     /**
767      * This method sets the long property value for the name.
768      *
769      * @param name The name of the property.
770      * @return value The new long value.
771      * @exception MessageServiceException
772      */

773     public void setLongProperty(String JavaDoc name, long value) throws
774             MessageServiceException {
775         properties.put(name, new Long JavaDoc(value));
776     }
777     
778     
779     /**
780      * This method returns the object property value for the name.
781      *
782      * @param name The name of the property.
783      * @param value The new object value to set.
784      * @exception MessageServiceException
785      * @exception InvalidProperty
786      */

787     public void setObjectProperty(String JavaDoc name, Object JavaDoc value) throws
788             MessageServiceException {
789         try {
790             properties.put(name, ObjectSerializer.serialize(value));
791         } catch (Exception JavaDoc ex) {
792             throw new MessageServiceException("Failed to store the object value :" +
793                     ex.getMessage(),ex);
794         }
795     }
796     
797     
798     /**
799      * This method sets the string property value for the name.
800      *
801      * @param name The name of the property.
802      * @return value The new string value to set.
803      * @exception MessageServiceException
804      */

805     public void setStringProperty(String JavaDoc name, String JavaDoc value) throws
806             MessageServiceException {
807         properties.put(name, value);
808     }
809     
810     
811     /**
812      * This method sets the value of the property.
813      *
814      * @param name The name of the property.
815      * @param value The property value.
816      * @exception MessageServiceException
817      * @exception InvalidProperty
818      */

819     public void setPropertyValue(String JavaDoc name,Object JavaDoc value) throws
820             MessageServiceException, InvalidProperty {
821         properties.put(name, value);
822     }
823     
824     /**
825      * This method acknowledges that this message has been successfully
826      * processed by a target.
827      *
828      * @exception MessageServiceException
829      */

830     public void acknowledge() throws MessageServiceException {
831         acknowledged = true;
832     }
833     
834     
835     /**
836      * This method returns the value of the acknowledged flag for this message.
837      *
838      * @return TRUE if acknowleded, FALSE if not.
839      * @exception MessageServiceException
840      */

841     public boolean isAcknowledged() throws MessageServiceException {
842         return acknowledged;
843     }
844     
845     
846     /**
847      * This method returns the current state of this message.
848      *
849      * @return The current state of this message.
850      * @exception MessageServiceException
851      */

852     public int getState() throws MessageServiceException {
853         return status;
854     }
855     
856     
857     /**
858      * This method sets the current state of this message.
859      *
860      * @param status The new status of this message.
861      * @exception MessageServiceException
862      */

863     public void setState(int status) throws MessageServiceException {
864         this.status = status;
865     }
866     
867     
868     /**
869      * This method returns list of errors
870      */

871     public List JavaDoc getErrors() throws MessageServiceException {
872         return errors;
873     }
874     
875     
876     /**
877      * This method adds an error to the list of errors for this message.
878      *
879      * @param level The level of the error.
880      * @param msg The message associated with the error.
881      */

882     public void addError(int level, String JavaDoc msg) throws MessageServiceException {
883         errors.add(new MessageError(new Date JavaDoc(),level,msg));
884     }
885     
886     
887     /**
888      * This method adds the error to the list of errors.
889      *
890      * @param error The error to add to the list.
891      */

892     public void addError(MessageError error) {
893         errors.add(error);
894     }
895     
896     
897     /**
898      * This method returns a clone of this object.
899      *
900      * @return A cloned instance of this object.
901      * @exception CloneNotSupportedException
902      */

903     public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
904         return super.clone();
905     }
906 }
907
Popular Tags