1 10 11 package org.mule.providers; 12 13 import edu.emory.mathcs.backport.java.util.concurrent.ConcurrentHashMap; 14 import edu.emory.mathcs.backport.java.util.concurrent.ConcurrentMap; 15 16 import java.io.Serializable ; 17 import java.io.UnsupportedEncodingException ; 18 import java.util.Iterator ; 19 import java.util.Map ; 20 import java.util.Set ; 21 22 import javax.activation.DataHandler ; 23 24 import org.apache.commons.lang.SerializationUtils; 25 import org.apache.commons.lang.StringUtils; 26 import org.apache.commons.logging.Log; 27 import org.apache.commons.logging.LogFactory; 28 import org.mule.MuleManager; 29 import org.mule.config.MuleProperties; 30 import org.mule.config.i18n.Message; 31 import org.mule.config.i18n.Messages; 32 import org.mule.umo.UMOExceptionPayload; 33 import org.mule.umo.provider.UMOMessageAdapter; 34 import org.mule.umo.transformer.TransformerException; 35 import org.mule.util.MapUtils; 36 import org.mule.util.UUID; 37 38 43 public abstract class AbstractMessageAdapter implements UMOMessageAdapter 44 { 45 46 49 protected transient Log logger = LogFactory.getLog(getClass()); 50 51 protected ConcurrentMap properties = new ConcurrentHashMap(); 52 protected ConcurrentMap attachments = new ConcurrentHashMap(); 53 protected String encoding = MuleManager.getConfiguration().getEncoding(); 54 55 protected UMOExceptionPayload exceptionPayload; 56 protected String id = UUID.getUUID(); 57 58 public String toString() 59 { 60 StringBuffer buf = new StringBuffer (120); 61 buf.append(getClass().getName()); 62 buf.append('{'); 63 buf.append("id=").append(getUniqueId()); 64 buf.append(", payload=").append(getPayload().getClass().getName()); 65 buf.append(", correlationId=").append(getCorrelationId()); 66 buf.append(", correlationGroup=").append(getCorrelationGroupSize()); 67 buf.append(", correlationSeq=").append(getCorrelationSequence()); 68 buf.append(", encoding=").append(getEncoding()); 69 buf.append(", exceptionPayload=").append(exceptionPayload); 70 buf.append(", properties=").append(MapUtils.toString(properties, true)); 71 buf.append('}'); 72 return buf.toString(); 73 } 74 75 public void addProperties(Map props) 76 { 77 if (props != null) 78 { 79 synchronized (props) 80 { 81 for (Iterator iter = props.entrySet().iterator(); iter.hasNext();) 82 { 83 Map.Entry entry = (Map.Entry)iter.next(); 84 setProperty((String )entry.getKey(), entry.getValue()); 85 } 86 } 87 } 88 } 89 90 public void clearProperties() 91 { 92 properties.clear(); 93 } 94 95 100 public Object removeProperty(String key) 101 { 102 return properties.remove(key); 103 } 104 105 110 public Object getProperty(String key) 111 { 112 return properties.get(key); 113 } 114 115 120 public Set getPropertyNames() 121 { 122 return properties.keySet(); 123 } 124 125 131 public void setProperty(String key, Object value) 132 { 133 if (key != null) 134 { 135 if (value != null) 136 { 137 properties.put(key, value); 138 } 139 else 140 { 141 logger.warn("setProperty(key, value) called with null value; removing key: " + key 142 + "; please report the following stack trace to dev@mule.codehaus.org.", 143 new Throwable ()); 144 properties.remove(key); 145 } 146 } 147 else 148 { 149 logger.warn("setProperty(key, value) ignored because of null key for object: " + value 150 + "; please report the following stack trace to dev@mule.codehaus.org.", 151 new Throwable ()); 152 } 153 } 154 155 public String getUniqueId() 156 { 157 return id; 158 } 159 160 public Object getProperty(String name, Object defaultValue) 161 { 162 return MapUtils.getObject(properties, name, defaultValue); 163 } 164 165 public int getIntProperty(String name, int defaultValue) 166 { 167 return MapUtils.getIntValue(properties, name, defaultValue); 168 } 169 170 public long getLongProperty(String name, long defaultValue) 171 { 172 return MapUtils.getLongValue(properties, name, defaultValue); 173 } 174 175 public double getDoubleProperty(String name, double defaultValue) 176 { 177 return MapUtils.getDoubleValue(properties, name, defaultValue); 178 } 179 180 public boolean getBooleanProperty(String name, boolean defaultValue) 181 { 182 return MapUtils.getBooleanValue(properties, name, defaultValue); 183 } 184 185 public String getStringProperty(String name, String defaultValue) 186 { 187 return MapUtils.getString(properties, name, defaultValue); 188 } 189 190 public void setBooleanProperty(String name, boolean value) 191 { 192 setProperty(name, Boolean.valueOf(value)); 193 } 194 195 public void setIntProperty(String name, int value) 196 { 197 setProperty(name, new Integer (value)); 198 } 199 200 public void setLongProperty(String name, long value) 201 { 202 setProperty(name, new Long (value)); 203 } 204 205 public void setDoubleProperty(String name, double value) 206 { 207 setProperty(name, new Double (value)); 208 } 209 210 public void setStringProperty(String name, String value) 211 { 212 setProperty(name, value); 213 } 214 215 public Object getReplyTo() 216 { 217 return getProperty(MuleProperties.MULE_REPLY_TO_PROPERTY); 218 } 219 220 public void setReplyTo(Object replyTo) 221 { 222 if (replyTo != null) 223 { 224 setProperty(MuleProperties.MULE_REPLY_TO_PROPERTY, replyTo); 225 } 226 else 227 { 228 removeProperty(MuleProperties.MULE_REPLY_TO_PROPERTY); 229 } 230 } 231 232 public String getCorrelationId() 233 { 234 return (String )getProperty(MuleProperties.MULE_CORRELATION_ID_PROPERTY); 235 } 236 237 public void setCorrelationId(String correlationId) 238 { 239 if (StringUtils.isNotBlank(correlationId)) 240 { 241 setProperty(MuleProperties.MULE_CORRELATION_ID_PROPERTY, correlationId); 242 } 243 else 244 { 245 removeProperty(MuleProperties.MULE_CORRELATION_ID_PROPERTY); 246 } 247 } 248 249 255 public int getCorrelationSequence() 256 { 257 return getIntProperty(MuleProperties.MULE_CORRELATION_SEQUENCE_PROPERTY, -1); 258 } 259 260 266 public void setCorrelationSequence(int sequence) 267 { 268 setIntProperty(MuleProperties.MULE_CORRELATION_SEQUENCE_PROPERTY, sequence); 269 } 270 271 276 public int getCorrelationGroupSize() 277 { 278 return getIntProperty(MuleProperties.MULE_CORRELATION_GROUP_SIZE_PROPERTY, -1); 279 } 280 281 286 public void setCorrelationGroupSize(int size) 287 { 288 setIntProperty(MuleProperties.MULE_CORRELATION_GROUP_SIZE_PROPERTY, size); 289 } 290 291 public UMOExceptionPayload getExceptionPayload() 292 { 293 return exceptionPayload; 294 } 295 296 public void setExceptionPayload(UMOExceptionPayload payload) 297 { 298 exceptionPayload = payload; 299 } 300 301 public void addAttachment(String name, DataHandler dataHandler) throws Exception 302 { 303 attachments.put(name, dataHandler); 304 } 305 306 public void removeAttachment(String name) throws Exception 307 { 308 attachments.remove(name); 309 } 310 311 public DataHandler getAttachment(String name) 312 { 313 return (DataHandler )attachments.get(name); 314 } 315 316 public Set getAttachmentNames() 317 { 318 return attachments.keySet(); 319 } 320 321 public String getEncoding() 322 { 323 return encoding; 324 } 325 326 331 public void setEncoding(String encoding) 332 { 333 this.encoding = encoding; 334 } 335 336 343 public final String getPayloadAsString() throws Exception 344 { 345 return getPayloadAsString(getEncoding()); 346 } 347 348 protected byte[] convertToBytes(Object object) throws TransformerException, UnsupportedEncodingException 349 { 350 if (object instanceof String ) 351 { 352 return object.toString().getBytes(getEncoding()); 353 } 354 355 if (object instanceof byte[]) 356 { 357 return (byte[])object; 358 } 359 else if (object instanceof Serializable ) 360 { 361 try 362 { 363 return SerializationUtils.serialize((Serializable )object); 364 } 365 catch (Exception e) 366 { 367 throw new TransformerException(new Message(Messages.TRANSFORM_FAILED_FROM_X_TO_X, 368 object.getClass().getName(), "byte[]"), e); 369 } 370 } 371 else 372 { 373 throw new TransformerException(new Message(Messages.TRANSFORM_ON_X_NOT_OF_SPECIFIED_TYPE_X, 374 object.getClass().getName(), "byte[] or " + Serializable .class.getName())); 375 } 376 } 377 } 378 | Popular Tags |