FileDocCategorySizeDatePackage
MessageEndpointInvocationHandler.javaAPI DocGlassfish v2 API9877Fri May 04 22:34:24 BST 2007com.sun.enterprise.connectors.inflow

MessageEndpointInvocationHandler

public final class MessageEndpointInvocationHandler extends Object implements InvocationHandler
This class handles the implementation of two interfaces: 1) javax.resource.spi.endpoint.MessageEndpoint; 2) any message listener type (e.g. javax.jms.MessageListener, OR javax.xml.messaging.OnewayListener)
author
Qingqing Ouyang

Fields Summary
private com.sun.ejb.MessageBeanListener
listener_
private boolean
beforeDeliveryCalled
private boolean
throwTransactedExceptions_
private com.sun.ejb.MessageBeanProtocolManager
messageBeanPM_
private static final String
MESSAGE_ENDPOINT
private static final String
THROW_TRANSACTED_EXCEPTIONS_PROP
private static final Logger
logger
private final Agent
callFlowAgent
Constructors Summary
public MessageEndpointInvocationHandler(com.sun.ejb.MessageBeanListener listener, com.sun.ejb.MessageBeanProtocolManager pm)
Constructs a MessageEndpointInvocationHandler.

param
listener MessageBeanListener to deliver messages.
param
pm Protocol object to MDB container.

 
    
                         
      
                                              
        this.listener_ = listener;
        this.messageBeanPM_ = pm;

	throwTransactedExceptions_ =
	    ConnectorConstants.THROW_TRANSACTED_EXCEPTIONS;

        if( throwTransactedExceptions_ != true ) {
            logger.info(ConnectorConstants.THROW_TRANSACTED_EXCEPTIONS_PROP + 
	                " set to false");
        } 

    
Methods Summary
public java.lang.Objectinvoke(java.lang.Object proxy, java.lang.reflect.Method method, java.lang.Object[] args)
Invokes the method

param
proxy Object
param
method Method to be executed.
param
args Arguments
throws
Throwable.


        // NOTE : be careful with "args" parameter.  It is null
        //        if method signature has 0 arguments.

        String methodClassName = method.getDeclaringClass().getName();
        String methodName = method.getName();
        Object returnValue = null;

        if (logger.isLoggable(Level.FINEST)) {
            
            String msg = "Invoking method [" + methodName 
                + "] from class [" + methodClassName + "]";
            logger.log(Level.FINEST, msg);
        }

        // delegate the beforeDelivery and afterDelivery calls
        // to the MDB container
        if (MESSAGE_ENDPOINT.equals(methodClassName)) {

            if ("beforeDelivery".equals(methodName)) {
                Method onMessageMethod = (Method) args[0];
                beforeDeliveryCalled = true;
                listener_.beforeMessageDelivery(onMessageMethod, false);

            } else if ("afterDelivery".equals(methodName)) {
                beforeDeliveryCalled = false; //reset
                listener_.afterMessageDelivery();
                
            } else if ("release".equals(methodName)) {
		messageBeanPM_.destroyMessageBeanListener(listener_);
            } else {
                logger.log(Level.SEVERE, "endpointfactory.method_not_defined",
                        new Object[] {methodName, MESSAGE_ENDPOINT});
                throw new RuntimeException(methodName);
            }
            
        } else if ("java.lang.Object".equals(methodClassName)) {
            returnValue = invokeJavaObjectMethod(this, method, args);
            
        } else { //the rest are considered methods for message delivery
            
            //RA did not call beforeDelivery, handle it here
            if (!beforeDeliveryCalled) {
                J2EETransactionManager txManager = 
                    Switch.getSwitch().getTransactionManager();
                boolean txImported = (txManager.getTransaction() != null);
                listener_.beforeMessageDelivery(method, txImported);
            }
            
            try {
                //returnValue = listener_.deliverMessage(method, args);
            
                // Notify Call Flow Agent. 
                try{
            	    callFlowAgent.requestStart(
                            RequestType.REMOTE_ASYNC_MESSAGE);
            	    callFlowAgent.startTime(
                            ContainerTypeOrApplicationType.EJB_CONTAINER);
                    // This is an opportunity to provide callerIPAddress and
                    // remote user name. But since this information is not
                    // currently available. So, we don't call
                    // callFlowAgent.setRequestInfo().
                } catch (Exception ex){
                    logger.log(Level.WARNING, "Call Flow Agent threw exception" + ex);
                }
	
                returnValue = listener_.deliverMessage(args);
            } catch (Throwable ex) {
                if( messageBeanPM_.isDeliveryTransacted(method) ) {
                    if( throwTransactedExceptions_ ) {
                        throw ex;
                    } else {
                        logger.log(Level.INFO, "Resource adapter eating " +
                                   " transacted exception", ex);
                    }
                } else {
                    throw ex;
                }
            } finally {

         	// Send end notification to call flow agent
                try{
                    callFlowAgent.endTime();
                    callFlowAgent.requestEnd();
                } catch (Exception ex){
                    logger.log(Level.WARNING, "Call Flow Agent threw exception" + ex);
                }
	
                //assume that if the RA didn't call beforeDelivery, it
                //would not call afterDelivery.  o.w. it will be hard to
                //to determine when to pair the afterDelivery call.
                if (!beforeDeliveryCalled) {
                    listener_.afterMessageDelivery(); 
                }
                beforeDeliveryCalled = false;
            }
        }
        return returnValue;
    
private java.lang.ObjectinvokeJavaObjectMethod(java.lang.reflect.InvocationHandler handler, java.lang.reflect.Method method, java.lang.Object[] args)
This is the same implementation in com.sun.ejb.container.InvocationHandlerUtil Need to abstract out at some point.


        Object returnValue = null;
        
        // Can only be one of : 
        //     boolean java.lang.Object.equals(Object)
        //     int     java.lang.Object.hashCode()
        //     String  java.lang.Object.toString()
        //
        // Optimize by comparing as few characters as possible.

        switch( method.getName().charAt(0) ) {
            case 'e" :
                Object other = Proxy.isProxyClass(args[0].getClass()) ?
                    Proxy.getInvocationHandler(args[0]) : args[0];
                returnValue = new Boolean(handler.equals(other));
                break;
            case 'h" :
                returnValue = new Integer(handler.hashCode());
                break;
            case 't" :
                returnValue = handler.toString();
                break;
            default :
                throw new RuntimeException(method.getName());
        }

        return returnValue;