1 10 11 package org.mule.impl; 12 13 import java.util.Iterator ; 14 15 import org.apache.commons.logging.Log; 16 import org.apache.commons.logging.LogFactory; 17 import org.mule.config.MuleProperties; 18 import org.mule.umo.UMOEvent; 19 import org.mule.umo.UMOEventContext; 20 import org.mule.umo.UMOExceptionPayload; 21 import org.mule.umo.UMOMessage; 22 23 28 public class RequestContext 29 { 30 private static final Log logger = LogFactory.getLog(RequestContext.class); 31 private static final ThreadLocal currentEvent = new ThreadLocal (); 32 33 public static UMOEventContext getEventContext() 34 { 35 UMOEvent event = getEvent(); 36 if (event != null) 37 { 38 return new MuleEventContext(event); 39 } 40 else 41 { 42 return null; 43 } 44 } 45 46 public static UMOEvent getEvent() 47 { 48 return (UMOEvent)currentEvent.get(); 49 } 50 51 public static void setEvent(UMOEvent event) 52 { 53 currentEvent.set(event); 54 } 55 56 62 public static void rewriteEvent(UMOMessage message) 63 { 64 if (message != null) 65 { 66 UMOEvent event = getEvent(); 67 if (event != null) 68 { 69 event = new MuleEvent(message, event); 70 setEvent(event); 71 } 72 } 73 } 74 75 public static void writeResponse(UMOMessage message) 76 { 77 if (message != null) 78 { 79 UMOEvent event = getEvent(); 80 if (event != null) 81 { 82 for (Iterator iterator = event.getMessage().getPropertyNames().iterator(); iterator.hasNext();) 83 { 84 String key = (String )iterator.next(); 85 if (key == null) 86 { 87 logger.warn("Message property key is null: please report the following stack trace to dev@mule.codehaus.org.", 88 new IllegalArgumentException ()); 89 } 90 else 91 { 92 if (key.startsWith(MuleProperties.PROPERTY_PREFIX)) 93 { 94 Object newValue = message.getProperty(key); 95 Object oldValue = event.getMessage().getProperty(key); 96 if (newValue == null) 97 { 98 message.setProperty(key, oldValue); 99 } 100 else if (logger.isInfoEnabled() && !newValue.equals(oldValue)) 101 { 102 logger.info("Message already contains property " + key + "=" + newValue 103 + " not replacing old value: " + oldValue); 104 } 105 } 106 } 107 } 108 109 event = new MuleEvent(message, event.getEndpoint(), event.getSession(), event.isSynchronous()); 110 setEvent(event); 111 } 112 } 113 } 114 115 118 public static void clear() 119 { 120 setEvent(null); 121 } 122 123 public static void setExceptionPayload(UMOExceptionPayload exceptionPayload) 124 { 125 getEvent().getMessage().setExceptionPayload(exceptionPayload); 126 } 127 128 public static UMOExceptionPayload getExceptionPayload() 129 { 130 return getEvent().getMessage().getExceptionPayload(); 131 } 132 133 } 134 | Popular Tags |