use of oap.reflect.ReflectException in project oap by oaplatform.
the class Kernel method initializeServices.
private Map<String, Module.Service> initializeServices(Map<String, Module.Service> services, Set<String> initialized, ApplicationConfiguration config) {
HashMap<String, Module.Service> deferred = new HashMap<>();
for (Map.Entry<String, Module.Service> entry : services.entrySet()) {
Module.Service service = entry.getValue();
service.name = service.name != null ? service.name : entry.getKey();
if (!service.enabled) {
initialized.add(service.name);
log.debug("service {} is disabled.", entry.getKey());
continue;
}
if (service.profile != null && !config.profiles.contains(service.profile)) {
log.debug("skipping " + entry.getKey() + " with profile " + service.profile);
continue;
}
List<String> dependsOn = Stream.of(service.dependsOn).filter(this::serviceEnabled).toList();
if (initialized.containsAll(dependsOn)) {
log.debug("initializing {} as {}", entry.getKey(), service.name);
if (service.implementation == null) {
throw new ApplicationException("failed to initialize service: " + service.name + ". implementation == null");
}
@SuppressWarnings("unchecked") Reflection reflect = Reflect.reflect(service.implementation, Module.coersions);
Object instance;
if (!service.isRemoteService()) {
try {
instance = linker.link(service, () -> reflect.newInstance(service.parameters));
initializeDynamicConfigurations(reflect, instance);
} catch (ReflectException e) {
log.info("service name = {}, remote = {}, profile = {}", service.name, service.remote, service.profile);
throw e;
}
} else
instance = RemoteInvocationHandler.proxy(service.remote, reflect.underlying);
register(service.name, instance);
if (!service.name.equals(entry.getKey()))
register(entry.getKey(), instance);
if (service.supervision.supervise)
supervisor.startSupervised(service.name, instance, service.supervision.startWith, service.supervision.stopWith, service.supervision.reloadWith);
if (service.supervision.thread)
supervisor.startThread(service.name, instance);
else {
if (service.supervision.schedule && service.supervision.cron != null)
supervisor.scheduleCron(service.name, (Runnable) instance, service.supervision.cron);
else if (service.supervision.schedule && service.supervision.delay != 0)
supervisor.scheduleWithFixedDelay(service.name, (Runnable) instance, service.supervision.delay, MILLISECONDS);
}
initialized.add(service.name);
} else {
log.debug("dependencies are not ready - deferring " + service.name + ": " + subtract(service.dependsOn, initialized));
deferred.put(entry.getKey(), service);
}
}
return deferred.size() == services.size() ? deferred : initializeServices(deferred, initialized, config);
}
use of oap.reflect.ReflectException in project oap by oaplatform.
the class Linker method linkListeners.
@SuppressWarnings("unchecked")
private void linkListeners(Module.Service service, Object instance) {
service.listen.forEach((listener, reference) -> {
log.debug("setting " + service.name + " to listen to " + reference + " with listener " + listener);
String methodName = "add" + StringUtils.capitalize(listener) + "Listener";
Object linked = kernel.service(linkedServiceName(reference));
if (linked == null)
throw new ApplicationException("for " + service.name + " listening object " + reference + " is not found");
Optionals.fork(Reflect.reflect(linked.getClass()).method(methodName)).ifPresent(m -> m.invoke(linked, instance)).ifAbsentThrow(() -> new ReflectException("listener " + listener + " should have method " + methodName + " in " + reference));
});
}
Aggregations