001: /*
002: * Copyright 2006-2007 The Scriptella Project Team.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package scriptella.core;
017:
018: import scriptella.configuration.ConfigurationEl;
019: import scriptella.configuration.ConnectionEl;
020: import scriptella.configuration.Location;
021: import scriptella.configuration.QueryEl;
022: import scriptella.configuration.ScriptEl;
023: import scriptella.configuration.ScriptingElement;
024: import scriptella.execution.EtlContext;
025: import scriptella.interactive.ProgressCallback;
026: import scriptella.spi.Connection;
027:
028: import java.util.ArrayList;
029: import java.util.HashMap;
030: import java.util.List;
031: import java.util.Map;
032:
033: /**
034: * A helper class for EtlContext to store and work with connections/executors.
035: * <p>Available from {@link EtlContext}
036: *
037: * @author Fyodor Kupolov
038: * @version 1.0
039: */
040: public class Session {
041: Map<String, ConnectionManager> managedConnections = new HashMap<String, ConnectionManager>();
042: private List<ExecutableElement> executors;
043: private List<Location> locations;
044:
045: public Session(final ConfigurationEl configuration,
046: final EtlContext ctx) {
047: final List<ConnectionEl> connections = configuration
048: .getConnections();
049:
050: ProgressCallback progressCallback = ctx.getProgressCallback()
051: .fork(50, connections.size());
052: for (ConnectionEl c : connections) {
053: final ConnectionManager con = new ConnectionManager(ctx, c);
054: if (c.isLazyInit()) {
055: progressCallback.step(1,
056: "Lazy-init specified for connection "
057: + (c.getId() == null ? "" : c.getId())
058: + " - initialization deferred.");
059: } else {
060: String id = connections.size() > 1 ? ("id=" + c.getId() + ", ")
061: : "";//print an ID if more than one connection
062: Connection connection = con.getConnection();
063: progressCallback.step(1, "Connection " + id
064: + connection.toString() + ", "
065: + connection.getDialectIdentifier()
066: + " registered");
067: }
068: managedConnections.put(c.getId(), con);
069: }
070:
071: final List<ScriptingElement> scripts = configuration
072: .getScriptingElements();
073: progressCallback = ctx.getProgressCallback().fork(50,
074: scripts.size());
075:
076: executors = new ArrayList<ExecutableElement>(scripts.size());
077: locations = new ArrayList<Location>(scripts.size());
078:
079: for (ScriptingElement s : scripts) {
080: locations.add(s.getLocation());
081:
082: if (s instanceof QueryEl) {
083: executors.add(QueryExecutor.prepare((QueryEl) s));
084: } else if (s instanceof ScriptEl) {
085: executors.add(ScriptExecutor.prepare((ScriptEl) s));
086: }
087: progressCallback.step(1, s.getLocation() + " prepared");
088: }
089:
090: }
091:
092: ConnectionManager getConnection(final String id) {
093: if (managedConnections.size() == 1) {
094: return managedConnections.values().iterator().next();
095: } else {
096: if (id == null) {
097: throw new IllegalArgumentException(
098: "Connection id must be specified");
099: }
100:
101: return managedConnections.get(id);
102: }
103: }
104:
105: public void execute(final EtlContext ctx) {
106: final ProgressCallback progress = ctx.getProgressCallback()
107: .fork(executors.size());
108: DynamicContext dynCtx = new DynamicContext(ctx);
109:
110: for (int i = 0, n = executors.size(); i < n; i++) {
111: ExecutableElement exec = executors.get(i);
112: exec.execute(dynCtx);
113: progress.step(1, locations.get(i).toString() + " executed");
114: }
115: }
116:
117: public long getExecutedStatementsCount() {
118: long s = 0;
119: if (managedConnections != null) {
120: for (ConnectionManager connectionManager : managedConnections
121: .values()) {
122: s += connectionManager.getExecutedStatementsCount();
123: }
124: }
125: return s;
126: }
127:
128: public void close() {
129: if (managedConnections != null) {
130: for (ConnectionManager connectionManager : managedConnections
131: .values()) {
132: connectionManager.close();
133: }
134: managedConnections = null;
135: }
136: }
137:
138: public void commit() {
139: if (managedConnections != null) {
140: for (ConnectionManager connectionManager : managedConnections
141: .values()) {
142: if (connectionManager != null) {
143: connectionManager.commit();
144: }
145: }
146: }
147: }
148:
149: public void rollback() {
150: if (managedConnections != null) {
151: for (ConnectionManager connectionManager : managedConnections
152: .values()) {
153: connectionManager.rollback();
154: connectionManager.close();
155: }
156: }
157: }
158: }
|