/** convenience for starting an entity, esp a new Startable instance which has been created dynamically * (after the application is started) */ public static void start(Entity e, Collection<? extends Location> locations) { if (!isManaged(e) && !manage(e)) { log.warn("Using deprecated discouraged mechanism to start management -- Entities.start(Application, Locations) -- caller should create and use the preferred management context"); startManagement(e); } if (e instanceof Startable) Entities.invokeEffector(e, e, Startable.START, MutableMap.of("locations", locations)).getUnchecked(); }
@Test public void testManageFailsIfEntityDeleted() throws Exception { TestEntity child = mgmt.getEntityManager().createEntity(EntitySpec.create(TestEntity.class) .parent(app)); Entities.unmanage(child); try { Entities.manage(child); fail("Managed deleted entity "+child+" in "+mgmt); } catch (IllegalArgumentException e) { if (!(e.toString().contains("Can't manage"))) throw e; } }
/** * @deprecated since 0.7; support for rebinding old-style entities is deprecated */ @Deprecated @Test public void testHandlesOldStyleEntity() throws Exception { MyOldStyleEntity origE = new MyOldStyleEntity(MutableMap.of("confName", "myval"), origApp); Entities.manage(origE); newApp = rebind(); MyOldStyleEntity newE = (MyOldStyleEntity) Iterables.find(newApp.getChildren(), EntityPredicates.idEqualTo(origE.getId())); assertEquals(newE.getConfig(MyOldStyleEntity.CONF_NAME), "myval"); }
@Test public void testManageIsNoop() throws Exception { TestEntity child = mgmt.getEntityManager().createEntity(EntitySpec.create(TestEntity.class) .parent(app)); Entities.manage(child); assertTrue(Entities.isManaged(child)); listener.assertEventsEqualsEventually(ImmutableList.of(new ChangeEvent(ChangeType.ADDED, child))); }
@Override public Entity addNode(@Nullable Location loc, Map<?, ?> extraFlags) { // In case subclasses are foolish and do not call super.init() when overriding. initialiseMemberId(); Map<?, ?> createFlags = MutableMap.builder() .putAll(getCustomChildFlags()) .putAll(extraFlags) .put(CLUSTER_MEMBER_ID, sensors().get(NEXT_CLUSTER_MEMBER_ID).get()) .build(); if (LOG.isTraceEnabled()) { LOG.trace("Creating and adding a node to cluster {}({}) with properties {}", new Object[] { this, getId(), Sanitizer.sanitize(createFlags) }); } // TODO should refactor to have a createNodeSpec; and spec should support initial sensor values Entity entity = createNode(loc, createFlags); entity.sensors().set(CLUSTER_MEMBER, true); entity.sensors().set(CLUSTER, this); // Continue to call manage(), because some uses of NodeFactory (in tests) still instantiate the // entity via its constructor Entities.manage(entity); addMember(entity); return entity; }
protected Entity addCluster(Location location) { String locationName = elvis(location.getDisplayName(), location.getDisplayName(), null); Map creation = Maps.newLinkedHashMap(); creation.putAll(getCustomChildFlags()); if (groovyTruth(getDisplayNamePrefix()) || groovyTruth(getDisplayNameSuffix())) { String displayName = "" + elvis(getDisplayNamePrefix(), "") + elvis(locationName, "unnamed") + elvis(getDisplayNameSuffix(),""); creation.put("displayName", displayName); } logger.info("Creating entity in fabric {} at {}{}", new Object[] {this, location, (creation!=null && !creation.isEmpty() ? ", properties "+creation : "") }); Entity entity = createCluster(location, creation); if (locationName != null) { if (entity.getDisplayName()==null) entity.setDisplayName(entity.getEntityType().getSimpleName() +" ("+locationName+")"); else if (!entity.getDisplayName().contains(locationName)) entity.setDisplayName(entity.getDisplayName() +" ("+locationName+")"); } if (entity.getParent()==null) entity.setParent(this); // Continue to call manage(), because some uses of NodeFactory (in tests) still instantiate the // entity via its constructor Entities.manage(entity); addMember(entity); return entity; }