001/* 002 * (C) Copyright 2006-2011 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * George Lefter 018 * Florent Guillaume 019 */ 020package org.nuxeo.ecm.directory.sql; 021 022import java.io.PrintWriter; 023import java.sql.Connection; 024import java.sql.SQLException; 025import java.sql.SQLFeatureNotSupportedException; 026import java.util.logging.Logger; 027 028import javax.sql.DataSource; 029 030import org.nuxeo.common.utils.JDBCUtils; 031import org.nuxeo.runtime.api.Framework; 032 033/** 034 * This class is used for tests, where datasources are set up from a driver, url and user/password instead of a JNDI 035 * name. 036 */ 037public class SimpleDataSource implements DataSource { 038 039 private final String url; 040 041 private final String user; 042 043 private final String password; 044 045 public SimpleDataSource(String url, String driver, String user, String password) { 046 url = Framework.expandVars(url); 047 driver = Framework.expandVars(driver); 048 user = Framework.expandVars(user); 049 password = Framework.expandVars(password); 050 try { 051 Class.forName(driver); 052 } catch (ClassNotFoundException e) { 053 throw new RuntimeException("driver class not found", e); 054 } 055 this.url = url; 056 this.user = user; 057 this.password = password; 058 } 059 060 @Override 061 public Connection getConnection() throws SQLException { 062 Connection con = JDBCUtils.getConnection(url, user, password); 063 con.setAutoCommit(true); 064 return con; 065 } 066 067 @Override 068 public Connection getConnection(String username, String password) throws SQLException { 069 throw new UnsupportedOperationException(); 070 } 071 072 @Override 073 public PrintWriter getLogWriter() throws SQLException { 074 throw new UnsupportedOperationException(); 075 } 076 077 @Override 078 public int getLoginTimeout() throws SQLException { 079 throw new UnsupportedOperationException(); 080 } 081 082 @Override 083 public void setLogWriter(PrintWriter out) throws SQLException { 084 throw new UnsupportedOperationException(); 085 } 086 087 @Override 088 public void setLoginTimeout(int seconds) throws SQLException { 089 throw new UnsupportedOperationException(); 090 } 091 092 // @Override in CommonDataSource for Java SE 7 / JDBC 4.1 093 @Override 094 public Logger getParentLogger() throws SQLFeatureNotSupportedException { 095 throw new SQLFeatureNotSupportedException(); 096 } 097 098 @Override 099 public boolean isWrapperFor(Class<?> iface) throws SQLException { 100 throw new UnsupportedOperationException(); 101 } 102 103 @Override 104 public <T> T unwrap(Class<T> iface) throws SQLException { 105 throw new UnsupportedOperationException(); 106 } 107 108}