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