From e57eea266e8886f699e7737c5a75419c5e2c8da9 Mon Sep 17 00:00:00 2001 From: edwardsmj Date: Thu, 6 Nov 2008 17:04:21 +0000 Subject: Changes to enable consumers and producers to be connected via binding.sca git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@711911 13f79535-47bb-0310-9956-ffa450edef68 --- .../java/introspect/impl/ConsumerProcessor.java | 83 +++++++++++++++++----- 1 file changed, 66 insertions(+), 17 deletions(-) (limited to 'sandbox/event') diff --git a/sandbox/event/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ConsumerProcessor.java b/sandbox/event/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ConsumerProcessor.java index f2488a6fa5..dbabfd78da 100644 --- a/sandbox/event/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ConsumerProcessor.java +++ b/sandbox/event/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ConsumerProcessor.java @@ -64,34 +64,83 @@ public class ConsumerProcessor extends BaseJavaClassVisitor { } JavaElementImpl element = new JavaElementImpl(method.getDeclaringClass()); try { - createConsumer(type, element, annotation.name(), method.getName()); + createConsumer(type, element, annotation.name(), method); } catch (InvalidInterfaceException e) { throw new IntrospectionException(e); } } /** - * @param type - * @param element + * Creates or updates a Consumer + * @param type - the JavaImplementation to which the consumer applies + * @param element - the Java class implementing the consumer + * @param consumerName - the Name of the consumer + * @param operationName - the Name of the operation (ie Method) annotated with @Consumer * @throws IllegalConsumerException */ - private void createConsumer(JavaImplementation type, JavaElementImpl element, String consumerName, String operationName) + private void createConsumer(JavaImplementation type, JavaElementImpl element, + String consumerName, Method method ) throws IllegalConsumerException, InvalidInterfaceException { - Class consumerClass = element.getType(); - - org.apache.tuscany.sca.assembly.Consumer consumer = assemblyFactory.createConsumer(); - JavaInterfaceContract interfaceContract = javaFactory.createJavaInterfaceContract(); - consumer.setInterfaceContract(interfaceContract); - consumer.setName(consumerName); - JavaInterface consumerInterface = javaFactory.createJavaInterface(); - consumerInterface.setRemotable(true); - javaFactory.createJavaInterface(consumerInterface, consumerClass); - consumer.getInterfaceContract().setInterface(consumerInterface); - consumer.setOperationName(operationName); + // First, see if the Consumer already exists + org.apache.tuscany.sca.assembly.Consumer consumer = findConsumerByName( type, consumerName ); + + if( consumer == null ) { + // Create the consumer object and add it to the JavaImplementation + Class consumerClass = element.getType(); + + consumer = assemblyFactory.createConsumer(); + JavaInterfaceContract interfaceContract = javaFactory.createJavaInterfaceContract(); + consumer.setInterfaceContract(interfaceContract); + consumer.setName(consumerName); + JavaInterface consumerInterface = javaFactory.createJavaInterface(); + consumerInterface.setRemotable(true); + javaFactory.createJavaInterface(consumerInterface, consumerClass); + consumer.getInterfaceContract().setInterface(consumerInterface); - type.getConsumers().add(consumer); - } + type.getConsumers().add(consumer); + } // end if + + // Add the operation to the consumer + consumer.addOperation(method.getName(), getOperationEventTypes(method) ); + + } // end method createConsumer + + private static String getOperationEventTypes( Method method ) { + // 1) If the method is annotated with @EventTypes, extract the supported event types from the annotation + org.osoa.sca.annotations.EventTypes annotation = method.getAnnotation(org.osoa.sca.annotations.EventTypes.class); + if (annotation != null) { + return annotation.value(); + } // end if + + // 2) If the method has a parameter that is NOT java.lang.Object, return its event type or its base Java type + Class parameter = method.getParameterTypes()[0]; + String className = parameter.getCanonicalName(); + if( !className.equals("java.lang.Object")) { + org.osoa.sca.annotations.EventType eventType = parameter.getAnnotation(org.osoa.sca.annotations.EventType.class); + if (eventType != null) { + return eventType.name(); + } else { + return className; + }// end if + } // end if + + // 3) Else, return "null" meaning that the method supports "any event type" + return null; + } // end getOperationEventTypes + + /** + * Finds a Consumer within a Java implementation by the name of the consumer + * @param impl - the Java implementation + * @param name - the consumer name + * @return - the Consumer with the specified name, or null if it does not exist + */ + private static org.apache.tuscany.sca.assembly.Consumer findConsumerByName( JavaImplementation impl, String name ) { + for( org.apache.tuscany.sca.assembly.Consumer consumer : impl.getConsumers() ) { + if( name.equals(consumer.getName()) ) return consumer; + } // end for + return null; + } // end method findConsumerByName } -- cgit v1.2.3