Search in sources :

Example 1 with Reflection

use of oap.reflect.Reflection in project oap by oaplatform.

the class IdFactory method get.

private static IdAccess get(Object value) {
    return ids.computeIfAbsent(value.getClass(), (c) -> {
        Reflection reflect = Reflect.reflect(c);
        val idFields = reflect.annotatedFields(Id.class);
        if (!idFields.isEmpty())
            return new FieldIdAccess(Lists.head(idFields));
        val idMethods = reflect.annotatedMethods(Id.class);
        Reflection.Method setter = null;
        Reflection.Method getter = null;
        for (Reflection.Method m : idMethods) {
            if (m.returnType().assignableTo(String.class))
                getter = m;
            else
                setter = m;
        }
        if (setter == null || getter == null)
            throw new RuntimeException("no @Id annotation");
        return new MethodIdAccess(setter, getter);
    });
}
Also used : lombok.val(lombok.val) Reflection(oap.reflect.Reflection)

Example 2 with Reflection

use of oap.reflect.Reflection 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)

Aggregations

Reflection (oap.reflect.Reflection)2 Collections.emptyMap (java.util.Collections.emptyMap)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1 ToString (lombok.ToString)1 lombok.val (lombok.val)1 ReflectException (oap.reflect.ReflectException)1