1 16 17 package org.apache.struts.chain; 18 19 20 import org.apache.commons.chain.Command; 21 import org.apache.commons.chain.Context; 22 import org.apache.commons.logging.Log; 23 import org.apache.commons.logging.LogFactory; 24 import org.apache.struts.chain.Constants; 25 import org.apache.struts.config.ActionConfig; 26 import org.apache.struts.config.ExceptionConfig; 27 import org.apache.struts.config.ForwardConfig; 28 import org.apache.struts.config.ModuleConfig; 29 30 31 38 39 public abstract class AbstractExceptionHandler implements Command { 40 41 42 44 45 private String actionConfigKey = Constants.ACTION_CONFIG_KEY; 46 private String exceptionKey = Constants.EXCEPTION_KEY; 47 private String forwardConfigKey = Constants.FORWARD_CONFIG_KEY; 48 private String moduleConfigKey = Constants.MODULE_CONFIG_KEY; 49 50 private static final Log log = 51 LogFactory.getLog(AbstractExceptionHandler.class); 52 53 54 56 57 62 public String getActionConfigKey() { 63 64 return (this.actionConfigKey); 65 66 } 67 68 69 76 public void setActionConfigKey(String actionConfigKey) { 77 78 this.actionConfigKey = actionConfigKey; 79 80 } 81 82 83 87 public String getExceptionKey() { 88 89 return (this.exceptionKey); 90 91 } 92 93 94 100 public void setExceptionKey(String exceptionKey) { 101 102 this.exceptionKey = exceptionKey; 103 104 } 105 106 107 112 public String getForwardConfigKey() { 113 114 return (this.forwardConfigKey); 115 116 } 117 118 119 126 public void setForwardConfigKey(String forwardConfigKey) { 127 128 this.forwardConfigKey = forwardConfigKey; 129 130 } 131 132 133 138 public String getModuleConfigKey() { 139 140 return (this.moduleConfigKey); 141 142 } 143 144 145 152 public void setModuleConfigKey(String moduleConfigKey) { 153 154 this.moduleConfigKey = moduleConfigKey; 155 156 } 157 158 159 161 162 174 public boolean execute(Context context) throws Exception { 175 176 Exception exception = (Exception ) 178 context.get(getExceptionKey()); 179 if (exception == null) { 180 log.warn("No Exception found under key '" + 181 getExceptionKey() + "'"); 182 return (true); 183 } 184 185 ExceptionConfig exceptionConfig = null; 187 ActionConfig actionConfig = (ActionConfig) 188 context.get(getActionConfigKey()); 189 ModuleConfig moduleConfig = (ModuleConfig) 190 context.get(getModuleConfigKey()); 191 192 193 if (actionConfig != null) { 194 log.debug("See if actionConfig " + actionConfig + " has an exceptionConfig for " + exception.getClass().getName()); 195 exceptionConfig = 196 actionConfig.findException(exception.getClass()); 197 } 198 199 if (exceptionConfig == null) { 201 log.warn("Unhandled exception", exception); 202 throw exception; 203 } 204 ForwardConfig forwardConfig = 205 handle(context, exception, exceptionConfig, 206 actionConfig, moduleConfig); 207 if (forwardConfig != null) { 208 context.put(getForwardConfigKey(), forwardConfig); 209 return (false); 210 } else { 211 return (true); 212 } 213 214 } 215 216 217 219 220 232 protected abstract ForwardConfig handle(Context context, 233 Exception exception, 234 ExceptionConfig exceptionConfig, 235 ActionConfig actionConfig, 236 ModuleConfig moduleConfig) 237 throws Exception ; 238 239 } 240 | Popular Tags |