You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tuscany.apache.org by jm...@apache.org on 2006/07/14 22:08:42 UTC
svn commit: r422008 - in /incubator/tuscany/sandbox/chianti/sca/core/src:
main/java/org/apache/tuscany/core/implementation/processor/
test/java/org/apache/tuscany/core/implementation/processor/
Author: jmarino
Date: Fri Jul 14 13:08:42 2006
New Revision: 422008
URL: http://svn.apache.org/viewvc?rev=422008&view=rev
Log:
enable heuristic constructor determinations when no props or refs specified
Added:
incubator/tuscany/sandbox/chianti/sca/core/src/main/java/org/apache/tuscany/core/implementation/processor/AmbiguousConstructorException.java (contents, props changed)
- copied, changed from r421926, incubator/tuscany/sandbox/chianti/sca/core/src/main/java/org/apache/tuscany/core/implementation/processor/UnresolvableConstructorParameterException.java
incubator/tuscany/sandbox/chianti/sca/core/src/test/java/org/apache/tuscany/core/implementation/processor/ProcessorUtilsServiceTestCase.java (with props)
incubator/tuscany/sandbox/chianti/sca/core/src/test/java/org/apache/tuscany/core/implementation/processor/ProcessorUtilsUniqueTestCase.java (with props)
Removed:
incubator/tuscany/sandbox/chianti/sca/core/src/main/java/org/apache/tuscany/core/implementation/processor/UnresolvableConstructorParameterException.java
Modified:
incubator/tuscany/sandbox/chianti/sca/core/src/main/java/org/apache/tuscany/core/implementation/processor/HeuristicPojoProcessor.java
incubator/tuscany/sandbox/chianti/sca/core/src/main/java/org/apache/tuscany/core/implementation/processor/ProcessorUtils.java
incubator/tuscany/sandbox/chianti/sca/core/src/test/java/org/apache/tuscany/core/implementation/processor/HeuristicConstructorTestCase.java
Copied: incubator/tuscany/sandbox/chianti/sca/core/src/main/java/org/apache/tuscany/core/implementation/processor/AmbiguousConstructorException.java (from r421926, incubator/tuscany/sandbox/chianti/sca/core/src/main/java/org/apache/tuscany/core/implementation/processor/UnresolvableConstructorParameterException.java)
URL: http://svn.apache.org/viewvc/incubator/tuscany/sandbox/chianti/sca/core/src/main/java/org/apache/tuscany/core/implementation/processor/AmbiguousConstructorException.java?p2=incubator/tuscany/sandbox/chianti/sca/core/src/main/java/org/apache/tuscany/core/implementation/processor/AmbiguousConstructorException.java&p1=incubator/tuscany/sandbox/chianti/sca/core/src/main/java/org/apache/tuscany/core/implementation/processor/UnresolvableConstructorParameterException.java&r1=421926&r2=422008&rev=422008&view=diff
==============================================================================
--- incubator/tuscany/sandbox/chianti/sca/core/src/main/java/org/apache/tuscany/core/implementation/processor/UnresolvableConstructorParameterException.java (original)
+++ incubator/tuscany/sandbox/chianti/sca/core/src/main/java/org/apache/tuscany/core/implementation/processor/AmbiguousConstructorException.java Fri Jul 14 13:08:42 2006
@@ -16,23 +16,23 @@
import org.apache.tuscany.core.implementation.ProcessingException;
/**
- * Thrown when a constructor parameter cannot be unambiguously resolved to a property or reference
+ * Thrown when constructor parameters cannot be unambiguously resolved to a property or reference
*
* @version $Rev$ $Date$
*/
-public class UnresolvableConstructorParameterException extends ProcessingException {
- public UnresolvableConstructorParameterException() {
+public class AmbiguousConstructorException extends ProcessingException {
+ public AmbiguousConstructorException() {
}
- public UnresolvableConstructorParameterException(String message) {
+ public AmbiguousConstructorException(String message) {
super(message);
}
- public UnresolvableConstructorParameterException(String message, Throwable cause) {
+ public AmbiguousConstructorException(String message, Throwable cause) {
super(message, cause);
}
- public UnresolvableConstructorParameterException(Throwable cause) {
+ public AmbiguousConstructorException(Throwable cause) {
super(cause);
}
}
Propchange: incubator/tuscany/sandbox/chianti/sca/core/src/main/java/org/apache/tuscany/core/implementation/processor/AmbiguousConstructorException.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: incubator/tuscany/sandbox/chianti/sca/core/src/main/java/org/apache/tuscany/core/implementation/processor/AmbiguousConstructorException.java
------------------------------------------------------------------------------
svn:keywords = Rev,Date
Modified: incubator/tuscany/sandbox/chianti/sca/core/src/main/java/org/apache/tuscany/core/implementation/processor/HeuristicPojoProcessor.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/sandbox/chianti/sca/core/src/main/java/org/apache/tuscany/core/implementation/processor/HeuristicPojoProcessor.java?rev=422008&r1=422007&r2=422008&view=diff
==============================================================================
--- incubator/tuscany/sandbox/chianti/sca/core/src/main/java/org/apache/tuscany/core/implementation/processor/HeuristicPojoProcessor.java (original)
+++ incubator/tuscany/sandbox/chianti/sca/core/src/main/java/org/apache/tuscany/core/implementation/processor/HeuristicPojoProcessor.java Fri Jul 14 13:08:42 2006
@@ -46,6 +46,7 @@
import static org.apache.tuscany.core.util.JavaIntrospectionHelper.getAllUniquePublicProtectedMethods;
import static org.apache.tuscany.core.util.JavaIntrospectionHelper.getBaseName;
import static org.apache.tuscany.core.util.JavaIntrospectionHelper.toPropertyName;
+import org.apache.tuscany.core.util.JavaIntrospectionHelper;
/**
* Heuristically evaluates an un-annotated Java implementation type to determine services, references, and properties
@@ -85,7 +86,7 @@
throw new ServiceTypeNotFoundException(clazz.getName());
}
}
- calculateConstructor(type, clazz);
+ calcConstructor(type, clazz);
return;
}
@@ -134,14 +135,21 @@
type.add(createProperty(field.getName(), field, paramType));
}
}
- calculateConstructor(type, clazz);
+ calcConstructor(type, clazz);
}
- private void calculateConstructor(PojoComponentType<JavaMappedService,
- JavaMappedReference,
- JavaMappedProperty<?>> type,
- Class<?> clazz) throws NoConstructorException,
- UnresolvableConstructorParameterException {
+ /**
+ * Determines the constructor to use based on the component type's references and properties
+ *
+ * @param type the component type
+ * @param clazz the implementation class corresponding to the component type
+ * @throws NoConstructorException if no suitable constructor is found
+ * @throws AmbiguousConstructorException if the parameters of a constructor cannot be unambiguously mapped to
+ * references and properties
+ */
+ private void calcConstructor(PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type,
+ Class<?> clazz) throws NoConstructorException,
+ AmbiguousConstructorException {
// determine constructor if one is not annotated
if (type.getConstructorDefinition() == null) {
Constructor[] constructors = clazz.getConstructors();
@@ -171,14 +179,37 @@
if (params.length != 0) {
Map<String, JavaMappedProperty<?>> props = type.getProperties();
Map<String, JavaMappedReference> refs = type.getReferences();
+ if (!ProcessorUtils.areUnique(params)) {
+ throw new AmbiguousConstructorException(
+ "Unable to resolve parameter types as they are not unique, use @Constructor");
+ }
+ if (!calcPropRefUniqueness(props.values(), refs.values())) {
+ throw new AmbiguousConstructorException(
+ "Unable to resolve parameter types as reference and property types are not unique, " +
+ "use @Constructor");
+ }
+ boolean empty = props.size() + refs.size() == 0;
List<String> paramNames = new ArrayList<String>();
- // the constructor param types must unambiguously match reference or property types
- for (Class param : params) {
- String name = findReferenceOrProperty(param, props, refs);
- if (name == null) {
- throw new UnresolvableConstructorParameterException(param.getName());
+ if (!empty) {
+ // the constructor param types must unambiguously match defined reference or property types
+ for (Class param : params) {
+ String name = findReferenceOrProperty(param, props, refs);
+ if (name == null) {
+ throw new AmbiguousConstructorException(param.getName());
+ }
+ paramNames.add(name);
+ }
+ } else {
+ // heuristically determine refs and props from the parameter types
+ for (Class<?> param : params) {
+ String name = JavaIntrospectionHelper.getBaseName(param).toLowerCase();
+ if (isReferenceType(param)) {
+ refs.put(name, createReference(name, null, param));
+ } else {
+ props.put(name, createProperty(name, null, param));
+ }
+ paramNames.add(name);
}
- paramNames.add(name);
}
ConstructorDefinition<?> definition = new ConstructorDefinition(constructor);
definition.setInjectionNames(paramNames);
@@ -192,22 +223,40 @@
}
/**
+ * Returns true if the union of the given collections of properties and references have unique Java types
+ */
+ private boolean calcPropRefUniqueness(Collection<JavaMappedProperty<?>> props,
+ Collection<JavaMappedReference> refs) {
+
+ Class[] classes = new Class[props.size() + refs.size()];
+ int i = 0;
+ for (JavaMappedProperty<?> property : props) {
+ classes[i] = property.getJavaType();
+ i++;
+ }
+ for (JavaMappedReference reference : refs) {
+ classes[i] = reference.getServiceContract().getInterfaceClass();
+ i++;
+ }
+ return ProcessorUtils.areUnique(classes);
+ }
+
+ /**
* Unambiguously finds the reference or property associated with the given type
*
* @return the name of the reference or property if found, null if not
- * @throws UnresolvableConstructorParameterException
- * if the constructor parameter cannot be resolved to a property or reference
+ * @throws AmbiguousConstructorException if the constructor parameter cannot be resolved to a property or reference
*/
private String findReferenceOrProperty(Class<?> type,
Map<String, JavaMappedProperty<?>> props,
Map<String, JavaMappedReference> refs)
- throws UnresolvableConstructorParameterException {
+ throws AmbiguousConstructorException {
String name = null;
for (JavaMappedProperty<?> property : props.values()) {
if (property.getJavaType().equals(type)) {
if (name != null) {
- UnresolvableConstructorParameterException e = new UnresolvableConstructorParameterException(
+ AmbiguousConstructorException e = new AmbiguousConstructorException(
"Cannot unambiguously determine property or reference for constructor type");
e.setIdentifier(type.getName());
throw e;
@@ -219,7 +268,7 @@
for (JavaMappedReference reference : refs.values()) {
if (reference.getServiceContract().getInterfaceClass().equals(type)) {
if (name != null) {
- UnresolvableConstructorParameterException e = new UnresolvableConstructorParameterException(
+ AmbiguousConstructorException e = new AmbiguousConstructorException(
"Cannot unambiguously determine property or reference for constructor type");
e.setIdentifier(type.getName());
throw e;
Modified: incubator/tuscany/sandbox/chianti/sca/core/src/main/java/org/apache/tuscany/core/implementation/processor/ProcessorUtils.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/sandbox/chianti/sca/core/src/main/java/org/apache/tuscany/core/implementation/processor/ProcessorUtils.java?rev=422008&r1=422007&r2=422008&view=diff
==============================================================================
--- incubator/tuscany/sandbox/chianti/sca/core/src/main/java/org/apache/tuscany/core/implementation/processor/ProcessorUtils.java (original)
+++ incubator/tuscany/sandbox/chianti/sca/core/src/main/java/org/apache/tuscany/core/implementation/processor/ProcessorUtils.java Fri Jul 14 13:08:42 2006
@@ -15,6 +15,9 @@
import org.osoa.sca.annotations.Callback;
import org.osoa.sca.annotations.Remotable;
+import org.osoa.sca.annotations.Scope;
+
+import org.apache.tuscany.spi.model.InteractionScope;
import org.apache.tuscany.core.implementation.JavaMappedService;
import org.apache.tuscany.core.implementation.JavaServiceContract;
@@ -40,6 +43,16 @@
service.setServiceInterface(interfaze);
JavaServiceContract contract = new JavaServiceContract();
contract.setInterfaceClass(interfaze);
+ Scope interactionScope = interfaze.getAnnotation(Scope.class);
+ if (interactionScope == null) {
+ contract.setInteractionScope(InteractionScope.NONCONVERSATIONAL);
+ } else {
+ if ("CONVERSATIONAL".equalsIgnoreCase(interactionScope.value())) {
+ contract.setInteractionScope(InteractionScope.CONVERSATIONAL);
+ } else {
+ contract.setInteractionScope(InteractionScope.NONCONVERSATIONAL);
+ }
+ }
Callback callback = interfaze.getAnnotation(Callback.class);
if (callback != null && !Void.class.equals(callback.value())) {
Class<?> callbackClass = callback.value();
@@ -48,6 +61,40 @@
}
service.setServiceContract(contract);
return service;
+ }
+
+ /**
+ * Determines if all the members of a collection have unique types
+ *
+ * @param collection the collection to analyze
+ * @return true if the types are unique
+ */
+ public static boolean areUnique(Class[] collection) {
+ if (collection.length == 0){
+ return true;
+ }
+ return areUnique(collection, 0);
+ }
+
+ /**
+ * Determines if all the members of a collection have unique types
+ *
+ * @param collection the collection to analyze
+ * @param start the position in the collection to start
+ * @return true if the types are unique
+ */
+ private static boolean areUnique(Class[] collection, int start) {
+ Object compare = collection[start];
+ for (int i = start + 1; i < collection.length; i++) {
+ if (compare.equals(collection[i])) {
+ return false;
+ }
+ }
+ if (start + 1 < collection.length) {
+ return areUnique(collection, start + 1);
+ } else {
+ return true;
+ }
}
}
Modified: incubator/tuscany/sandbox/chianti/sca/core/src/test/java/org/apache/tuscany/core/implementation/processor/HeuristicConstructorTestCase.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/sandbox/chianti/sca/core/src/test/java/org/apache/tuscany/core/implementation/processor/HeuristicConstructorTestCase.java?rev=422008&r1=422007&r2=422008&view=diff
==============================================================================
--- incubator/tuscany/sandbox/chianti/sca/core/src/test/java/org/apache/tuscany/core/implementation/processor/HeuristicConstructorTestCase.java (original)
+++ incubator/tuscany/sandbox/chianti/sca/core/src/test/java/org/apache/tuscany/core/implementation/processor/HeuristicConstructorTestCase.java Fri Jul 14 13:08:42 2006
@@ -1,13 +1,15 @@
package org.apache.tuscany.core.implementation.processor;
+import org.osoa.sca.annotations.Remotable;
+
import org.apache.tuscany.spi.model.ServiceContract;
import junit.framework.TestCase;
import org.apache.tuscany.core.implementation.JavaMappedProperty;
import org.apache.tuscany.core.implementation.JavaMappedReference;
import org.apache.tuscany.core.implementation.JavaMappedService;
-import org.apache.tuscany.core.implementation.PojoComponentType;
import org.apache.tuscany.core.implementation.JavaServiceContract;
+import org.apache.tuscany.core.implementation.PojoComponentType;
/**
* @version $Rev$ $Date$
@@ -70,15 +72,19 @@
}
- public void testSingleConstructorUnresolvableParam() throws Exception {
+ public void testSingleConstructorResolvableParam() throws Exception {
PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type =
new PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>>();
- try {
- processor.visitEnd(null, Foo1.class, type, null);
- fail();
- } catch (UnresolvableConstructorParameterException e) {
- // expected
- }
+ processor.visitEnd(null, Foo5.class, type, null);
+ assertEquals(String.class, type.getProperties().get("string").getJavaType());
+ }
+
+ public void testSingleConstructorResolvableRef() throws Exception {
+ PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>> type =
+ new PojoComponentType<JavaMappedService, JavaMappedReference, JavaMappedProperty<?>>();
+ processor.visitEnd(null, Foo6.class, type, null);
+ assertEquals(Ref.class,
+ type.getReferences().get("heuristicconstructortestcase$ref").getServiceContract().getInterfaceClass());
}
public void testSingleConstructorAmbiguousRef() throws Exception {
@@ -96,7 +102,7 @@
try {
processor.visitEnd(null, Foo4.class, type, null);
fail();
- } catch (UnresolvableConstructorParameterException e) {
+ } catch (AmbiguousConstructorException e) {
// expected
}
}
@@ -124,6 +130,25 @@
public static class Foo4 {
public Foo4(Foo1 ref) {
+ }
+ }
+
+ public static class Prop {
+
+ }
+
+ @Remotable
+ public static interface Ref {
+
+ }
+
+ public static class Foo5 {
+ public Foo5(String val) {
+ }
+ }
+
+ public static class Foo6 {
+ public Foo6(Ref ref) {
}
}
Added: incubator/tuscany/sandbox/chianti/sca/core/src/test/java/org/apache/tuscany/core/implementation/processor/ProcessorUtilsServiceTestCase.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/sandbox/chianti/sca/core/src/test/java/org/apache/tuscany/core/implementation/processor/ProcessorUtilsServiceTestCase.java?rev=422008&view=auto
==============================================================================
--- incubator/tuscany/sandbox/chianti/sca/core/src/test/java/org/apache/tuscany/core/implementation/processor/ProcessorUtilsServiceTestCase.java (added)
+++ incubator/tuscany/sandbox/chianti/sca/core/src/test/java/org/apache/tuscany/core/implementation/processor/ProcessorUtilsServiceTestCase.java Fri Jul 14 13:08:42 2006
@@ -0,0 +1,63 @@
+/**
+ *
+ * Copyright 2006 The Apache Software Foundation or its licensors, as applicable.
+ *
+ * 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.implementation.processor;
+
+import org.osoa.sca.annotations.Callback;
+import org.osoa.sca.annotations.Remotable;
+import org.osoa.sca.annotations.Scope;
+
+import org.apache.tuscany.spi.model.InteractionScope;
+
+import junit.framework.TestCase;
+import org.apache.tuscany.core.implementation.JavaMappedService;
+import org.apache.tuscany.core.implementation.JavaServiceContract;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public class ProcessorUtilsServiceTestCase extends TestCase {
+
+ public void testCreateConversationalService() {
+ JavaMappedService service = ProcessorUtils.createService(Foo.class);
+ assertTrue(Foo.class.equals(service.getServiceContract().getInterfaceClass()));
+ assertTrue(service.isRemotable());
+ assertEquals(InteractionScope.CONVERSATIONAL, service.getServiceContract().getInteractionScope());
+ JavaServiceContract serviceContract = (JavaServiceContract) service.getServiceContract();
+ assertTrue(Bar.class.equals(serviceContract.getCallbackClass()));
+ assertTrue("ProcessorUtilsServiceTestCase$Bar".equals(serviceContract.getCallbackName()));
+ }
+
+ public void testCreateDefaultService() {
+ JavaMappedService service = ProcessorUtils.createService(Baz.class);
+ assertTrue(Baz.class.equals(service.getServiceContract().getInterfaceClass()));
+ assertTrue(!service.isRemotable());
+ assertEquals(InteractionScope.NONCONVERSATIONAL, service.getServiceContract().getInteractionScope());
+ }
+
+
+ @Callback(Bar.class)
+ @Remotable
+ @Scope("CONVERSATIONAL")
+ public interface Foo {
+
+ }
+
+ public interface Bar {
+
+ }
+
+ public interface Baz{
+
+ }
+}
Propchange: incubator/tuscany/sandbox/chianti/sca/core/src/test/java/org/apache/tuscany/core/implementation/processor/ProcessorUtilsServiceTestCase.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: incubator/tuscany/sandbox/chianti/sca/core/src/test/java/org/apache/tuscany/core/implementation/processor/ProcessorUtilsServiceTestCase.java
------------------------------------------------------------------------------
svn:keywords = Rev,Date
Added: incubator/tuscany/sandbox/chianti/sca/core/src/test/java/org/apache/tuscany/core/implementation/processor/ProcessorUtilsUniqueTestCase.java
URL: http://svn.apache.org/viewvc/incubator/tuscany/sandbox/chianti/sca/core/src/test/java/org/apache/tuscany/core/implementation/processor/ProcessorUtilsUniqueTestCase.java?rev=422008&view=auto
==============================================================================
--- incubator/tuscany/sandbox/chianti/sca/core/src/test/java/org/apache/tuscany/core/implementation/processor/ProcessorUtilsUniqueTestCase.java (added)
+++ incubator/tuscany/sandbox/chianti/sca/core/src/test/java/org/apache/tuscany/core/implementation/processor/ProcessorUtilsUniqueTestCase.java Fri Jul 14 13:08:42 2006
@@ -0,0 +1,56 @@
+/**
+ *
+ * Copyright 2006 The Apache Software Foundation or its licensors, as applicable.
+ *
+ * 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.implementation.processor;
+
+import junit.framework.TestCase;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public class ProcessorUtilsUniqueTestCase extends TestCase {
+
+ public void testUniquess1() throws Exception{
+ Class[] classes = new Class[2];
+ classes[0] = String.class;
+ classes[1] = Integer.class;
+ assertTrue(ProcessorUtils.areUnique(classes));
+ }
+
+ public void testUniquess2() throws Exception{
+ Class[] classes = new Class[2];
+ classes[0] = String.class;
+ classes[1] = String.class;
+ assertFalse(ProcessorUtils.areUnique(classes));
+ }
+
+ public void testUniquess3() throws Exception{
+ Class[] classes = new Class[1];
+ classes[0] = String.class;
+ assertTrue(ProcessorUtils.areUnique(classes));
+ }
+
+ public void testUniquess4() throws Exception{
+ Class[] classes = new Class[3];
+ classes[0] = String.class;
+ classes[1] = Integer.class;
+ classes[2] = String.class;
+ assertFalse(ProcessorUtils.areUnique(classes));
+ }
+
+ public void testUniquess5() throws Exception{
+ Class[] classes = new Class[0];
+ assertTrue(ProcessorUtils.areUnique(classes));
+ }
+
+}
Propchange: incubator/tuscany/sandbox/chianti/sca/core/src/test/java/org/apache/tuscany/core/implementation/processor/ProcessorUtilsUniqueTestCase.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: incubator/tuscany/sandbox/chianti/sca/core/src/test/java/org/apache/tuscany/core/implementation/processor/ProcessorUtilsUniqueTestCase.java
------------------------------------------------------------------------------
svn:keywords = Rev,Date
---------------------------------------------------------------------
To unsubscribe, e-mail: tuscany-commits-unsubscribe@ws.apache.org
For additional commands, e-mail: tuscany-commits-help@ws.apache.org