package net.sourceforge.jdbclogger.spring;
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You 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.
*/
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
if (!isEnabled()) {
log.info("Configuring data source ["+beanName+"] skipped, disabled by configuration.");
return bean;
}
if (bean instanceof DataSource) {
replaceDataSourceDriverClassName((DataSource)bean, beanName, dataSourceConfiguration.getDriverClassNamePropertyName());
}
else log.warn("Configured data source bean ["+beanName+"] is not a javax.sql.DataSource instance, ignored.");
return bean;
}
public void afterPropertiesSet() throws Exception {
if (!isEnabled()) {
log.info("Configuring target driver class names skipped, disabled by configuration.");
return;
}
//if there were no custom data source settings registered, register the default one
if (dataSourceConfigurations.size() == 0)
dataSourceConfigurations.add(new JdbcLoggerDataSourceConfiguration());
if (getTargetDriverClassNames().size() == 1) {
String aTargetDriverClassName = (String)getTargetDriverClassNames().get(0);
System.setProperty(JdbcLoggerConstants.USER_DRIVER_PROPERTY_NAME, aTargetDriverClassName);
}
else if (getTargetDriverClassNames().size() > 1) {
int index = 0;
Iterator it = getTargetDriverClassNames().listIterator();
while (it.hasNext()) {
String aTargetDriverClassName = (String)it.next();
//set din target driver class into the system's properties
String propertyName = JdbcLoggerConstants.USER_DRIVER_PROPERTY_NAME + "_"+String.valueOf(index);
System.setProperty(propertyName, aTargetDriverClassName);
/**
* This method replace the driver class name of the given data source instance, via reflection.
*
* @param dataSource
* @param dataSourceBeanName
* @param driverClassNamePropertyName
*/
protected void replaceDataSourceDriverClassName(DataSource dataSource, String dataSourceBeanName, String driverClassNamePropertyName) {
try {
BeanUtils.setProperty(dataSource, driverClassNamePropertyName, "net.sourceforge.jdbclogger.JdbcLoggerDriver");
log.info("Driver class name successfuly replaced for data source ["+dataSourceBeanName+"]");
}
catch (IllegalAccessException exc) {
log.error("Could not replace target data source driver class name", exc);
}
catch (InvocationTargetException exc) {
log.error("Could not replace target data source driver class name", exc);
}
}
/**
* @param dataSourceBeanName
* @return
*/
private JdbcLoggerDataSourceConfiguration getDataSourceConfiguration(String dataSourceBeanName) {
Iterator it = getDataSourceConfigurations().listIterator();
while (it.hasNext()) {
JdbcLoggerDataSourceConfiguration aConfiguration = (JdbcLoggerDataSourceConfiguration)it.next();
if (dataSourceBeanName.equals(aConfiguration.getDataSourceBeanName()))
return aConfiguration;
}
return null;
}
/**
* @return the dataSourceConfigurations
*/
public List getDataSourceConfigurations() {
return dataSourceConfigurations;
}
/**
* @param dataSourceConfigurations the dataSourceConfigurations to set
*/
public void setDataSourceConfigurations(List dataSourceConfigurations) {
this.dataSourceConfigurations = dataSourceConfigurations;
}
/**
* @return the targetDriverClassNames
*/
public List getTargetDriverClassNames() {
return targetDriverClassNames;
}
/**
* @param targetDriverClassNames the targetDriverClassNames to set
*/
public void setTargetDriverClassNames(List targetDriverClassNames) {
this.targetDriverClassNames = targetDriverClassNames;
}
/**
* @return the enabled
*/
public boolean isEnabled() {
return enabled;
}
/**
* @param enabled the enabled to set
*/
public void setEnabled(boolean enabled) {
this.enabled = enabled;
}
}