KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > scriptella > core > Session


1 /*
2  * Copyright 2006-2007 The Scriptella Project Team.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

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 JavaDoc;
29 import java.util.HashMap JavaDoc;
30 import java.util.List JavaDoc;
31 import java.util.Map JavaDoc;
32
33
34 /**
35  * A helper class for EtlContext to store and work with connections/executors.
36  * <p>Available from {@link EtlContext}
37  *
38  * @author Fyodor Kupolov
39  * @version 1.0
40  */

41 public class Session {
42     Map JavaDoc<String JavaDoc, ConnectionManager> managedConnections = new HashMap JavaDoc<String JavaDoc, ConnectionManager>();
43     private List JavaDoc<ExecutableElement> executors;
44     private List JavaDoc<Location> locations;
45
46     public Session(final ConfigurationEl configuration, final EtlContext ctx) {
47         final List JavaDoc<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 JavaDoc id = connections.size() > 1 ? ("id=" + c.getId() + ", ") : "";//print an ID if more than one connection
57
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 JavaDoc<ScriptingElement> scripts = configuration.getScriptingElements();
65         progressCallback = ctx.getProgressCallback().fork(50, scripts.size());
66
67         executors = new ArrayList JavaDoc<ExecutableElement>(scripts.size());
68         locations = new ArrayList JavaDoc<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 JavaDoc id) {
84         if (managedConnections.size() == 1) {
85             return managedConnections.values().iterator().next();
86         } else {
87             if (id == null) {
88                 throw new IllegalArgumentException JavaDoc(
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