Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * 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.implementation.java.invocation;
 
 import java.util.List;
 import java.util.Map;
 
Implementation of the ResponseDispatch interface of the OASIS SCA Java API This is used for invocations of asynchronous services, where it is passed as a parameter on async service operations and it provides the path by which the service implementation returns the response to the request, or a Fault Note that this class is serializable and can be serialized, stored and deserialized by the service implementation

Parameters:
<T> - type of the response message
 
 public class ResponseDispatchImpl<T> implements ResponseDispatch<T>, Serializable {

Generated serialVersionUID value
 
 	private static final long serialVersionUID = 300158355992568592L;
 	
 	// A latch used to ensure that the sendResponse() and sendFault() operations are used at most once
 	// The latch is initialized with the value "false"
 	private AtomicBoolean latch = new AtomicBoolean();
 	
 	private final Lock lock = new ReentrantLock();
     private final Condition completed  = .newCondition(); 
 	
 	// The result
 	private transient volatile T response = null;
 	private transient volatile Throwable fault = null
 	
 	private transient ExtensionPointRegistry registry;
 	
 	// Service Reference used for the callback
 	private String 						messageID;

No-arg constructor for serialization purposes
 
 	public ResponseDispatchImpl() {
 		super();
 	} // end constructor
 	
 	public ResponseDispatchImplMessage msg ) {
 		super();
 		
     	//if( respInvoker == null ) throw new ServiceRuntimeException("Async Implementation invoked with no response invoker");
    	
    	if == null ) {
    		 = getAsyncCallbackRefmsg );
    	} // end if 
    	
// end constructor
	public static <T> ResponseDispatchImpl<T> newInstanceClass<T> typeMessage msg ) {
		return new ResponseDispatchImpl<T>( msg );
	}

Provide Context data for this ResponseDispatch that the service implementation can use
	public Map<StringObjectgetContext() {
		return null;
	}

Send a Fault. Must only be invoked once for this ResponseDispatch object

Parameters:
e - the Fault to send
Throws:
java.lang.IllegalStateException if either the sendResponse method or the sendFault method have been called previously
	public void sendFault(Throwable e) {
		ifsendOK() ) {
			try {
				 = e;
finally {
// end try
else {
			throw new IllegalStateException("sendResponse() or sendFault() has been called previously");
// end if
		// Use response invoker if present
		if != null ) {
			//respInvoker.invokeAsyncResponse(new AsyncFaultWrapper(e));
			return;
// end if
		// Now dispatch the response to the callback...
// end method sendFault
Send the response message. Must only be invoked once for this ResponseDispatch object

Parameters:
res - the response message, which is of type T
Throws:
java.lang.IllegalStateException if either the sendResponse method or the sendFault method have been called previously
	@SuppressWarnings("unchecked")
	public void sendResponse(T res) {
		ifsendOK() ) {
			try {
				 = res;
finally {
// end try
else {
			throw new IllegalStateException("sendResponse() or sendFault() has been called previously");
// end if
		// Now dispatch the response to the callback, if present...
		if != null ) {
			handler.setResponse(res);
// end if
		// Use response invoker if present
		if != null ) {
			return;
// end if
// end method sendResponse
	public T get(long timeoutTimeUnit unitthrows Throwable {
		try {
			// wait for result to be available
			if == null &&  == null ) .awaittimeoutunit);
			if != null ) return ;
			if != null ) throw ;
finally {
// end try
		return null;
// end method get
Indicates that sending a response is OK - this is a transactional query in that it also updates the state of this ResponseDispatch, so that it will return true once and once only

Returns:
- true if it is OK to send the response, false otherwise
	private boolean sendOK() {
		return .compareAndSet(falsetrue);
	}

Creates a service reference for the async callback, based on information contained in the supplied message

Parameters:
msg - the incoming message
Returns:
- a CallBackServiceReference
	@SuppressWarnings("unchecked")
    	ifcallbackEPR == null ) return null;
    	
    	CompositeContext compositeContext = callbackEPR.getCompositeContext();
         = compositeContext.getExtensionPointRegistry();
    	ProxyFactory proxyFactory = ExtensibleProxyFactory.getInstance();
    	List<EndpointReferenceeprList = new ArrayList<EndpointReference>();
    	eprList.add(callbackEPR);
    	ObjectFactory<?> factory = new CallbackReferenceObjectFactory(AsyncResponseHandler.classproxyFactoryeprList);
    	
    	return (ServiceReference<AsyncResponseHandler<?>>) factory.getInstance();
    	
    } // end method getAsyncCallbackEPR
Sets the values of various headers in the response message
	private void setResponseHeaders() {
		// Is there an existing message context?
		Message msgContext = ThreadMessageContext.getMessageContext();
		ifmsgContext == null ) {
			// Create a message context
			msgContext = .createMessage();
// end if
		// Add in the header for the RelatesTo Message ID
		ThreadMessageContext.setMessageContext(msgContext);
// end method setResponseHeaders
        FactoryExtensionPoint modelFactories = .getExtensionPoint(FactoryExtensionPoint.class);
        return modelFactories.getFactory(MessageFactory.class);
// end method getMessageFactory
New to GrepCode? Check out our FAQ X