KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * $Id: DefaultComponentExceptionStrategy.java 3798 2006-11-04 04:07:14Z 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 org.mule.impl.model.AbstractComponent;
14 import org.mule.management.stats.ComponentStatistics;
15 import org.mule.umo.UMOComponent;
16 import org.mule.umo.UMOEvent;
17 import org.mule.umo.UMOMessage;
18 import org.mule.umo.endpoint.UMOEndpoint;
19 import org.mule.umo.endpoint.UMOImmutableEndpoint;
20
21 /**
22  * <code>DefaultComponentExceptionStrategy</code> is the default exception handler
23  * for components. The handler logs errors and will forward the message and exception
24  * to an exception endpointUri if one is set on this Exception strategy
25  *
26  * @author <a HREF="mailto:ross.mason@symphonysoft.com">Ross Mason</a>
27  * @version $Revision: 3798 $
28  */

29 public class DefaultComponentExceptionStrategy extends DefaultExceptionStrategy
30 {
31     /**
32      * The component to which the Exception handler belongs
33      */

34     protected UMOComponent component;
35
36     protected ComponentStatistics statistics;
37
38     public DefaultComponentExceptionStrategy()
39     {
40         super();
41     }
42
43     /**
44      * Constructor
45      *
46      * @param component the owner of this exception strategy
47      * @see DefaultLifecycleAdapter
48      */

49     public DefaultComponentExceptionStrategy(UMOComponent component)
50     {
51         super();
52         setComponent(component);
53     }
54
55     /**
56      * @return the UniversalMessageObject to which this handler is attached
57      */

58     public UMOComponent getComponent()
59     {
60         return component;
61     }
62
63     protected void defaultHandler(Throwable JavaDoc t)
64     {
65         // Lazzy initialisation of the component
66
// This strategy should be associated with only one component
67
// and thus there is no concurrency problem
68
if (component == null)
69         {
70             UMOEvent event = RequestContext.getEvent();
71             if (event == null)
72             { // very bad should not happen
73
logger.fatal("The Default Component Exception Strategy has been invoked but there is no current event on the context");
74                 logger.fatal("The error is: " + t.getMessage(), t);
75             }
76             else
77             {
78                 setComponent(event.getComponent());
79             }
80         }
81
82         if (statistics != null)
83         {
84             statistics.incExecutionError();
85         }
86
87         if (component != null)
88         {
89             logger.error("Caught exception in Exception Strategy for: " + component.getDescriptor().getName()
90                          + ": " + t, t);
91         }
92         super.defaultHandler(t);
93
94     }
95
96     protected void logFatal(UMOMessage message, Throwable JavaDoc t)
97     {
98         super.logFatal(message, t);
99         if (statistics != null)
100         {
101             statistics.incFatalError();
102         }
103     }
104
105     protected void routeException(UMOMessage message, UMOImmutableEndpoint failedEndpoint, Throwable JavaDoc t)
106     {
107         UMOEndpoint ep = getEndpoint(t);
108         if (ep != null)
109         {
110             super.routeException(message, failedEndpoint, t);
111             if (statistics != null)
112             {
113                 statistics.getOutboundRouterStat().incrementRoutedMessage(ep);
114             }
115         }
116     }
117
118     public void setComponent(UMOComponent component)
119     {
120         this.component = component;
121         if (component instanceof AbstractComponent)
122         {
123             if (statistics != null)
124             {
125                 this.statistics = ((AbstractComponent)component).getStatistics();
126             }
127         }
128     }
129 }
130
Popular Tags