You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tomee.apache.org by jg...@apache.org on 2011/06/20 19:28:35 UTC

svn commit: r1137712 - in /openejb/trunk/openejb3/container/openejb-core/src: main/java/org/apache/openejb/config/AnnotationDeployer.java test/java/org/apache/openejb/config/AnnotationDeployerTest.java

Author: jgallimore
Date: Mon Jun 20 17:28:34 2011
New Revision: 1137712

URL: http://svn.apache.org/viewvc?rev=1137712&view=rev
Log:
OPENEJB-1534 fix default values for config properties

Modified:
    openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/config/AnnotationDeployer.java
    openejb/trunk/openejb3/container/openejb-core/src/test/java/org/apache/openejb/config/AnnotationDeployerTest.java

Modified: openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/config/AnnotationDeployer.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/config/AnnotationDeployer.java?rev=1137712&r1=1137711&r2=1137712&view=diff
==============================================================================
--- openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/config/AnnotationDeployer.java (original)
+++ openejb/trunk/openejb3/container/openejb-core/src/main/java/org/apache/openejb/config/AnnotationDeployer.java Mon Jun 20 17:28:34 2011
@@ -705,94 +705,92 @@ public class AnnotationDeployer implemen
 			
 			try {
 				Class<?> clazz = classLoader.loadClass(cls);
+				Object o = clazz.newInstance();
 				
-				// add any annotated method/fields
-				Field[] declaredFields = clazz.getDeclaredFields();
-				for (Field field : declaredFields) {
-					javax.resource.spi.ConfigProperty annotation = field.getAnnotation(javax.resource.spi.ConfigProperty.class);
-					if (annotation == null) {
-						continue;
-					}
-					
-					String name = field.getName();
-					if (! containsConfigProperty(configProperties, name)) {
-						String type = getConfigPropertyType(annotation, field.getType());
-						
-						if (type != null) {
-							ConfigProperty configProperty = new ConfigProperty();
-							configProperties.add(configProperty);
-							
-							configProperty.setConfigPropertyName(name);
-							configProperty.setConfigPropertyType(type);
-							configProperty.setConfigPropertyConfidential(annotation.confidential());
-							configProperty.setConfigPropertyIgnore(annotation.ignore());
-							configProperty.setConfigPropertySupportsDynamicUpdates(annotation.supportsDynamicUpdates());
-							configProperty.setConfigPropertyValue(annotation.defaultValue());
-						}
-					}
-				}
+				// add any introspected properties
+				BeanInfo beanInfo = Introspector.getBeanInfo(clazz);
+				PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
 				
-				Method[] declaredMethods = clazz.getDeclaredMethods();
-				for (Method method : declaredMethods) {
-					if (! method.getName().startsWith("set")) {
-						continue;
-					}
-					
-					if (! (method.getParameterTypes().length == 1)) {
-						continue;
+				for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
+					String name = propertyDescriptor.getName();
+					Class<?> type = propertyDescriptor.getPropertyType();
+					if (type.isPrimitive()) {
+						type = getWrapper(type.getName());
 					}
 					
-					javax.resource.spi.ConfigProperty annotation = method.getAnnotation(javax.resource.spi.ConfigProperty.class);
-					if (annotation == null) {
+					if (! allowedTypes.contains(type.getName())) {
 						continue;
 					}
 					
-					String name = method.getName();
-					
-					
 					if (! containsConfigProperty(configProperties, name)) {
-						String type = getConfigPropertyType(annotation, method.getParameterTypes()[0]);
-						
 						if (type != null) {
 							ConfigProperty configProperty = new ConfigProperty();
 							configProperties.add(configProperty);
 							
+							Object value = null;
+							try {
+								value = propertyDescriptor.getReadMethod().invoke(o);
+							} catch (Exception e) {
+							}
+							
+							javax.resource.spi.ConfigProperty annotation = propertyDescriptor.getWriteMethod().getAnnotation(javax.resource.spi.ConfigProperty.class);
+							if (annotation == null && clazz.getDeclaredField(name) != null) {
+								// if there's no annotation on the setter, we'll try and scrape one off the field itself (assuming the same name)
+								annotation = clazz.getDeclaredField(name).getAnnotation(javax.resource.spi.ConfigProperty.class);
+							}
+							
 							configProperty.setConfigPropertyName(name);
-							configProperty.setConfigPropertyType(type);
-							configProperty.setConfigPropertyConfidential(annotation.confidential());
-							configProperty.setConfigPropertyIgnore(annotation.ignore());
-							configProperty.setConfigPropertySupportsDynamicUpdates(annotation.supportsDynamicUpdates());
-							configProperty.setConfigPropertyValue(annotation.defaultValue());
-							configProperty.setDescriptions(stringsToTexts(annotation.description()));
+							configProperty.setConfigPropertyType(getConfigPropertyType(annotation, type));
+							if (value != null) {
+								configProperty.setConfigPropertyValue(value.toString());
+							}
+							
+							if (annotation != null) {
+								if (annotation.defaultValue() != null && annotation.defaultValue().length() > 0) {
+									configProperty.setConfigPropertyValue(annotation.defaultValue());
+								}
+								configProperty.setConfigPropertyConfidential(annotation.confidential());
+								configProperty.setConfigPropertyIgnore(annotation.ignore());
+								configProperty.setConfigPropertySupportsDynamicUpdates(annotation.supportsDynamicUpdates());
+								configProperty.setDescriptions(stringsToTexts(annotation.description()));
+							}
 						}
 					}
 				}
 				
-				// add any introspected properties
-				BeanInfo beanInfo = Introspector.getBeanInfo(clazz);
-				PropertyDescriptor[] propertyDescriptors = beanInfo.getPropertyDescriptors();
-				
-				for (PropertyDescriptor propertyDescriptor : propertyDescriptors) {
-					String name = propertyDescriptor.getName();
-					Class<?> type = propertyDescriptor.getPropertyType();
-					if (type.isPrimitive()) {
-						type = getWrapper(type.getName());
-					}
+				// add any annotated fields we haven't already picked up
+				Field[] declaredFields = clazz.getDeclaredFields();
+				for (Field field : declaredFields) {
+					javax.resource.spi.ConfigProperty annotation = field.getAnnotation(javax.resource.spi.ConfigProperty.class);
 					
-					if (! allowedTypes.contains(type.getName())) {
-						continue;
+					String name = field.getName();
+					Object value = null;
+					try {
+						value = field.get(o);
+					} catch (Exception e) {
 					}
-					
+
 					if (! containsConfigProperty(configProperties, name)) {
+						String type = getConfigPropertyType(annotation, field.getType());
+						
 						if (type != null) {
 							ConfigProperty configProperty = new ConfigProperty();
 							configProperties.add(configProperty);
 							
 							configProperty.setConfigPropertyName(name);
-							configProperty.setConfigPropertyType(type.getName());
-							configProperty.setConfigPropertyConfidential(false);
-							configProperty.setConfigPropertyIgnore(false);
-							configProperty.setConfigPropertySupportsDynamicUpdates(false);
+							configProperty.setConfigPropertyType(type);
+							if (value != null) {
+								configProperty.setConfigPropertyValue(value.toString());
+							}
+							
+							if (annotation != null) {
+								if (annotation.defaultValue() != null) {
+									configProperty.setConfigPropertyValue(annotation.defaultValue());
+								}
+								configProperty.setConfigPropertyConfidential(annotation.confidential());
+								configProperty.setConfigPropertyIgnore(annotation.ignore());
+								configProperty.setConfigPropertySupportsDynamicUpdates(annotation.supportsDynamicUpdates());
+							}
 						}
 					}
 				}
@@ -802,7 +800,7 @@ public class AnnotationDeployer implemen
 		}
 
 		private String getConfigPropertyType(javax.resource.spi.ConfigProperty annotation, Class<?> type) {
-			Class<?> t = annotation.type();
+			Class<?> t = (annotation == null) ? null : annotation.type();
 			if (t == null || t.equals(Object.class)) {
 				t = type;
 			}

Modified: openejb/trunk/openejb3/container/openejb-core/src/test/java/org/apache/openejb/config/AnnotationDeployerTest.java
URL: http://svn.apache.org/viewvc/openejb/trunk/openejb3/container/openejb-core/src/test/java/org/apache/openejb/config/AnnotationDeployerTest.java?rev=1137712&r1=1137711&r2=1137712&view=diff
==============================================================================
--- openejb/trunk/openejb3/container/openejb-core/src/test/java/org/apache/openejb/config/AnnotationDeployerTest.java (original)
+++ openejb/trunk/openejb3/container/openejb-core/src/test/java/org/apache/openejb/config/AnnotationDeployerTest.java Mon Jun 20 17:28:34 2011
@@ -289,6 +289,7 @@ public class AnnotationDeployerTest {
 		Assert.assertEquals("java.lang.Integer", configProperty.get(0).getConfigPropertyType());
 		Assert.assertEquals("myProperty", configProperty.get(1).getConfigPropertyName());
 		Assert.assertEquals("java.lang.String", configProperty.get(1).getConfigPropertyType());
+		Assert.assertEquals("This is a test", configProperty.get(1).getConfigPropertyValue());
     }
 
 	@ApplicationException(rollback = true)
@@ -459,7 +460,7 @@ public class AnnotationDeployerTest {
     
     @AdministeredObject(adminObjectInterfaces={TestAdminObjectInterface.class})
     public static class TestAdminObject implements TestAdminObjectInterface, SomeOtherInterface {
-    	private String myProperty;
+    	private String myProperty = "This is a test";
     	
     	@javax.resource.spi.ConfigProperty(ignore=true)
     	private int myNumber;