1 10 package org.mmbase.module.database; 11 12 import java.sql.Connection ; 13 import java.sql.SQLException ; 14 import javax.sql.DataSource ; 15 import javax.sql.PooledConnection ; 16 import javax.sql.ConnectionPoolDataSource ; 17 import javax.naming.InitialContext ; 18 import javax.naming.Context ; 19 20 import org.mmbase.module.*; 21 import org.mmbase.util.logging.*; 22 23 32 public class Naming extends ProcessorModule implements JDBCInterface { 33 private static Logger log = Logging.getLoggerInstance(Naming.class.getName()); 34 private static String PROPERTY_CONTEXT_NAME = "context"; 35 private static String PROPERTY_DATASOURCE_NAME = "datasource"; 36 private Object datasource = null; 37 38 39 private class NamingMultiConnection extends MultiConnection { 40 41 NamingMultiConnection(Connection con) { 42 super(null, con); 43 state = CON_BUSY; 45 } 46 47 public void close() throws SQLException { 48 con.close(); 49 } 50 51 public void claim() { 52 } 53 54 public void release() { 55 } 56 57 public int getUsage() { 58 return 0; 59 } 60 61 public int getStartTime() { 62 return 0; 63 } 64 65 public long getStartTimeMillis() { 66 return 0; 67 } 68 } 69 70 74 public void init() { 75 String context = getInitParameter(PROPERTY_CONTEXT_NAME); 76 if(context == null) throw new RuntimeException ("the property '" + PROPERTY_CONTEXT_NAME + "' was not set"); 77 String source = getInitParameter(PROPERTY_DATASOURCE_NAME); 78 if(source == null) throw new RuntimeException ("the property '" + PROPERTY_CONTEXT_NAME + "' was not set"); 79 80 try { 82 Context initCtx = new InitialContext (); 83 Context envCtx = (Context ) initCtx.lookup(context); 84 datasource = envCtx.lookup(source); 85 if(datasource == null) { 86 String msg = "datasource was null for context:" + context + " with source:" + source; 87 log.error(msg); 88 throw new RuntimeException (msg); 89 } 90 if(datasource instanceof ConnectionPoolDataSource ) { 91 ConnectionPoolDataSource ds = (ConnectionPoolDataSource )datasource; 92 log.info("Using the interface:" + ConnectionPoolDataSource .class.getName() + "(implemented by:" + ds.getClass().getName() + " to get new database connections(time out: " + ds.getLoginTimeout() + " seconds)."); 93 } 94 else if(datasource instanceof DataSource ) { 95 log.info("Using the interface:" + DataSource .class.getName() + "(implemented by:" + datasource.getClass().getName() + " to get new database connections."); 96 } 97 else { 98 String msg = "Dont know how to retrieve a connection from datasource:" + datasource.getClass().getName(); 99 log.error(msg); 100 throw new RuntimeException (msg); 101 } 102 103 Connection con = getConnection(); 105 if(con == null) { 106 String msg = "Test run of retrieving a test-run failed."; 107 log.error(msg); 108 throw new RuntimeException (msg); 109 } 110 con.close(); 112 113 } catch(javax.naming.NamingException ne) { 114 String msg = "The following error occured while trying to initalise the datasource for context:'" + context + "' datasource:'" + source + "' :\n" + Logging.stackTrace(ne); 115 log.error(msg); 116 throw new RuntimeException (msg); 117 } 118 catch(java.sql.SQLException se) { 119 String msg = "The following error occured while trying to retrieve a connection from the datasource for context:'" + context + "' datasource:'" + source + "' :\n" + Logging.stackTrace(se); 120 log.error(msg); 121 throw new RuntimeException (msg); 122 } 123 } 124 125 128 public void reload() { 129 init(); 130 } 131 132 136 private Connection getConnection() throws java.sql.SQLException { 137 if (datasource == null) { 138 log.error("Getting connection before init of jdbc module. Trying to reinitalize the database layer."); 139 init(); 140 } 141 142 if(datasource instanceof ConnectionPoolDataSource ) { 143 ConnectionPoolDataSource ds = (ConnectionPoolDataSource ) datasource; 144 PooledConnection pc = ds.getPooledConnection(); 145 return pc.getConnection(); 146 } 147 else if(datasource instanceof DataSource ) { 148 DataSource ds = (DataSource ) datasource; 149 return ds.getConnection(); 150 } 151 else { 152 String msg = "Dont know how to retrieve a connection from datasource:" + (datasource != null ? datasource : datasource.getClass().getName()); 153 log.error(msg); 154 throw new RuntimeException (msg); 155 } 156 } 157 public MultiConnection getConnection(String url, String name, String password) throws SQLException { 158 return new NamingMultiConnection(getConnection()); 159 } 160 public MultiConnection getConnection(String url) throws SQLException { 161 return new NamingMultiConnection(getConnection()); 162 } 163 164 public Connection getDirectConnection(String url) throws SQLException { 165 return getConnection(); 166 } 167 public Connection getDirectConnection(String url,String name,String password) throws SQLException { 168 return getConnection(); 169 } 170 171 public void unload() {} 173 public void shutdown() {} 174 public String makeUrl(){return null;} 175 public String makeUrl(String dbm) {return null;} 176 public String makeUrl(String host,String dbm) {return null;} 177 public String makeUrl(String host,int port,String dbm) {return null;} 178 public String getUser() {return null;} 179 public String getPassword() {return null;} 180 public String getDatabaseName(){return null;} 181 public void checkTime(){} 182 } 183 | Popular Tags |