Package Framework.anchored

Source Code of Framework.anchored.ServiceProxy

/*
Copyright (c) 2003-2008 ITerative Consulting Pty Ltd. All Rights Reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted
provided that the following conditions are met:

o Redistributions of source code must retain the above copyright notice, this list of conditions and
the following disclaimer.
 
o Redistributions in binary form must reproduce the above copyright notice, this list of conditions
and the following disclaimer in the documentation and/or other materials provided with the distribution.
   
o This jcTOOL Helper Class software, whether in binary or source form may not be used within,
or to derive, any other product without the specific prior written permission of the copyright holder

 
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING,
BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.


*/
package Framework.anchored;

import java.io.Serializable;
import java.lang.reflect.Method;
import java.net.SocketException;
import java.rmi.RemoteException;
import java.util.Map;
import java.util.Set;

import org.apache.log4j.Logger;

import Framework.AppContextHolder;
import Framework.CloneHelper;
import Framework.ErrorMgr;
import Framework.FrameworkUtils;
import Framework.ParameterHolder;
import Framework.RemoteAccessException;
import Framework.remoting.PluggableRmiProxyFactoryBean;
import GenericDBMS.TransactionMgr;

public class ServiceProxy implements Serializable {
    private static Logger _log = Logger.getLogger(ServiceProxy.class);
    private static final long  serialVersionUID  = -7427653354916457068L;
    private MethodInvoker delegate = null;
    private String objectName;
    private String hostName;
    private int port;

    private Class<?> proxyClass;

    public ServiceProxy(String objectName, String hostname, int port, Class<?> proxyClass) {
        this.objectName = objectName;
        this.hostName = hostname;
        this.port = port;
        this.proxyClass = proxyClass;     
    }

    /**
     * Returns the unique service URL for this proxy. The service URL is the endpoint
     * of the exposed service, so all proxies to the same service should return the
     * same value from this method.
     * @return
     */
    public String getServiceUrl() {
      return "rmi://" + this.hostName + ":" + this.port + "/" + this.objectName;
    }
    /**
     * Determine whether the passed method should be proxied (ie delegated
     * back to the calling method) or invoked on the super class as normal
     * @param method
     * @return
     */
    private boolean shouldProxy(Method method) {
        String methodName = method.getName();
        Class<?>[] paramTypes = method.getParameterTypes();

        if (methodName.equals("finalize") && paramTypes.length == 0) {
            return false;
        }
        else if (methodName.equals("toString") && paramTypes.length == 0) {
            return false;
        }
        else if (methodName.equals("setIsAnchored") && paramTypes.length == 1 && paramTypes[0].equals(Boolean.TYPE)) {
            // Not quite sure what effect setting isAnchored has on a remote proxy, but I
            // think it should be not proxied as it's most likely being called from the
            // constructor.
            return false;
        }
        else if (methodName.equals("writeReplace") && paramTypes.length == 0) {
            return false;
        }
        else if (methodName.equals("readResolve") && paramTypes.length == 0) {
            return false;
        }

        return true;
    }

    /**
     * Invoke the desired method across JVM boundaries, passing the objects as arguments. The methodName is the unique
     * name of the method returned from the MethodMapper, and if method != MethodMapper.getMethod(this.proxyClass,
     * @param methodName
     * @param method
     * @param args
     * @return
     */
    private Object invoke(String methodName, Method method, Object...args) {
        try {
            _log.debug("Beginning invocation of: " + methodName + " on " + delegate);
            Map<String, Object> appContextValues = FrameworkUtils.getAppContextValues();
            TransactionMgr.beginRemoteCall(this, appContextValues);
            AppContextHolder result = delegate.invoke(this.objectName, methodName, args, appContextValues);

            TransactionMgr.endRemoteCall(result);
           
            _log.debug("Ending invocation of: " + methodName + " on " + delegate);
            /*
             * Now unpack any parameters
             */
            if (method != null) {
              Set<Integer> parameterIndicies = MethodMapper.getOutputParameters(method);
              for (Integer parameterIndex : parameterIndicies) {
                    Object outputParam = result.getParameter("arg" + parameterIndex);
                    if (outputParam instanceof ParameterHolder) {
                        ((ParameterHolder)outputParam).duplicateIntoTarget(((ParameterHolder)args[parameterIndex]), false);
                    } else {
                        CloneHelper.duplicateIntoTarget(outputParam, args[parameterIndex], false);
                    }
              }
            }
            return result.get();
        }
        catch (SocketException se) {
          throw new RemoteAccessException(se);
        }
        catch (RemoteException re) {
          throw new RemoteAccessException(re);
        }
        catch (org.springframework.remoting.RemoteAccessException re) {
          throw new RemoteAccessException(re);
        }
        catch (Throwable t) {
            if (t instanceof RuntimeException) {
                throw (RuntimeException)t;
            }
            else {
                RuntimeException errorVar = new RuntimeException(t);
                ErrorMgr.addError(errorVar);
                throw errorVar;
            }
        }
    }

    public Object intercept(String methodName, Object... args) {
        if (delegate == null) {
            delegate = (MethodInvoker)resolveProxy();
        }
        Method method = MethodMapper.getMethod(proxyClass, methodName);
        return invoke(methodName, method, args);
    }

    /**
     * The intercept method is the
     */
    public Object interceptWhenAllowed(Object obj, String methodName, Object... args) {
        try {
            if (delegate == null) {
                delegate = (MethodInvoker)resolveProxy();
            }
            Method method = MethodMapper.getMethod(proxyClass, methodName);

            if (shouldProxy(method)) {
                return invoke(methodName, method, args);
            }
            else {
                // This method should not be proxied, just invoked directly
                return method.invoke(obj, args);
            }
        }
        catch (Throwable t) {
            if (t instanceof RuntimeException) {
                throw (RuntimeException)t;
            }
            else {
                RuntimeException errorVar = new RuntimeException(t);
                ErrorMgr.addError(errorVar);
                throw errorVar;
            }
        }
    }

    protected Object resolveProxy(){
        // client side
        PluggableRmiProxyFactoryBean bean = new PluggableRmiProxyFactoryBean();
        // set service URI amd interface
        bean.setServiceUrl(getServiceUrl());
        _log.debug("resolveProxy serviceURL: " + bean.getServiceUrl());
        bean.setServiceInterface(MethodInvoker.class);
        // TF:13/05/2009:Set this method to retry a connection if the server connection drops out for some reason
        bean.setRefreshStubOnConnectFailure(true);
        bean.afterPropertiesSet();
        // get the proxy
        return bean.getObject();
    }

}
TOP

Related Classes of Framework.anchored.ServiceProxy

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.