001: /*
002: * $Id: AbstractExceptionHandler.java 471754 2006-11-06 14:55:09Z husted $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts.chain.commands;
022:
023: import org.apache.commons.logging.Log;
024: import org.apache.commons.logging.LogFactory;
025: import org.apache.struts.chain.contexts.ActionContext;
026: import org.apache.struts.config.ActionConfig;
027: import org.apache.struts.config.ExceptionConfig;
028: import org.apache.struts.config.ForwardConfig;
029: import org.apache.struts.config.ModuleConfig;
030:
031: /**
032: * <p>Invoke the local or global exception handler configured for the
033: * exception class that occurred.</p>
034: *
035: * @version $Rev: 471754 $ $Date: 2005-11-12 13:01:44 -0500 (Sat, 12 Nov 2005)
036: * $
037: */
038: public abstract class AbstractExceptionHandler extends
039: ActionCommandBase {
040: // ------------------------------------------------------ Instance Variables
041:
042: /**
043: * Provide a Commons logging instance for this class.
044: */
045: private static final Log LOG = LogFactory
046: .getLog(AbstractExceptionHandler.class);
047:
048: // ---------------------------------------------------------- Public Methods
049:
050: /**
051: * <p>Invoke the appropriate <code>Action</code> for this request, and
052: * cache the returned <code>ActionForward</code>.</p>
053: *
054: * @param actionCtx The <code>Context</code> for the current request
055: * @return <code>false</code> if a <code>ForwardConfig</code> is returned,
056: * else <code>true</code> to complete processing
057: * @throws Exception if thrown by the Action class and not declared by an
058: * Exception Handler
059: */
060: public boolean execute(ActionContext actionCtx) throws Exception {
061: // Look up the exception that was thrown
062: Exception exception = actionCtx.getException();
063:
064: if (exception == null) {
065: LOG.warn("No Exception found in ActionContext");
066:
067: return (true);
068: }
069:
070: // Look up the local or global exception handler configuration
071: ExceptionConfig exceptionConfig = null;
072: ActionConfig actionConfig = actionCtx.getActionConfig();
073: ModuleConfig moduleConfig = actionCtx.getModuleConfig();
074:
075: if (actionConfig != null) {
076: if (LOG.isDebugEnabled()) {
077: LOG.debug("See if actionConfig " + actionConfig
078: + " has an exceptionConfig for "
079: + exception.getClass().getName());
080: }
081:
082: exceptionConfig = actionConfig.findException(exception
083: .getClass());
084: } else if (moduleConfig != null) {
085: if (LOG.isDebugEnabled()) {
086: LOG.debug("No action yet, see if moduleConfig "
087: + moduleConfig + " has an exceptionConfig "
088: + exception.getClass().getName());
089: }
090:
091: exceptionConfig = moduleConfig.findException(exception
092: .getClass());
093: }
094:
095: // Handle the exception in the configured manner
096: if (exceptionConfig == null) {
097: LOG.warn("Unhandled exception", exception);
098: throw exception;
099: }
100:
101: ForwardConfig forwardConfig = handle(actionCtx, exception,
102: exceptionConfig, actionConfig, moduleConfig);
103:
104: if (forwardConfig != null) {
105: actionCtx.setForwardConfig(forwardConfig);
106:
107: return (false);
108: } else {
109: return (true);
110: }
111: }
112:
113: // ------------------------------------------------------- Protected Methods
114:
115: /**
116: * <p>Perform the required handling of the specified exception.</p>
117: *
118: * @param context The <code>Context</code> for this request
119: * @param exception The exception being handled
120: * @param exceptionConfig The corresponding {@link ExceptionConfig}
121: * @param actionConfig The {@link ActionConfig} for this request
122: * @param moduleConfig The {@link ModuleConfig} for this request
123: * @return the <code>ForwardConfig</code> to be processed next (if any),
124: * or <code>null</code> if processing has been completed
125: * @throws Exception if there are any problems handling the exception
126: */
127: protected abstract ForwardConfig handle(ActionContext context,
128: Exception exception, ExceptionConfig exceptionConfig,
129: ActionConfig actionConfig, ModuleConfig moduleConfig)
130: throws Exception;
131: }
|