Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: jbreport/data/JavaPhysicalConnection.java


1   /*
2    * $Id: JavaPhysicalConnection.java,v 1.1.1.1 2000/08/31 13:14:31 grantfin Exp $
3    *
4    * jbReport - A reporting library for Java
5    * Copyright (C) 2000 Grant Finnemore <grantfin@users.sourceforge.net>
6    *
7    * This library is free software; you can redistribute it and/or
8    * modify it under the terms of the GNU Lesser General Public
9    * License as published by the Free Software Foundation; either
10   * version 2 of the License, or (at your option) any later version.
11   *
12   * This library is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this library; if not, write to the Free Software
19   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20   */
21  package jbreport.data;
22  
23  import java.sql.Connection;
24  import java.sql.SQLException;
25  import java.util.Map;
26  
27  import jbreport.Constants;
28  import jbreport.ConnectionFactory;
29  import jbreport.ReportException;
30  
31  /**
32   * This is used to extract a database connection from an existing application
33   * connection.
34   *
35   * @author Grant Finnemore
36   * @version $Revision: 1.1.1.1 $
37   */
38  class JavaPhysicalConnection implements Constants, PhysicalConnection {
39  
40     /** The factory used to obtain the database connection. We keep it here as 
41      * the fetch is only done when the connection is requested.
42      */
43     private ConnectionFactory factory;
44  
45     /** The connection to the database */
46     private Connection connection;
47  
48     //
49     // Constructors
50     //
51  
52     /**
53      * Required by the interface.
54      */
55     public JavaPhysicalConnection() {}
56  
57     //
58     // Implementation methods
59     //
60  
61     public void initialize(String datasourceType, Map properties) 
62        throws ReportException {
63  
64        if(!properties.containsKey(DS_JAVA_CONN_FACTORY)) {
65           throw new ReportException("DS_JAVA_CONN_FACTORY property not set");
66        }
67        // Create the appropriate factory class
68        try {
69           Class cls = 
70              Class.forName(properties.get(DS_JAVA_CONN_FACTORY).toString());
71           factory = (ConnectionFactory)cls.newInstance();
72        }
73        catch(Exception e) {
74           throw new ReportException("Could not create ConnectionFactory " + 
75                                     properties.get(DS_JAVA_CONN_FACTORY),
76                                     e);
77        }
78     }
79  
80     public Connection getConnection() throws ReportException {
81        // Fetch the connection from the factory - if necessary
82        if(connection == null) {
83           try {
84              connection = factory.fetchConnection();
85           }
86           catch(SQLException e) {
87              throw new ReportException("Problem while obtaining the connection "
88                                        + "from the ConnectionFactory", e);
89           }
90        }
91        return connection;
92     }
93  
94  }