View Javadoc

1   /*
2    * EL4J, the Extension Library for the J2EE, adds incremental enhancements to
3    * the spring framework, http://el4j.sf.net
4    * Copyright (C) 2008 by ELCA Informatique SA, Av. de la Harpe 22-24,
5    * 1000 Lausanne, Switzerland, http://www.elca.ch
6    *
7    * EL4J is published under the GNU Lesser General Public License (LGPL)
8    * Version 2.1. See http://www.gnu.org/licenses/
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13   * GNU Lesser General Public License for more details.
14   *
15   * For alternative licensing, please contact info@elca.ch
16   */
17  package ch.elca.el4j.services.monitoring;
18  
19  import org.springframework.beans.BeansException;
20  import org.springframework.beans.factory.config.BeanDefinition;
21  import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
22  import org.springframework.beans.factory.config.PropertyOverrideConfigurer;
23  import org.springframework.beans.factory.config.TypedStringValue;
24  
25  /**
26   * A property override configurer that makes the data source bean use the JAMon JDBC interceptor.
27   *
28   * @svnLink $Revision: 3878 $;$Date: 2009-08-04 15:06:35 +0200 (Di, 04. Aug 2009) $;$Author: swismer $;$URL: https://el4j.svn.sourceforge.net/svnroot/el4j/branches/el4j_3_1/el4j/framework/modules/database/jamon/src/main/java/ch/elca/el4j/services/monitoring/JamonDatasourcePropertyOverrideConfigurer.java $
29   *
30   * @author Stefan Wismer (SWI)
31   */
32  public class JamonDatasourcePropertyOverrideConfigurer extends PropertyOverrideConfigurer {
33  	
34  	private static final String DATA_SOURCE_BEAN_NAME = "dataSource";
35  	private static final String DRIVER_CLASS = "driverClass";
36  	private static final String JDBC_URL = "jdbcUrl";
37  	
38  	/** {@inheritDoc} */
39  	@Override
40  	public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
41  		BeanDefinition bd = beanFactory.getBeanDefinition(DATA_SOURCE_BEAN_NAME);
42  		while (bd.getOriginatingBeanDefinition() != null) {
43  			bd = bd.getOriginatingBeanDefinition();
44  		}
45  		
46  		String oldDriver = getPropertyValue(bd, DRIVER_CLASS);
47  		String oldJdbcUrl = getPropertyValue(bd, JDBC_URL);
48  		
49  		String newJdbcUrl = oldJdbcUrl.replace("jdbc:", "jdbc:jamon:") + "jamonrealdriver=" + oldDriver;
50  		
51  		bd.getPropertyValues().addPropertyValue(DRIVER_CLASS, new TypedStringValue("com.jamonapi.proxy.JAMonDriver"));
52  		bd.getPropertyValues().addPropertyValue(JDBC_URL, new TypedStringValue(newJdbcUrl));
53  	}
54  
55  	/**
56  	 * @param bd          the beanDefinition
57  	 * @param property    the property name
58  	 * @return            the property value
59  	 */
60  	private String getPropertyValue(BeanDefinition bd, String property) {
61  		Object valueObject = bd.getPropertyValues().getPropertyValue(property).getValue();
62  		if (valueObject instanceof TypedStringValue) {
63  			return ((TypedStringValue) valueObject).getValue();
64  		} else {
65  			return valueObject.toString();
66  		}
67  	}
68  }