io.vlingo.xoom.actors.World Maven / Gradle / Ivy
// Copyright © 2012-2021 VLINGO LABS. All rights reserved.
//
// This Source Code Form is subject to the terms of the
// Mozilla Public License, v. 2.0. If a copy of the MPL
// was not distributed with this file, You can obtain
// one at https://mozilla.org/MPL/2.0/.
package io.vlingo.xoom.actors;
import java.lang.reflect.Constructor;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import io.vlingo.xoom.actors.plugin.completes.DefaultCompletesEventuallyProviderKeeper;
import io.vlingo.xoom.actors.plugin.logging.DefaultLoggerProviderKeeper;
import io.vlingo.xoom.actors.plugin.mailbox.DefaultMailboxProviderKeeper;
/**
* The {@code World} of the actor runtime through which all Stage and Actor instances are created and run.
* All plugins and all default facilities are registered through the {@code World}.
*/
public final class World implements Registrar {
static final long PRIVATE_ROOT_ID = Long.MAX_VALUE;
static final String PRIVATE_ROOT_NAME = "#private";
static final long PUBLIC_ROOT_ID = PRIVATE_ROOT_ID - 1;
static final String PUBLIC_ROOT_NAME = "#public";
static final long DEADLETTERS_ID = PUBLIC_ROOT_ID - 1;
static final String DEADLETTERS_NAME = "#deadLetters";
public static final long HIGH_ROOT_ID = DEADLETTERS_ID - 1;
static final String DEFAULT_STAGE = "__defaultStage";
private final AddressFactory addressFactory;
private final Configuration configuration;
private final String name;
private final Map stages;
private final Map dynamicDependencies;
private ClassLoader classLoader;
private CompletesEventuallyProviderKeeper completesProviderKeeper;
private DeadLetters deadLetters;
private Logger defaultLogger;
private Actor defaultParent;
private Supervisor defaultSupervisor;
private LoggerProviderKeeper loggerProviderKeeper;
private MailboxProviderKeeper mailboxProviderKeeper;
private Stoppable privateRoot;
private Stoppable publicRoot;
/**
* Answers a new {@code World} with the given {@code name} and that is configured with
* the contents of the {@code xoom-actors.properties} file.
* @param name the {@code String} name to assign to the new {@code World} instance
* @return {@code World}
*/
public static World start(final String name) {
return start(name, io.vlingo.xoom.actors.Properties.properties);
}
/**
* Answers a new {@code World} with the given {@code name} and that is configured with
* the contents of the properties.
* @param name the String name to assign to the new {@code World} instance
* @param properties the java.util.Properties used for configuration
* @return {@code World}
*/
public static World start(final String name, final java.util.Properties properties) {
return start(name, Configuration.defineWith(properties));
}
/**
* Answers a new {@code World} with the given {@code name} and that is configured with
* the contents of the {@code configuration}.
* @param name the {@code String} name to assign to the new {@code World} instance
* @param configuration the {@code Configuration} used for configuration
* @return {@code World}
*/
public static World start(final String name, final Configuration configuration) {
if (name == null) {
throw new IllegalArgumentException("The world name must not be null.");
}
return new World(name, configuration);
}
/**
* Answers a new {@code World} with the given {@code name} and that is configured with
* the contents of the default {@code Configuration} of sensible settings.
* @param name the {@code String} name to assign to the new {@code World} instance
* @return {@code World}
*/
public static World startWithDefaults(final String name) {
return start(name, Configuration.define());
}
/**
* Answers the {@code T} protocol of the newly created {@code Actor} that implements the {@code protocol}.
* @param protocol the {@code Class} protocol
* @param type the {@code Class extends Actor>} of the {@code Actor} to create
* @param instantiator the {@code ActorInstantiator} used to instantiate the Actor
* @param the protocol type
* @param the Actor type
* @return T
*/
public T actorFor(final Class protocol, final Class extends Actor> type, final ActorInstantiator instantiator) {
if (isTerminated()) {
throw new IllegalStateException("XOOM: Stopped.");
}
return stage().actorFor(protocol, type, instantiator);
}
/**
* Answers the {@code T} protocol of the newly created {@code Actor} that implements the {@code protocol}.
* @param protocol the {@code Class} protocol
* @param type the {@code Class extends Actor>} of the {@code Actor} to create
* @param parameters the {@code Object[]} of constructor parameters
* @param the protocol type
* @return T
*/
public T actorFor(final Class protocol, final Class extends Actor> type, final Object...parameters) {
if (isTerminated()) {
throw new IllegalStateException("XOOM: Stopped.");
}
return stage().actorFor(protocol, type, parameters);
}
/**
* Answers the {@code T} protocol of the newly created {@code Actor} that is defined by
* the parameters of {@code definition} that implements the {@code protocol}.
* @param protocol the {@code Class} protocol that the {@code Actor} supports
* @param the protocol type
* @param definition the {@code Definition} providing parameters to the {@code Actor}
* @return T
*/
public T actorFor(final Class protocol, final Definition definition) {
if (isTerminated()) {
throw new IllegalStateException("XOOM: Stopped.");
}
return stage().actorFor(protocol, definition);
}
/**
* Answers a {@code Protocols} that provides one or more supported protocols for the
* newly created {@code Actor} according to {@code definition}.
* @param protocols the {@code Class>[]} array of protocols that the {@code Actor} supports
* @param type the {@code Class extends Actor>} of the {@code Actor} to create
* @param parameters the {@code Object[]} of constructor parameters
* @return {@code Protocols}
*/
public Protocols actorFor(final Class>[] protocols, final Class extends Actor> type, final Object...parameters) {
if (isTerminated()) {
throw new IllegalStateException("XOOM: Stopped.");
}
return stage().actorFor(protocols, type, parameters);
}
/**
* Answers a {@code Protocols} that provides one or more supported protocols for the
* newly created {@code Actor} according to {@code definition}.
* @param protocols the {@code Class>[]} array of protocols that the {@code Actor} supports
* @param definition the {@code Definition} providing parameters to the {@code Actor}
* @return {@code Protocols}
*/
public Protocols actorFor(final Class>[] protocols, final Definition definition) {
if (isTerminated()) {
throw new IllegalStateException("XOOM: Stopped.");
}
return stage().actorFor(protocols, definition);
}
/**
* Answers the {@code AddressFactory} for this {@code World}.
* @return {@code AddressFactory}
*/
public AddressFactory addressFactory() {
return addressFactory;
}
/**
* Answers the {@code Configuration} for this {@code World}.
* @return {@code Configuration}
*/
public Configuration configuration() {
return configuration;
}
/**
* Answers the {@code DeadLetters} for this {@code World}, which is backed
* by an {@code Actor}. Interested parties may register for notifications
* as a {@code DeadLettersListener} via the {@code DeadLetters} protocol.
* @return DeadLetters
*/
public DeadLetters deadLetters() {
return deadLetters;
}
/**
* Answers a new {@code CompletesEventually} instance that backs the {@code clientReturns}.
* This manages the {@code Returns} using the {@code CompletesEventually} plugin {@code Actor} pool.
* @param clientReturns the {@code CompletesEventually} allocated for eventual completion of {@code clientReturns}
* @param the type of the Returns
* @return CompletesEventually
*/
public CompletesEventually completesFor(final Returns clientReturns) {
return completesProviderKeeper.findDefault().provideCompletesFor(clientReturns);
}
/**
* Answers a {@code CompletesEventually} instance identified by {@code address} that backs the {@code clientReturns}.
* This manages the {@code Returns} using the {@code CompletesEventually} plugin {@code Actor} pool.
* @param address the {@code Address} of the CompletesEventually actor to reuse
* @param clientReturns the {@code CompletesEventually} allocated for eventual completion of {@code clientReturns}
* @return CompletesEventually
*/
public CompletesEventually completesFor(final Address address, final Returns