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

io.vlingo.actors.DirectoryScannerActor 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.Optional;

import io.vlingo.common.Completes;

public class DirectoryScannerActor extends Actor implements DirectoryScanner {
  private final Directory directory;

  public DirectoryScannerActor(final Directory directory) {
    this.directory = directory;
  }

  /*
   * @see io.vlingo.actors.DirectoryScanner#actorOf(java.lang.Class, io.vlingo.actors.Address)
   */
  @Override
  public  Completes actorOf(final Class protocol, final Address address) {
    return completes().with(internalActorOf(protocol, address));
  }

  /*
   * @see io.vlingo.actors.DirectoryScanner#maybeActorOf(java.lang.Class, io.vlingo.actors.Address)
   */
  @Override
  public  Completes> maybeActorOf(Class protocol, Address address) {
    final T typed = internalActorOf(protocol, address);
    final Optional maybe = typed == null ? Optional.empty() : Optional.of(typed);
    return completes().with(maybe);
  }

  /**
   * Answer the actor as the {@code protocol} or {@code null}.
   * @param protocol the {@code Class} of the protocol that the actor must support
   * @param address the {@code Address} of the actor to find
   * @param T the protocol type
   * @return T
   */
  private  T internalActorOf(final Class protocol, final Address address) {
    final Actor actor = directory.actorOf(address);

    try {
      if (actor != null) {
        return stage().actorAs(actor, protocol);
      } else {
        logger().log("Actor with address: " + address + " not found; protocol is: " + protocol.getName());
      }
    } catch (Exception e) {
      logger().log("Error providing protocol: " + protocol.getName() + " for actor with address: " + address, e);
    }
    return null;
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy