KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > providers > soap > glue > GlueMessageAdapter


1 /*
2  * $Id: GlueMessageAdapter.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.soap.glue;
12
13 import electric.glue.context.ThreadContext;
14 import electric.service.IService;
15
16 import org.apache.commons.collections.IteratorUtils;
17 import org.mule.config.MuleProperties;
18 import org.mule.providers.AbstractMessageAdapter;
19 import org.mule.transformers.simple.SerializableToByteArray;
20 import org.mule.umo.transformer.UMOTransformer;
21
22 import java.util.Iterator JavaDoc;
23
24 /**
25  * <code>GlueMessageAdapter</code> wraps a Glue soap request
26  *
27  * @author <a HREF="mailto:ross.mason@symphonysoft.com">Ross Mason</a>
28  * @version $Revision: 3982 $
29  */

30 public class GlueMessageAdapter extends AbstractMessageAdapter
31 {
32     /**
33      * Serial version
34      */

35     private static final long serialVersionUID = 7330508491857055854L;
36
37     private static final UMOTransformer transformer = new SerializableToByteArray();
38
39     private final Object JavaDoc message;
40
41     public GlueMessageAdapter(Object JavaDoc message)
42     {
43         if (message instanceof GlueMessageHolder)
44         {
45             GlueMessageHolder holder = (GlueMessageHolder)message;
46             this.message = holder.getMessage();
47             Iterator JavaDoc iter = IteratorUtils.asIterator(holder.getService().getContext().getPropertyNames());
48             String JavaDoc key;
49             while (iter.hasNext())
50             {
51                 key = iter.next().toString();
52                 setProperty(key, holder.getService().getContext().getProperty(key));
53             }
54         }
55         else
56         {
57             this.message = message;
58         }
59         String JavaDoc value = (String JavaDoc)ThreadContext.removeProperty(MuleProperties.MULE_REPLY_TO_PROPERTY);
60         if (value != null)
61         {
62             setReplyTo(value);
63         }
64         value = (String JavaDoc)ThreadContext.removeProperty(MuleProperties.MULE_CORRELATION_ID_PROPERTY);
65         if (value != null)
66         {
67             setCorrelationId(value);
68         }
69
70         value = (String JavaDoc)ThreadContext.removeProperty(MuleProperties.MULE_CORRELATION_SEQUENCE_PROPERTY);
71         if (value != null && !"-1".equals(value))
72         {
73             setCorrelationSequence(Integer.parseInt(value));
74         }
75         value = (String JavaDoc)ThreadContext.removeProperty(MuleProperties.MULE_CORRELATION_GROUP_SIZE_PROPERTY);
76         if (value != null && !"-1".equals(value))
77         {
78             setCorrelationGroupSize(Integer.parseInt(value));
79         }
80     }
81
82     /**
83      * Converts the message implementation into a String representation
84      *
85      * @param encoding The encoding to use when transforming the message (if
86      * necessary). The parameter is used when converting from a byte array
87      * @return String representation of the message payload
88      * @throws Exception Implementation may throw an endpoint specific exception
89      */

90     public String JavaDoc getPayloadAsString(String JavaDoc encoding) throws Exception JavaDoc
91     {
92         return new String JavaDoc(getPayloadAsBytes(), encoding);
93     }
94
95     /**
96      * Converts the message implementation into a String representation
97      *
98      * @return String representation of the message
99      * @throws Exception Implemetation may throw an endpoint specific exception
100      */

101     public byte[] getPayloadAsBytes() throws Exception JavaDoc
102     {
103         return (byte[])transformer.transform(message);
104     }
105
106     /**
107      * @return the current message
108      */

109     public Object JavaDoc getPayload()
110     {
111         return message;
112     }
113
114     public static class GlueMessageHolder
115     {
116         private Object JavaDoc message;
117         private IService service;
118
119         public GlueMessageHolder(Object JavaDoc message, IService service)
120         {
121             this.message = message;
122             this.service = service;
123         }
124
125         public Object JavaDoc getMessage()
126         {
127             return message;
128         }
129
130         public IService getService()
131         {
132             return service;
133         }
134     }
135 }
136
Popular Tags