1 16 package scriptella.core; 17 18 import scriptella.configuration.ConfigurationEl; 19 import scriptella.configuration.ConnectionEl; 20 import scriptella.configuration.Location; 21 import scriptella.configuration.QueryEl; 22 import scriptella.configuration.ScriptEl; 23 import scriptella.configuration.ScriptingElement; 24 import scriptella.execution.EtlContext; 25 import scriptella.interactive.ProgressCallback; 26 import scriptella.spi.Connection; 27 28 import java.util.ArrayList ; 29 import java.util.HashMap ; 30 import java.util.List ; 31 import java.util.Map ; 32 33 34 41 public class Session { 42 Map <String , ConnectionManager> managedConnections = new HashMap <String , ConnectionManager>(); 43 private List <ExecutableElement> executors; 44 private List <Location> locations; 45 46 public Session(final ConfigurationEl configuration, final EtlContext ctx) { 47 final List <ConnectionEl> connections = configuration.getConnections(); 48 49 ProgressCallback progressCallback = ctx.getProgressCallback().fork(50, connections.size()); 50 for (ConnectionEl c : connections) { 51 final ConnectionManager con = new ConnectionManager(ctx, c); 52 if (c.isLazyInit()) { 53 progressCallback.step(1, "Lazy-init specified for connection " + (c.getId() == null ? "" : c.getId()) + 54 " - initialization deferred."); 55 } else { 56 String id = connections.size() > 1 ? ("id=" + c.getId() + ", ") : ""; Connection connection = con.getConnection(); 58 progressCallback.step(1, "Connection " + id + connection.toString() + 59 ", " + connection.getDialectIdentifier() + " registered"); 60 } 61 managedConnections.put(c.getId(), con); 62 } 63 64 final List <ScriptingElement> scripts = configuration.getScriptingElements(); 65 progressCallback = ctx.getProgressCallback().fork(50, scripts.size()); 66 67 executors = new ArrayList <ExecutableElement>(scripts.size()); 68 locations = new ArrayList <Location>(scripts.size()); 69 70 for (ScriptingElement s : scripts) { 71 locations.add(s.getLocation()); 72 73 if (s instanceof QueryEl) { 74 executors.add(QueryExecutor.prepare((QueryEl) s)); 75 } else if (s instanceof ScriptEl) { 76 executors.add(ScriptExecutor.prepare((ScriptEl) s)); 77 } 78 progressCallback.step(1, s.getLocation() + " prepared"); 79 } 80 81 } 82 83 ConnectionManager getConnection(final String id) { 84 if (managedConnections.size() == 1) { 85 return managedConnections.values().iterator().next(); 86 } else { 87 if (id == null) { 88 throw new IllegalArgumentException ( 89 "Connection id must be specified"); 90 } 91 92 return managedConnections.get(id); 93 } 94 } 95 96 public void execute(final EtlContext ctx) { 97 final ProgressCallback progress = ctx.getProgressCallback() 98 .fork(executors.size()); 99 DynamicContext dynCtx = new DynamicContext(ctx); 100 101 for (int i = 0, n = executors.size(); i < n; i++) { 102 ExecutableElement exec = executors.get(i); 103 exec.execute(dynCtx); 104 progress.step(1, locations.get(i).toString()+" executed"); 105 } 106 } 107 108 public long getExecutedStatementsCount() { 109 long s = 0; 110 if (managedConnections != null) { 111 for (ConnectionManager connectionManager : managedConnections.values()) { 112 s += connectionManager.getExecutedStatementsCount(); 113 } 114 } 115 return s; 116 } 117 118 public void close() { 119 if (managedConnections != null) { 120 for (ConnectionManager connectionManager : managedConnections.values()) { 121 connectionManager.close(); 122 } 123 managedConnections = null; 124 } 125 } 126 127 public void commit() { 128 if (managedConnections != null) { 129 for (ConnectionManager connectionManager : managedConnections.values()) { 130 if (connectionManager != null) { 131 connectionManager.commit(); 132 } 133 } 134 } 135 } 136 137 public void rollback() { 138 if (managedConnections != null) { 139 for (ConnectionManager connectionManager : managedConnections.values()) { 140 connectionManager.rollback(); 141 connectionManager.close(); 142 } 143 } 144 } 145 } 146 | Popular Tags |