summaryrefslogtreecommitdiffstats
path: root/sandbox/event/modules/binding-event/src/main/java/org/apache/tuscany/sca/binding/event/impl/EventBindingInterceptor.java
diff options
context:
space:
mode:
Diffstat (limited to 'sandbox/event/modules/binding-event/src/main/java/org/apache/tuscany/sca/binding/event/impl/EventBindingInterceptor.java')
-rw-r--r--sandbox/event/modules/binding-event/src/main/java/org/apache/tuscany/sca/binding/event/impl/EventBindingInterceptor.java158
1 files changed, 158 insertions, 0 deletions
diff --git a/sandbox/event/modules/binding-event/src/main/java/org/apache/tuscany/sca/binding/event/impl/EventBindingInterceptor.java b/sandbox/event/modules/binding-event/src/main/java/org/apache/tuscany/sca/binding/event/impl/EventBindingInterceptor.java
new file mode 100644
index 0000000000..cf15e54c33
--- /dev/null
+++ b/sandbox/event/modules/binding-event/src/main/java/org/apache/tuscany/sca/binding/event/impl/EventBindingInterceptor.java
@@ -0,0 +1,158 @@
+/*
+ * 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.tuscany.sca.binding.event.impl;
+
+import java.util.List;
+
+import org.apache.tuscany.sca.assembly.Binding;
+import org.apache.tuscany.sca.interfacedef.InterfaceContractMapper;
+import org.apache.tuscany.sca.interfacedef.Operation;
+import org.apache.tuscany.sca.invocation.Interceptor;
+import org.apache.tuscany.sca.invocation.InvocationChain;
+import org.apache.tuscany.sca.invocation.Invoker;
+import org.apache.tuscany.sca.invocation.Message;
+import org.apache.tuscany.sca.runtime.RuntimeComponentService;
+import org.apache.tuscany.sca.runtime.RuntimeWire;
+import org.osoa.sca.annotations.EventType;
+
+/**
+ * Policy interceptor for events
+ *
+ * @version $$
+ */
+public class EventBindingInterceptor implements Interceptor {
+ private Invoker next;
+ @SuppressWarnings("unused")
+ private Operation operation;
+ private RuntimeComponentService consumerService;
+ private Binding binding;
+ private InterfaceContractMapper interfaceContractMapper;
+
+ public EventBindingInterceptor(RuntimeComponentService consumerService,
+ Binding binding,
+ Operation operation,
+ InterfaceContractMapper interfaceContractMapper) {
+ super();
+ this.consumerService = consumerService;
+ this.operation = operation;
+ this.binding = binding;
+ this.interfaceContractMapper = interfaceContractMapper;
+ }
+
+ public Message invoke(Message msg) {
+ try {
+
+ Object arg = msg.getBody();
+ if (arg instanceof Object[]) {
+ arg = ((Object [])arg)[0];
+ }
+
+ Operation consumerOperation = findOperation(consumerService.getInterfaceContract().getInterface().getOperations(), arg);
+
+ InvocationChain chain = getInvocationChain(consumerService.getRuntimeWire(binding), consumerOperation);
+ Invoker invoker = chain.getHeadInvoker();
+ while (invoker instanceof Interceptor && ((Interceptor)invoker).getNext() != null) {
+ invoker = ((Interceptor)invoker).getNext();
+ }
+ msg = invoker.invoke(msg);
+ } catch (Exception e) {
+ e.printStackTrace();
+ msg.setBody(e);
+ }
+ return msg;
+ }
+
+ public Invoker getNext() {
+ return next;
+ }
+
+ public void setNext(Invoker next) {
+ this.next = next;
+ }
+
+ private String getEventType(Class<?> clazz) {
+ EventType eventTypeAnnotation = clazz.getAnnotation(EventType.class);
+ if (eventTypeAnnotation != null)
+ return eventTypeAnnotation.name();
+ if (clazz.getSuperclass() != null && clazz.getSuperclass() != Object.class) {
+ String eventType = getEventType(clazz.getSuperclass());
+ if (eventType != null)
+ return eventType;
+ }
+ for (Class<?> interfaze : clazz.getInterfaces()) {
+ String eventType = getEventType(interfaze);
+ if (eventType != null)
+ return eventType;
+ }
+ return null;
+ }
+
+ @SuppressWarnings("unchecked")
+ private Operation findOperation(List<Operation> operations, Object arg) {
+ if (arg == null) {
+ return operations.get(0);
+ }
+ else {
+ String eventType = getEventType(arg.getClass());
+
+ Operation firstMatching = null;
+ for (Operation op : operations) {
+ String[] eventTypes = op.getEventTypes();
+ if (eventTypes == null || eventTypes.length == 0) {
+
+ if (op.getInputType().getLogical().get(0).getPhysical().isAssignableFrom(arg.getClass())) {
+
+ // If no event type is specified, return the first method which
+ // (1) has assignable parameters
+ // (2) has no explicit event types specified
+ if (eventType == null) {
+
+ return op;
+ }
+ else if (firstMatching == null) {
+ // Remember first matching method without event types and
+ // return this operation if no operation has explicitly specified
+ // this event type.
+ firstMatching = op;
+ }
+ }
+ } else {
+ for (String type : eventTypes) {
+ if (type.equals(eventType)) {
+ return op;
+ }
+ }
+ }
+
+ }
+ return firstMatching;
+ }
+ }
+
+ public InvocationChain getInvocationChain(RuntimeWire wire, Operation operation) {
+ for (InvocationChain chain : wire.getInvocationChains()) {
+ Operation op = chain.getTargetOperation();
+ if (interfaceContractMapper.isCompatible(operation, op, op.getInterface().isRemotable())) {
+ return chain;
+ }
+ }
+ return null;
+ }
+
+}