X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;ds=sidebyside;f=org.argeo.util%2Fsrc%2Forg%2Fargeo%2Futil%2Fregister%2FComponent.java;fp=org.argeo.util%2Fsrc%2Forg%2Fargeo%2Futil%2Fregister%2FComponent.java;h=cb69b133dd4ebb67c4e322548806add7e7590a5c;hb=cc1dd97ebcc32e1bd754073ad23def182f460452;hp=66ac2ada9233b9d9b121eb24b6d29648ef3e4869;hpb=584b2b87593892c6ded7b645425b603a4f554026;p=lgpl%2Fargeo-commons.git diff --git a/org.argeo.util/src/org/argeo/util/register/Component.java b/org.argeo.util/src/org/argeo/util/register/Component.java index 66ac2ada9..cb69b133d 100644 --- a/org.argeo.util/src/org/argeo/util/register/Component.java +++ b/org.argeo.util/src/org/argeo/util/register/Component.java @@ -6,12 +6,14 @@ import java.util.HashSet; import java.util.Map; import java.util.Set; import java.util.concurrent.CompletableFuture; +import java.util.concurrent.CompletionStage; import java.util.function.Consumer; +import java.util.function.Supplier; /** * A wrapper for an object, whose dependencies and life cycle can be managed. */ -public class Component { +public class Component implements Supplier, Comparable> { private final I instance; @@ -20,6 +22,7 @@ public class Component { private final Map, PublishedType> types; private final Set> dependencies; + private final Map properties; private CompletableFuture activationStarted = null; private CompletableFuture activated = null; @@ -27,10 +30,13 @@ public class Component { private CompletableFuture deactivationStarted = null; private CompletableFuture deactivated = null; + // internal private Set> dependants = new HashSet<>(); - Component(Consumer> register, I instance, Runnable init, Runnable close, - Set> dependencies, Set> classes) { + private RankingKey rankingKey; + + Component(ComponentRegister register, I instance, Runnable init, Runnable close, Set> dependencies, + Set> classes, Map properties) { assert instance != null; assert init != null; assert close != null; @@ -64,7 +70,11 @@ public class Component { // TODO check whether context is active, so that we start right away prepareNextActivation(); - register.accept(this); + long serviceId = register.register(this); + Map props = new HashMap<>(properties); + props.put(RankingKey.SERVICE_ID, serviceId); + this.properties = Collections.unmodifiableMap(props); + rankingKey = new RankingKey(properties); } private void prepareNextActivation() { @@ -130,7 +140,8 @@ public class Component { dependants.add(dependant); } - public I getInstance() { + @Override + public I get() { return instance; } @@ -145,6 +156,15 @@ public class Component { return types.containsKey(clss); } + public Map getProperties() { + return properties; + } + + @Override + public int compareTo(Component o) { + return rankingKey.compareTo(rankingKey); + } + /** A type which has been explicitly exposed by a component. */ public static class PublishedType { private Component component; @@ -165,10 +185,14 @@ public class Component { public Class getType() { return clss; } + + public CompletionStage getValue() { + return value.minimalCompletionStage(); + } } /** Builds a {@link Component}. */ - public static class Builder { + public static class Builder implements Supplier { private final I instance; private Runnable init; @@ -176,12 +200,13 @@ public class Component { private Set> dependencies = new HashSet<>(); private Set> types = new HashSet<>(); + private final Map properties = new HashMap<>(); public Builder(I instance) { this.instance = instance; } - public Component build(Consumer> register) { + public Component build(ComponentRegister register) { // default values if (types.isEmpty()) { types.add(getInstanceClass()); @@ -195,7 +220,7 @@ public class Component { }; // instantiation - Component component = new Component(register, instance, init, close, dependencies, types); + Component component = new Component(register, instance, init, close, dependencies, types, properties); for (Dependency dependency : dependencies) { dependency.type.getPublisher().addDependant(dependency); } @@ -226,6 +251,13 @@ public class Component { return this; } + public void addProperty(String key, Object value) { + if (properties.containsKey(key)) + throw new IllegalStateException("Key " + key + " is already set."); + properties.put(key, value); + } + + @Override public I get() { return instance; }