| 1 |
/* |
| 2 |
* Copyright 2001-2004 The Apache Software Foundation. |
| 3 |
* |
| 4 |
* Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 |
* you may not use this file except in compliance with the License. |
| 6 |
* You may obtain a copy of the License at |
| 7 |
* |
| 8 |
* http://www.apache.org/licenses/LICENSE-2.0 |
| 9 |
* |
| 10 |
* Unless required by applicable law or agreed to in writing, software |
| 11 |
* distributed under the License is distributed on an "AS IS" BASIS, |
| 12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 |
* See the License for the specific language governing permissions and |
| 14 |
* limitations under the License. |
| 15 |
*/ |
| 16 |
|
| 17 |
package org.apache.axis ; |
| 18 |
|
| 19 |
import org.apache.axis.components.logger.LogFactory; |
| 20 |
import org.apache.axis.handlers.BasicHandler; |
| 21 |
import org.apache.axis.utils.Messages; |
| 22 |
import org.apache.commons.logging.Log; |
| 23 |
|
| 24 |
import javax.xml.namespace.QName; |
| 25 |
import java.util.Enumeration; |
| 26 |
import java.util.Hashtable; |
| 27 |
|
| 28 |
/** |
| 29 |
* A <code>FaultableHandler</code> is essentially a wrapper for any other |
| 30 |
* Handler which provides flexible fault handling semantics. |
| 31 |
* |
| 32 |
* |
| 33 |
* @author Doug Davis ([email protected]) |
| 34 |
* @author Glen Daniels ([email protected]) |
| 35 |
*/ |
| 36 |
public class FaultableHandler extends BasicHandler { |
| 37 |
/** |
| 38 |
* The <code>Log</code> used to log all events that would be of general |
| 39 |
* interest. |
| 40 |
*/ |
| 41 |
protected static Log log = |
| 42 |
LogFactory.getLog(FaultableHandler.class.getName()); |
| 43 |
|
| 44 |
/** |
| 45 |
* The <code>Log</code> used for enterprise-centric logging. |
| 46 |
* |
| 47 |
* The enterprise category is for stuff that an enterprise product might |
| 48 |
* want to track, but in a simple environment (like the AXIS build) would |
| 49 |
* be nothing more than a nuisance. |
| 50 |
*/ |
| 51 |
protected static Log entLog = |
| 52 |
LogFactory.getLog(Constants.ENTERPRISE_LOG_CATEGORY); |
| 53 |
|
| 54 |
/** |
| 55 |
* The <code>Handler</code> that will do the actual work of handeling the |
| 56 |
* fault. |
| 57 |
*/ |
| 58 |
protected Handler workHandler ; |
| 59 |
|
| 60 |
/** |
| 61 |
* Create a new FaultHandler. |
| 62 |
* |
| 63 |
* @param workHandler the Handler we're going to wrap with Fault semantics. |
| 64 |
*/ |
| 65 |
public FaultableHandler(Handler workHandler) |
| 66 |
{ |
| 67 |
this.workHandler = workHandler; |
| 68 |
} |
| 69 |
|
| 70 |
public void init() { |
| 71 |
workHandler.init(); |
| 72 |
} |
| 73 |
|
| 74 |
public void cleanup() { |
| 75 |
workHandler.cleanup(); |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Invokes the specified handler. If there's a fault the appropriate |
| 80 |
* key will be calculated and used to find the fault chain to be |
| 81 |
* invoked. This assumes that the workHandler has caught the exception |
| 82 |
* and already done its fault processing - as needed. |
| 83 |
* |
| 84 |
* @param msgContext the <code>MessageContext</code> to process |
| 85 |
* @throws AxisFault if anything goes terminally wrong |
| 86 |
*/ |
| 87 |
public void invoke(MessageContext msgContext) throws AxisFault { |
| 88 |
log.debug("Enter: FaultableHandler::invoke"); |
| 89 |
try { |
| 90 |
workHandler.invoke( msgContext ); |
| 91 |
} |
| 92 |
catch( Exception e ) { |
| 93 |
entLog.info(Messages.getMessage("toAxisFault00"), e ); |
| 94 |
AxisFault fault = AxisFault.makeFault(e); |
| 95 |
|
| 96 |
// AxisEngine engine = msgContext.getAxisEngine(); |
| 97 |
|
| 98 |
/** Index off fault code. |
| 99 |
* |
| 100 |
* !!! TODO: This needs to be able to handle searching by faultcode |
| 101 |
* hierarchy, i.e. "Server.General.*" or "Server.*", with the |
| 102 |
* most specific match winning. |
| 103 |
*/ |
| 104 |
/* |
| 105 |
QFault key = fault.getFaultCode() ; |
| 106 |
Handler faultHandler = (Handler) faultHandlers.get( key ); |
| 107 |
*/ |
| 108 |
Handler faultHandler = null; |
| 109 |
|
| 110 |
Hashtable options = getOptions(); |
| 111 |
if (options != null) { |
| 112 |
Enumeration enumeration = options.keys(); |
| 113 |
while (enumeration.hasMoreElements()) { |
| 114 |
String s = (String) enumeration.nextElement(); |
| 115 |
if (s.equals("fault-" + fault.getFaultCode().getLocalPart())) { |
| 116 |
faultHandler = (Handler)options.get(s); |
| 117 |
} |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
if ( faultHandler != null ) { |
| 122 |
/** faultHandler will (re)throw if it's appropriate, but it |
| 123 |
* might also eat the fault. Which brings up another issue - |
| 124 |
* should we have a way to pass the Fault directly to the |
| 125 |
* faultHandler? Maybe another well-known MessageContext |
| 126 |
* property? |
| 127 |
*/ |
| 128 |
faultHandler.invoke( msgContext ); |
| 129 |
} else { |
| 130 |
throw fault; |
| 131 |
} |
| 132 |
} |
| 133 |
log.debug("Exit: FaultableHandler::invoke"); |
| 134 |
} |
| 135 |
|
| 136 |
/** |
| 137 |
* Some handler later on has faulted so we need to process the fault. |
| 138 |
* |
| 139 |
* @param msgContext the context to process |
| 140 |
*/ |
| 141 |
public void onFault(MessageContext msgContext) { |
| 142 |
log.debug("Enter: FaultableHandler::onFault"); |
| 143 |
workHandler.onFault( msgContext ); |
| 144 |
log.debug("Exit: FaultableHandler::onFault"); |
| 145 |
} |
| 146 |
|
| 147 |
public boolean canHandleBlock(QName qname) { |
| 148 |
return( workHandler.canHandleBlock(qname) ); |
| 149 |
} |
| 150 |
}; |