summaryrefslogtreecommitdiffstats
path: root/sandbox/dougsleite/policy-guardianExceptionHandling/src/main/java/org/apache/tuscany/sca/policy/guardianExceptionHandling/GuardianExceptionHandlingPolicyInterceptor.java
blob: a98b300aae416a729bb0085dc7f1cd38484ef38b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
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;
    }
}