KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > providers > gs > JiniMessageAdapter


1 /*
2  * $Id: JiniMessageAdapter.java 3982 2006-11-22 14:28:01Z lajos $
3  * --------------------------------------------------------------------------------------
4  * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
5  *
6  * The software in this package is published under the terms of the MuleSource MPL
7  * license, a copy of which has been included with this distribution in the
8  * LICENSE.txt file.
9  */

10
11 package org.mule.providers.gs;
12
13 import net.jini.core.entry.Entry;
14
15 import org.apache.commons.lang.StringUtils;
16 import org.mule.MuleManager;
17 import org.mule.providers.AbstractMessageAdapter;
18 import org.mule.umo.MessagingException;
19 import org.mule.umo.provider.MessageTypeNotSupportedException;
20 import org.mule.util.UUID;
21
22 /**
23  * <code>JiniMessageAdapter</code> wraps a JavaSpaceMessage entry.
24  *
25  * @see Entry
26  */

27
28 public class JiniMessageAdapter extends AbstractMessageAdapter
29 {
30     /**
31      * Serial version
32      */

33     private static final long serialVersionUID = 3480872474322069206L;
34
35     protected final Entry message;
36     protected final String JavaDoc id;
37
38     public JiniMessageAdapter(Object JavaDoc message) throws MessagingException
39     {
40         if (message == null)
41         {
42             throw new MessageTypeNotSupportedException(message, getClass());
43         }
44
45         if (message instanceof Entry)
46         {
47             this.message = (Entry)message;
48         }
49         else
50         {
51             this.message = new JiniMessage(null, message);
52         }
53
54         if (this.message instanceof JiniMessage)
55         {
56             JiniMessage jm = (JiniMessage)this.message;
57
58             // accept or create
59
String JavaDoc msgId = jm.getMessageId();
60             if (msgId == null)
61             {
62                 msgId = UUID.getUUID();
63             }
64             this.id = msgId;
65
66             // accept null
67
this.setCorrelationId(jm.getCorrelationId());
68
69             // accept or default
70
Integer JavaDoc value = jm.getCorrelationGroupSize();
71             this.setCorrelationGroupSize(value != null ? value.intValue() : -1);
72
73             // accept or default
74
value = jm.getCorrelationSequence();
75             this.setCorrelationSequence(value != null ? value.intValue() : -1);
76
77             // accept null
78
this.setReplyTo(jm.getReplyTo());
79
80             // accept or default
81
this.setEncoding(StringUtils.defaultIfEmpty(jm.getEncoding(), MuleManager.getConfiguration()
82                 .getEncoding()));
83
84             // accept null
85
this.setExceptionPayload(jm.getExceptionPayload());
86
87             // accept all (as handled by superclass)
88
this.addProperties(jm.getProperties());
89         }
90         else
91         {
92             id = UUID.getUUID();
93         }
94     }
95
96     /**
97      * Converts the message implementation into a String representation
98      *
99      * @param encoding The encoding to use when transforming the message (if
100      * necessary). The parameter is used when converting from a byte array
101      * @return String representation of the message payload
102      * @throws Exception Implementation may throw an endpoint specific exception
103      */

104     public String JavaDoc getPayloadAsString(String JavaDoc encoding) throws Exception JavaDoc
105     {
106         if (message instanceof JiniMessage)
107         {
108             Object JavaDoc payload = ((JiniMessage)message).getPayload();
109             if (payload != null)
110             {
111                 return payload.toString();
112             }
113             else
114             {
115                 return null;
116             }
117         }
118         else
119         {
120             return message.toString();
121         }
122     }
123
124     public byte[] getPayloadAsBytes() throws Exception JavaDoc
125     {
126         Object JavaDoc payload = message;
127         if (message instanceof JiniMessage)
128         {
129             payload = ((JiniMessage)message).getPayload();
130         }
131
132         return convertToBytes(payload);
133     }
134
135     public Object JavaDoc getPayload()
136     {
137         if (message instanceof JiniMessage)
138         {
139             return ((JiniMessage)message).getPayload();
140         }
141         else
142         {
143             return message;
144         }
145     }
146
147     public String JavaDoc getUniqueId()
148     {
149         return id;
150     }
151 }
152
Popular Tags