All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.vlingo.actors.Environment Maven / Gradle / Ivy

Go to download

Type safe Actor Model toolkit for reactive concurrency and resiliency using Java and other JVM languages.

There is a newer version: 1.7.5
Show newest version
// Copyright © 2012-2018 Vaughn Vernon. 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.actors;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;

public class Environment {
  final Address address;
  final List children;
  Address completesEventuallyAddress;
  final Definition definition;
  final FailureMark failureMark;
  final Logger logger;
  final Mailbox mailbox;
  final Supervisor maybeSupervisor;
  final Actor parent;
  final Map proxyCache;
  final Stage stage;
  final Stowage stowage;
  final Stowage suspended;

  private final AtomicBoolean secured;
  private final AtomicBoolean stopped;

  private Class[] stowageOverrides;

  protected Environment(
          final Stage stage,
          final Address address,
          final Definition definition,
          final Actor parent,
          final Mailbox mailbox,
          final Supervisor maybeSupervisor,
          final Logger logger) {
    assert(stage != null);
    this.stage = stage;
    assert(address != null);
    this.address = address;
    assert(definition != null);
    this.definition = definition;
    if (address.id() != World.PRIVATE_ROOT_ID) assert(parent != null);
    this.parent = parent;
    assert(mailbox != null);
    this.mailbox = mailbox;
    this.maybeSupervisor = maybeSupervisor;
    this.failureMark = new FailureMark();
    this.logger = logger;
    this.children = new ArrayList(0);
    this.proxyCache = new HashMap<>();
    this.stowage = new Stowage();
    this.stowageOverrides = null;
    this.suspended = new Stowage();
    
    this.secured = new AtomicBoolean(false);
    this.stopped = new AtomicBoolean(false);
  }

  void addChild(final Actor child) {
    children.add(child);
  }

  CompletesEventually completesEventually(final ResultCompletes completes) {
    if (completesEventuallyAddress == null) {
      final CompletesEventually completesEventually = stage.world().completesFor(completes.clientCompletes());
      completesEventuallyAddress = completesEventually.address();
      return completesEventually;
    }
    return stage.world().completesFor(completesEventuallyAddress, completes.clientCompletes());
  }

   void cacheProxy(final T proxy) {
    proxyCache.put(proxy.getClass().getName(), proxy);
  }

  @SuppressWarnings("unchecked")
   T lookUpProxy(final Class protocol) {
    return (T) proxyCache.get(protocol.getName());
  }

  boolean isSecured() {
    return secured.get();
  }

  void setSecured() {
    secured.set(true);
  }

  boolean isStopped() {
    return stopped.get();
  }

  void stop() {
    if (stopped.compareAndSet(false, true)) {
      stopChildren();

      suspended.reset();

      stowage.reset();

      mailbox.close();
    }
  }

  boolean isStowageOverride(final Class protocol) {
    if (stowageOverrides != null) {
      for (final Class override : stowageOverrides) {
        if (override == protocol) {
          return true;
        }
      }
    }
    return false;
  }

  void stowageOverrides(final Class... overrides) {
    stowageOverrides = overrides;
  }

  private void stopChildren() {
    // TODO: re-implement as: children.forEach(child -> selfAs(Stoppable.class).stop());
    children.forEach(child -> child.stop());
    children.clear();
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy