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

com.vaadin.server.ServerRpcMethodInvocation Maven / Gradle / Ivy

There is a newer version: 8.27.3
Show newest version
/*
 * Vaadin Framework 7
 *
 * Copyright (C) 2000-2024 Vaadin Ltd
 *
 * This program is available under Vaadin Commercial License and Service Terms.
 *
 * See  for the full
 * license.
 */
package com.vaadin.server;

import java.lang.reflect.Method;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import com.vaadin.shared.communication.MethodInvocation;
import com.vaadin.shared.communication.ServerRpc;

public class ServerRpcMethodInvocation extends MethodInvocation {

    private static final Map invocationMethodCache = new ConcurrentHashMap(
            128, 0.75f, 1);

    private final Method method;

    private final Class interfaceClass;

    public ServerRpcMethodInvocation(String connectorId,
            Class interfaceClass, String methodName,
            int parameterCount) {
        super(connectorId, interfaceClass.getName(), methodName);

        assert ServerRpc.class.isAssignableFrom(interfaceClass);
        this.interfaceClass = interfaceClass;

        method = findInvocationMethod(interfaceClass, methodName,
                parameterCount);
    }

    public Class getInterfaceClass() {
        return interfaceClass;
    }

    public Method getMethod() {
        return method;
    }

    /**
     * Tries to find the method from the cache or alternatively by invoking
     * {@link #doFindInvocationMethod(Class, String, int)} and updating the
     * cache.
     *
     * @param targetType
     * @param methodName
     * @param parameterCount
     * @return
     */
    private Method findInvocationMethod(Class targetType, String methodName,
            int parameterCount) {
        // TODO currently only using method name and number of parameters as the
        // signature
        String signature = targetType.getName() + "." + methodName + "("
                + parameterCount;
        Method invocationMethod = invocationMethodCache.get(signature);

        if (invocationMethod == null) {
            invocationMethod = doFindInvocationMethod(targetType, methodName,
                    parameterCount);

            if (invocationMethod != null) {
                invocationMethodCache.put(signature, invocationMethod);
            }
        }

        if (invocationMethod == null) {
            throw new IllegalStateException("Can't find method " + methodName
                    + " with " + parameterCount + " parameters in "
                    + targetType.getName());
        }

        return invocationMethod;
    }

    /**
     * Tries to find the method from the class by looping through available
     * methods.
     *
     * @param targetType
     * @param methodName
     * @param parameterCount
     * @return
     */
    private Method doFindInvocationMethod(Class targetType,
            String methodName, int parameterCount) {
        Method[] methods = targetType.getMethods();
        for (Method method : methods) {
            Class[] parameterTypes = method.getParameterTypes();
            if (method.getName().equals(methodName)
                    && parameterTypes.length == parameterCount) {
                return method;
            }
        }
        return null;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy