Author: jboynes
Date: Sat Mar 25 13:52:05 2006
New Revision: 388839
URL: http://svn.apache.org/viewcvs?rev=388839&view=rev
Log:
add support for arbitrary configuration property types to StAX parser
Added:
incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/loader/InvalidPropertyFactoryException.java
(with props)
incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/loader/StAXPropertyFactory.java
(with props)
incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/loader/impl/StringParserPropertyFactory.java
(with props)
incubator/tuscany/java/sca/core/src/test/java/org/apache/tuscany/core/loader/StringParserPropertyFactoryTestCase.java
(with props)
incubator/tuscany/java/sca/core/src/test/java/org/apache/tuscany/core/loader/assembly/
incubator/tuscany/java/sca/core/src/test/java/org/apache/tuscany/core/loader/assembly/ComponentLoaderTestCase.java
(with props)
Modified:
incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/config/impl/StAXModuleComponentConfigurationLoaderImpl.java
incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/loader/StAXLoaderRegistry.java
incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/loader/assembly/ComponentLoader.java
incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/loader/impl/StAXLoaderRegistryImpl.java
incubator/tuscany/java/sca/model/src/main/java/org/apache/tuscany/model/assembly/AssemblyFactory.java
incubator/tuscany/java/sca/model/src/main/java/org/apache/tuscany/model/assembly/impl/AssemblyFactoryImpl.java
Modified:
incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/config/impl/StAXModuleComponentConfigurationLoaderImpl.java
URL:
http://svn.apache.org/viewcvs/incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/config/impl/StAXModuleComponentConfigurationLoaderImpl.java?rev=388839&r1=388838&r2=388839&view=diff
==============================================================================
---
incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/config/impl/StAXModuleComponentConfigurationLoaderImpl.java
(original)
+++
incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/config/impl/StAXModuleComponentConfigurationLoaderImpl.java
Sat Mar 25 13:52:05 2006
@@ -44,6 +44,7 @@
}
public Module loadModule(URL url) throws ConfigurationLoadException {
+ registry.setContext(modelContext);
try {
XMLStreamReader reader =
xmlFactory.createXMLStreamReader(url.openStream());
getDocumentRoot(reader);
@@ -52,10 +53,13 @@
throw (ConfigurationLoadException) new
ConfigurationLoadException(url.toString()).initCause(e);
} catch (IOException e) {
throw new ConfigurationLoadException(url.toString(), e);
+ } finally {
+ registry.setContext(null);
}
}
public ModuleFragment loadModuleFragment(URL url) throws
ConfigurationLoadException {
+ registry.setContext(modelContext);
try {
XMLStreamReader reader =
xmlFactory.createXMLStreamReader(url.openStream());
getDocumentRoot(reader);
@@ -64,10 +68,12 @@
throw (ConfigurationLoadException) new
ConfigurationLoadException(url.toString()).initCause(e);
} catch (IOException e) {
throw new ConfigurationLoadException(url.toString(), e);
+ } finally {
+ registry.setContext(null);
}
}
- private void getDocumentRoot(XMLStreamReader reader) throws
XMLStreamException {
+ private static void getDocumentRoot(XMLStreamReader reader) throws
XMLStreamException {
while (true) {
if (reader.next() == XMLStreamConstants.START_ELEMENT) {
return;
Added:
incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/loader/InvalidPropertyFactoryException.java
URL:
http://svn.apache.org/viewcvs/incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/loader/InvalidPropertyFactoryException.java?rev=388839&view=auto
==============================================================================
---
incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/loader/InvalidPropertyFactoryException.java
(added)
+++
incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/loader/InvalidPropertyFactoryException.java
Sat Mar 25 13:52:05 2006
@@ -0,0 +1,47 @@
+/**
+ *
+ * Copyright 2006 The Apache Software Foundation
+ *
+ * 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 org.apache.tuscany.core.loader;
+
+import org.apache.tuscany.core.config.ConfigurationLoadException;
+
+/**
+ * Exception raised if there is a problem configuring a PropertyFactory.
+ *
+ * @version $Rev$ $Date$
+ */
+public class InvalidPropertyFactoryException extends
ConfigurationLoadException {
+ private static final long serialVersionUID = 5017976138519117474L;
+
+ /**
+ * Constructor indicating the cause why the property factory could not be
created.
+ *
+ * @param className the name of the class that is intended to be the
PropertyFactory
+ * @param cause the Throwable that prevented the PropertyFactory from
being created
+ */
+ public InvalidPropertyFactoryException(String className, Throwable cause) {
+ super(className);
+ initCause(cause);
+ }
+
+ /**
+ * Returns the name of the property factory implementation class.
+ * @return the name of the property factory implementation class
+ */
+ public String getClassName() {
+ return getMessage();
+ }
+}
Propchange:
incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/loader/InvalidPropertyFactoryException.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/loader/InvalidPropertyFactoryException.java
------------------------------------------------------------------------------
svn:keywords = Rev,Date
Modified:
incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/loader/StAXLoaderRegistry.java
URL:
http://svn.apache.org/viewcvs/incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/loader/StAXLoaderRegistry.java?rev=388839&r1=388838&r2=388839&view=diff
==============================================================================
---
incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/loader/StAXLoaderRegistry.java
(original)
+++
incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/loader/StAXLoaderRegistry.java
Sat Mar 25 13:52:05 2006
@@ -20,6 +20,7 @@
import javax.xml.stream.XMLStreamReader;
import org.apache.tuscany.model.assembly.AssemblyModelObject;
+import org.apache.tuscany.model.assembly.AssemblyModelContext;
import org.apache.tuscany.common.resource.ResourceLoader;
import org.apache.tuscany.core.config.ConfigurationLoadException;
@@ -66,4 +67,22 @@
* @throws XMLStreamException if there was a problem reading the stream
*/
AssemblyModelObject load(XMLStreamReader reader, ResourceLoader
resourceLoader) throws XMLStreamException, ConfigurationLoadException;
+
+ /**
+ * Hack to allow loaders to initialize model objects on the fly.
+ * Remove when initialization has been moved from the model implementation
to the loader.
+ *
+ * @return the model context for this load operation
+ */
+ @Deprecated
+ AssemblyModelContext getContext();
+
+ /**
+ * Hack to allow loaders to initialize model objects on the fly.
+ * Remove when initialization has been moved from the model implementation
to the loader.
+ *
+ * @param context the model context for this load operation
+ */
+ @Deprecated
+ void setContext(AssemblyModelContext context);
}
Added:
incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/loader/StAXPropertyFactory.java
URL:
http://svn.apache.org/viewcvs/incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/loader/StAXPropertyFactory.java?rev=388839&view=auto
==============================================================================
---
incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/loader/StAXPropertyFactory.java
(added)
+++
incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/loader/StAXPropertyFactory.java
Sat Mar 25 13:52:05 2006
@@ -0,0 +1,42 @@
+/**
+ *
+ * Copyright 2006 The Apache Software Foundation
+ *
+ * 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 org.apache.tuscany.core.loader;
+
+import javax.xml.stream.XMLStreamReader;
+import javax.xml.stream.XMLStreamException;
+
+import org.apache.tuscany.core.builder.ObjectFactory;
+import org.apache.tuscany.core.config.ConfigurationLoadException;
+import org.apache.tuscany.model.assembly.Property;
+
+/**
+ * A factory that will create an ObjectFactory for a property by parsing a
StAX XMLStreamReader.
+ *
+ * @version $Rev$ $Date$
+ */
+public interface StAXPropertyFactory<T> {
+ /**
+ * Return an ObjectFactory for instances of a property defined in an XML
stream.
+ *
+ * @param reader the reader to use to access the XML stream
+ * @param property the Property definition that the resulting
ObjectFactory must be able to assign to
+ * @return an ObjectFactory that can produce instances that can be
assigned to the supplied Property
+ * @throws XMLStreamException if there is a problem reading the stream
+ * @throws ConfigurationLoadException if there is a problem creating the
ObjectFactory
+ */
+ ObjectFactory<T> createObjectFactory(XMLStreamReader reader, Property
property) throws XMLStreamException, ConfigurationLoadException;
+}
Propchange:
incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/loader/StAXPropertyFactory.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/loader/StAXPropertyFactory.java
------------------------------------------------------------------------------
svn:keywords = Rev,Date
Modified:
incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/loader/assembly/ComponentLoader.java
URL:
http://svn.apache.org/viewcvs/incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/loader/assembly/ComponentLoader.java?rev=388839&r1=388838&r2=388839&view=diff
==============================================================================
---
incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/loader/assembly/ComponentLoader.java
(original)
+++
incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/loader/assembly/ComponentLoader.java
Sat Mar 25 13:52:05 2006
@@ -27,23 +27,31 @@
import org.osoa.sca.annotations.Scope;
import org.apache.tuscany.common.resource.ResourceLoader;
+import org.apache.tuscany.core.builder.ObjectFactory;
import org.apache.tuscany.core.config.ConfigurationLoadException;
+import org.apache.tuscany.core.loader.InvalidPropertyFactoryException;
+import org.apache.tuscany.core.loader.StAXPropertyFactory;
+import org.apache.tuscany.core.loader.StAXUtil;
import static
org.apache.tuscany.core.loader.assembly.AssemblyConstants.COMPONENT;
import static
org.apache.tuscany.core.loader.assembly.AssemblyConstants.PROPERTIES;
import static
org.apache.tuscany.core.loader.assembly.AssemblyConstants.REFERENCES;
-import org.apache.tuscany.core.loader.StAXUtil;
+import org.apache.tuscany.core.loader.impl.StringParserPropertyFactory;
import org.apache.tuscany.model.assembly.AssemblyModelObject;
import org.apache.tuscany.model.assembly.Component;
import org.apache.tuscany.model.assembly.ComponentImplementation;
+import org.apache.tuscany.model.assembly.ComponentType;
import org.apache.tuscany.model.assembly.ConfiguredProperty;
import org.apache.tuscany.model.assembly.ConfiguredReference;
import org.apache.tuscany.model.assembly.OverrideOption;
+import org.apache.tuscany.model.assembly.Property;
/**
* @version $Rev$ $Date$
*/
@Scope("MODULE")
public class ComponentLoader extends AbstractLoader {
+ private static final StAXPropertyFactory<?> defaultPropertyFactory = new
StringParserPropertyFactory();
+
public QName getXMLType() {
return COMPONENT;
}
@@ -54,6 +62,7 @@
public Component load(XMLStreamReader reader, ResourceLoader
resourceLoader) throws XMLStreamException, ConfigurationLoadException {
assert COMPONENT.equals(reader.getName());
+
Component component = factory.createSimpleComponent();
component.setName(reader.getAttributeValue(null, "name"));
@@ -62,13 +71,15 @@
case START_ELEMENT:
QName name = reader.getName();
if (PROPERTIES.equals(name)) {
- loadProperties(reader, component);
+ loadProperties(reader, resourceLoader, component);
} else if (REFERENCES.equals(name)) {
loadReferences(reader, component);
} else {
AssemblyModelObject o = registry.load(reader,
resourceLoader);
if (o instanceof ComponentImplementation) {
-
component.setComponentImplementation((ComponentImplementation) o);
+ ComponentImplementation impl =
(ComponentImplementation) o;
+ impl.initialize(registry.getContext());
+ component.setComponentImplementation(impl);
}
}
reader.next();
@@ -79,15 +90,36 @@
}
}
- protected void loadProperties(XMLStreamReader reader, Component component)
throws XMLStreamException {
+ protected void loadProperties(XMLStreamReader reader, ResourceLoader
resourceLoader, Component component) throws XMLStreamException,
ConfigurationLoadException {
+ ComponentType componentType =
component.getComponentImplementation().getComponentType();
List<ConfiguredProperty> configuredProperties =
component.getConfiguredProperties();
while (true) {
switch (reader.next()) {
case START_ELEMENT:
String name = reader.getLocalName();
+ Property property = componentType.getProperty(name);
+ if (property == null) {
+ throw new ConfigurationLoadException(name);
+ }
OverrideOption override =
StAXUtil.overrideOption(reader.getAttributeValue(null, "override"),
OverrideOption.NO);
- String value = reader.getElementText();
+
+ // get a factory for the property
+ StAXPropertyFactory<?> propertyFactory;
+ String factoryName = reader.getAttributeValue(null, "factory");
+ if (factoryName == null) {
+ propertyFactory = defaultPropertyFactory;
+ } else {
+ propertyFactory = getPropertyFactory(factoryName,
resourceLoader);
+ }
+
+ // create the property value
+ // FIXME to support complex types we probably should store the
factory in the ConfiguredProperty
+ // FIXME instead of the value as the value may be mutable and
should not be shared between instances
+ ObjectFactory<?> objectFactory =
propertyFactory.createObjectFactory(reader, property);
+ Object value = objectFactory.getInstance();
+
+ // create the configured property definition
ConfiguredProperty configuredProperty =
factory.createConfiguredProperty();
configuredProperty.setName(name);
configuredProperty.setValue(value);
@@ -97,6 +129,30 @@
case END_ELEMENT:
return;
}
+ }
+ }
+
+ protected StAXPropertyFactory<?> getPropertyFactory(String factoryName,
ResourceLoader resourceLoader) throws InvalidPropertyFactoryException {
+ Class<?> impl;
+ try {
+ // try to load factory from application classloader
+ impl = resourceLoader.loadClass(factoryName);
+ } catch (ClassNotFoundException e) {
+ try {
+ // try to load factory from container classloader
+ impl = Class.forName(factoryName);
+ } catch (ClassNotFoundException e1) {
+ throw new InvalidPropertyFactoryException(factoryName, e);
+ }
+ }
+ try {
+ return (StAXPropertyFactory<?>) impl.newInstance();
+ } catch (InstantiationException e) {
+ throw new InvalidPropertyFactoryException(factoryName, e);
+ } catch (IllegalAccessException e) {
+ throw new InvalidPropertyFactoryException(factoryName, e);
+ } catch (ClassCastException e) {
+ throw new InvalidPropertyFactoryException(factoryName, e);
}
}
Modified:
incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/loader/impl/StAXLoaderRegistryImpl.java
URL:
http://svn.apache.org/viewcvs/incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/loader/impl/StAXLoaderRegistryImpl.java?rev=388839&r1=388838&r2=388839&view=diff
==============================================================================
---
incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/loader/impl/StAXLoaderRegistryImpl.java
(original)
+++
incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/loader/impl/StAXLoaderRegistryImpl.java
Sat Mar 25 13:52:05 2006
@@ -27,6 +27,7 @@
import org.apache.tuscany.core.loader.StAXElementLoader;
import org.apache.tuscany.core.loader.StAXLoaderRegistry;
import org.apache.tuscany.model.assembly.AssemblyModelObject;
+import org.apache.tuscany.model.assembly.AssemblyModelContext;
/**
* @version $Rev$ $Date$
@@ -52,4 +53,16 @@
}
}
+
+ private final ThreadLocal<AssemblyModelContext> modelContext = new
ThreadLocal<AssemblyModelContext>();
+
+ @Deprecated
+ public AssemblyModelContext getContext() {
+ return modelContext.get();
+ }
+
+ @Deprecated
+ public void setContext(AssemblyModelContext context) {
+ modelContext.set(context);
+ }
}
Added:
incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/loader/impl/StringParserPropertyFactory.java
URL:
http://svn.apache.org/viewcvs/incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/loader/impl/StringParserPropertyFactory.java?rev=388839&view=auto
==============================================================================
---
incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/loader/impl/StringParserPropertyFactory.java
(added)
+++
incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/loader/impl/StringParserPropertyFactory.java
Sat Mar 25 13:52:05 2006
@@ -0,0 +1,104 @@
+/**
+ *
+ * Copyright 2006 The Apache Software Foundation
+ *
+ * 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 org.apache.tuscany.core.loader.impl;
+
+import java.lang.reflect.Method;
+import java.lang.reflect.Modifier;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Constructor;
+import java.beans.PropertyEditorManager;
+import java.beans.PropertyEditor;
+import javax.xml.stream.XMLStreamReader;
+import javax.xml.stream.XMLStreamException;
+
+import org.apache.tuscany.core.loader.StAXPropertyFactory;
+import org.apache.tuscany.core.builder.ObjectFactory;
+import org.apache.tuscany.core.injection.SingletonObjectFactory;
+import org.apache.tuscany.core.config.ConfigurationLoadException;
+import org.apache.tuscany.model.assembly.Property;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public class StringParserPropertyFactory implements StAXPropertyFactory {
+ public ObjectFactory<?> createObjectFactory(XMLStreamReader reader,
Property property) throws XMLStreamException, ConfigurationLoadException {
+ Class<?> type = property.getType();
+ assert type != null : "property type is null";
+ String text = reader.getElementText();
+
+ // degenerate case where we are returning a String
+ if (String.class.equals(type)) {
+ return new SingletonObjectFactory(text);
+ }
+
+ // special handler to convert hexBinary to a byte[]
+ if (byte[].class.equals(type)) {
+ byte[] instance = new byte[text.length() >> 1];
+ for (int i = 0; i < instance.length; i++) {
+ instance[i] = (byte) (Character.digit(text.charAt(i << 1), 16)
<< 4 | Character.digit(text.charAt((i << 1) + 1), 16));
+ }
+ return new SingletonObjectFactory(instance);
+ }
+
+ // does this type have a static valueOf(String) method?
+ try {
+ Method valueOf = type.getMethod("valueOf", String.class);
+ if (Modifier.isStatic(valueOf.getModifiers())) {
+ try {
+ return new SingletonObjectFactory(valueOf.invoke(null,
text));
+ } catch (IllegalAccessException e) {
+ throw new AssertionError("getMethod returned an
inaccessible method");
+ } catch (InvocationTargetException e) {
+ // FIXME we should throw something better
+ throw (ConfigurationLoadException) new
ConfigurationLoadException(property.getName()).initCause(e.getCause());
+ }
+ }
+ } catch (NoSuchMethodException e) {
+ // try something else
+ }
+
+ // does this type have a constructor that takes a String?
+ try {
+ Constructor<?> ctr = type.getConstructor(String.class);
+ return new SingletonObjectFactory(ctr.newInstance(text));
+ } catch (NoSuchMethodException e) {
+ // try something else
+ } catch (IllegalAccessException e) {
+ throw new AssertionError("getConstructor returned an inaccessible
method");
+ } catch (InstantiationException e) {
+ throw new ConfigurationLoadException("Property type cannot be
instantiated: " + type.getName());
+ } catch (InvocationTargetException e) {
+ // FIXME we should throw something better
+ throw (ConfigurationLoadException) new
ConfigurationLoadException(property.getName()).initCause(e.getCause());
+ }
+
+ // do we have a property editor for it?
+ PropertyEditor editor = PropertyEditorManager.findEditor(type);
+ if (editor != null) {
+ try {
+ editor.setAsText(text);
+ return new SingletonObjectFactory(editor.getValue());
+ } catch (IllegalArgumentException e) {
+ // FIXME we should throw something better
+ throw (ConfigurationLoadException) new
ConfigurationLoadException(property.getName()).initCause(e.getCause());
+ }
+ }
+
+ // FIXME we should throw something better
+ throw new ConfigurationLoadException("Do not have a way to parse a
String into a " + type.getName());
+ }
+}
Propchange:
incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/loader/impl/StringParserPropertyFactory.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
incubator/tuscany/java/sca/core/src/main/java/org/apache/tuscany/core/loader/impl/StringParserPropertyFactory.java
------------------------------------------------------------------------------
svn:keywords = Rev,Date
Added:
incubator/tuscany/java/sca/core/src/test/java/org/apache/tuscany/core/loader/StringParserPropertyFactoryTestCase.java
URL:
http://svn.apache.org/viewcvs/incubator/tuscany/java/sca/core/src/test/java/org/apache/tuscany/core/loader/StringParserPropertyFactoryTestCase.java?rev=388839&view=auto
==============================================================================
---
incubator/tuscany/java/sca/core/src/test/java/org/apache/tuscany/core/loader/StringParserPropertyFactoryTestCase.java
(added)
+++
incubator/tuscany/java/sca/core/src/test/java/org/apache/tuscany/core/loader/StringParserPropertyFactoryTestCase.java
Sat Mar 25 13:52:05 2006
@@ -0,0 +1,112 @@
+/**
+ *
+ * Copyright 2006 The Apache Software Foundation
+ *
+ * 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 org.apache.tuscany.core.loader;
+
+import java.io.StringReader;
+import java.net.URI;
+import java.util.Arrays;
+import java.beans.PropertyEditorManager;
+import java.beans.PropertyEditorSupport;
+import javax.xml.stream.XMLInputFactory;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamReader;
+
+import junit.framework.TestCase;
+
+import org.apache.tuscany.core.builder.ObjectFactory;
+import org.apache.tuscany.core.config.ConfigurationLoadException;
+import org.apache.tuscany.core.loader.impl.StringParserPropertyFactory;
+import org.apache.tuscany.model.assembly.AssemblyFactory;
+import org.apache.tuscany.model.assembly.Property;
+import org.apache.tuscany.model.assembly.impl.AssemblyFactoryImpl;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public class StringParserPropertyFactoryTestCase extends TestCase {
+ private StringParserPropertyFactory factory;
+ private XMLInputFactory xmlFactory;
+ private Property property;
+
+ public void testSimpleString() throws XMLStreamException,
ConfigurationLoadException {
+ String instance = getInstance(String.class, "<foo>Hello World</foo>");
+ assertEquals("Hello World", instance);
+ }
+
+ public void testByteArray() throws XMLStreamException,
ConfigurationLoadException {
+ byte[] instance = getInstance(byte[].class, "<foo>01020304</foo>");
+ assertTrue(Arrays.equals(new byte[]{1, 2, 3, 4}, instance));
+ }
+
+ public void testInteger() throws XMLStreamException,
ConfigurationLoadException {
+ Integer instance = getInstance(Integer.class, "<foo>1234</foo>");
+ assertEquals(Integer.valueOf(1234), instance);
+ }
+
+ public void testInt() throws XMLStreamException,
ConfigurationLoadException {
+ int instance = getInstance(Integer.TYPE, "<foo>1234</foo>");
+ assertEquals(1234, instance);
+ }
+
+ public void testBoolean() throws XMLStreamException,
ConfigurationLoadException {
+ Boolean instance = getInstance(Boolean.class, "<foo>true</foo>");
+ assertSame(Boolean.TRUE, instance);
+ }
+
+ public void testConstructor() throws XMLStreamException,
ConfigurationLoadException {
+ // java.net.URI has a ctr that takes a String
+ URI instance = getInstance(URI.class,
"<foo>http://www.apache.org</foo>");
+ assertEquals(URI.create("http://www.apache.org"), instance);
+ }
+
+ public void testPropertyEditor() throws XMLStreamException,
ConfigurationLoadException {
+ // register a property editor for java.lang.Class
+ PropertyEditorManager.registerEditor(Class.class, ClassEditor.class);
+ try {
+ Class<?> instance = getInstance(Class.class,
"<foo>java.lang.Integer</foo>");
+ assertEquals(Integer.class, instance);
+ } finally{
+ PropertyEditorManager.registerEditor(Class.class, null);
+ }
+ }
+
+ private <T> T getInstance(Class<T> type, String xml) throws
XMLStreamException, ConfigurationLoadException {
+ property.setType(type);
+ XMLStreamReader reader = xmlFactory.createXMLStreamReader(new
StringReader(xml));
+ reader.next();
+ ObjectFactory<T> objectFactory = (ObjectFactory<T>)
factory.createObjectFactory(reader, property);
+ return objectFactory.getInstance();
+ }
+
+ protected void setUp() throws Exception {
+ super.setUp();
+ factory = new StringParserPropertyFactory();
+ xmlFactory = XMLInputFactory.newInstance();
+ AssemblyFactory assemblyFactory = new AssemblyFactoryImpl();
+ property = assemblyFactory.createProperty();
+ }
+
+ public static class ClassEditor extends PropertyEditorSupport {
+ public void setAsText(String text) throws IllegalArgumentException {
+ try {
+ setValue(Class.forName(text));
+ } catch (ClassNotFoundException e) {
+ throw new IllegalArgumentException(text);
+ }
+ }
+ }
+}
Propchange:
incubator/tuscany/java/sca/core/src/test/java/org/apache/tuscany/core/loader/StringParserPropertyFactoryTestCase.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
incubator/tuscany/java/sca/core/src/test/java/org/apache/tuscany/core/loader/StringParserPropertyFactoryTestCase.java
------------------------------------------------------------------------------
svn:keywords = Rev,Date
Added:
incubator/tuscany/java/sca/core/src/test/java/org/apache/tuscany/core/loader/assembly/ComponentLoaderTestCase.java
URL:
http://svn.apache.org/viewcvs/incubator/tuscany/java/sca/core/src/test/java/org/apache/tuscany/core/loader/assembly/ComponentLoaderTestCase.java?rev=388839&view=auto
==============================================================================
---
incubator/tuscany/java/sca/core/src/test/java/org/apache/tuscany/core/loader/assembly/ComponentLoaderTestCase.java
(added)
+++
incubator/tuscany/java/sca/core/src/test/java/org/apache/tuscany/core/loader/assembly/ComponentLoaderTestCase.java
Sat Mar 25 13:52:05 2006
@@ -0,0 +1,154 @@
+/**
+ *
+ * Copyright 2006 The Apache Software Foundation
+ *
+ * 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 org.apache.tuscany.core.loader.assembly;
+
+import java.io.StringReader;
+import javax.xml.stream.XMLInputFactory;
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamReader;
+
+import junit.framework.TestCase;
+
+import org.apache.tuscany.common.resource.ResourceLoader;
+import org.apache.tuscany.common.resource.impl.ResourceLoaderImpl;
+import org.apache.tuscany.core.config.ConfigurationLoadException;
+import org.apache.tuscany.core.system.assembly.SystemAssemblyFactory;
+import org.apache.tuscany.core.system.assembly.SystemImplementation;
+import org.apache.tuscany.core.system.assembly.impl.SystemAssemblyFactoryImpl;
+import org.apache.tuscany.core.loader.StAXPropertyFactory;
+import org.apache.tuscany.core.builder.ObjectFactory;
+import org.apache.tuscany.core.injection.SingletonObjectFactory;
+import org.apache.tuscany.model.assembly.AssemblyModelContext;
+import org.apache.tuscany.model.assembly.Component;
+import org.apache.tuscany.model.assembly.ConfiguredProperty;
+import org.apache.tuscany.model.assembly.Property;
+import org.apache.tuscany.model.assembly.impl.AssemblyModelContextImpl;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public class ComponentLoaderTestCase extends TestCase {
+ private ComponentLoader loader;
+ private XMLInputFactory xmlFactory;
+ private SystemAssemblyFactory assemblyFactory;
+ private ResourceLoader resourceLoader;
+ private AssemblyModelContext modelContext;
+
+ public void testStringProperty() throws XMLStreamException,
ConfigurationLoadException {
+ String xml =
"<properties><propString>HelloWorld</propString></properties>";
+ Component component = createFooComponent();
+ loadProperties(xml, component);
+ ConfiguredProperty prop =
component.getConfiguredProperty("propString");
+ assertEquals("HelloWorld", prop.getValue());
+ }
+
+ public void testIntProperty() throws XMLStreamException,
ConfigurationLoadException {
+ String xml = "<properties><propInt>1234</propInt></properties>";
+ Component component = createFooComponent();
+ loadProperties(xml, component);
+ ConfiguredProperty prop = component.getConfiguredProperty("propInt");
+ assertEquals(1234, prop.getValue());
+ }
+
+ public void testIntegerProperty() throws XMLStreamException,
ConfigurationLoadException {
+ String xml =
"<properties><propInteger>1234</propInteger></properties>";
+ Component component = createFooComponent();
+ loadProperties(xml, component);
+ ConfiguredProperty prop =
component.getConfiguredProperty("propInteger");
+ assertEquals(Integer.valueOf(1234), prop.getValue());
+ }
+
+ public void testCustomProperty() throws XMLStreamException,
ConfigurationLoadException {
+ String xml = "<properties><propFoo factory='"+
FooFactory.class.getName() + "'><name>Hello</name></propFoo></properties>";
+ Component component = createFooComponent();
+ loadProperties(xml, component);
+ ConfiguredProperty prop = component.getConfiguredProperty("propFoo");
+ Foo instance = (Foo) prop.getValue();
+ assertEquals("Hello", instance.name);
+ }
+
+ private void loadProperties(String xml, Component component) throws
XMLStreamException, ConfigurationLoadException {
+ XMLStreamReader reader = xmlFactory.createXMLStreamReader(new
StringReader(xml));
+ reader.next();
+ loader.loadProperties(reader, resourceLoader, component);
+ component.initialize(modelContext);
+ }
+
+ private Component createFooComponent() {
+ SystemImplementation impl =
assemblyFactory.createSystemImplementation();
+ impl.setImplementationClass(ServiceImpl.class);
+ impl.initialize(modelContext);
+ Component component = assemblyFactory.createSimpleComponent();
+ component.setComponentImplementation(impl);
+ return component;
+ }
+
+ protected void setUp() throws Exception {
+ super.setUp();
+ xmlFactory = XMLInputFactory.newInstance();
+ assemblyFactory = new SystemAssemblyFactoryImpl();
+ resourceLoader = new ResourceLoaderImpl(getClass().getClassLoader());
+ loader = new ComponentLoader();
+ loader.setFactory(assemblyFactory);
+ modelContext = new AssemblyModelContextImpl(assemblyFactory, null,
resourceLoader);
+ }
+
+ public static interface Service {
+ }
+
+ public static class ServiceImpl implements Service {
+ public String propString;
+ public int propInt;
+ public Integer propInteger;
+ public Foo propFoo;
+ }
+
+ public static class Foo {
+ public Foo() {
+ }
+
+ private String name;
+ private Foo foo;
+
+ public void setName(String val) {
+ name = val;
+ }
+
+ public void setFoo(Foo val) {
+ foo = val;
+ }
+/*
+
+ private MyJaxBThing jaxBThing;
+
+ public void setMyJaxBThing(MyJaxBThing thing) {
+ jaxBthing = thing;
+ }
+*/
+ }
+
+ public static class FooFactory implements StAXPropertyFactory<Foo> {
+ public ObjectFactory<Foo> createObjectFactory(XMLStreamReader reader,
Property property) throws XMLStreamException, ConfigurationLoadException {
+ reader.nextTag();
+ String name = reader.getElementText();
+ reader.next();
+ Foo foo = new Foo();
+ foo.setName(name);
+ return new SingletonObjectFactory(foo);
+ }
+ }
+}
Propchange:
incubator/tuscany/java/sca/core/src/test/java/org/apache/tuscany/core/loader/assembly/ComponentLoaderTestCase.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
incubator/tuscany/java/sca/core/src/test/java/org/apache/tuscany/core/loader/assembly/ComponentLoaderTestCase.java
------------------------------------------------------------------------------
svn:keywords = Rev,Date
Modified:
incubator/tuscany/java/sca/model/src/main/java/org/apache/tuscany/model/assembly/AssemblyFactory.java
URL:
http://svn.apache.org/viewcvs/incubator/tuscany/java/sca/model/src/main/java/org/apache/tuscany/model/assembly/AssemblyFactory.java?rev=388839&r1=388838&r2=388839&view=diff
==============================================================================
---
incubator/tuscany/java/sca/model/src/main/java/org/apache/tuscany/model/assembly/AssemblyFactory.java
(original)
+++
incubator/tuscany/java/sca/model/src/main/java/org/apache/tuscany/model/assembly/AssemblyFactory.java
Sat Mar 25 13:52:05 2006
@@ -229,4 +229,13 @@
* @return an EntryPoint that exposes the supplied service using the
supplied bindng and which is wired using the supplied reference
*/
EntryPoint createEntryPoint(String entryPointName, ConfiguredService
configuredService, Binding binding, ConfiguredReference configuredReference);
+
+ /**
+ * Helper method for creating a simple Property.
+ *
+ * @param name the property name
+ * @param type the Java type of the property
+ * @return a Property with the supplied name and type
+ */
+ Property createProperty(String name, Class<?> type);
}
Modified:
incubator/tuscany/java/sca/model/src/main/java/org/apache/tuscany/model/assembly/impl/AssemblyFactoryImpl.java
URL:
http://svn.apache.org/viewcvs/incubator/tuscany/java/sca/model/src/main/java/org/apache/tuscany/model/assembly/impl/AssemblyFactoryImpl.java?rev=388839&r1=388838&r2=388839&view=diff
==============================================================================
---
incubator/tuscany/java/sca/model/src/main/java/org/apache/tuscany/model/assembly/impl/AssemblyFactoryImpl.java
(original)
+++
incubator/tuscany/java/sca/model/src/main/java/org/apache/tuscany/model/assembly/impl/AssemblyFactoryImpl.java
Sat Mar 25 13:52:05 2006
@@ -253,4 +253,11 @@
entryPoint.getBindings().add((Binding)binding);
return entryPoint;
}
+
+ public Property createProperty(String name, Class<?> type) {
+ Property property = createProperty();
+ property.setName(name);
+ property.setType(type);
+ return property;
+ }
}