/* * 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.core.context.impl; import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; import org.apache.tuscany.sca.assembly.Binding; import org.apache.tuscany.sca.assembly.Endpoint; import org.apache.tuscany.sca.assembly.EndpointReference; import org.apache.tuscany.sca.assembly.SCABinding; import org.apache.tuscany.sca.assembly.builder.BindingBuilder; import org.apache.tuscany.sca.assembly.builder.BuilderContext; import org.apache.tuscany.sca.context.CompositeContext; import org.apache.tuscany.sca.context.ThreadMessageContext; import org.apache.tuscany.sca.core.invocation.CallbackHandler; import org.apache.tuscany.sca.core.invocation.Constants; import org.apache.tuscany.sca.invocation.Message; import org.apache.tuscany.sca.runtime.RuntimeEndpoint; import org.apache.tuscany.sca.runtime.RuntimeEndpointReference; import org.oasisopen.sca.ServiceRuntimeException; /** * Represent the reference to the callback service. This class basically wraps a Tuscany EndpointReference. * The callback EPR is selected based on the are 3 basic scenarios we are trying to cater for * * A/ * * * callbackEPRs) { if(!callbackEPRs.isEmpty()) { RuntimeEndpointReference epr = (RuntimeEndpointReference) callbackEPRs.get(0); return epr.getCompositeContext(); } return null; } @Override protected B createProxy() throws Exception { return proxyFactory.createCallbackProxy(this); } public RuntimeEndpointReference getCallbackEPR() { return callbackEPR; } public Endpoint getResolvedEndpoint() { return resolvedEndpoint; } private RuntimeEndpointReference selectCallbackEPR(Message msgContext) { // look for callback binding with same name as service binding Endpoint to = msgContext.getTo(); if (to == null) { //FIXME: need better exception throw new ServiceRuntimeException("Destination for forward call is not available"); } for (EndpointReference epr : callbackEPRs) { if (epr.getBinding().getName().equals(to.getBinding().getName())) { return (RuntimeEndpointReference) epr; } } // if no match, look for callback binding with same type as service binding for (EndpointReference epr : callbackEPRs) { if (epr.getBinding().getType().equals(to.getBinding().getType())) { return (RuntimeEndpointReference) epr; } } // no suitable callback wire was found return null; } private RuntimeEndpointReference setCallbackAddress(RuntimeEndpointReference endpointReference) { try { RuntimeEndpointReference epr = endpointReference; if (callbackHandler.getCloneCallbackWire()){ epr = (RuntimeEndpointReference)endpointReference.clone(); } // TUSCANY-3932 // If it's the default binding then we're going to look the callback endpoint // up in the registry. Most remote protocols, which may be used as delegates // for binding.sca, will expect to deal with absolute URLs so flip the // callback endpoint back to force the lookup to happen if (epr.getBinding().getType().equals(SCABinding.TYPE)){ // A/ create a callback endpoint to allow the // callback lookup to take place epr.setStatus(EndpointReference.Status.WIRED_TARGET_NOT_FOUND); // if an endpoint it provided in the forward message use it or // if not create one if (resolvedEndpoint == null ){ RuntimeEndpoint callbackEndpoint = (RuntimeEndpoint)assemblyFactory.createEndpoint(); callbackEndpoint.setURI(callbackHandler.getCallbackTargetURI()); callbackEndpoint.setUnresolved(true); epr.setTargetEndpoint(callbackEndpoint); } else { epr.setTargetEndpoint(resolvedEndpoint); } } else { // B/ and C/ assume that the callback EPR is already resolved // and set the binding URI if one is provided with the // forward message. Some bindings may want to do other // things to determine the callback URI to the // CallbackHandler will be sent in the callback message // header. This is particularly true if the clone isn't // called above because resetting the URI will not // be thread safe. epr.setStatus(EndpointReference.Status.RESOLVED_BINDING); if ( callbackHandler.getCallbackTargetURI() != null ){ epr.getBinding().setURI(callbackHandler.getCallbackTargetURI()); } else { // do nothing and rely on whatever the user has configured // in the SCDL } } return epr; } catch (CloneNotSupportedException e) { // will not happen throw new ServiceRuntimeException(e); } } }