public ManagementContextBuilder persistPeriodMillis(long persistPeriodMillis) {
   checkArgument(
       persistPeriodMillis > 0,
       "persistPeriodMillis must be greater than 0; was " + persistPeriodMillis);
   return persistPeriod(Duration.millis(persistPeriodMillis));
 }
  public static class ManagementContextBuilder {
    final ClassLoader classLoader;
    BrooklynProperties properties;
    PersistenceObjectStore objectStore;
    Duration persistPeriod = Duration.millis(100);
    HighAvailabilityMode haMode;
    boolean forLive;
    boolean enableOsgi = false;
    boolean emptyCatalog;
    private boolean enablePersistenceBackups = true;

    ManagementContextBuilder(File mementoDir, ClassLoader classLoader) {
      this(classLoader, new FileBasedObjectStore(mementoDir));
    }

    ManagementContextBuilder(ClassLoader classLoader, File mementoDir) {
      this(classLoader, new FileBasedObjectStore(mementoDir));
    }

    ManagementContextBuilder(ClassLoader classLoader, PersistenceObjectStore objStore) {
      this.classLoader = checkNotNull(classLoader, "classLoader");
      this.objectStore = checkNotNull(objStore, "objStore");
    }

    public ManagementContextBuilder persistPeriodMillis(long persistPeriodMillis) {
      checkArgument(
          persistPeriodMillis > 0,
          "persistPeriodMillis must be greater than 0; was " + persistPeriodMillis);
      return persistPeriod(Duration.millis(persistPeriodMillis));
    }

    public ManagementContextBuilder persistPeriod(Duration persistPeriod) {
      checkNotNull(persistPeriod);
      this.persistPeriod = persistPeriod;
      return this;
    }

    public ManagementContextBuilder properties(BrooklynProperties properties) {
      this.properties = checkNotNull(properties, "properties");
      return this;
    }

    public ManagementContextBuilder forLive(boolean val) {
      this.forLive = val;
      return this;
    }

    public ManagementContextBuilder enablePersistenceBackups(boolean val) {
      this.enablePersistenceBackups = val;
      return this;
    }

    public ManagementContextBuilder enableOsgi(boolean val) {
      this.enableOsgi = val;
      return this;
    }

    public ManagementContextBuilder emptyCatalog() {
      this.emptyCatalog = true;
      return this;
    }

    public ManagementContextBuilder emptyCatalog(boolean val) {
      this.emptyCatalog = val;
      return this;
    }

    public ManagementContextBuilder haMode(HighAvailabilityMode val) {
      this.haMode = val;
      return this;
    }

    public LocalManagementContext buildUnstarted() {
      LocalManagementContext unstarted;
      BrooklynProperties properties =
          this.properties != null ? this.properties : BrooklynProperties.Factory.newDefault();
      if (this.emptyCatalog) {
        properties.putIfAbsent(
            BrooklynServerConfig.BROOKLYN_CATALOG_URL, ManagementContextInternal.EMPTY_CATALOG_URL);
      }
      if (!enablePersistenceBackups) {
        properties.putIfAbsent(
            BrooklynServerConfig.PERSISTENCE_BACKUPS_REQUIRED_ON_DEMOTION, false);
        properties.putIfAbsent(
            BrooklynServerConfig.PERSISTENCE_BACKUPS_REQUIRED_ON_PROMOTION, false);
        properties.putIfAbsent(BrooklynServerConfig.PERSISTENCE_BACKUPS_REQUIRED, false);
      }
      if (forLive) {
        unstarted = new LocalManagementContext(properties);
      } else {
        unstarted =
            LocalManagementContextForTests.builder(true)
                .useProperties(properties)
                .disableOsgi(!enableOsgi)
                .build();
      }

      objectStore.injectManagementContext(unstarted);
      objectStore.prepareForSharedUse(
          PersistMode.AUTO, (haMode == null ? HighAvailabilityMode.DISABLED : haMode));
      BrooklynMementoPersisterToObjectStore newPersister =
          new BrooklynMementoPersisterToObjectStore(
              objectStore, unstarted.getBrooklynProperties(), classLoader);
      ((RebindManagerImpl) unstarted.getRebindManager()).setPeriodicPersistPeriod(persistPeriod);
      unstarted
          .getRebindManager()
          .setPersister(newPersister, PersistenceExceptionHandlerImpl.builder().build());
      // set the HA persister, in case any children want to use HA
      unstarted
          .getHighAvailabilityManager()
          .setPersister(
              new ManagementPlaneSyncRecordPersisterToObjectStore(
                  unstarted, objectStore, classLoader));
      return unstarted;
    }

    public LocalManagementContext buildStarted() {
      LocalManagementContext unstarted = buildUnstarted();
      unstarted.getHighAvailabilityManager().disabled();
      unstarted.getRebindManager().startPersistence();
      return unstarted;
    }
  }