KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mule > impl > RequestContext


1 /*
2  * $Id: RequestContext.java 4259 2006-12-14 03:12:07Z aperepel $
3  * --------------------------------------------------------------------------------------
4  * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
5  *
6  * The software in this package is published under the terms of the MuleSource MPL
7  * license, a copy of which has been included with this distribution in the
8  * LICENSE.txt file.
9  */

10
11 package org.mule.impl;
12
13 import java.util.Iterator JavaDoc;
14
15 import org.apache.commons.logging.Log;
16 import org.apache.commons.logging.LogFactory;
17 import org.mule.config.MuleProperties;
18 import org.mule.umo.UMOEvent;
19 import org.mule.umo.UMOEventContext;
20 import org.mule.umo.UMOExceptionPayload;
21 import org.mule.umo.UMOMessage;
22
23 /**
24  * <code>RequestContext</code> is a thread context where components can get the
25  * current event or set response properties that will be sent on the outgoing
26  * message.
27  */

28 public class RequestContext
29 {
30     private static final Log logger = LogFactory.getLog(RequestContext.class);
31     private static final ThreadLocal JavaDoc currentEvent = new ThreadLocal JavaDoc();
32
33     public static UMOEventContext getEventContext()
34     {
35         UMOEvent event = getEvent();
36         if (event != null)
37         {
38             return new MuleEventContext(event);
39         }
40         else
41         {
42             return null;
43         }
44     }
45
46     public static UMOEvent getEvent()
47     {
48         return (UMOEvent)currentEvent.get();
49     }
50
51     public static void setEvent(UMOEvent event)
52     {
53         currentEvent.set(event);
54     }
55
56     /**
57      * Sets a new message payload in the RequestContext but maintains all other
58      * properties (session, endpoint, synchronous, etc.) from the previous event.
59      *
60      * @param message - current message payload
61      */

62     public static void rewriteEvent(UMOMessage message)
63     {
64         if (message != null)
65         {
66             UMOEvent event = getEvent();
67             if (event != null)
68             {
69                 event = new MuleEvent(message, event);
70                 setEvent(event);
71             }
72         }
73     }
74
75     public static void writeResponse(UMOMessage message)
76     {
77         if (message != null)
78         {
79             UMOEvent event = getEvent();
80             if (event != null)
81             {
82                 for (Iterator JavaDoc iterator = event.getMessage().getPropertyNames().iterator(); iterator.hasNext();)
83                 {
84                     String JavaDoc key = (String JavaDoc)iterator.next();
85                     if (key == null)
86                     {
87                         logger.warn("Message property key is null: please report the following stack trace to dev@mule.codehaus.org.",
88                             new IllegalArgumentException JavaDoc());
89                     }
90                     else
91                     {
92                         if (key.startsWith(MuleProperties.PROPERTY_PREFIX))
93                         {
94                             Object JavaDoc newValue = message.getProperty(key);
95                             Object JavaDoc oldValue = event.getMessage().getProperty(key);
96                             if (newValue == null)
97                             {
98                                 message.setProperty(key, oldValue);
99                             }
100                             else if (logger.isInfoEnabled() && !newValue.equals(oldValue))
101                             {
102                                 logger.info("Message already contains property " + key + "=" + newValue
103                                             + " not replacing old value: " + oldValue);
104                             }
105                         }
106                     }
107                 }
108
109                 event = new MuleEvent(message, event.getEndpoint(), event.getSession(), event.isSynchronous());
110                 setEvent(event);
111             }
112         }
113     }
114
115     /**
116      * Resets the current request context (clears all information).
117      */

118     public static void clear()
119     {
120         setEvent(null);
121     }
122
123     public static void setExceptionPayload(UMOExceptionPayload exceptionPayload)
124     {
125         getEvent().getMessage().setExceptionPayload(exceptionPayload);
126     }
127
128     public static UMOExceptionPayload getExceptionPayload()
129     {
130         return getEvent().getMessage().getExceptionPayload();
131     }
132
133 }
134
Popular Tags