KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > language > markup > xsp > Cocoon2EsqlConnection


1 /*
2  * Copyright 1999-2004 The Apache Software Foundation.
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 org.apache.cocoon.components.language.markup.xsp;
17
18 import org.apache.avalon.excalibur.datasource.DataSourceComponent;
19
20 import java.sql.Connection JavaDoc;
21 import java.sql.SQLException JavaDoc;
22 import java.sql.DriverManager JavaDoc;
23
24 /**
25  * This is the Cocoon2 specific part of an AbstractEsqlConnection.
26  *
27  * @author <a HREF="mailto:tcurdt@apache.org">Torsten Curdt</a>
28  * @version CVS $Id: Cocoon2EsqlConnection.java 30932 2004-07-29 17:35:38Z vgritsenko $
29  */

30
31 final public class Cocoon2EsqlConnection extends AbstractEsqlConnection {
32     final private DataSourceComponent datasource;
33     private Connection JavaDoc connection = null;
34
35     public Cocoon2EsqlConnection() {
36         this.datasource = null;
37         this.connection = null;
38     }
39
40     /**
41      * Someone passed the connection
42      * @param connection
43      */

44     public Cocoon2EsqlConnection( Connection JavaDoc connection ) {
45         this.datasource = null;
46         this.connection = connection;
47     }
48
49     /**
50      * Get the connection from the pool
51      * @param datasource
52      */

53     public Cocoon2EsqlConnection( DataSourceComponent datasource ) {
54         this.datasource = datasource;
55     }
56
57     public Connection JavaDoc getConnection() throws SQLException JavaDoc {
58         if (connection != null) {
59             return(connection);
60         }
61         else {
62             if (datasource != null) {
63                 // get the connection from the pool
64
connection = datasource.getConnection();
65             }
66             else {
67                 // open a new connection
68
connection = DriverManager.getConnection(getURL(), getProperties());
69             }
70             if (connection != null) {
71                 return(connection);
72             }
73             else {
74                 throw new SQLException JavaDoc("Could not obtain connection");
75             }
76         }
77     }
78 }
79
Popular Tags