X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;f=org.argeo.enterprise%2Fsrc%2Forg%2Fargeo%2Futil%2Fregister%2FStaticRegister.java;fp=org.argeo.enterprise%2Fsrc%2Forg%2Fargeo%2Futil%2Fregister%2FStaticRegister.java;h=0c55bddb2dda02eab787fc822aec1345d7835102;hb=df60db390a109c60ac76980ab918f4bdbec1364b;hp=0000000000000000000000000000000000000000;hpb=11dff0ce7fa612be5557822d8a0128cd61f54f8d;p=lgpl%2Fargeo-commons.git diff --git a/org.argeo.enterprise/src/org/argeo/util/register/StaticRegister.java b/org.argeo.enterprise/src/org/argeo/util/register/StaticRegister.java new file mode 100644 index 000000000..0c55bddb2 --- /dev/null +++ b/org.argeo.enterprise/src/org/argeo/util/register/StaticRegister.java @@ -0,0 +1,65 @@ +package org.argeo.util.register; + +import java.util.HashSet; +import java.util.IdentityHashMap; +import java.util.Set; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.atomic.AtomicBoolean; + +/** A minimal component register. */ +public class StaticRegister { + private final static AtomicBoolean started = new AtomicBoolean(false); + private final static IdentityHashMap> components = new IdentityHashMap<>(); + + static synchronized void registerComponent(Component component) { + if (started.get()) // TODO make it really dynamic + throw new IllegalStateException("Already activated"); + if (components.containsKey(component.getInstance())) + throw new IllegalArgumentException("Already registered as component"); + components.put(component.getInstance(), component); + } + + static synchronized Component get(Object instance) { + if (!components.containsKey(instance)) + throw new IllegalArgumentException("Not registered as component"); + return components.get(instance); + } + + synchronized static void activate() { + if (started.get()) + throw new IllegalStateException("Already activated"); + Set> constraints = new HashSet<>(); + for (Component component : components.values()) { + component.startActivating(); + constraints.add(component.getActivated()); + } + + // wait + try { + CompletableFuture.allOf(constraints.toArray(new CompletableFuture[0])).thenRun(() -> started.set(true)) + .get(); + } catch (ExecutionException | InterruptedException e) { + throw new RuntimeException(e); + } + } + + synchronized static void deactivate() { + if (!started.get()) + throw new IllegalStateException("Not activated"); + Set> constraints = new HashSet<>(); + for (Component component : components.values()) { + component.startDeactivating(); + constraints.add(component.getDeactivated()); + } + + // wait + try { + CompletableFuture.allOf(constraints.toArray(new CompletableFuture[0])).thenRun(() -> started.set(false)) + .get(); + } catch (ExecutionException | InterruptedException e) { + throw new RuntimeException(e); + } + } + +}