1 10 package org.mmbase.storage.implementation.database; 11 12 import java.sql.*; 13 14 import javax.sql.DataSource ; 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 35 public final class GenericDataSource implements DataSource { 36 private static final Logger log = Logging.getLoggerInstance(GenericDataSource.class); 37 38 final private JDBC jdbc; 40 41 private java.io.PrintWriter printWriter = null; 42 43 final private String dataDir; 44 final private boolean meta; 45 46 private boolean basePathOk = false; 47 48 55 GenericDataSource(MMBase mmbase, String 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 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 public Connection getConnection() throws SQLException { 76 String url = makeUrl(); 77 if (log.isTraceEnabled()) { 78 log.trace("Getting " + (meta ? "META " : "") + "connection for " + url); 79 } 80 if (meta) { 81 String name = jdbc.getInitParameter("user"); 82 String 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 95 public Connection getDirectConnection() throws SQLException { 96 String url = makeUrl(); 97 return jdbc.getDirectConnection(url); 98 } 99 100 public Connection getConnection(String userName, String password) throws SQLException { 102 String 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 public int getLoginTimeout() { 115 return 0; 116 } 117 118 public java.io.PrintWriter getLogWriter() { 120 return printWriter; 121 } 122 123 127 public void setLoginTimeout(int seconds) { 128 } 130 131 132 public void setLogWriter(java.io.PrintWriter out) { 134 printWriter = out; 135 } 136 137 142 protected String makeUrl() { 143 if (meta) { 144 DatabaseStorageLookup lookup = new DatabaseStorageLookup(); 145 try { 146 String metaUrl = lookup.getMetaURL(Class.forName(jdbc.getInitParameter("driver"))); 147 if (metaUrl != null) { 148 String database = jdbc.getInitParameter("database"); 149 if (database != null) { 150 metaUrl = metaUrl.replaceAll("\\$DBM", database); 151 } 152 String host = jdbc.getInitParameter("host"); 153 if (host != null) { 154 metaUrl = metaUrl.replaceAll("\\$HOST", host); 155 } 156 String port = jdbc.getInitParameter("port"); 157 if (port != null) { 158 metaUrl = metaUrl.replaceAll("\\$PORT", port); 159 } 160 return metaUrl; 161 } 162 } catch (ClassNotFoundException cnfe) { 163 log.error(cnfe); 164 } 165 } 166 String url = jdbc.makeUrl(); 167 String 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 |