summaryrefslogtreecommitdiffstats
path: root/tags/java/sca/1.5.1/modules/binding-sca-axis2/src/main/java/org/apache/tuscany/sca/binding/sca/axis2/impl/Axis2SCABindingInvoker.java
blob: e4bb708a017334c651db9c772a6b966e8974876b (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
/*
 * 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.sca.axis2.impl;

import java.util.logging.Logger;

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.runtime.EndpointReference;
import org.osoa.sca.ServiceUnavailableException;


/**
 * A wrapper for the Axis2BindingInvoker that ensures that the URL of the target
 * service is correct by looking it up in the service registry if it is not provided
 * 
 * @version $Rev: 563772 $ $Date: 2007-08-08 07:50:49 +0100 (Wed, 08 Aug 2007) $
 */
public class Axis2SCABindingInvoker implements Interceptor {
    
    private static final Logger logger = Logger.getLogger(Axis2SCABindingInvoker.class.getName());    
    
    private Invoker axis2Invoker;
    private Axis2SCAReferenceBindingProvider provider;

    public Axis2SCABindingInvoker(Axis2SCAReferenceBindingProvider provider, Invoker axis2Invoker) {
        this.axis2Invoker = axis2Invoker;
        this.provider = provider;
    }

    public void setNext(Invoker next) {
    }

    public Invoker getNext() {
        return null;
    }

    /**
     * Fix up the URL for the message. The "to" EndPoint comes from the wire
     * target and needs to be replaced with the endpoint from the registry. 
     * The default URL for an Endpoint URI where there is no 
     * target component or service information, as in the case of a 
     * wire crossing a node boundary, is "/"
     */
    public Message invoke(Message msg) {

        // make sure that the epr of the target service is set in the TO
        // field of the message
        EndpointReference to = msg.getTo();

        // check to see if we either don't have an endpoint set or if the URI 
        // is dynamic or the target service is marked as unresolved
        if ((to == null) || (to.getURI().equals("/") || (to.getContract() == null) || (to.getContract().isUnresolved()))) {

            EndpointReference eprTo = provider.getServiceEndpoint();

            if ( (eprTo == null) || (eprTo.getURI() == null)) {
                throw new ServiceUnavailableException("Endpoint for service: " + provider.getSCABinding().getURI()
                    + " can't be found for component: "
                    + provider.getComponent().getName()
                    + " reference: "
                    + provider.getComponentReference().getName());
            }
            msg.setTo(eprTo);
        }

        // make sure that the epr of the callback service (if there is one) is set
        // in the callbackReference field of the message. 
        EndpointReference callbackEPR = msg.getFrom().getReferenceParameters().getCallbackReference();

        if ((callbackEPR == null) || (callbackEPR.getURI().equals("/"))) {

            callbackEPR = provider.getCallbackEndpoint();

            if (callbackEPR != null) {
                msg.getFrom().getReferenceParameters().setCallbackReference(callbackEPR);
            }
        }

        // do the axis2 stuff
        return axis2Invoker.invoke(msg);
    }
}