KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > storage > implementation > database > GenericDataSource


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10 package org.mmbase.storage.implementation.database;
11
12 import java.sql.*;
13
14 import javax.sql.DataSource JavaDoc;
15
16 import org.mmbase.module.Module;
17 import org.mmbase.module.core.MMBase;
18 import org.mmbase.module.database.JDBC;
19 import org.mmbase.storage.StorageInaccessibleException;
20 import org.mmbase.util.logging.*;
21
22 /**
23  * This class functions as a Datasource wrapper around the JDBC Module.
24  * It is intended for use when MMBase runs outside an applicationserver, or when the server does not (or poorly)
25  * support pooled datasources.
26  * I can also be used by older systems that use the JDBC Module and do not want to change their configuration.
27  * Note that the JDBC Module will likely be fased out as a module at some point in the future,
28  * with code and supporting classes to be moved to this class instead.
29  *
30  * @author Pierre van Rooden
31  * @author Michiel Meeuwissen
32  * @since MMBase-1.7
33  * @version $Id: GenericDataSource.java,v 1.12 2006/07/15 14:35:06 michiel Exp $
34  */

35 public final class GenericDataSource implements DataSource JavaDoc {
36     private static final Logger log = Logging.getLoggerInstance(GenericDataSource.class);
37
38     // Reference to the JDBC Module
39
final private JDBC jdbc;
40
41     private java.io.PrintWriter JavaDoc printWriter = null;
42
43     final private String JavaDoc dataDir;
44     final private boolean meta;
45
46     private boolean basePathOk = false;
47
48     /**
49      * Constructs a datasource for accessing the database belonging to the given MMBase module.
50      * The MMBase parameter is not currently used, but is included for future expansion
51      * @param mmbase the MMBase instance
52      * @param A Datadir (as a string ending in a /) which may be used in some URL's (most noticably those of HSQLDB). Can be <code>null</code> if not used.
53      * @throws StorageInaccessibleException if the JDBC module used in creating the datasource is inaccessible
54      */

55     GenericDataSource(MMBase mmbase, String JavaDoc dataDir) throws StorageInaccessibleException {
56         jdbc = (JDBC) Module.getModule("JDBC", true);
57         if (jdbc == null) {
58             throw new StorageInaccessibleException("Cannot load Datasource or JDBC Module");
59         }
60         this.dataDir = dataDir == null ? "" : dataDir;
61         meta = false;
62     }
63     /**
64      */

65     GenericDataSource(MMBase mmbase) throws StorageInaccessibleException {
66         jdbc = (JDBC) Module.getModule("JDBC", false);
67         if (jdbc == null) {
68             throw new StorageInaccessibleException("Cannot load Datasource or JDBC Module");
69         }
70         dataDir = null;
71         meta = true;
72     }
73
74     // see javax.sql.DataSource
75
public Connection getConnection() throws SQLException {
76         String JavaDoc url = makeUrl();
77         if (log.isTraceEnabled()) {
78             log.trace("Getting " + (meta ? "META " : "") + "connection for " + url);
79         }
80         if (meta) {
81             String JavaDoc name = jdbc.getInitParameter("user");
82             String JavaDoc password = jdbc.getInitParameter("password");
83             if (name.equals("url") && password.equals("url")) {
84                 return DriverManager.getConnection(url);
85             } else {
86                 return DriverManager.getConnection(url, name, password);
87             }
88         } else {
89             return jdbc.getConnection(url);
90         }
91     }
92     /**
93      * @since MMBase-1.8
94      */

95     public Connection getDirectConnection() throws SQLException {
96         String JavaDoc url = makeUrl();
97         return jdbc.getDirectConnection(url);
98     }
99
100     // see javax.sql.DataSource
101
public Connection getConnection(String JavaDoc userName, String JavaDoc password) throws SQLException {
102         String JavaDoc url = makeUrl();
103         if (log.isDebugEnabled()) {
104             log.trace("Getting " + (meta ? "META " : "") + "connection for " + url);
105         }
106         if (meta) {
107             return DriverManager.getConnection(url, userName, password);
108         } else {
109             return jdbc.getConnection(url, userName, password);
110         }
111     }
112
113     // see javax.sql.DataSource
114
public int getLoginTimeout() {
115         return 0;
116     }
117
118     // see javax.sql.DataSource
119
public java.io.PrintWriter JavaDoc getLogWriter() {
120         return printWriter;
121     }
122
123     /**
124      * {@inheritDoc}
125      * Note: currently this code does not actually change the timeout. Login timeout is not implemented by JDBC module
126      */

127     public void setLoginTimeout(int seconds) {
128         // loginTimeout = seconds;
129
}
130
131
132     // see javax.sql.DataSource
133
public void setLogWriter(java.io.PrintWriter JavaDoc out) {
134         printWriter = out;
135     }
136
137     /**
138      * Makes URL which can be used to produce a Conncetion. If this is a 'meta' datasource, then
139      * 'lookup.xml' will be tried, for a 'meta url'.
140      * @since MMBase-1.8
141      */

142     protected String JavaDoc makeUrl() {
143         if (meta) {
144             DatabaseStorageLookup lookup = new DatabaseStorageLookup();
145             try {
146                 String JavaDoc metaUrl = lookup.getMetaURL(Class.forName(jdbc.getInitParameter("driver")));
147                 if (metaUrl != null) {
148                     String JavaDoc database = jdbc.getInitParameter("database");
149                     if (database != null) {
150                         metaUrl = metaUrl.replaceAll("\\$DBM", database);
151                     }
152                     String JavaDoc host = jdbc.getInitParameter("host");
153                     if (host != null) {
154                         metaUrl = metaUrl.replaceAll("\\$HOST", host);
155                     }
156                     String JavaDoc port = jdbc.getInitParameter("port");
157                     if (port != null) {
158                         metaUrl = metaUrl.replaceAll("\\$PORT", port);
159                     }
160                     return metaUrl;
161                 }
162             } catch (ClassNotFoundException JavaDoc cnfe) {
163                 log.error(cnfe);
164             }
165         }
166         String JavaDoc url = jdbc.makeUrl();
167         String JavaDoc newUrl = url.replaceAll("\\$DATADIR", dataDir);
168         if ((!basePathOk) && (! newUrl.equals(url))) {
169             basePathOk = DatabaseStorageManagerFactory.checkBinaryFileBasePath(dataDir);
170         }
171         return newUrl;
172     }
173
174
175
176 }
177
Popular Tags