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.ExceptionPayload;
10  import org.mule.api.MuleEvent;
11  import org.mule.api.MuleEventContext;
12  import org.mule.api.MuleMessage;
13  import org.mule.api.ThreadSafeAccess;
14  
15  /**
16   * <code>RequestContext</code> is a thread context where components can get the
17   * current event or set response properties that will be sent on the outgoing
18   * message.
19   *
20   * <p>RequestContext seems to be used to allow thread local mutation of events that
21   * are not otherwise available in the scope.  so this is a good place to create a new
22   * thread local copy - it will be read because supporting code is expecting mutation.</p>
23   *
24   * @deprecated
25   *    If access to MuleEvent or MuleMessage is required,
26   *    then implement a {@link org.mule.api.processor.MessageProcessor}
27   *    or {@link org.mule.api.lifecycle.Callable} instead
28   */
29  @Deprecated
30  public final class RequestContext
31  {
32      // to clarify "safe" in constructors
33      public static final boolean SAFE = true;
34      public static final boolean UNSAFE = true;
35  
36      // setting this to false gives old (mutable) semantics in non-critical cases
37      private static final boolean DEFAULT_ACTION = SAFE;
38  
39      private static final ThreadLocal currentEvent = new ThreadLocal();
40  
41      /** Do not instanciate. */
42      protected RequestContext()
43      {
44          // no-op
45      }
46  
47      public static MuleEventContext getEventContext()
48      {
49          MuleEvent event = getEvent();
50          if (event != null)
51          {
52              return new DefaultMuleEventContext(event);
53          }
54          else
55          {
56              return null;
57          }
58      }
59  
60      public static MuleEvent getEvent()
61      {
62          return (MuleEvent) currentEvent.get();
63      }
64  
65      /**
66       * Set an event for out-of-scope thread access.  Safe: use by default
67       *
68       * @param event - the event to set
69       * @return A new mutable copy of the event set
70       */
71      public static MuleEvent setEvent(MuleEvent event)
72      {
73          return internalSetEvent(newEvent(event, DEFAULT_ACTION));
74      }
75  
76      protected static MuleEvent internalSetEvent(MuleEvent event)
77      {
78          currentEvent.set(event);
79          return event;
80      }
81  
82      protected static MuleMessage internalRewriteEvent(MuleMessage message, boolean safe)
83      {
84          if (message != null)
85          {
86              MuleEvent event = getEvent();
87              if (event != null)
88              {
89                  MuleMessage copy = newMessage(message, safe);
90                  MuleEvent newEvent = new DefaultMuleEvent(copy, event);
91                  if (safe)
92                  {
93                      resetAccessControl(copy);
94                  }
95                  internalSetEvent(newEvent);
96                  return copy;
97              }
98          }
99          return message;
100     }
101 
102     /**
103      * Resets the current request context (clears all information).
104      */
105     public static void clear()
106     {
107         setEvent(null);
108     }
109 
110     /**
111      * There is no unsafe version of this because it shouldn't be performance critical
112      *
113      * @param exceptionPayload
114      */
115     public static void setExceptionPayload(ExceptionPayload exceptionPayload)
116     {
117         MuleEvent newEvent = newEvent(getEvent(), SAFE);
118         newEvent.getMessage().setExceptionPayload(exceptionPayload);
119         internalSetEvent(newEvent);
120     }
121 
122     public static ExceptionPayload getExceptionPayload()
123     {
124         return getEvent().getMessage().getExceptionPayload();
125     }
126 
127     public static MuleMessage safeMessageCopy(MuleMessage message)
128     {
129         return newMessage(message, SAFE);
130     }
131 
132     protected static MuleEvent newEvent(MuleEvent event, boolean safe)
133     {
134         if (safe && event instanceof ThreadSafeAccess)
135         {
136             return (MuleEvent) ((ThreadSafeAccess)event).newThreadCopy();
137         }
138         else
139         {
140             return event;
141         }
142     }
143 
144     protected static MuleMessage newMessage(MuleMessage message, boolean safe)
145     {
146         if (safe && message instanceof ThreadSafeAccess)
147         {
148             return (MuleMessage) ((ThreadSafeAccess)message).newThreadCopy();
149         }
150         else
151         {
152             return message;
153         }
154     }
155 
156     protected static void resetAccessControl(Object object)
157     {
158         if (object instanceof ThreadSafeAccess)
159         {
160             ((ThreadSafeAccess) object).resetAccessControl();
161         }
162     }
163 
164 }