use of com.arm.mbed.cloud.sdk.testserver.internal.model.APIModule 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.APIModule 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