All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.ow2.cmi.rpc.StatefulPOJOInvocationHandler Maven / Gradle / Ivy

The newest version!
/**
 * CMI : Cluster Method Invocation
 * Copyright (C) 2008 Bull S.A.S.
 * Contact: [email protected]
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA
 * --------------------------------------------------------------------------
 * $Id: StatefulPOJOInvocationHandler.java 2438 2010-02-24 16:20:30Z benoitf $
 * --------------------------------------------------------------------------
 */

package org.ow2.cmi.rpc;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

import javax.naming.NamingException;

import org.ow2.cmi.controller.common.ClusterViewManager;
import org.ow2.cmi.controller.server.ProtocolNotFoundException;
import org.ow2.cmi.ha.RequestId;
import org.ow2.cmi.ha.SessionId;
import org.ow2.cmi.ha.interceptor.HACurrent;
import org.ow2.cmi.reference.CMIProxyHandle;
import org.ow2.cmi.reference.CMIProxyHandleImpl;
import org.ow2.cmi.reference.CMIReference;
import org.ow2.cmi.reference.CMIReferenceable;
import org.ow2.cmi.reference.ObjectNotFoundException;
import org.ow2.util.log.Log;
import org.ow2.util.log.LogFactory;
/**
 * This is the {@link InvocationHandler} for stateful POJOs.
 * @author Loris Bouzonnet
 */
public class StatefulPOJOInvocationHandler extends CMIInvocationHandler {

    /**
     * Id for serializable class.
     */
    private static final long serialVersionUID = -5482752408858461947L;

    /**
     * Logger.
     */
    private static Log logger = LogFactory.getLog(StatefulPOJOInvocationHandler.class);

    /**
     * The classloader to use.
     * When the smart factory is used, it is the smart classloader.
     */
    private transient ClassLoader classLoader;

    /**
     * True if the proxy is on failover.
     */
    private boolean onFailover = false;

    /**
     * Identify the session associated with this proxy.
     */
    private SessionId sessionId;

    /**
     * A number of request.
     */
    private int requestNb = 0;

    /**
     * @param classLoader
     * @param clusterViewManager
     * @param objectName
     * @param protocolName
     * @param itf
     */
    protected StatefulPOJOInvocationHandler(
            final ClassLoader classLoader,
            final ClusterViewManager clusterViewManager,
            final String objectName,
            final String protocolName,
            final Class itf,
            final SessionId sessionId) {
        super(clusterViewManager, objectName, protocolName, true, itf);
        this.classLoader = classLoader;
        this.sessionId = sessionId;
    }

    /**
     * Handles remote methods.
     **/
    @Override
    protected Object invokeRemoteMethod(
            final Object proxy, final Method method, final Object... args) throws Throwable {

        // Use the same classloader that during the creation of this object
        ClassLoader oldClassLoader = Thread.currentThread().getContextClassLoader();
        Thread.currentThread().setContextClassLoader(classLoader);
        setClassLoader(oldClassLoader);
        try {
            // Generate a requestId
            RequestId requestId = new RequestId(sessionId, requestNb);
            // Set this request in the current context
            HACurrent.getHACurrent().putNextReq(requestId);

            Object result = super.invokeRemoteMethod(proxy, method, args);

            // Update the request number now that all is OK
            requestNb++;
            if (onFailover) {
                onFailover = false;
                HACurrent.getHACurrent().setOnFailover(false);
            }
            return result;
        } finally {
            Thread.currentThread().setContextClassLoader(oldClassLoader);
        }
    }


    @Override
    protected void checkInitialized() throws CMIInvocationHandlerException {
        if(classLoader == null) {
            try {
                classLoader = Thread.currentThread().getContextClassLoader();
            } catch(Exception e) {
                throw new POJOInvocationHandlerException(
                        "Cannot retrieve the classloader for object with name " + objectName, e);
            }
        }
    }

    @Override
    protected CMIProxyHandle getHandle(final CMIProxy cmiProxy) {
        if(cmiProxyHandle == null) {
            cmiProxyHandle = new CMIProxyHandleImpl(objectName, itf.getName(), cmiProxy);
        }
        return cmiProxyHandle;
    }

    @SuppressWarnings("unchecked")
    @Override
    protected CMIReferenceable getCMIReferenceable(
            final CMIReference cmiReference) throws POJOInvocationHandlerException {
        try {
            return (CMIReferenceable) StubOrProxyHelper.download(clusterViewManager, cmiReference);
        } catch (ProtocolNotFoundException e) {
            logger.error("Cannot get a CMIReferenceable for " + cmiReference, e);
            throw new POJOInvocationHandlerException("Cannot get a CMIReferenceable for " + cmiReference, e);
        } catch (ObjectNotFoundException e) {
            logger.error("Cannot get a CMIReferenceable for " + cmiReference, e);
            throw new POJOInvocationHandlerException("Cannot get a CMIReferenceable for " + cmiReference, e);
        } catch (NamingException e) {
            logger.debug("No CMIReferenceable is bound for " + cmiReference, e);
            throw new POJOInvocationHandlerException(
                    "No CMIReferenceable is bound for " + cmiReference, e);
        }
    }

    @Override
    protected void onExceptionHook(
            final String objectName, final CMIReferenceable cmiReferenceable)
            throws Throwable {
        setCurrentRef(null);
        // Set onFailover
        onFailover = true;
        // Propagate onFailover status to the server
        HACurrent.getHACurrent().setOnFailover(true);
    }

    @Override
    protected void onFinallyHook(
            final String objectName, final CMIReferenceable cmiReferenceable) {
        // Do nothing
    }

}