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;
}
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();
}
});
}
}
}
}
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;
}
Aggregations