From caa7f5579b7e4847793e8bbe26af92af78ff2d41 Mon Sep 17 00:00:00 2001 From: bdaniel Date: Thu, 19 Aug 2010 07:33:11 +0000 Subject: JCA_9013 Validate property types are compatible with getProperty() arguments git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@987051 13f79535-47bb-0310-9956-ffa450edef68 --- .../injection/JavaPropertyValueObjectFactory.java | 29 +++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) (limited to 'sca-java-2.x/trunk/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation') diff --git a/sca-java-2.x/trunk/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/injection/JavaPropertyValueObjectFactory.java b/sca-java-2.x/trunk/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/injection/JavaPropertyValueObjectFactory.java index 606984659d..a190b761e2 100644 --- a/sca-java-2.x/trunk/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/injection/JavaPropertyValueObjectFactory.java +++ b/sca-java-2.x/trunk/modules/implementation-java-runtime/src/main/java/org/apache/tuscany/sca/implementation/java/injection/JavaPropertyValueObjectFactory.java @@ -100,10 +100,34 @@ public class JavaPropertyValueObjectFactory implements PropertyValueFactory { } public B createPropertyValue(ComponentProperty property, Class type) { + + validateTypes(property, type); + ObjectFactory factory = this.createValueFactory(property, property.getValue(), type); return factory.getInstance(); } + private void validateTypes(ComponentProperty property, Class type) { + // JAXB seems to do some strange things with conversions, so + // we can't rely on the databinding conversion from Node->Java to catch + // incompatible types. + + DataType prop1 = property.getProperty().getDataType(); + + if ( (prop1 != null) && (type.isAssignableFrom(prop1.getPhysical())) ) { + return; + } else if ( simpleTypeMapper.getXMLType(type) != null ) { + if ( simpleTypeMapper.getXMLType(type).getQName().equals(property.getXSDType())) + return; + } else if ( isSimpleType(property) ) { + if ( type.isAssignableFrom(simpleTypeMapper.getJavaType(property.getXSDType()))) + return; + } + + throw new IllegalArgumentException("Property type " + prop1.getPhysical().getName() + " is not compatible with " + type.getName()); + + } + abstract class ObjectFactoryImplBase implements ObjectFactory { protected SimpleTypeMapper simpleTypeMapper = new SimpleTypeMapperImpl(); protected Property property; @@ -304,9 +328,12 @@ public class JavaPropertyValueObjectFactory implements PropertyValueFactory { * @return */ private static List getValues(Document document) { + List propValues = new ArrayList(); + if ( document == null ) + return propValues; // The root is the property element Element rootElement = document.getDocumentElement(); - List propValues = new ArrayList(); + NodeList nodes = rootElement.getChildNodes(); for (int count = 0; count < nodes.getLength(); ++count) { Node node = nodes.item(count); -- cgit v1.2.3