@Override
 protected LocalManagementContext createNewManagementContext(
     File mementoDir, HighAvailabilityMode haMode) {
   BrooklynProperties properties = BrooklynProperties.Factory.newDefault();
   properties.put(
       BrooklynServerConfig.BROOKLYN_CATALOG_URL,
       "classpath://brooklyn/entity/rebind/rebind-catalog-item-test-catalog.xml");
   return RebindTestUtils.managementContextBuilder(mementoDir, classLoader)
       .properties(properties)
       .forLive(useLiveManagementContext())
       .haMode(haMode)
       .emptyCatalog(useEmptyCatalog())
       .buildUnstarted();
 }
 @Override
 protected LocalManagementContext createOrigManagementContext() {
   BrooklynProperties properties = BrooklynProperties.Factory.newDefault();
   properties.put(
       BrooklynServerConfig.BROOKLYN_CATALOG_URL,
       "classpath://brooklyn/entity/rebind/rebind-catalog-item-test-catalog.xml");
   properties.put(
       BrooklynServerConfig.CATALOG_LOAD_MODE,
       org.apache.brooklyn.core.catalog.CatalogLoadMode.LOAD_BROOKLYN_CATALOG_URL);
   return RebindTestUtils.managementContextBuilder(mementoDir, classLoader)
       .properties(properties)
       .persistPeriodMillis(getPersistPeriodMillis())
       .forLive(useLiveManagementContext())
       .buildStarted();
 }
  private void doTestAddAndRebindAndDeleteLocation(boolean suppressPeriodicCheckpointing)
      throws Exception {
    String symbolicName = "sample_location";
    String yaml =
        Joiner.on("\n")
            .join(
                "brooklyn.catalog:",
                "  items:",
                "  - id: " + symbolicName,
                "    version: " + TEST_VERSION,
                "    itemType: location",
                "    item:",
                "      type: " + LocalhostMachineProvisioningLocation.class.getName(),
                "      brooklyn.config:",
                "        cfg1: 111",
                "        cfg2: 222");
    CatalogLocationItemDto item =
        CatalogItemBuilder.newLocation(symbolicName, TEST_VERSION)
            .displayName(symbolicName)
            .plan(yaml)
            .build();
    origManagementContext.getCatalog().addItem(item);
    assertEquals(item.getCatalogItemType(), CatalogItemType.LOCATION);
    if (!suppressPeriodicCheckpointing) {
      rebindAndAssertCatalogsAreEqual();
    } else {
      LocalManagementContext nmc =
          RebindTestUtils.managementContextBuilder(mementoDir, classLoader)
              .forLive(useLiveManagementContext())
              .emptyCatalog(useEmptyCatalog())
              .persistPeriod(Duration.PRACTICALLY_FOREVER)
              .buildUnstarted();
      rebind(RebindOptions.create().newManagementContext(nmc));
    }

    deleteItem(newManagementContext, item.getSymbolicName(), item.getVersion());

    switchOriginalToNewManagementContext();
    rebindAndAssertCatalogsAreEqual();
  }
  public static Collection<Application> rebindAll(RebindOptions options) throws Exception {
    File mementoDir = options.mementoDir;
    File mementoDirBackup = options.mementoDirBackup;
    ClassLoader classLoader = checkNotNull(options.classLoader, "classLoader");
    ManagementContextInternal origManagementContext =
        (ManagementContextInternal) options.origManagementContext;
    ManagementContextInternal newManagementContext =
        (ManagementContextInternal) options.newManagementContext;
    PersistenceObjectStore objectStore = options.objectStore;
    HighAvailabilityMode haMode =
        (options.haMode == null ? HighAvailabilityMode.DISABLED : options.haMode);
    RebindExceptionHandler exceptionHandler = options.exceptionHandler;
    boolean hasPersister =
        newManagementContext != null
            && newManagementContext.getRebindManager().getPersister() != null;
    boolean checkSerializable = options.checkSerializable;
    boolean terminateOrigManagementContext = options.terminateOrigManagementContext;
    Function<BrooklynMementoPersister, Void> stateTransformer = options.stateTransformer;

    LOG.info("Rebinding app, using mementoDir " + mementoDir + "; object store " + objectStore);

    if (newManagementContext == null) {
      // TODO Could use empty properties, to save reading brooklyn.properties file.
      // Would that affect any tests?
      newManagementContext =
          new LocalManagementContextForTests(BrooklynProperties.Factory.newDefault());
    }
    if (!hasPersister) {
      if (objectStore == null) {
        objectStore =
            new FileBasedObjectStore(
                checkNotNull(mementoDir, "mementoDir and objectStore must not both be null"));
      }
      objectStore.injectManagementContext(newManagementContext);
      objectStore.prepareForSharedUse(PersistMode.AUTO, haMode);

      BrooklynMementoPersisterToObjectStore newPersister =
          new BrooklynMementoPersisterToObjectStore(
              objectStore, newManagementContext.getBrooklynProperties(), classLoader);
      newManagementContext
          .getRebindManager()
          .setPersister(newPersister, PersistenceExceptionHandlerImpl.builder().build());
    } else {
      if (objectStore != null)
        throw new IllegalStateException(
            "Must not supply ManagementContext with persister and an object store");
    }

    if (checkSerializable) {
      checkNotNull(
          origManagementContext, "must supply origManagementContext with checkSerializable");
      RebindTestUtils.checkCurrentMementoSerializable(origManagementContext);
    }

    if (terminateOrigManagementContext) {
      checkNotNull(
          origManagementContext,
          "must supply origManagementContext with terminateOrigManagementContext");
      origManagementContext.terminate();
    }

    if (mementoDirBackup != null) {
      FileUtil.copyDir(mementoDir, mementoDirBackup);
      FileUtil.setFilePermissionsTo700(mementoDirBackup);
    }

    if (stateTransformer != null) {
      BrooklynMementoPersister persister = newManagementContext.getRebindManager().getPersister();
      stateTransformer.apply(persister);
    }

    List<Application> newApps =
        newManagementContext
            .getRebindManager()
            .rebind(
                classLoader,
                exceptionHandler,
                (haMode == HighAvailabilityMode.DISABLED)
                    ? ManagementNodeState.MASTER
                    : ManagementNodeState.of(haMode).get());
    newManagementContext.getRebindManager().startPersistence();
    return newApps;
  }