Author: edwardsmj
Date: Thu Nov 6 09:04:21 2008
New Revision: 711911
URL: http://svn.apache.org/viewvc?rev=711911&view=rev
Log:
Changes to enable consumers and producers to be connected via binding.sca
Modified:
tuscany/sandbox/event/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ConsumerProcessor.java
Modified:
tuscany/sandbox/event/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ConsumerProcessor.java
URL:
http://svn.apache.org/viewvc/tuscany/sandbox/event/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ConsumerProcessor.java?rev=711911&r1=711910&r2=711911&view=diff
==============================================================================
---
tuscany/sandbox/event/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ConsumerProcessor.java
(original)
+++
tuscany/sandbox/event/modules/implementation-java/src/main/java/org/apache/tuscany/sca/implementation/java/introspect/impl/ConsumerProcessor.java
Thu Nov 6 09:04:21 2008
@@ -64,34 +64,83 @@
}
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
}