1 10 11 package org.mule.providers; 12 13 import java.util.Iterator ; 14 import java.util.Map ; 15 16 import org.mule.MuleRuntimeException; 17 import org.mule.config.i18n.Message; 18 import org.mule.config.i18n.Messages; 19 import org.mule.umo.provider.UMOMessageAdapter; 20 21 26 27 public class DefaultMessageAdapter extends AbstractMessageAdapter 28 { 29 32 private static final long serialVersionUID = 1908152148142575505L; 33 34 37 protected Object message; 38 39 46 public DefaultMessageAdapter(Object message) 47 { 48 if (message == null) 49 { 50 this.message = new NullPayload(); 51 } 52 else 53 { 54 this.message = message; 55 } 56 } 57 58 public DefaultMessageAdapter(Object message, UMOMessageAdapter previous) 59 { 60 if (previous != null) 61 { 62 id = previous.getUniqueId(); 63 64 if (message == null) 65 { 66 this.message = new NullPayload(); 67 } 68 else 69 { 70 this.message = message; 71 } 72 for (Iterator iterator = previous.getAttachmentNames().iterator(); iterator.hasNext();) 73 { 74 String name = (String )iterator.next(); 75 try 76 { 77 addAttachment(name, previous.getAttachment(name)); 78 } 79 catch (Exception e) 80 { 81 throw new MuleRuntimeException(new Message(Messages.FAILED_TO_READ_PAYLOAD), e); 82 } 83 } 84 for (Iterator iterator = previous.getPropertyNames().iterator(); iterator.hasNext();) 85 { 86 String name = (String )iterator.next(); 87 try 88 { 89 setProperty(name, previous.getProperty(name)); 90 } 91 catch (Exception e) 92 { 93 throw new MuleRuntimeException(new Message(Messages.FAILED_TO_READ_PAYLOAD), e); 94 } 95 } 96 } 97 else 98 { 99 throw new NullPointerException ("previousAdapter"); 100 } 101 } 102 103 114 public DefaultMessageAdapter(Object message, Map properties, Map attachments) 115 { 116 this(message); 117 if (properties != null) 118 { 119 this.properties.putAll(properties); 120 } 121 if (attachments != null) 122 { 123 this.attachments.putAll(attachments); 124 } 125 } 126 127 135 public String getPayloadAsString(String encoding) throws Exception 136 { 137 if (message instanceof byte[]) 138 { 139 if (encoding != null) 140 { 141 return new String ((byte[])message, encoding); 142 } 143 else 144 { 145 return new String ((byte[])message); 146 } 147 } 148 else 149 { 150 return message.toString(); 151 } 152 } 153 154 160 public byte[] getPayloadAsBytes() throws Exception 161 { 162 return convertToBytes(message); 163 } 164 165 168 public Object getPayload() 169 { 170 return message; 171 } 172 173 public String getUniqueId() 174 { 175 return id; 176 } 177 } 178 | Popular Tags |