1 16 package scriptella.core; 17 18 import scriptella.configuration.ContentEl; 19 import scriptella.configuration.OnErrorEl; 20 import scriptella.configuration.ScriptEl; 21 import scriptella.spi.Connection; 22 import scriptella.spi.DialectIdentifier; 23 import scriptella.spi.Resource; 24 import scriptella.util.ExceptionUtils; 25 import scriptella.util.StringUtils; 26 27 import java.util.logging.Level ; 28 import java.util.logging.Logger ; 29 30 31 37 public class ScriptExecutor extends ContentExecutor<ScriptEl> { 38 private static final Logger LOG = Logger.getLogger(ScriptExecutor.class.getName()); 39 private final boolean debug=LOG.isLoggable(Level.FINE); 40 41 public ScriptExecutor(ScriptEl scriptEl) { 42 super(scriptEl); 43 } 44 45 public void execute(final DynamicContext ctx) { 46 Connection con = ctx.getConnection(); 47 ScriptEl scriptEl = getElement(); 48 49 Resource content = getContent(con.getDialectIdentifier()); 50 if (content == ContentEl.NULL_CONTENT) { 51 warnEmptyContent(); 52 return; 53 } 54 if (debug) { 55 LOG.fine("Executing script " + getLocation()); 56 } 57 boolean repeat; 58 do { 59 repeat = false; 60 try { 61 con.executeScript(content, ctx); 62 if (debug) { 63 LOG.fine("Script " + getLocation() + " completed"); 64 } 65 66 } catch (Throwable t) { 67 if (scriptEl.getOnerrorElements() != null) { 68 repeat = onError(t, new OnErrorHandler(scriptEl), ctx); 69 } else { 70 ExceptionUtils.throwUnchecked(t); 71 } 72 } 73 } while (repeat); } 75 76 private void warnEmptyContent() { 77 LOG.info("Script " + getLocation() + " has no supported dialects"); 78 } 79 80 88 private boolean onError(Throwable t, OnErrorHandler errorHandler, DynamicContext ctx) { 89 OnErrorEl onErrorEl = errorHandler.onError(t); 90 Connection con = ctx.getConnection(); 91 DialectIdentifier dialectId = con.getDialectIdentifier(); 92 if (onErrorEl != null) { Resource content = onErrorEl.getContent(dialectId); 94 if (LOG.isLoggable(Level.INFO)) { 95 LOG.log(Level.INFO, StringUtils.consoleFormat("Script " + getLocation() + " failed: " + t + 96 "\nUsing onError handler: " + onErrorEl)); 97 } 98 99 try { 100 con.executeScript(content, ctx); 101 return onErrorEl.isRetry(); 102 } catch (Exception e) { 103 return onError(e, errorHandler, ctx); } 105 } ExceptionUtils.throwUnchecked(t); 107 return false; 108 109 } 110 111 public static ExecutableElement prepare(final ScriptEl s) { 112 ExecutableElement se = new ScriptExecutor(s); 113 se = StatisticInterceptor.prepare(se, s.getLocation()); 114 se = TxInterceptor.prepare(se, s); 115 se = ConnectionInterceptor.prepare(se, s); 116 se = ExceptionInterceptor.prepare(se, s.getLocation()); 117 se = IfInterceptor.prepare(se, s); 118 119 return se; 120 } 121 } 122 | Popular Tags |