/** 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 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((EntityLocal)e, e, Startable.START, MutableMap.of("locations", locations)).getUnchecked(); }
public static DownloadResolver newDownloader(EntityDriver driver) { return newDownloader(driver, ImmutableMap.<String,Object>of()); }
@Override public void init() { super.init(); Entities.getRequiredUrlConfig(this, TEMPLATE_CONFIGURATION_URL); }
/** convenience - invokes in parallel if multiple, but otherwise invokes the item directly */ public static Task<?> invokeEffector(EntityLocal callingEntity, Iterable<? extends Entity> entitiesToCall, final Effector<?> effector, final Map<String,?> parameters) { if (Iterables.size(entitiesToCall)==1) return invokeEffector(callingEntity, entitiesToCall.iterator().next(), effector, parameters); else return invokeEffectorList(callingEntity, entitiesToCall, effector, parameters); } /** convenience - invokes in parallel if multiple, but otherwise invokes the item directly */
/** stops, destroys, and unmanages the given entity -- does as many as are valid given the type and state */ public static void destroy(Entity e) { if (isManaged(e)) { if (e instanceof Startable) Entities.invokeEffector((EntityLocal)e, e, Startable.STOP).getUnchecked(); if (e instanceof EntityInternal) ((EntityInternal)e).destroy(); unmanage(e); log.debug("destroyed and unmanaged "+e+"; mgmt now "+ (e.getApplicationId()==null ? "(no app)" : e.getApplication().getManagementContext())+" - managed? "+isManaged(e)); } else { log.debug("skipping destroy of "+e+": not managed"); } }
@Override public boolean apply(@Nullable Entity input) { return input != null && Entities.isManaged(input); } };
/** convenience - invokes in parallel if multiple, but otherwise invokes the item directly */ public static Task<?> invokeEffector(EntityLocal callingEntity, Iterable<? extends Entity> entitiesToCall, final Effector<?> effector) { return invokeEffector(callingEntity, entitiesToCall, effector, Collections.<String,Object>emptyMap()); }
/** * @deprecated since 0.6; use {@link #addNode(Location)}, so can take that location into account when configuring node */ protected Entity addNode() { Map<?,?> creation = Maps.newLinkedHashMap(getCustomChildFlags()); if (LOG.isDebugEnabled()) LOG.debug("Creating and adding a node to cluster {}({}) with properties {}", new Object[] {this, getId(), creation}); Entity entity = createNode(null, creation); Entities.manage(entity); addMember(entity); return entity; }
/** * Builds and manages the application, calling the user's {@link #doBuild()} method. * * @throws IllegalStateException If already managed, or if called during {@link #doBuild()}, or if * multiple concurrent calls */ public final StartableApplication manage(ManagementContext managementContext) { if (!inManage.compareAndSet(false, true)) { throw new IllegalStateException("Concurrent and re-entrant calls to manage() forbidden on "+this); } try { checkNotManaged(); this.app = managementContext.getEntityManager().createEntity(appSpec); this.managementContext = managementContext; doBuild(); Entities.startManagement(app, managementContext); managed = true; return app; } finally { inManage.set(false); } }
for (ConfigKey<?> it : sortConfigKeys(e.getEntityType().getConfigKeys())) { Object v = ((EntityInternal)e).getConfigMap().getRawConfig(it); if (!isTrivial(v)) { out.append(currentIndentation+tab+tab+it.getName()); out.append(" = "); if (isSecret(it.getName())) out.append("xxxxxxxx"); else if ((v instanceof Task) && ((Task<?>)v).isDone()) { if (((Task<?>)v).isError()) { for (Sensor<?> it : sortSensors(e.getEntityType().getSensors())) { if (it instanceof AttributeSensor) { Object v = e.getAttribute((AttributeSensor<?>)it); if (!isTrivial(v)) { out.append(currentIndentation+tab+tab+it.getName()); out.append(": "); if (isSecret(it.getName())) out.append("xxxxxxxx"); else out.append(""+v); out.append("\n"); dumpInfo(policy, out, currentIndentation+tab+tab+tab, tab); dumpInfo(it, out, currentIndentation+tab, tab);
keyO.toString(); Object val = entry.getValue(); if (!isTrivial(val)) { out.append(currentIndentation+tab+tab+key); out.append(" = "); if (isSecret(key)) out.append("xxxxxxxx"); else out.append(""+val); out.append("\n"); for (Map.Entry<String,?> entry : sortMap(FlagUtils.getFieldsWithFlags(loc)).entrySet()) { String key = entry.getKey(); Object val = entry.getValue(); if (!isTrivial(val)) { out.append(currentIndentation+tab+tab+key); out.append(" = "); if (isSecret(key)) out.append("xxxxxxxx"); else out.append(""+val); out.append("\n"); dumpInfo(it, out, currentIndentation+tab, tab);
/** stops, destroys, and unmanages all apps in the given context, * and then terminates the management context */ public static void destroyAll(ManagementContext mgmt) { Exception error = null; if (mgmt instanceof NonDeploymentManagementContext) { // log here because it is easy for tests to destroyAll(app.getMgmtContext()) // which will *not* destroy the mgmt context if the app has been stopped! log.warn("Entities.destroyAll invoked on non-deployment "+mgmt+" - not likely to have much effect! " + "(This usually means the mgmt context has been taken from entity has been destroyed. " + "To destroy other things on the management context ensure you keep a handle to the context " + "before the entity is destroyed, such as by creating the management context first.)"); } if (!mgmt.isRunning()) return; log.debug("destroying all apps in "+mgmt+": "+mgmt.getApplications()); for (Application app: mgmt.getApplications()) { log.debug("destroying app "+app+" (managed? "+isManaged(app)+"; mgmt is "+mgmt+")"); try { destroy(app); log.debug("destroyed app "+app+"; mgmt now "+mgmt); } catch (Exception e) { log.warn("problems destroying app "+app+" (mgmt now "+mgmt+", will rethrow at least one exception): "+e); if (error==null) error = e; } } if (mgmt instanceof ManagementContextInternal) ((ManagementContextInternal)mgmt).terminate(); if (error!=null) throw Exceptions.propagate(error); }
public synchronized void updateNeeded() { if (updateNeeded) return; updateNeeded = true; LOG.debug("queueing an update-needed task for "+this+"; update will occur shortly"); Entities.submit(this, Tasks.builder().name("update-needed").body(new Runnable() { @Override public void run() { if (updateNeeded) AbstractControllerImpl.this.update(); } }).build()); }
public static void dumpInfo(Iterable<? extends Entity> entities) { for (Entity e : entities) { dumpInfo(e); } }
public static void dumpInfo(Policy pol, Writer out, String currentIndentation, String tab) throws IOException { out.append(currentIndentation+pol.toString()+"\n"); for (ConfigKey<?> key : sortConfigKeys(pol.getPolicyType().getConfigKeys())) { Object val = ((AbstractPolicy)pol).getConfigMap().getRawConfig(key); if (!isTrivial(val)) { out.append(currentIndentation+tab+tab+key); out.append(" = "); if (isSecret(key.getName())) out.append("xxxxxxxx"); else out.append(""+val); out.append("\n"); } } out.flush(); }
/** Launches the web console on the given port, and a Brooklyn application, with a single command. * For readability and flexibility, clients may prefer the {@link #newLauncher()} builder-style syntax. * * @deprecated in 0.5; use newInstance().webconsolePort(port).shutdownOnExit(shutdownApp).webconsole(startWebConsole).application(app).start() */ @Deprecated public static ManagementContext manage(final AbstractApplication app, int port, boolean shutdownApp, boolean startWebConsole) { // Locate the management context Entities.startManagement(app); LocalManagementContext context = (LocalManagementContext) app.getManagementContext(); if (startWebConsole) { try { new BrooklynWebServer(context, port).start(); } catch (Exception e) { LOG.warn("Failed to start Brooklyn web-console", e); } } if (shutdownApp) Entities.invokeStopOnShutdown(app); return context; }
public static <K> Map<K,Object> sanitize(Map<K,?> input) { Map<K,Object> result = Maps.newLinkedHashMap(); for (Map.Entry<K,?> e: input.entrySet()) { if (isSecret(""+e.getKey())) result.put(e.getKey(), "xxxxxxxx"); else result.put(e.getKey(), e.getValue()); } return result; }
/** * stops, destroys, and unmanages the given application -- and terminates the mangaement context; * does as many as are valid given the type and state * @deprecated since 0.6.0 use destroy(Application) if you DONT want to destroy the mgmt context, * or destroy(app.getManagementContext()) if you want to destory everything in the app's mgmt context */ @Deprecated public static void destroyAll(Application app) { if (isManaged(app)) { ManagementContext managementContext = app.getManagementContext(); if (app instanceof Startable) Entities.invokeEffector((EntityLocal)app, app, Startable.STOP).getUnchecked(); if (app instanceof AbstractEntity) ((AbstractEntity)app).destroy(); unmanage(app); if (managementContext instanceof ManagementContextInternal) ((ManagementContextInternal)managementContext).terminate(); } }