1 16 package scriptella.core; 17 18 import scriptella.configuration.OnErrorEl; 19 import scriptella.configuration.ScriptEl; 20 import scriptella.spi.ProviderException; 21 import scriptella.util.ExceptionUtils; 22 23 import java.util.ArrayList ; 24 import java.util.Collections ; 25 import java.util.List ; 26 import java.util.Set ; 27 import java.util.regex.Pattern ; 28 29 38 public class OnErrorHandler { 39 private List <OnErrorEl> onerrorElements; 40 42 47 public OnErrorHandler(ScriptEl scriptEl) { 48 List <OnErrorEl> onerrorElements = scriptEl.getOnerrorElements(); 49 if (onerrorElements != null && onerrorElements.size() > 0) { 50 this.onerrorElements = new ArrayList <OnErrorEl>(onerrorElements); 51 } 52 } 53 54 62 public OnErrorEl onError(Throwable throwable) { 63 if (onerrorElements == null) { 64 return null; 65 } 66 for (Throwable t = throwable; t != null; t = ExceptionUtils.getCause(t)) { 68 for (OnErrorEl onErrorEl : onerrorElements) { 70 Pattern type = onErrorEl.getType(); 71 if (type != null && !type.matcher(t.getClass().getName()).matches()) { 72 continue; 73 } 74 Pattern msg = onErrorEl.getMessage(); 75 if (msg != null && (t.getMessage() == null || !msg.matcher(t.getMessage()).matches())) { 77 continue; 78 } 79 Set <String > codes = onErrorEl.getCodes(); 80 if (codes != null && !codes.isEmpty()) { 81 Set <String > errorCodes = getErrorCodes(t); 82 boolean match = false; 83 for (String ec : errorCodes) { 85 if (codes.contains(ec)) { 86 match = true; 87 break; 88 } 89 } 90 if (!match) { 91 continue; 92 } 93 } 94 onerrorElements.remove(onErrorEl); 96 return onErrorEl; 97 } 98 } 99 return null; 100 101 } 102 103 107 protected Set <String > getErrorCodes(Throwable t) { 108 if (t instanceof ProviderException) { 109 return ((ProviderException) t).getErrorCodes(); 110 } 111 return Collections.emptySet(); 112 } 113 114 115 } 116 | Popular Tags |