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

co.easimart.EasimartCorePlugins Maven / Gradle / Ivy

package co.easimart;

import java.io.File;
import java.util.concurrent.atomic.AtomicReference;

/** package */ class EasimartCorePlugins {

  private static final EasimartCorePlugins INSTANCE = new EasimartCorePlugins();
  public static EasimartCorePlugins getInstance() {
    return INSTANCE;
  }

  /* package */ static final String FILENAME_CURRENT_USER = "currentUser";
  /* package */ static final String PIN_CURRENT_USER = "_currentUser";
  /* package */ static final String FILENAME_CURRENT_INSTALLATION = "currentInstallation";
  /* package */ static final String PIN_CURRENT_INSTALLATION = "_currentInstallation";
  /* package */ static final String FILENAME_CURRENT_CONFIG = "currentConfig";

  private AtomicReference objectController = new AtomicReference<>();
  private AtomicReference userController = new AtomicReference<>();
  private AtomicReference sessionController = new AtomicReference<>();

  // TODO(mengyan): Inject into EasimartUserInstanceController
  private AtomicReference currentUserController =
    new AtomicReference<>();
  // TODO(mengyan): Inject into EasimartInstallationInstanceController
  private AtomicReference currentInstallationController =
      new AtomicReference<>();

  private AtomicReference authenticationController =
      new AtomicReference<>();

  private AtomicReference queryController = new AtomicReference<>();
  private AtomicReference fileController = new AtomicReference<>();
  private AtomicReference analyticsController = new AtomicReference<>();
  private AtomicReference cloudCodeController = new AtomicReference<>();
  private AtomicReference configController = new AtomicReference<>();
  private AtomicReference pushController = new AtomicReference<>();
  private AtomicReference pushChannelsController =
      new AtomicReference<>();
  private AtomicReference defaultACLController = new AtomicReference<>();

  private AtomicReference localIdManager = new AtomicReference<>();

  private EasimartCorePlugins() {
    // do nothing
  }

  /* package for tests */ void reset() {
    objectController.set(null);
    userController.set(null);
    sessionController.set(null);

    currentUserController.set(null);
    currentInstallationController.set(null);

    authenticationController.set(null);

    queryController.set(null);
    fileController.set(null);
    analyticsController.set(null);
    cloudCodeController.set(null);
    configController.set(null);
    pushController.set(null);
    pushChannelsController.set(null);
    defaultACLController.set(null);

    localIdManager.set(null);
  }

  public EasimartObjectController getObjectController() {
    if (objectController.get() == null) {
      // TODO(grantland): Do not rely on Easimart global
      objectController.compareAndSet(
          null, new NetworkObjectController(EasimartPlugins.get().restClient()));
    }
    return objectController.get();
  }

  public void registerObjectController(EasimartObjectController controller) {
    if (!objectController.compareAndSet(null, controller)) {
      throw new IllegalStateException(
          "Another object controller was already registered: " + objectController.get());
    }
  }

  public EasimartUserController getUserController() {
    if (userController.get() == null) {
      // TODO(grantland): Do not rely on Easimart global
      userController.compareAndSet(
          null, new NetworkUserController(EasimartPlugins.get().restClient()));
    }
    return userController.get();
  }

  public void registerUserController(EasimartUserController controller) {
    if (!userController.compareAndSet(null, controller)) {
      throw new IllegalStateException(
          "Another user controller was already registered: " + userController.get());
    }
  }

  public EasimartSessionController getSessionController() {
    if (sessionController.get() == null) {
      // TODO(grantland): Do not rely on Easimart global
      sessionController.compareAndSet(
          null, new NetworkSessionController(EasimartPlugins.get().restClient()));
    }
    return sessionController.get();
  }

  public void registerSessionController(EasimartSessionController controller) {
    if (!sessionController.compareAndSet(null, controller)) {
      throw new IllegalStateException(
          "Another session controller was already registered: " + sessionController.get());
    }
  }

  public EasimartCurrentUserController getCurrentUserController() {
    if (currentUserController.get() == null) {
      File file = new File(Easimart.getEasimartDir(), FILENAME_CURRENT_USER);
      FileObjectStore fileStore =
          new FileObjectStore<>(EasimartUser.class, file, EasimartUserCurrentCoder.get());
      EasimartObjectStore store = Easimart.isLocalDatastoreEnabled()
          ? new OfflineObjectStore<>(EasimartUser.class, PIN_CURRENT_USER, fileStore)
          : fileStore;
      EasimartCurrentUserController controller = new CachedCurrentUserController(store);
      currentUserController.compareAndSet(null, controller);
    }
    return currentUserController.get();
  }

  public void registerCurrentUserController(EasimartCurrentUserController controller) {
    if (!currentUserController.compareAndSet(null, controller)) {
      throw new IllegalStateException(
          "Another currentUser controller was already registered: " +
              currentUserController.get());
    }
  }

  public EasimartQueryController getQueryController() {
    if (queryController.get() == null) {
      NetworkQueryController networkController = new NetworkQueryController(
          EasimartPlugins.get().restClient());
      EasimartQueryController controller;
      // TODO(grantland): Do not rely on Easimart global
      if (Easimart.isLocalDatastoreEnabled()) {
        controller = new OfflineQueryController(
            Easimart.getLocalDatastore(),
            networkController);
      } else {
        controller = new CacheQueryController(networkController);
      }
      queryController.compareAndSet(null, controller);
    }
    return queryController.get();
  }

  public void registerQueryController(EasimartQueryController controller) {
    if (!queryController.compareAndSet(null, controller)) {
      throw new IllegalStateException(
          "Another query controller was already registered: " + queryController.get());
    }
  }

  public EasimartFileController getFileController() {
    if (fileController.get() == null) {
      // TODO(grantland): Do not rely on Easimart global
      fileController.compareAndSet(null, new EasimartFileController(
          EasimartPlugins.get().restClient(),
          Easimart.getEasimartCacheDir("files")));
    }
    return fileController.get();
  }

  public void registerFileController(EasimartFileController controller) {
    if (!fileController.compareAndSet(null, controller)) {
      throw new IllegalStateException(
          "Another file controller was already registered: " + fileController.get());
    }
  }

  public EasimartAnalyticsController getAnalyticsController() {
    if (analyticsController.get() == null) {
      // TODO(mengyan): Do not rely on Easimart global
      analyticsController.compareAndSet(null,
          new EasimartAnalyticsController(Easimart.getEventuallyQueue()));
    }
    return analyticsController.get();
  }

  public void registerAnalyticsController(EasimartAnalyticsController controller) {
    if (!analyticsController.compareAndSet(null, controller)) {
      throw new IllegalStateException(
          "Another analytics controller was already registered: " + analyticsController.get());
    }
  }

  public EasimartCloudCodeController getCloudCodeController() {
    if (cloudCodeController.get() == null) {
      cloudCodeController.compareAndSet(null, new EasimartCloudCodeController(
          EasimartPlugins.get().restClient()));
    }
    return cloudCodeController.get();
  }

  public void registerCloudCodeController(EasimartCloudCodeController controller) {
    if (!cloudCodeController.compareAndSet(null, controller)) {
      throw new IllegalStateException(
          "Another cloud code controller was already registered: " + cloudCodeController.get());
    }
  }

  public EasimartConfigController getConfigController() {
    if (configController.get() == null) {
      // TODO(mengyan): Do not rely on Easimart global
      File file = new File(EasimartPlugins.get().getEasimartDir(), FILENAME_CURRENT_CONFIG);
      EasimartCurrentConfigController currentConfigController =
          new EasimartCurrentConfigController(file);
      configController.compareAndSet(null, new EasimartConfigController(
          EasimartPlugins.get().restClient(), currentConfigController));
    }
    return configController.get();
  }

  public void registerConfigController(EasimartConfigController controller) {
    if (!configController.compareAndSet(null, controller)) {
      throw new IllegalStateException(
          "Another config controller was already registered: " + configController.get());
    }
  }

  public EasimartPushController getPushController() {
    if (pushController.get() == null) {
      pushController.compareAndSet(null, new EasimartPushController(EasimartPlugins.get().restClient()));
    }
    return pushController.get();
  }

  public void registerPushController(EasimartPushController controller) {
    if (!pushController.compareAndSet(null, controller)) {
      throw new IllegalStateException(
          "Another push controller was already registered: " + pushController.get());
    }
  }

  public EasimartPushChannelsController getPushChannelsController() {
    if (pushChannelsController.get() == null) {
      pushChannelsController.compareAndSet(null, new EasimartPushChannelsController());
    }
    return pushChannelsController.get();
  }

  public void registerPushChannelsController(EasimartPushChannelsController controller) {
    if (!pushChannelsController.compareAndSet(null, controller)) {
      throw new IllegalStateException(
          "Another pushChannels controller was already registered: " +
              pushChannelsController.get());
    }
  }

  public EasimartCurrentInstallationController getCurrentInstallationController() {
    if (currentInstallationController.get() == null) {
      File file = new File(EasimartPlugins.get().getEasimartDir(), FILENAME_CURRENT_INSTALLATION);
      FileObjectStore fileStore =
          new FileObjectStore<>(EasimartInstallation.class, file, EasimartObjectCurrentCoder.get());
      EasimartObjectStore store = Easimart.isLocalDatastoreEnabled()
          ? new OfflineObjectStore<>(EasimartInstallation.class, PIN_CURRENT_INSTALLATION, fileStore)
          : fileStore;
      CachedCurrentInstallationController controller =
          new CachedCurrentInstallationController(store, EasimartPlugins.get().installationId());
      currentInstallationController.compareAndSet(null, controller);
    }
    return currentInstallationController.get();
  }

  public void registerCurrentInstallationController(EasimartCurrentInstallationController controller) {
    if (!currentInstallationController.compareAndSet(null, controller)) {
      throw new IllegalStateException(
          "Another currentInstallation controller was already registered: " +
              currentInstallationController.get());
    }
  }

  public EasimartAuthenticationManager getAuthenticationManager() {
    if (authenticationController.get() == null) {
      EasimartAuthenticationManager controller =
          new EasimartAuthenticationManager(getCurrentUserController());
      authenticationController.compareAndSet(null, controller);
    }
    return authenticationController.get();
  }

  public void registerAuthenticationManager(EasimartAuthenticationManager manager) {
    if (!authenticationController.compareAndSet(null, manager)) {
      throw new IllegalStateException(
          "Another authentication manager was already registered: " +
              authenticationController.get());
    }
  }

  public EasimartDefaultACLController getDefaultACLController() {
    if (defaultACLController.get() == null) {
      EasimartDefaultACLController controller = new EasimartDefaultACLController();
      defaultACLController.compareAndSet(null, controller);
    }
    return defaultACLController.get();
  }

  public void registerDefaultACLController(EasimartDefaultACLController controller) {
    if (!defaultACLController.compareAndSet(null, controller)) {
      throw new IllegalStateException(
          "Another defaultACL controller was already registered: " + defaultACLController.get());
    }
  }

  public LocalIdManager getLocalIdManager() {
    if (localIdManager.get() == null) {
      LocalIdManager manager = new LocalIdManager(Easimart.getEasimartDir());
      localIdManager.compareAndSet(null, manager);
    }
    return localIdManager.get();
  }

  public void registerLocalIdManager(LocalIdManager manager) {
    if (!localIdManager.compareAndSet(null, manager)) {
      throw new IllegalStateException(
          "Another localId manager was already registered: " + localIdManager.get());
    }
  }
}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy