01: /*
02: * $Id: OptimizedRequestContext.java 10489 2008-01-23 17:53:38Z dfeist $
03: * --------------------------------------------------------------------------------------
04: * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
05: *
06: * The software in this package is published under the terms of the CPAL v1.0
07: * license, a copy of which has been included with this distribution in the
08: * LICENSE.txt file.
09: */
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: private static final boolean DOCUMENT_UNSAFE_CALLS = false;
32: private static final Log logger = LogFactory
33: .getLog(OptimizedRequestContext.class);
34:
35: /**
36: * Do not instantiate.
37: */
38: private OptimizedRequestContext() {
39: // unused
40: }
41:
42: /**
43: * Set an event for out-of-scope thread access. Unsafe: use only when known to be single threaded.
44: *
45: * @param event - the event to set
46: * @return The event set
47: */
48: public static MuleEvent unsafeSetEvent(MuleEvent event) {
49: documentUnsafeCall("unsafeSetEvent");
50: return RequestContext.internalSetEvent(event);
51: }
52:
53: /**
54: * Set an event for out-of-scope thread access. Critical: thread safety known to be required
55: *
56: * @param event - the event to set
57: * @return A new mutable copy of the event set
58: */
59: public static MuleEvent criticalSetEvent(MuleEvent event) {
60: return RequestContext.internalSetEvent(RequestContext.newEvent(
61: event, RequestContext.SAFE));
62: }
63:
64: /**
65: * Sets a new message payload in the RequestContext but maintains all other
66: * properties (session, endpoint, synchronous, etc.) from the previous event.
67: * Unsafe: use only when known to be single threaded
68: *
69: * @param message - the new message payload
70: * @return The message set
71: */
72: public static MuleMessage unsafeRewriteEvent(MuleMessage message) {
73: documentUnsafeCall("unsafeRewriteEvent");
74: return RequestContext.internalRewriteEvent(message,
75: RequestContext.UNSAFE);
76: }
77:
78: private static void documentUnsafeCall(String message) {
79: if (DOCUMENT_UNSAFE_CALLS) {
80: logger.debug(message, new Exception(message));
81: }
82: }
83:
84: }
|