public void addChild(Location child) {
    // Previously, setParent delegated to addChildLocation and we sometimes ended up with
    // duplicate entries here. Instead this now uses a similar scheme to
    // AbstractLocation.setParent/addChild (with any weaknesses for distribution that such a
    // scheme might have...).
    //
    // We continue to use a list to allow identical-looking locations, but they must be different
    // instances.

    synchronized (children) {
      for (Location contender : children) {
        if (contender == child) {
          // don't re-add; no-op
          return;
        }
      }

      children.add(child);
    }

    if (isManaged()) {
      Locations.manage(child, managementContext);
    } else if (managementContext != null) {
      if (((LocalLocationManager) managementContext.getLocationManager())
              .getLocationEvenIfPreManaged(child.getId())
          == null) {
        ((ManagementContextInternal) managementContext).prePreManage(child);
      }
    }

    children.add(child);
    child.setParent(this);
  }
 protected void assertNotYetManaged() {
   if (!inConstruction
       && (managementContext != null && managementContext.getLocationManager().isManaged(this))) {
     LOG.warn(
         "Configuration being made to {} after deployment; may not be supported in future versions",
         this);
   }
   // throw new IllegalStateException("Cannot set configuration "+key+" on active location "+this)
 }
  @BeforeMethod(alwaysRun = true)
  public void setup() throws Exception {
    mgmt = new LocalManagementContext();

    LocalhostMachineProvisioningLocation lhc =
        mgmt.getLocationManager()
            .createLocation(LocationSpec.create(LocalhostMachineProvisioningLocation.class));
    host = lhc.obtain();
    clearExpectedFailure();
    tempDir = Os.newTempDir(getClass());
  }
  protected boolean removeChild(Location child) {
    boolean removed;
    synchronized (children) {
      removed = children.remove(child);
    }
    if (removed) {
      if (child instanceof Closeable) {
        Streams.closeQuietly((Closeable) child);
      }
      child.setParent(null);

      if (isManaged()) {
        managementContext.getLocationManager().unmanage(child);
      }
    }
    return removed;
  }
 @BeforeMethod(alwaysRun = true)
 public void setUp() throws Exception {
   localManagementContext = newPersistingManagementContext();
   if (persister == null) {
     persister = localManagementContext.getRebindManager().getPersister();
   }
   if (objectStore == null && persister instanceof BrooklynMementoPersisterToObjectStore) {
     objectStore = ((BrooklynMementoPersisterToObjectStore) persister).getObjectStore();
   }
   app =
       ApplicationBuilder.newManagedApp(
           EntitySpec.create(TestApplication.class), localManagementContext);
   location =
       localManagementContext
           .getLocationManager()
           .createLocation(
               LocationSpec.create(SshMachineLocation.class).configure("address", "localhost"));
   entity = app.createAndManageChild(EntitySpec.create(TestEntity.class).location(location));
   enricher = app.addEnricher(Enrichers.builder().propagatingAll().from(entity).build());
   app.addPolicy(policy = new TestPolicy());
 }
 protected <T extends Location> T addChild(LocationSpec<T> spec) {
   T child = managementContext.getLocationManager().createLocation(spec);
   addChild(child);
   return child;
 }