01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.apache.cocoon.components.language.markup.xsp;
18:
19: import org.apache.avalon.excalibur.datasource.DataSourceComponent;
20:
21: import java.sql.Connection;
22: import java.sql.SQLException;
23: import java.sql.DriverManager;
24:
25: /**
26: * This is the Cocoon2 specific part of an AbstractEsqlConnection.
27: *
28: * @author <a href="mailto:tcurdt@apache.org">Torsten Curdt</a>
29: * @version CVS $Id: Cocoon2EsqlConnection.java 433543 2006-08-22 06:22:54Z crossley $
30: */
31:
32: final public class Cocoon2EsqlConnection extends AbstractEsqlConnection {
33: final private DataSourceComponent datasource;
34: private Connection connection = null;
35:
36: public Cocoon2EsqlConnection() {
37: this .datasource = null;
38: this .connection = null;
39: }
40:
41: /**
42: * Someone passed the connection
43: * @param connection
44: */
45: public Cocoon2EsqlConnection(Connection connection) {
46: this .datasource = null;
47: this .connection = connection;
48: }
49:
50: /**
51: * Get the connection from the pool
52: * @param datasource
53: */
54: public Cocoon2EsqlConnection(DataSourceComponent datasource) {
55: this .datasource = datasource;
56: }
57:
58: public Connection getConnection() throws SQLException {
59: if (connection != null) {
60: return (connection);
61: } else {
62: if (datasource != null) {
63: // get the connection from the pool
64: connection = datasource.getConnection();
65: } else {
66: // open a new connection
67: connection = DriverManager.getConnection(getURL(),
68: getProperties());
69: }
70: if (connection != null) {
71: return (connection);
72: } else {
73: throw new SQLException("Could not obtain connection");
74: }
75: }
76: }
77: }
|