KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > scriptella > core > ConnectionManager


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.ConfigurationException;
19 import scriptella.configuration.ConnectionEl;
20 import scriptella.execution.EtlContext;
21 import scriptella.spi.Connection;
22 import scriptella.spi.ConnectionParameters;
23 import scriptella.spi.ScriptellaDriver;
24 import scriptella.util.UrlPathTokenizer;
25
26 import java.net.MalformedURLException JavaDoc;
27 import java.net.URL JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.logging.Level JavaDoc;
31 import java.util.logging.Logger JavaDoc;
32
33
34 /**
35  * TODO: Add documentation
36  *
37  * @author Fyodor Kupolov
38  * @version 1.0
39  */

40 public class ConnectionManager {
41     private static final Logger JavaDoc LOG = Logger.getLogger(ConnectionManager.class.getName());
42     Connection connection;
43     List JavaDoc<Connection> newConnections;
44     private ScriptellaDriver driver;
45     ConnectionParameters connectionParameters;
46
47     public ConnectionManager(EtlContext ctx, ConnectionEl c) {
48         //Obtains a classloader
49
ClassLoader JavaDoc cl = getClass().getClassLoader();
50         if (c.getClasspath() != null) { //if classpath specified
51
//Parse it and create a new classloader
52
UrlPathTokenizer tok = new UrlPathTokenizer(ctx.getScriptFileURL());
53             try {
54                 URL JavaDoc[] urls = tok.split(c.getClasspath());
55                 if (urls.length > 0) {
56                     cl = new DriverClassLoader(urls);
57                 }
58             } catch (MalformedURLException JavaDoc e) {
59                 throw new ConfigurationException("Unable to parse classpath parameter for " + c, e);
60             }
61         }
62         connectionParameters = new ConnectionParameters(c, ctx);
63         try {
64             driver = DriverFactory.getDriver(c.getDriver(), cl);
65         } catch (ClassNotFoundException JavaDoc e) {
66             throw new ConfigurationException("Driver class " + c.getDriver() + " not found for " + connectionParameters +
67                     ".Please check if the class name is correct and required libraries available on classpath", e);
68         } catch (Exception JavaDoc e) {
69             throw new ConfigurationException("Unable to initialize driver for " + connectionParameters + ":" + e.getMessage(), e);
70         }
71     }
72
73     public Connection getConnection() {
74         if (connection == null) {
75             connection = driver.connect(connectionParameters);
76             if (connection == null) {
77                 throw new ConfigurationException("Driver returned null connection for " + connectionParameters);
78             }
79         }
80
81         return connection;
82     }
83
84     public Connection newConnection() {
85         final Connection c = driver.connect(connectionParameters);
86         if (c == null) {
87             throw new ConfigurationException("Driver returned null connection for " + connectionParameters);
88         }
89
90         if (newConnections == null) {
91             newConnections = new ArrayList JavaDoc<Connection>();
92         }
93
94         newConnections.add(c);
95
96         return c;
97     }
98
99     public void rollback() {
100         for (Connection c : getAllConnections()) {
101             try {
102                 if (LOG.isLoggable(Level.FINE)) {
103                     LOG.fine("Rolling back " + c);
104                 }
105                 c.rollback();
106             } catch (UnsupportedOperationException JavaDoc e) {
107                 String JavaDoc msg = e.getMessage();
108                 LOG.log(Level.WARNING,
109                         "Unable to rollback transaction for connection " + c + (msg == null ? "" : ": " + msg));
110
111             } catch (Exception JavaDoc e) {
112                 LOG.log(Level.WARNING,
113                         "Unable to rollback transaction for connection " + c, e);
114             }
115         }
116     }
117
118     public void commit() {
119         for (Connection c : getAllConnections()) {
120             if (LOG.isLoggable(Level.FINE)) {
121                 LOG.fine("Commiting connection " + c);
122             }
123             c.commit();
124         }
125     }
126
127
128     public void close() {
129         for (Connection c : getAllConnections()) {
130             if (c != null) {
131                 try {
132                     if (LOG.isLoggable(Level.FINE)) {
133                         LOG.fine("Closing " + c);
134                     }
135                     c.close();
136                 } catch (Exception JavaDoc e) {
137                     LOG.log(Level.WARNING, "Problem occured while trying to close connection " + c, e);
138                 }
139             }
140         }
141
142         connection = null;
143         newConnections = null;
144         connectionParameters = null;
145         driver = null;
146     }
147
148     /**
149      * Returns number of executed statements by managed connections.
150      */

151     public long getExecutedStatementsCount() {
152         long s = 0;
153         if (connection != null) {
154             s += connection.getExecutedStatementsCount();
155
156         }
157         if (newConnections != null) {
158             for (Connection c : newConnections) {
159                 s += c.getExecutedStatementsCount();
160             }
161         }
162         return s;
163     }
164
165     /**
166      * @return connection and newtx connections
167      */

168     private List JavaDoc<Connection> getAllConnections() {
169         List JavaDoc<Connection> cl = new ArrayList JavaDoc<Connection>();
170
171         if (newConnections != null) {
172             cl.addAll(newConnections);
173         }
174
175         if (connection != null) {
176             cl.add(connection);
177         }
178         return cl;
179     }
180
181
182 }
183
Popular Tags