001: /*
002: * $Id: DefaultServiceExceptionStrategy.java 11373 2008-03-15 05:03:10Z dfeist $
003: * --------------------------------------------------------------------------------------
004: * Copyright (c) MuleSource, Inc. All rights reserved. http://www.mulesource.com
005: *
006: * The software in this package is published under the terms of the CPAL v1.0
007: * license, a copy of which has been included with this distribution in the
008: * LICENSE.txt file.
009: */
010:
011: package org.mule.service;
012:
013: import org.mule.DefaultExceptionStrategy;
014: import org.mule.RequestContext;
015: import org.mule.api.MuleEvent;
016: import org.mule.api.MuleMessage;
017: import org.mule.api.endpoint.ImmutableEndpoint;
018: import org.mule.api.service.Service;
019: import org.mule.component.DefaultLifecycleAdapter;
020: import org.mule.management.stats.ServiceStatistics;
021:
022: /**
023: * <code>DefaultServiceExceptionStrategy</code> is the default exception handler
024: * for components. The handler logs errors and will forward the message and exception
025: * to an exception endpointUri if one is set on this Exception strategy
026: */
027: public class DefaultServiceExceptionStrategy extends
028: DefaultExceptionStrategy {
029: /**
030: * The service to which the Exception handler belongs
031: */
032: protected Service service;
033:
034: protected ServiceStatistics statistics;
035:
036: public DefaultServiceExceptionStrategy() {
037: super ();
038: }
039:
040: /**
041: * Constructor
042: *
043: * @param service the owner of this exception strategy
044: * @see DefaultLifecycleAdapter
045: */
046: public DefaultServiceExceptionStrategy(Service service) {
047: super ();
048: setService(service);
049: }
050:
051: /**
052: * @return the UniversalMessageObject to which this handler is attached
053: */
054: public Service getService() {
055: return service;
056: }
057:
058: protected void defaultHandler(Throwable t) {
059: // Lazy initialisation of the service
060: // This strategy should be associated with only one service
061: // and thus there is no concurrency problem
062: if (service == null) {
063: MuleEvent event = RequestContext.getEvent();
064: if (event == null) {
065: // very bad should not happen
066: logger
067: .fatal("The Default Service Exception Strategy has been invoked but there is no current event on the context");
068: logger.fatal("The error is: " + t.getMessage(), t);
069: } else {
070: setService(event.getService());
071: }
072: }
073:
074: if (statistics != null) {
075: statistics.incExecutionError();
076: }
077:
078: super .defaultHandler(t);
079: }
080:
081: protected void logFatal(MuleMessage message, Throwable t) {
082: super .logFatal(message, t);
083: if (statistics != null) {
084: statistics.incFatalError();
085: }
086: }
087:
088: protected void routeException(MuleMessage message,
089: ImmutableEndpoint failedEndpoint, Throwable t) {
090: ImmutableEndpoint ep = getEndpoint(t);
091: if (ep != null) {
092: super .routeException(message, failedEndpoint, t);
093: if (statistics != null) {
094: statistics.getOutboundRouterStat()
095: .incrementRoutedMessage(ep);
096: }
097: }
098: }
099:
100: public void setService(Service service) {
101: this .service = service;
102: if (service instanceof AbstractService) {
103: this .statistics = ((AbstractService) service)
104: .getStatistics();
105: }
106: }
107: }
|