Coverage Report - org.mule.RequestContext
 
Classes in this File Line Coverage Branch Coverage Complexity
RequestContext
0%
0/38
0%
0/18
0
 
 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  0
     private static final ThreadLocal currentEvent = new ThreadLocal();
 40  
 
 41  
     /** Do not instanciate. */
 42  
     protected RequestContext()
 43  0
     {
 44  
         // no-op
 45  0
     }
 46  
 
 47  
     public static MuleEventContext getEventContext()
 48  
     {
 49  0
         MuleEvent event = getEvent();
 50  0
         if (event != null)
 51  
         {
 52  0
             return new DefaultMuleEventContext(event);
 53  
         }
 54  
         else
 55  
         {
 56  0
             return null;
 57  
         }
 58  
     }
 59  
 
 60  
     public static MuleEvent getEvent()
 61  
     {
 62  0
         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  0
         return internalSetEvent(newEvent(event, DEFAULT_ACTION));
 74  
     }
 75  
 
 76  
     protected static MuleEvent internalSetEvent(MuleEvent event)
 77  
     {
 78  0
         currentEvent.set(event);
 79  0
         return event;
 80  
     }
 81  
 
 82  
     protected static MuleMessage internalRewriteEvent(MuleMessage message, boolean safe)
 83  
     {
 84  0
         if (message != null)
 85  
         {
 86  0
             MuleEvent event = getEvent();
 87  0
             if (event != null)
 88  
             {
 89  0
                 MuleMessage copy = newMessage(message, safe);
 90  0
                 MuleEvent newEvent = new DefaultMuleEvent(copy, event);
 91  0
                 if (safe)
 92  
                 {
 93  0
                     resetAccessControl(copy);
 94  
                 }
 95  0
                 internalSetEvent(newEvent);
 96  0
                 return copy;
 97  
             }
 98  
         }
 99  0
         return message;
 100  
     }
 101  
 
 102  
     /**
 103  
      * Resets the current request context (clears all information).
 104  
      */
 105  
     public static void clear()
 106  
     {
 107  0
         setEvent(null);
 108  0
     }
 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  0
         MuleEvent newEvent = newEvent(getEvent(), SAFE);
 118  0
         newEvent.getMessage().setExceptionPayload(exceptionPayload);
 119  0
         internalSetEvent(newEvent);
 120  0
     }
 121  
 
 122  
     public static ExceptionPayload getExceptionPayload()
 123  
     {
 124  0
         return getEvent().getMessage().getExceptionPayload();
 125  
     }
 126  
 
 127  
     public static MuleMessage safeMessageCopy(MuleMessage message)
 128  
     {
 129  0
         return newMessage(message, SAFE);
 130  
     }
 131  
 
 132  
     protected static MuleEvent newEvent(MuleEvent event, boolean safe)
 133  
     {
 134  0
         if (safe && event instanceof ThreadSafeAccess)
 135  
         {
 136  0
             return (MuleEvent) ((ThreadSafeAccess)event).newThreadCopy();
 137  
         }
 138  
         else
 139  
         {
 140  0
             return event;
 141  
         }
 142  
     }
 143  
 
 144  
     protected static MuleMessage newMessage(MuleMessage message, boolean safe)
 145  
     {
 146  0
         if (safe && message instanceof ThreadSafeAccess)
 147  
         {
 148  0
             return (MuleMessage) ((ThreadSafeAccess)message).newThreadCopy();
 149  
         }
 150  
         else
 151  
         {
 152  0
             return message;
 153  
         }
 154  
     }
 155  
 
 156  
     protected static void resetAccessControl(Object object)
 157  
     {
 158  0
         if (object instanceof ThreadSafeAccess)
 159  
         {
 160  0
             ((ThreadSafeAccess) object).resetAccessControl();
 161  
         }
 162  0
     }
 163  
 
 164  
 }