/* * 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 participantList = (List) ((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 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; } }