diff options
author | dougsleite <dougsleite@13f79535-47bb-0310-9956-ffa450edef68> | 2009-10-29 18:45:38 +0000 |
---|---|---|
committer | dougsleite <dougsleite@13f79535-47bb-0310-9956-ffa450edef68> | 2009-10-29 18:45:38 +0000 |
commit | c4800a6c1b96b327f7f964ebf468348624363abf (patch) | |
tree | 1f96f5b9b87b6f25fab6602ea4a4dfc042e99aca /sandbox/dougsleite/policy-guardianExceptionHandling/src/main/java/org/apache/tuscany/sca/policy/guardianExceptionHandling/GuardianExceptionHandlingPolicyInterceptor.java | |
parent | a1a3fca873e35c3f7dd4036aa1a734df3b7100e9 (diff) |
- Changing the implementation-guardian to work with the policy-guardianException module
- Adding the policy-guardianException module: The guardian member is implemented as policies
git-svn-id: http://svn.us.apache.org/repos/asf/tuscany@831060 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'sandbox/dougsleite/policy-guardianExceptionHandling/src/main/java/org/apache/tuscany/sca/policy/guardianExceptionHandling/GuardianExceptionHandlingPolicyInterceptor.java')
-rw-r--r-- | sandbox/dougsleite/policy-guardianExceptionHandling/src/main/java/org/apache/tuscany/sca/policy/guardianExceptionHandling/GuardianExceptionHandlingPolicyInterceptor.java | 172 |
1 files changed, 172 insertions, 0 deletions
diff --git a/sandbox/dougsleite/policy-guardianExceptionHandling/src/main/java/org/apache/tuscany/sca/policy/guardianExceptionHandling/GuardianExceptionHandlingPolicyInterceptor.java b/sandbox/dougsleite/policy-guardianExceptionHandling/src/main/java/org/apache/tuscany/sca/policy/guardianExceptionHandling/GuardianExceptionHandlingPolicyInterceptor.java new file mode 100644 index 0000000000..a98b300aae --- /dev/null +++ b/sandbox/dougsleite/policy-guardianExceptionHandling/src/main/java/org/apache/tuscany/sca/policy/guardianExceptionHandling/GuardianExceptionHandlingPolicyInterceptor.java @@ -0,0 +1,172 @@ +/* + * 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.policy.guardianExceptionHandling; + +import java.util.List; +import java.util.logging.Level; +import java.util.logging.Logger; +import org.apache.tuscany.sca.implementation.guardian.common.Context; +import org.apache.tuscany.sca.interfacedef.Operation; +import org.apache.tuscany.sca.invocation.Interceptor; +import org.apache.tuscany.sca.invocation.Invoker; +import org.apache.tuscany.sca.invocation.Message; +import org.apache.tuscany.sca.provider.ImplementationProvider; +import org.apache.tuscany.sca.runtime.RuntimeComponent; +import org.apache.tuscany.sca.runtime.RuntimeWire; +import org.apache.tuscany.sca.invocation.MessageFactory; +import org.apache.tuscany.sca.assembly.Binding; +import org.apache.tuscany.sca.implementation.guardian.GuardianMember; +import org.apache.tuscany.sca.implementation.guardian.common.GlobalException; +import org.apache.tuscany.sca.implementation.guardian.common.GlobalExceptionInterface; +import org.apache.tuscany.sca.runtime.RuntimeComponentService; + +public class GuardianExceptionHandlingPolicyInterceptor implements Interceptor { + + public static final String ENABLE_CONTEXT = "enableContext"; + public static final String REMOVE_CONTEXT = "removeContext"; + public static final String GTHROW = "gthrow"; + public static final String PROPAGATE = "propagate"; + public static final String CHECK_EXCEPTION_STATUS = "checkExceptionStatus"; + private Invoker next; + private RuntimeWire runtimeWire; + private RuntimeComponentService service; + + public GuardianExceptionHandlingPolicyInterceptor(RuntimeComponentService service, RuntimeWire runtimeWire) { + super(); + this.runtimeWire = runtimeWire; + this.service = service; + } + + //IMPLEMENTAR A LOGICA ENTRE {P <-> GM <-> GG} + public Message invoke(Message msg) { + + Class targetComponentImplClass = msg.getTo().getComponent().getImplementation().getClass(); + + String implementationGuardian = "org.apache.tuscany.sca.implementation.guardian.impl.GuardianGroupImplementationImpl"; + Message responseMsg = null; + +// //Test the target component - must be a implemenation.guardian +// try { +// if (!targetComponentImplClass.equals(Class.forName(implementationJava))) { +// throw new InvalidPolicyAssociationException("The target component must be a implementation.guardian"); +// } +// } catch (ClassNotFoundException ex) { +// Logger.getLogger(GuardianExceptionHandlingPolicyInterceptor.class.getName()).log(Level.SEVERE, null, ex); +// } + + try { + if (targetComponentImplClass.equals(Class.forName(implementationGuardian))) { + + String msgOperationName = msg.getOperation().getName(); + String componentName = msg.getFrom().getComponent().getName(); + + GuardianMemberFactoryImpl factory = GuardianMemberFactoryImpl.getInstance(); + GuardianMember gm = factory.createGuardianMember(componentName, msg, this); + + Object msgBody = msg.getBody(); + + if (msgOperationName.equals(ENABLE_CONTEXT)) { + Context context = (Context)((Object[]) msg.getBody())[0]; + gm.enableContext(context); + msg.setBody(null); + return msg; + } else if (msgOperationName.equals(REMOVE_CONTEXT)) { + Context removedContext = gm.removeContext(); + msg.setBody(removedContext); + + //All the contexts were removed + if(gm.getCurrentContext().equals(Context.INIT_CONTEXT)) { + factory.removeGuardianMember(componentName, msg, this); + } + + return msg; + } else if (msgOperationName.equals(GTHROW)) { + GlobalExceptionInterface ex = (GlobalExceptionInterface) ((Object[]) msgBody)[0]; + List<String> participantList = (List<String>) ((Object[]) msgBody)[1]; + gm.gthrow(ex, participantList); + msg.setBody(null); + return msg; + } else if (msgOperationName.equals(PROPAGATE)) { + GlobalExceptionInterface ex = (GlobalExceptionInterface) ((Object[]) msgBody)[0]; + boolean response = gm.propagate(ex); + msg.setBody(response); + return msg; + } else if (msgOperationName.equals(CHECK_EXCEPTION_STATUS)) { + try { + gm.checkExceptionStatus(); + msg.setBody(null); + } catch (GlobalException ex) { + msg.setFaultBody(ex); + } + return msg; + } + } else { + //throw new UnsupportedOperationException("Invocation of " + msgOperationName + " is not allowed"); + responseMsg = getNext().invoke(msg); + } + } catch (ClassNotFoundException ex) { + Logger.getLogger(GuardianExceptionHandlingPolicyInterceptor.class.getName()).log(Level.SEVERE, null, ex); + } + + return responseMsg; + } + + public Message invokeGuardianGroupOperation(String operationName, Message msg) { + + //Changing the operation + Operation desiredOperation = findOperation(operationName); + + msg.setOperation(desiredOperation); + + RuntimeComponent component = msg.getTo().getComponent(); + ImplementationProvider implProvider = component.getImplementationProvider(); + + Message responseMsg = implProvider.createInvoker(this.service, desiredOperation).invoke(msg); + + return responseMsg; + } + + private Operation findOperation(String method) { + if (method.contains(".")) { + method = method.substring(method.lastIndexOf(".") + 1); + } + + List<Operation> operations = runtimeWire.getTarget().getInterfaceContract().getInterface().getOperations(); + + Operation result = null; + for (Operation o : operations) { + if (o.getName().equalsIgnoreCase(method)) { + result = o; + break; + } + } + + return result; + } + + + + public Invoker getNext() { + return next; + } + + public void setNext(Invoker next) { + this.next = next; + } +} |