001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019:
020: package org.apache.synapse.mediators;
021:
022: import org.apache.synapse.*;
023: import org.apache.synapse.mediators.base.SequenceMediator;
024: import org.apache.commons.logging.Log;
025: import org.apache.commons.logging.LogFactory;
026:
027: /**
028: * This implements the FaultHandler interface as a mediator fault handler. That is the fault handler is
029: * specified by a sequence and this handler implements the logic of handling the fault through the set
030: * of mediators present in the sequence.
031: *
032: * @see org.apache.synapse.FaultHandler
033: */
034: public class MediatorFaultHandler extends FaultHandler {
035:
036: private static final Log log = LogFactory
037: .getLog(MediatorFaultHandler.class);
038: private static final Log trace = LogFactory
039: .getLog(SynapseConstants.TRACE_LOGGER);
040:
041: /**
042: * This holds the fault sequence for the mediator fault handler
043: */
044: private Mediator faultMediator = null;
045:
046: /**
047: * Constructs the FaultHandler object for handling mediator faults
048: *
049: * @param faultMediator Mediator in which fault sequence is specified
050: */
051: public MediatorFaultHandler(Mediator faultMediator) {
052:
053: this .faultMediator = faultMediator;
054: }
055:
056: /**
057: * Implements the fault handling method for the mediators (basically sequences)
058: *
059: * @param synCtx Synapse Message Context of which mediation occurs
060: * @throws SynapseException in case there is a failure in the fault execution
061: * @see org.apache.synapse.FaultHandler#handleFault(org.apache.synapse.MessageContext)
062: */
063: public void onFault(MessageContext synCtx) throws SynapseException {
064:
065: boolean traceOn = synCtx.getTracingState() == SynapseConstants.TRACING_ON;
066: boolean traceOrDebugOn = traceOn || log.isDebugEnabled();
067:
068: String name = null;
069: if (faultMediator instanceof SequenceMediator) {
070: name = ((SequenceMediator) faultMediator).getName();
071: }
072: if (name == null) {
073: name = faultMediator.getClass().getName();
074: }
075:
076: if (traceOrDebugOn) {
077: traceOrDebugWarn(traceOn,
078: "Executing fault handler mediator : " + name);
079: }
080:
081: synCtx.getServiceLog().warn(
082: "Executing fault sequence mediator : " + name);
083: this .faultMediator.mediate(synCtx);
084: }
085:
086: /**
087: * Getter for the mediator describing the fault sequence
088: *
089: * @return Mediator specifying the fault sequence for mediator fault handler
090: */
091: public Mediator getFaultMediator() {
092: return faultMediator;
093: }
094:
095: /**
096: * Setter of the mediator describing the fault sequence
097: *
098: * @param faultMediator Mediator specifying the fault sequence to be used by the handler
099: */
100: public void setFaultMediator(Mediator faultMediator) {
101: this .faultMediator = faultMediator;
102: }
103:
104: private void traceOrDebugWarn(boolean traceOn, String msg) {
105: if (traceOn) {
106: trace.warn(msg);
107: }
108: log.warn(msg);
109: }
110:
111: }
|