You are viewing a plain text version of this content. The canonical link for it is here.
Posted to scm@geronimo.apache.org by ga...@apache.org on 2009/04/29 21:46:44 UTC
svn commit: r769897 - in /geronimo/sandbox/blueprint/blueprint-core/src:
main/java/org/apache/geronimo/blueprint/context/
main/java/org/apache/geronimo/blueprint/convert/
test/java/org/apache/geronimo/blueprint/
test/java/org/apache/geronimo/blueprint/...
Author: gawor
Date: Wed Apr 29 19:46:43 2009
New Revision: 769897
URL: http://svn.apache.org/viewvc?rev=769897&view=rev
Log:
injection and type conversion updates
Added:
geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/context/TypedRecipe.java (with props)
Removed:
geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/context/NullRecipe.java
Modified:
geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/context/BlueprintContextImpl.java
geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/context/BlueprintObjectRecipe.java
geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/convert/ConversionServiceImpl.java
geronimo/sandbox/blueprint/blueprint-core/src/test/java/org/apache/geronimo/blueprint/WiringTest.java
geronimo/sandbox/blueprint/blueprint-core/src/test/java/org/apache/geronimo/blueprint/convert/ConversionServiceImplTest.java
geronimo/sandbox/blueprint/blueprint-core/src/test/java/org/apache/geronimo/blueprint/pojos/Multiple.java
geronimo/sandbox/blueprint/blueprint-core/src/test/resources/test-constructor.xml
Modified: geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/context/BlueprintContextImpl.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/context/BlueprintContextImpl.java?rev=769897&r1=769896&r2=769897&view=diff
==============================================================================
--- geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/context/BlueprintContextImpl.java (original)
+++ geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/context/BlueprintContextImpl.java Wed Apr 29 19:46:43 2009
@@ -107,7 +107,7 @@
this.sender = sender;
this.handlers = handlers;
this.urls = urls;
- this.conversionService = new ConversionServiceImpl();
+ this.conversionService = new ConversionServiceImpl(this);
this.helperComponentDefinitionRegistry = new ComponentDefinitionRegistryImpl();
this.componentDefinitionRegistry = new ComponentDefinitionRegistryImpl();
this.executors = executors;
Modified: geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/context/BlueprintObjectRecipe.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/context/BlueprintObjectRecipe.java?rev=769897&r1=769896&r2=769897&view=diff
==============================================================================
--- geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/context/BlueprintObjectRecipe.java (original)
+++ geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/context/BlueprintObjectRecipe.java Wed Apr 29 19:46:43 2009
@@ -37,6 +37,8 @@
import org.apache.geronimo.blueprint.utils.ArgumentsMatcher;
import org.apache.geronimo.blueprint.utils.ReflectionUtils;
import org.osgi.service.blueprint.reflect.BeanArgument;
+import org.osgi.service.blueprint.reflect.CollectionMetadata;
+import org.osgi.service.blueprint.reflect.MapMetadata;
import org.osgi.service.blueprint.reflect.Metadata;
import org.osgi.service.blueprint.reflect.ValueMetadata;
@@ -140,15 +142,16 @@
BeanArgument argument = beanArguments.get(i);
Class type = loadClass(argument.getValueType());
Object obj = arguments.get(i);
- if (obj == null) {
- obj = new NullRecipe(type);
- } else if (obj instanceof Recipe) {
- if (type != null || shouldPreinstantiate(argument.getValue())) {
- obj = RecipeHelper.convert(Object.class, obj, refAllowed);
- obj = convert(obj, type);
- }
+ if (type != null) {
+ obj = new TypedRecipe(blueprintContext.getConversionService(), type, obj);
} else {
- obj = convert(obj, type);
+ if (obj == null) {
+ obj = new TypedRecipe();
+ } else if (obj instanceof Recipe) {
+ if (shouldPreinstantiate(argument.getValue())) {
+ obj = RecipeHelper.convert(Object.class, obj, refAllowed);
+ }
+ }
}
args.add(obj);
}
@@ -160,6 +163,10 @@
if (metadata instanceof ValueMetadata) {
ValueMetadata stringValue = (ValueMetadata) metadata;
return (stringValue.getTypeName() != null);
+ } else if (metadata instanceof MapMetadata) {
+ return false;
+ } else if (metadata instanceof CollectionMetadata) {
+ return false;
}
return true;
}
Added: geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/context/TypedRecipe.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/context/TypedRecipe.java?rev=769897&view=auto
==============================================================================
--- geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/context/TypedRecipe.java (added)
+++ geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/context/TypedRecipe.java Wed Apr 29 19:46:43 2009
@@ -0,0 +1,76 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.geronimo.blueprint.context;
+
+import java.lang.reflect.Type;
+
+import org.apache.xbean.recipe.AbstractRecipe;
+import org.apache.xbean.recipe.ConstructionException;
+import org.apache.xbean.recipe.Recipe;
+import org.apache.xbean.recipe.RecipeHelper;
+import org.osgi.service.blueprint.convert.ConversionService;
+
+/**
+ * Recipe that always returns null but only for a specified type.
+ *
+ * @author <a href="mailto:dev@geronimo.apache.org">Apache Geronimo Project</a>
+ * @version $Rev$, $Date$
+ */
+public class TypedRecipe extends AbstractRecipe {
+
+ private Class type;
+ private Object value;
+ private ConversionService conversionService;
+
+ public TypedRecipe() {
+ this(null, null, null);
+ }
+
+ public TypedRecipe(ConversionService conversionService, Class type, Object value) {
+ this.conversionService = conversionService;
+ this.type = (type == null) ? Object.class : type;
+ this.value = value;
+ }
+
+ @Override
+ protected Object internalCreate(Type expectedType, boolean lazyRefAllowed) throws ConstructionException {
+ if (!canCreate(expectedType)) {
+ throw new ConstructionException("Invalid expectedType: " + expectedType + " " + type);
+ }
+
+ Object obj = value;
+ if (value == null) {
+ return null;
+ } else if (value instanceof Recipe) {
+ obj = RecipeHelper.convert(Object.class, value, lazyRefAllowed);
+ }
+
+ try {
+ return conversionService.convert(obj, type);
+ } catch (Exception e) {
+ throw new ConstructionException("Failed to convert", e);
+ }
+ }
+
+ public boolean canCreate(Type expectedType) {
+ Class expectedClass = RecipeHelper.toClass(expectedType);
+ return expectedClass == type;
+ }
+
+}
Propchange: geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/context/TypedRecipe.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/context/TypedRecipe.java
------------------------------------------------------------------------------
svn:keywords = Date Revision
Propchange: geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/context/TypedRecipe.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified: geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/convert/ConversionServiceImpl.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/convert/ConversionServiceImpl.java?rev=769897&r1=769896&r2=769897&view=diff
==============================================================================
--- geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/convert/ConversionServiceImpl.java (original)
+++ geronimo/sandbox/blueprint/blueprint-core/src/main/java/org/apache/geronimo/blueprint/convert/ConversionServiceImpl.java Wed Apr 29 19:46:43 2009
@@ -29,6 +29,7 @@
import java.util.Properties;
import java.util.regex.Pattern;
+import org.apache.geronimo.blueprint.context.BlueprintContextImpl;
import org.osgi.service.blueprint.convert.ConversionService;
import org.osgi.service.blueprint.convert.Converter;
@@ -47,8 +48,13 @@
*/
public class ConversionServiceImpl implements ConversionService {
+ private BlueprintContextImpl blueprintContext;
private Map<Class, List<Converter>> convertersMap = new HashMap<Class, List<Converter>>();
+ public ConversionServiceImpl(BlueprintContextImpl blueprintContext) {
+ this.blueprintContext = blueprintContext;
+ }
+
public void registerConverter(Converter converter) {
Class type = converter.getTargetClass();
List<Converter> converters = convertersMap.get(type);
@@ -70,8 +76,6 @@
public Object convert(Object fromValue, Class toType) throws Exception {
if (toType.isInstance(fromValue)) {
return fromValue;
- } else if (Class.class == toType) {
- return fromValue.getClass();
}
Object value = doConvert(fromValue, toType);
if (value == null) {
@@ -127,7 +131,13 @@
}
private Object convertString(String value, Class toType) throws Exception {
- if (Locale.class == toType) {
+ if (Class.class == toType) {
+ try {
+ return blueprintContext.getClassLoader().loadClass(value);
+ } catch (ClassNotFoundException e) {
+ throw new Exception("Unable to convert", e);
+ }
+ } else if (Locale.class == toType) {
String[] tokens = value.split("_");
if (tokens.length == 1) {
return new Locale(tokens[0]);
Modified: geronimo/sandbox/blueprint/blueprint-core/src/test/java/org/apache/geronimo/blueprint/WiringTest.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/blueprint/blueprint-core/src/test/java/org/apache/geronimo/blueprint/WiringTest.java?rev=769897&r1=769896&r2=769897&view=diff
==============================================================================
--- geronimo/sandbox/blueprint/blueprint-core/src/test/java/org/apache/geronimo/blueprint/WiringTest.java (original)
+++ geronimo/sandbox/blueprint/blueprint-core/src/test/java/org/apache/geronimo/blueprint/WiringTest.java Wed Apr 29 19:46:43 2009
@@ -195,19 +195,22 @@
}
Object obj6 = graph.create("multipleInt");
- testMultiple(obj6, null, 123);
+ testMultiple(obj6, null, 123, null);
- Object obj7 = graph.create("multipleString");
- testMultiple(obj7, "123", -1);
+ Object obj7 = graph.create("multipleInteger");
+ testMultiple(obj7, null, -1, new Integer(123));
- Object obj8 = graph.create("multipleStringConvertable");
- testMultiple(obj8, "hello", -1);
+ Object obj8 = graph.create("multipleString");
+ testMultiple(obj8, "123", -1, null);
- Object obj9 = graph.create("multipleFactory1");
- testMultiple(obj9, null, 1234);
+ Object obj9 = graph.create("multipleStringConvertable");
+ testMultiple(obj9, "hello", -1, null);
+
+ Object obj10 = graph.create("multipleFactory1");
+ testMultiple(obj10, null, 1234, null);
- Object obj10 = graph.create("multipleFactory2");
- testMultiple(obj10, "helloCreate-boolean", -1);
+ Object obj11 = graph.create("multipleFactory2");
+ testMultiple(obj11, "helloCreate-boolean", -1, null);
try {
graph.create("multipleFactoryNull");
@@ -217,8 +220,8 @@
// TODO: check the exception string?
}
- Object obj11 = graph.create("multipleFactoryTypedNull");
- testMultiple(obj11, "hello-boolean", -1);
+ Object obj12 = graph.create("multipleFactoryTypedNull");
+ testMultiple(obj12, "hello-boolean", -1, null);
}
private void testPojoB(Object obj, URI uri, int intValue) {
@@ -229,11 +232,12 @@
assertEquals(intValue, pojob.getNumber());
}
- private void testMultiple(Object obj, String stringValue, int intValue) {
+ private void testMultiple(Object obj, String stringValue, int intValue, Integer integerValue) {
assertNotNull(obj);
assertTrue(obj instanceof Multiple);
assertEquals(intValue, ((Multiple)obj).getInt());
assertEquals(stringValue, ((Multiple)obj).getString());
- }
+ assertEquals(integerValue, ((Multiple)obj).getInteger());
+ }
}
Modified: geronimo/sandbox/blueprint/blueprint-core/src/test/java/org/apache/geronimo/blueprint/convert/ConversionServiceImplTest.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/blueprint/blueprint-core/src/test/java/org/apache/geronimo/blueprint/convert/ConversionServiceImplTest.java?rev=769897&r1=769896&r2=769897&view=diff
==============================================================================
--- geronimo/sandbox/blueprint/blueprint-core/src/test/java/org/apache/geronimo/blueprint/convert/ConversionServiceImplTest.java (original)
+++ geronimo/sandbox/blueprint/blueprint-core/src/test/java/org/apache/geronimo/blueprint/convert/ConversionServiceImplTest.java Wed Apr 29 19:46:43 2009
@@ -26,6 +26,8 @@
import java.io.ByteArrayOutputStream;
import junit.framework.TestCase;
+
+import org.apache.geronimo.blueprint.TestBlueprintContext;
import org.osgi.service.blueprint.convert.ConversionService;
import org.osgi.service.blueprint.convert.Converter;
@@ -34,7 +36,7 @@
private ConversionService service;
protected void setUp() {
- service = new ConversionServiceImpl();
+ service = new ConversionServiceImpl(new TestBlueprintContext(null));
}
public void testConvertSimpleTypes() throws Exception {
@@ -123,13 +125,13 @@
assertEquals(new Locale("de", "", "POSIX"), result);
}
- public void testConvertWeird() throws Exception {
+ public void testConvertClass() throws Exception {
assertEquals(this, service.convert(this, ConversionServiceImplTest.class));
- assertEquals(ConversionServiceImplTest.class, service.convert(this, Class.class));
+ assertEquals(ConversionServiceImplTest.class, service.convert(this.getClass().getName(), Class.class));
}
public void testCustom() throws Exception {
- ConversionServiceImpl s = new ConversionServiceImpl();
+ ConversionServiceImpl s = new ConversionServiceImpl(new TestBlueprintContext(null));
s.registerConverter(new RegionConverter());
s.registerConverter(new EuRegionConverter());
@@ -143,7 +145,7 @@
assertTrue(result instanceof EuRegion);
// find first converter that matches the type
- s = new ConversionServiceImpl();
+ s = new ConversionServiceImpl(new TestBlueprintContext(null));
s.registerConverter(new AsianRegionConverter());
s.registerConverter(new EuRegionConverter());
Modified: geronimo/sandbox/blueprint/blueprint-core/src/test/java/org/apache/geronimo/blueprint/pojos/Multiple.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/blueprint/blueprint-core/src/test/java/org/apache/geronimo/blueprint/pojos/Multiple.java?rev=769897&r1=769896&r2=769897&view=diff
==============================================================================
--- geronimo/sandbox/blueprint/blueprint-core/src/test/java/org/apache/geronimo/blueprint/pojos/Multiple.java (original)
+++ geronimo/sandbox/blueprint/blueprint-core/src/test/java/org/apache/geronimo/blueprint/pojos/Multiple.java Wed Apr 29 19:46:43 2009
@@ -21,6 +21,7 @@
public class Multiple {
private int intValue = -1;
+ private Integer integerValue = null;
private String stringValue = null;
public Multiple(String arg) {
@@ -30,10 +31,18 @@
public Multiple(int arg) {
intValue = arg;
}
+
+ public Multiple(Integer arg) {
+ integerValue = arg;
+ }
public int getInt() {
return intValue;
}
+
+ public Integer getInteger() {
+ return integerValue;
+ }
public String getString() {
return stringValue;
Modified: geronimo/sandbox/blueprint/blueprint-core/src/test/resources/test-constructor.xml
URL: http://svn.apache.org/viewvc/geronimo/sandbox/blueprint/blueprint-core/src/test/resources/test-constructor.xml?rev=769897&r1=769896&r2=769897&view=diff
==============================================================================
--- geronimo/sandbox/blueprint/blueprint-core/src/test/resources/test-constructor.xml (original)
+++ geronimo/sandbox/blueprint/blueprint-core/src/test/resources/test-constructor.xml Wed Apr 29 19:46:43 2009
@@ -41,6 +41,10 @@
<argument type="int" value="123"/>
</bean>
+ <bean id="multipleInteger" class="org.apache.geronimo.blueprint.pojos.Multiple">
+ <argument type="java.lang.Integer" value="123"/>
+ </bean>
+
<bean id="multipleString" class="org.apache.geronimo.blueprint.pojos.Multiple">
<argument type="java.lang.String" value="123"/>
</bean>