Search in sources :

Example 1 with ReflectException

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);
}
Also used : HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Reflection(oap.reflect.Reflection) ToString(lombok.ToString) ReflectException(oap.reflect.ReflectException) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map) Collections.emptyMap(java.util.Collections.emptyMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 2 with ReflectException

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));
    });
}
Also used : Reflect(oap.reflect.Reflect) LinkedHashMap(java.util.LinkedHashMap) Slf4j(lombok.extern.slf4j.Slf4j) List(java.util.List) Optionals(oap.util.Optionals) Iterator(java.util.Iterator) ListIterator(java.util.ListIterator) Map(java.util.Map) StringUtils(org.apache.commons.lang3.StringUtils) Supplier(java.util.function.Supplier) ReflectException(oap.reflect.ReflectException) ReflectException(oap.reflect.ReflectException)

Aggregations

Map (java.util.Map)2 ReflectException (oap.reflect.ReflectException)2 Collections.emptyMap (java.util.Collections.emptyMap)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 ListIterator (java.util.ListIterator)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1 Supplier (java.util.function.Supplier)1 ToString (lombok.ToString)1 Slf4j (lombok.extern.slf4j.Slf4j)1 Reflect (oap.reflect.Reflect)1 Reflection (oap.reflect.Reflection)1 Optionals (oap.util.Optionals)1 StringUtils (org.apache.commons.lang3.StringUtils)1