From: <fg...@us...> - 2009-03-15 15:34:45
|
Revision: 1095 http://openutils.svn.sourceforge.net/openutils/?rev=1095&view=rev Author: fgiust Date: 2009-03-15 15:34:35 +0000 (Sun, 15 Mar 2009) Log Message: ----------- added processing of annotations Modified Paths: -------------- trunk/openutils-deployment/src/main/java/it/openutils/deployment/spring/EnvironmentPropertyConfigurer.java trunk/openutils-deployment/src/site/changes/changes.xml Added Paths: ----------- trunk/openutils-deployment/src/main/java/com/ trunk/openutils-deployment/src/main/java/com/sinapsi/ trunk/openutils-deployment/src/main/java/com/sinapsi/spikes/ trunk/openutils-deployment/src/main/java/com/sinapsi/spikes/ecom/ trunk/openutils-deployment/src/main/java/com/sinapsi/spikes/ecom/utils/ trunk/openutils-deployment/src/main/java/it/openutils/deployment/spring/Property.java trunk/openutils-deployment/src/main/java/it/openutils/deployment/spring/PropertyAnnotationsUtils.java trunk/openutils-deployment/src/test/java/it/openutils/deployment/ trunk/openutils-deployment/src/test/java/it/openutils/deployment/spring/ trunk/openutils-deployment/src/test/java/it/openutils/deployment/spring/PropertyInjectBeanPostProcessorTest.java trunk/openutils-deployment/src/test/java/it/openutils/deployment/spring/TestBean.java trunk/openutils-deployment/src/test/resources/ trunk/openutils-deployment/src/test/resources/environment.properties trunk/openutils-deployment/src/test/resources/log4j.dtd trunk/openutils-deployment/src/test/resources/log4j.xml trunk/openutils-deployment/src/test/resources/spring-tests.xml Modified: trunk/openutils-deployment/src/main/java/it/openutils/deployment/spring/EnvironmentPropertyConfigurer.java =================================================================== --- trunk/openutils-deployment/src/main/java/it/openutils/deployment/spring/EnvironmentPropertyConfigurer.java 2009-03-15 15:30:30 UTC (rev 1094) +++ trunk/openutils-deployment/src/main/java/it/openutils/deployment/spring/EnvironmentPropertyConfigurer.java 2009-03-15 15:34:35 UTC (rev 1095) @@ -15,8 +15,10 @@ */ package it.openutils.deployment.spring; +import java.beans.PropertyDescriptor; import java.io.IOException; import java.io.InputStream; +import java.lang.reflect.Constructor; import java.net.InetAddress; import java.net.MalformedURLException; import java.net.URL; @@ -30,8 +32,10 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.BeansException; +import org.springframework.beans.PropertyValues; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer; +import org.springframework.beans.factory.config.SmartInstantiationAwareBeanPostProcessor; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.core.io.Resource; @@ -44,7 +48,10 @@ * @author fgiust * @version $Id: $ */ -public class EnvironmentPropertyConfigurer extends PropertyPlaceholderConfigurer implements ApplicationContextAware +public class EnvironmentPropertyConfigurer extends PropertyPlaceholderConfigurer + implements + ApplicationContextAware, + SmartInstantiationAwareBeanPostProcessor { /** @@ -336,4 +343,47 @@ // ignore, we are not in a web project or spring web is not available } } + + public boolean postProcessAfterInstantiation(Object bean, String beanName) throws BeansException + { + PropertyAnnotationsUtils.autowireProperties(bean, properties); + return true; + } + + public Class predictBeanType(Class beanClass, String beanName) + { + return null; + } + + public Constructor[] determineCandidateConstructors(Class beanClass, String beanName) throws BeansException + { + return null; + } + + public Object getEarlyBeanReference(Object bean, String beanName) throws BeansException + { + return bean; + } + + public Object postProcessBeforeInstantiation(Class beanClass, String beanName) throws BeansException + { + return null; + } + + public PropertyValues postProcessPropertyValues(PropertyValues pvs, PropertyDescriptor[] pds, Object bean, + String beanName) throws BeansException + { + + return pvs; + } + + public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException + { + return bean; + } + + public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException + { + return bean; + } } Added: trunk/openutils-deployment/src/main/java/it/openutils/deployment/spring/Property.java =================================================================== --- trunk/openutils-deployment/src/main/java/it/openutils/deployment/spring/Property.java (rev 0) +++ trunk/openutils-deployment/src/main/java/it/openutils/deployment/spring/Property.java 2009-03-15 15:34:35 UTC (rev 1095) @@ -0,0 +1,38 @@ +/* + * Copyright Openmind http://www.openmindonline.it + * + * 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 it.openutils.deployment.spring; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + + +/** + * Autowire a property value in a field. + * @author fgiust + * @version $Id$ + */ +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.FIELD) +public @interface Property { + + /** + * The property key + * @return The property key + */ + String value(); +} \ No newline at end of file Property changes on: trunk/openutils-deployment/src/main/java/it/openutils/deployment/spring/Property.java ___________________________________________________________________ Added: svn:mime-type + text/plain Added: svn:keywords + Author Date Id Revision Added: svn:eol-style + native Added: trunk/openutils-deployment/src/main/java/it/openutils/deployment/spring/PropertyAnnotationsUtils.java =================================================================== --- trunk/openutils-deployment/src/main/java/it/openutils/deployment/spring/PropertyAnnotationsUtils.java (rev 0) +++ trunk/openutils-deployment/src/main/java/it/openutils/deployment/spring/PropertyAnnotationsUtils.java 2009-03-15 15:34:35 UTC (rev 1095) @@ -0,0 +1,69 @@ +/* + * Copyright Openmind http://www.openmindonline.it + * + * 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 it.openutils.deployment.spring; + +import java.lang.reflect.Field; +import java.lang.reflect.Modifier; +import java.util.Properties; + +import org.springframework.beans.SimpleTypeConverter; +import org.springframework.util.ReflectionUtils; + + +/** + * Utility class for doing property replacement in fields. + * @author fgiust + * @version $Id$ + */ +public final class PropertyAnnotationsUtils +{ + + private static SimpleTypeConverter typeConverter = new SimpleTypeConverter(); + + private PropertyAnnotationsUtils() + { + // don't instantiate + } + + public static void autowireProperties(final Object bean, final Properties properties) + { + ReflectionUtils.doWithFields(bean.getClass(), new ReflectionUtils.FieldCallback() + { + + public void doWith(Field field) throws IllegalArgumentException, IllegalAccessException + { + Property annotation = field.getAnnotation(Property.class); + if (annotation != null) + { + if (Modifier.isStatic(field.getModifiers())) + { + throw new IllegalStateException( + "PropertyAutowired annotation is not supported on static fields"); + } + + Object strValue = properties.get(annotation.value()); + + if (strValue != null) + { + Object value = typeConverter.convertIfNecessary(strValue, field.getType()); + ReflectionUtils.makeAccessible(field); + field.set(bean, value); + } + } + } + }); + } +} Property changes on: trunk/openutils-deployment/src/main/java/it/openutils/deployment/spring/PropertyAnnotationsUtils.java ___________________________________________________________________ Added: svn:mime-type + text/plain Added: svn:keywords + Author Date Id Revision Added: svn:eol-style + native Modified: trunk/openutils-deployment/src/site/changes/changes.xml =================================================================== --- trunk/openutils-deployment/src/site/changes/changes.xml 2009-03-15 15:30:30 UTC (rev 1094) +++ trunk/openutils-deployment/src/site/changes/changes.xml 2009-03-15 15:34:35 UTC (rev 1095) @@ -8,6 +8,10 @@ <author email="fgiust(at)users.sourceforge.net">Fabrizio Giustina</author> </properties> <body> + <release version="2.1" date="2009-03-15" description=""> + <action type="add" dev="fgiust">EnvironmentPropertyConfigurer now also inject properties in fields annotated with + the @Property annotation.</action> + </release> <release version="2.0.2" date="2008-09-18" description=""> <action type="fix" dev="fgiust"> Added debug logs to DeploymentResolver, and solved resolution of webapp root when the root real path returned by the container ends with "/".</action> Added: trunk/openutils-deployment/src/test/java/it/openutils/deployment/spring/PropertyInjectBeanPostProcessorTest.java =================================================================== --- trunk/openutils-deployment/src/test/java/it/openutils/deployment/spring/PropertyInjectBeanPostProcessorTest.java (rev 0) +++ trunk/openutils-deployment/src/test/java/it/openutils/deployment/spring/PropertyInjectBeanPostProcessorTest.java 2009-03-15 15:34:35 UTC (rev 1095) @@ -0,0 +1,48 @@ +/* + * Copyright Openmind http://www.openmindonline.it + * + * 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 it.openutils.deployment.spring; + +import junit.framework.Assert; + +import org.junit.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests; + + +/** + * @author fgiust + * @version $Id$ + */ +@ContextConfiguration(locations = {"/spring-tests.xml" }) +public class PropertyInjectBeanPostProcessorTest extends AbstractJUnit4SpringContextTests +{ + + @Autowired(required = true) + private TestBean testBean; + + @Test + public void testIntProperty() + { + Assert.assertEquals(new Integer(2), testBean.getIntProperty()); + } + + @Test + public void testStringProperty() + { + Assert.assertEquals("property", testBean.getStringProperty()); + } +} Property changes on: trunk/openutils-deployment/src/test/java/it/openutils/deployment/spring/PropertyInjectBeanPostProcessorTest.java ___________________________________________________________________ Added: svn:mime-type + text/plain Added: svn:keywords + Author Date Id Revision Added: svn:eol-style + native Added: trunk/openutils-deployment/src/test/java/it/openutils/deployment/spring/TestBean.java =================================================================== --- trunk/openutils-deployment/src/test/java/it/openutils/deployment/spring/TestBean.java (rev 0) +++ trunk/openutils-deployment/src/test/java/it/openutils/deployment/spring/TestBean.java 2009-03-15 15:34:35 UTC (rev 1095) @@ -0,0 +1,52 @@ +/* + * Copyright Openmind http://www.openmindonline.it + * + * 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 it.openutils.deployment.spring; + +import org.springframework.stereotype.Component; + + +/** + * @author fgiust + * @version $Id$ + */ +@Component +public class TestBean +{ + + @Property("intProperty") + private Integer intProperty; + + @Property("stringProperty") + private String stringProperty; + + /** + * Returns the intProperty. + * @return the intProperty + */ + public Integer getIntProperty() + { + return intProperty; + } + + /** + * Returns the stringProperty. + * @return the stringProperty + */ + public String getStringProperty() + { + return stringProperty; + } +} Property changes on: trunk/openutils-deployment/src/test/java/it/openutils/deployment/spring/TestBean.java ___________________________________________________________________ Added: svn:mime-type + text/plain Added: svn:keywords + Author Date Id Revision Added: svn:eol-style + native Added: trunk/openutils-deployment/src/test/resources/environment.properties =================================================================== --- trunk/openutils-deployment/src/test/resources/environment.properties (rev 0) +++ trunk/openutils-deployment/src/test/resources/environment.properties 2009-03-15 15:34:35 UTC (rev 1095) @@ -0,0 +1,2 @@ +intProperty=2 +stringProperty=property Property changes on: trunk/openutils-deployment/src/test/resources/environment.properties ___________________________________________________________________ Added: svn:mime-type + text/plain Added: svn:keywords + Author Date Id Revision Added: svn:eol-style + native Added: trunk/openutils-deployment/src/test/resources/log4j.dtd =================================================================== --- trunk/openutils-deployment/src/test/resources/log4j.dtd (rev 0) +++ trunk/openutils-deployment/src/test/resources/log4j.dtd 2009-03-15 15:34:35 UTC (rev 1095) @@ -0,0 +1,166 @@ +<?xml version="1.0" encoding="UTF-8" ?> + +<!-- Authors: Chris Taylor, Ceki Gulcu. --> + +<!-- Version: 1.2 --> + +<!-- A configuration element consists of optional renderer +elements,appender elements, categories and an optional root +element. --> + +<!ELEMENT log4j:configuration (renderer*, appender*,(category|logger)*,root?, + categoryFactory?)> + +<!-- The "threshold" attribute takes a level value such that all --> +<!-- logging statements with a level equal or below this value are --> +<!-- disabled. --> + +<!-- Setting the "debug" enable the printing of internal log4j logging --> +<!-- statements. --> + +<!-- By default, debug attribute is "null", meaning that we not do touch --> +<!-- internal log4j logging settings. The "null" value for the threshold --> +<!-- attribute can be misleading. The threshold field of a repository --> +<!-- cannot be set to null. The "null" value for the threshold attribute --> +<!-- simply means don't touch the threshold field, the threshold field --> +<!-- keeps its old value. --> + +<!ATTLIST log4j:configuration + xmlns:log4j CDATA #FIXED "http://jakarta.apache.org/log4j/" + threshold (all|debug|info|warn|error|fatal|off|null) "null" + debug (true|false|null) "null" +> + +<!-- renderer elements allow the user to customize the conversion of --> +<!-- message objects to String. --> + +<!ELEMENT renderer EMPTY> +<!ATTLIST renderer + renderedClass CDATA #REQUIRED + renderingClass CDATA #REQUIRED +> + +<!-- Appenders must have a name and a class. --> +<!-- Appenders may contain an error handler, a layout, optional parameters --> +<!-- and filters. They may also reference (or include) other appenders. --> +<!ELEMENT appender (errorHandler?, param*, layout?, filter*, appender-ref*)> +<!ATTLIST appender + name ID #REQUIRED + class CDATA #REQUIRED +> + +<!ELEMENT layout (param*)> +<!ATTLIST layout + class CDATA #REQUIRED +> + +<!ELEMENT filter (param*)> +<!ATTLIST filter + class CDATA #REQUIRED +> + +<!-- ErrorHandlers can be of any class. They can admit any number of --> +<!-- parameters. --> + +<!ELEMENT errorHandler (param*, root-ref?, logger-ref*, appender-ref?)> +<!ATTLIST errorHandler + class CDATA #REQUIRED +> + +<!ELEMENT root-ref EMPTY> + +<!ELEMENT logger-ref EMPTY> +<!ATTLIST logger-ref + ref IDREF #REQUIRED +> + +<!ELEMENT param EMPTY> +<!ATTLIST param + name CDATA #REQUIRED + value CDATA #REQUIRED +> + + +<!-- The priority class is org.apache.log4j.Level by default --> +<!ELEMENT priority (param*)> +<!ATTLIST priority + class CDATA #IMPLIED + value CDATA #REQUIRED +> + +<!-- The level class is org.apache.log4j.Level by default --> +<!ELEMENT level (param*)> +<!ATTLIST level + class CDATA #IMPLIED + value CDATA #REQUIRED +> + + +<!-- If no level element is specified, then the configurator MUST not --> +<!-- touch the level of the named category. --> +<!ELEMENT category (param*,(priority|level)?,appender-ref*)> +<!ATTLIST category + class CDATA #IMPLIED + name CDATA #REQUIRED + additivity (true|false) "true" +> + +<!-- If no level element is specified, then the configurator MUST not --> +<!-- touch the level of the named logger. --> +<!ELEMENT logger (level?,appender-ref*)> +<!ATTLIST logger + name ID #REQUIRED + additivity (true|false) "true" +> + + +<!ELEMENT categoryFactory (param*)> +<!ATTLIST categoryFactory + class CDATA #REQUIRED> + +<!ELEMENT appender-ref EMPTY> +<!ATTLIST appender-ref + ref IDREF #REQUIRED +> + +<!-- If no priority element is specified, then the configurator MUST not --> +<!-- touch the priority of root. --> +<!-- The root category always exists and cannot be subclassed. --> +<!ELEMENT root (param*, (priority|level)?, appender-ref*)> + + +<!-- ==================================================================== --> +<!-- A logging event --> +<!-- ==================================================================== --> +<!ELEMENT log4j:eventSet (log4j:event*)> +<!ATTLIST log4j:eventSet + xmlns:log4j CDATA #FIXED "http://jakarta.apache.org/log4j/" + version (1.1|1.2) "1.2" + includesLocationInfo (true|false) "true" +> + + + +<!ELEMENT log4j:event (log4j:message, log4j:NDC?, log4j:throwable?, + log4j:locationInfo?) > + +<!-- The timestamp format is application dependent. --> +<!ATTLIST log4j:event + logger CDATA #REQUIRED + level CDATA #REQUIRED + thread CDATA #REQUIRED + timestamp CDATA #REQUIRED +> + +<!ELEMENT log4j:message (#PCDATA)> +<!ELEMENT log4j:NDC (#PCDATA)> + +<!ELEMENT log4j:throwable (#PCDATA)> + +<!ELEMENT log4j:locationInfo EMPTY> +<!ATTLIST log4j:locationInfo + class CDATA #REQUIRED + method CDATA #REQUIRED + file CDATA #REQUIRED + line CDATA #REQUIRED +> Property changes on: trunk/openutils-deployment/src/test/resources/log4j.dtd ___________________________________________________________________ Added: svn:mime-type + text/xml Added: svn:eol-style + native Added: trunk/openutils-deployment/src/test/resources/log4j.xml =================================================================== --- trunk/openutils-deployment/src/test/resources/log4j.xml (rev 0) +++ trunk/openutils-deployment/src/test/resources/log4j.xml 2009-03-15 15:34:35 UTC (rev 1095) @@ -0,0 +1,29 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"> +<log4j:configuration> + <!-- log4j test configuration --> + <appender name="test-appender" class="org.apache.log4j.ConsoleAppender"> + <layout class="org.apache.log4j.PatternLayout"> + <param name="ConversionPattern" value="%-5p %c.%M(%C{1}.java:%L) %m%n" /> + </layout> + </appender> + <category name="org"> + <priority value="WARN" /> + </category> + <category name="com"> + <priority value="WARN" /> + </category> + <category name="net"> + <priority value="WARN" /> + </category> + <category name="info"> + <priority value="WARN" /> + </category> + <category name="it"> + <priority value="WARN" /> + </category> + <root> + <priority value="debug" /> + <appender-ref ref="test-appender" /> + </root> +</log4j:configuration> \ No newline at end of file Property changes on: trunk/openutils-deployment/src/test/resources/log4j.xml ___________________________________________________________________ Added: svn:mime-type + text/plain Added: svn:keywords + Author Date Id Revision Added: svn:eol-style + native Added: trunk/openutils-deployment/src/test/resources/spring-tests.xml =================================================================== --- trunk/openutils-deployment/src/test/resources/spring-tests.xml (rev 0) +++ trunk/openutils-deployment/src/test/resources/spring-tests.xml 2009-03-15 15:34:35 UTC (rev 1095) @@ -0,0 +1,13 @@ +<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xmlns:context="http://www.springframework.org/schema/context" + xsi:schemaLocation="http://www.springframework.org/schema/beans + http://www.springframework.org/schema/beans/spring-beans-2.5.xsd + http://www.springframework.org/schema/context + http://www.springframework.org/schema/context/spring-context-2.5.xsd"> + <bean id="environmentProperties" class="it.openutils.deployment.spring.EnvironmentPropertyConfigurer"> + <property name="fileLocation" value="classpath:environment.properties" /> + <property name="ignoreUnresolvablePlaceholders" value="true" /> + </bean> + <context:annotation-config /> + <context:component-scan base-package="it.openutils.deployment.spring" /> +</beans> \ No newline at end of file Property changes on: trunk/openutils-deployment/src/test/resources/spring-tests.xml ___________________________________________________________________ Added: svn:mime-type + text/plain Added: svn:keywords + Author Date Id Revision Added: svn:eol-style + native This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |