org.fabric3.security.spring.factory.DataSourceWrapper Maven / Gradle / Ivy
/*
* Fabric3
* Copyright (c) 2009-2015 Metaform Systems
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.fabric3.security.spring.factory;
import javax.sql.DataSource;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.SQLFeatureNotSupportedException;
import java.util.logging.Logger;
import org.fabric3.datasource.spi.DataSourceRegistry;
/**
* Wraps a DataSource to retrieve it lazily from the {@link DataSourceRegistry}. Wrapping is necessary as a DataSource may not be registered until
* after the Spring Security configuration has been instantiated.
*/
public class DataSourceWrapper implements DataSource {
private String name;
private DataSourceRegistry registry;
private DataSource delegate;
public DataSourceWrapper(String name, DataSourceRegistry registry) {
this.name = name;
this.registry = registry;
}
public Connection getConnection() throws SQLException {
return getDataSource().getConnection();
}
public Connection getConnection(String username, String password) throws SQLException {
return getDataSource().getConnection(username, password);
}
public T unwrap(Class iface) throws SQLException {
return getDataSource().unwrap(iface);
}
public boolean isWrapperFor(Class> iface) throws SQLException {
return getDataSource().isWrapperFor(iface);
}
public PrintWriter getLogWriter() throws SQLException {
return getDataSource().getLogWriter();
}
public void setLogWriter(PrintWriter out) throws SQLException {
getDataSource().setLogWriter(out);
}
public void setLoginTimeout(int seconds) throws SQLException {
getDataSource().setLoginTimeout(seconds);
}
public int getLoginTimeout() throws SQLException {
return getDataSource().getLoginTimeout();
}
public Logger getParentLogger() throws SQLFeatureNotSupportedException {
throw new SQLFeatureNotSupportedException();
}
private DataSource getDataSource() throws SQLException {
if (delegate == null) {
delegate = registry.getDataSource(name);
if (delegate == null) {
throw new SQLException("DataSource not registered: " + name);
}
}
return delegate;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy