1 16 17 package org.apache.struts.chain; 18 19 20 import org.apache.commons.chain.Catalog; 21 import org.apache.commons.chain.CatalogFactory; 22 import org.apache.commons.chain.Command; 23 import org.apache.commons.chain.Context; 24 import org.apache.commons.chain.Filter; 25 import org.apache.commons.logging.Log; 26 import org.apache.commons.logging.LogFactory; 27 import org.apache.struts.chain.Constants; 28 29 30 39 40 public class ExceptionCatcher implements Filter { 41 42 43 45 46 private String catalogName = null; 47 private String exceptionCommand = null; 48 private String exceptionKey = Constants.EXCEPTION_KEY; 49 50 private static final Log log = LogFactory.getLog(ExceptionCatcher.class); 51 52 53 55 56 60 public String getCatalogName() { 61 62 return (this.catalogName); 63 64 } 65 66 67 73 public void setCatalogName(String catalogName) { 74 75 this.catalogName = catalogName; 76 77 } 78 79 80 84 public String getExceptionCommand() { 85 86 return (this.exceptionCommand); 87 88 } 89 90 91 97 public void setExceptionCommand(String exceptionCommand) { 98 99 this.exceptionCommand = exceptionCommand; 100 101 } 102 103 104 108 public String getExceptionKey() { 109 110 return (this.exceptionKey); 111 112 } 113 114 115 121 public void setExceptionKey(String exceptionKey) { 122 123 this.exceptionKey = exceptionKey; 124 125 } 126 127 128 130 131 139 public boolean execute(Context context) throws Exception { 140 141 context.remove(getExceptionKey()); 142 return (false); 143 144 } 145 146 147 158 public boolean postprocess(Context context, Exception exception) { 159 160 if (exception == null) { 162 return (false); 163 } 164 165 if (log.isDebugEnabled()) { 167 log.debug("Attempting to handle a thrown exception"); 168 } 169 context.put(getExceptionKey(), exception); 170 171 try { 173 String catalogName = getCatalogName(); 174 Catalog catalog = null; 175 if (catalogName == null) { 176 catalog = CatalogFactory.getInstance().getCatalog(); 177 if (catalog == null) { 178 log.error("Cannot find default catalog"); 179 throw new IllegalArgumentException 180 ("Cannot find default catalog"); 181 } 182 } else { 183 catalog = CatalogFactory.getInstance().getCatalog(catalogName); 184 if (catalog == null) { 185 log.error("Cannot find catalog '" + catalogName + "'"); 186 throw new IllegalArgumentException 187 ("Cannot find catalog '" + catalogName + "'"); 188 } 189 } 190 String exceptionCommand = getExceptionCommand(); 191 if (exceptionCommand == null) { 192 log.error("No exceptionCommand property specified"); 193 throw new IllegalStateException 194 ("No exceptionCommand property specfied"); 195 } 196 Command command = catalog.getCommand(exceptionCommand); 197 if (command == null) { 198 log.error("Cannot find exceptionCommand '" + 199 exceptionCommand + "'"); 200 throw new IllegalStateException 201 ("Cannot find exceptionCommand '" + 202 exceptionCommand + "'"); 203 } 204 if (log.isTraceEnabled()) { 205 log.trace("Calling exceptionCommand '" + exceptionCommand 206 + "'"); 207 } 208 command.execute(context); 209 } catch (Exception e) { 210 log.warn("Exception from exceptionCommand '" + 211 exceptionCommand + "'", e); 212 throw new IllegalStateException ("Exception chain threw exception"); 213 } 214 return (true); 215 216 } 217 218 219 220 } 221 | Popular Tags |