Search in sources :

Example 1 with APIMethod

use of com.arm.mbed.cloud.sdk.testserver.internal.model.APIMethod in project mbed-cloud-sdk-java by ARMmbed.

the class APIMappingGenerator method recordAPIMethod.

private APIMethod recordAPIMethod(Method method) {
    if (method == null || !method.isAnnotationPresent(API.class)) {
        return null;
    }
    APIMethod m = new APIMethod(method.getName());
    if (method.isAnnotationPresent(Daemon.class)) {
        Daemon daemonControl = method.getAnnotation(Daemon.class);
        if (daemonControl.start()) {
            m.setDaemonControl(DaemonControl.START);
        }
        if (daemonControl.stop()) {
            m.setDaemonControl(DaemonControl.STOP);
        }
        if (daemonControl.shutdown()) {
            m.setDaemonControl(DaemonControl.SHUTDOWN);
        }
    }
    if (method.getParameterCount() > 0) {
        Parameter[] parameters = method.getParameters();
        for (Parameter parameter : parameters) {
            String defaultValue = determineParameterDefaultValue(parameter);
            determineContentType(parameter);
            APIMethodArgument arg = new APIMethodArgument(parameter.getName(), parameter.getType(), determineContentType(parameter), defaultValue);
            m.addArgument(arg);
        }
    }
    APIMethodArgument returnArg = new APIMethodArgument(method.getReturnType(), null);
    m.setReturnArgument(returnArg);
    return m;
}
Also used : APIMethod(com.arm.mbed.cloud.sdk.testserver.internal.model.APIMethod) Daemon(com.arm.mbed.cloud.sdk.annotations.Daemon) Parameter(java.lang.reflect.Parameter) APIMethodArgument(com.arm.mbed.cloud.sdk.testserver.internal.model.APIMethodArgument)

Example 2 with APIMethod

use of com.arm.mbed.cloud.sdk.testserver.internal.model.APIMethod in project mbed-cloud-sdk-java by ARMmbed.

the class Engine method stopInstanceDaemons.

private void stopInstanceDaemons(String instanceId) throws ServerCacheException, MissingInstanceException {
    APIModule module = cache.fetchModuleFromInstance(instanceId);
    if (module != null) {
        if (module.hasDaemonControlMethods()) {
            ModuleInstance instance = retrieveInstance(instanceId);
            APICaller caller = createAnApiCaller();
            logger.logInfo("Stopping SDK module instance [" + instanceId + "] daemon threads");
            List<APIMethod> stoppingMethods = module.getStopDaemonMethods();
            if (stoppingMethods != null) {
                stoppingMethods.forEach(m -> {
                    try {
                        caller.callAPIOnModuleInstance(instance, m.getName(), null);
                    } catch (UnknownAPIException | APICallException e) {
                        e.printStackTrace();
                    }
                });
            }
            logger.logInfo("Shutting down SDK module instance [" + instanceId + "] daemon executor service");
            List<APIMethod> shuttingdownMethods = module.getShutdownDaemonMethods();
            if (shuttingdownMethods != null) {
                shuttingdownMethods.forEach(m -> {
                    try {
                        caller.callAPIOnModuleInstance(instance, m.getName(), null);
                    } catch (UnknownAPIException | APICallException e) {
                        e.printStackTrace();
                    }
                });
            }
        }
    }
}
Also used : UnknownAPIException(com.arm.mbed.cloud.sdk.testserver.internal.model.UnknownAPIException) APIModule(com.arm.mbed.cloud.sdk.testserver.internal.model.APIModule) APIMethod(com.arm.mbed.cloud.sdk.testserver.internal.model.APIMethod) APICaller(com.arm.mbed.cloud.sdk.testutils.APICaller) APICallException(com.arm.mbed.cloud.sdk.testutils.APICallException) ModuleInstance(com.arm.mbed.cloud.sdk.testserver.internal.model.ModuleInstance)

Example 3 with APIMethod

use of com.arm.mbed.cloud.sdk.testserver.internal.model.APIMethod in project mbed-cloud-sdk-java by ARMmbed.

the class APICaller method callAPIOnModuleInstance.

@SuppressWarnings("null")
public APIMethodResult callAPIOnModuleInstance(ModuleInstance moduleInstance, String method, Map<String, Object> parameters) throws UnknownAPIException, APICallException {
    if (moduleInstance == null) {
        throwMissingModule(null);
    }
    APIModule moduleDescription = moduleInstance.getModuleDescription();
    if (moduleDescription == null) {
        throwMissingModule(moduleDescription);
    }
    if (method == null) {
        throwUnknownAPI(moduleDescription.getSimpleName(), method);
    }
    final List<APIMethod> methodObjs = moduleDescription.getMethod(method);
    if (methodObjs == null) {
        throwUnknownAPI(moduleDescription.getSimpleName(), method);
    }
    APICallException lastException = null;
    APIMethodResult result = null;
    Object instance = moduleInstance.getInstance();
    // then last exception is raised or the last result is returned if not null.
    for (final APIMethod methodObj : methodObjs) {
        try {
            result = null;
            lastException = null;
            API api = new API(moduleDescription, methodObj);
            result = api.call(instance, parameters);
            // other methods
            if (!result.wasExceptionRaised()) {
                return result;
            }
        } catch (APICallException exception) {
            lastException = exception;
        }
    }
    // If the call was successful but an exception was raised during it then the failure is returned.
    if (result != null) {
        return result;
    }
    // If the call was not successful and hence, an exception was raised, then it is thrown.
    throw lastException;
}
Also used : APIModule(com.arm.mbed.cloud.sdk.testserver.internal.model.APIModule) APIMethod(com.arm.mbed.cloud.sdk.testserver.internal.model.APIMethod) APIMethodResult(com.arm.mbed.cloud.sdk.testserver.internal.model.APIMethodResult)

Aggregations

APIMethod (com.arm.mbed.cloud.sdk.testserver.internal.model.APIMethod)3 APIModule (com.arm.mbed.cloud.sdk.testserver.internal.model.APIModule)2 Daemon (com.arm.mbed.cloud.sdk.annotations.Daemon)1 APIMethodArgument (com.arm.mbed.cloud.sdk.testserver.internal.model.APIMethodArgument)1 APIMethodResult (com.arm.mbed.cloud.sdk.testserver.internal.model.APIMethodResult)1 ModuleInstance (com.arm.mbed.cloud.sdk.testserver.internal.model.ModuleInstance)1 UnknownAPIException (com.arm.mbed.cloud.sdk.testserver.internal.model.UnknownAPIException)1 APICallException (com.arm.mbed.cloud.sdk.testutils.APICallException)1 APICaller (com.arm.mbed.cloud.sdk.testutils.APICaller)1 Parameter (java.lang.reflect.Parameter)1