1 /* 2 * $Id: OptimizedRequestContext.java 20321 2010-11-24 15:21:24Z dfeist $ 3 * -------------------------------------------------------------------------------------- 4 * Copyright (c) MuleSoft, Inc. All rights reserved. http://www.mulesoft.com 5 * 6 * The software in this package is published under the terms of the CPAL v1.0 7 * license, a copy of which has been included with this distribution in the 8 * LICENSE.txt file. 9 */ 10 11 package org.mule; 12 13 import org.mule.api.MuleEvent; 14 import org.mule.api.MuleMessage; 15 16 import org.apache.commons.logging.Log; 17 import org.apache.commons.logging.LogFactory; 18 19 /** 20 * NOT FOR PUBLIC USE - please use the interface provided by RequestContext. 21 * This is a temporary interface that helps provide an (optimized) fix for message 22 * scribbling. 23 * 24 * <p>Mutating methods have three versions: default (RequestContext; safe, makes and returns a new 25 * copy - although this can be changed via {@link RequestContext#DEFAULT_ACTION}); 26 * unsafe (doesn't make a copy, use only where certain no threading); critical (safe, 27 * documents that threading a known issue).</p> 28 */ 29 public final class OptimizedRequestContext 30 { 31 32 private static final boolean DOCUMENT_UNSAFE_CALLS = false; 33 private static final Log logger = LogFactory.getLog(OptimizedRequestContext.class); 34 35 /** 36 * Do not instantiate. 37 */ 38 private OptimizedRequestContext() 39 { 40 // unused 41 } 42 43 /** 44 * Set an event for out-of-scope thread access. Unsafe: use only when known to be single threaded. 45 * 46 * @param event - the event to set 47 * @return The event set 48 */ 49 public static MuleEvent unsafeSetEvent(MuleEvent event) 50 { 51 documentUnsafeCall("unsafeSetEvent"); 52 return RequestContext.internalSetEvent(event); 53 } 54 55 /** 56 * Set an event for out-of-scope thread access. Critical: thread safety known to be required 57 * 58 * @param event - the event to set 59 * @return A new mutable copy of the event set 60 */ 61 public static MuleEvent criticalSetEvent(MuleEvent event) 62 { 63 return RequestContext.internalSetEvent(RequestContext.newEvent(event, RequestContext.SAFE)); 64 } 65 66 /** 67 * Sets a new message payload in the RequestContext but maintains all other 68 * properties (session, endpoint, synchronous, etc.) from the previous event. 69 * Unsafe: use only when known to be single threaded 70 * 71 * @param message - the new message payload 72 * @return The message set 73 */ 74 public static MuleMessage unsafeRewriteEvent(MuleMessage message) 75 { 76 documentUnsafeCall("unsafeRewriteEvent"); 77 return RequestContext.internalRewriteEvent(message, RequestContext.UNSAFE); 78 } 79 80 private static void documentUnsafeCall(String message) 81 { 82 if (DOCUMENT_UNSAFE_CALLS) 83 { 84 logger.debug(message, new Exception(message)); 85 } 86 } 87 88 }