public static ExecutionTarget fromString(String name) { Maybe<ExecutionTarget> parsed = tryFromString(name); return parsed.get(); } public static Maybe<ExecutionTarget> tryFromString(String name) {
private synchronized static SystemFramework createFramework() { if (framework == null) { framework = FrameworkLookup.lookup(SystemFramework.class).get(); } return framework; } }
@Override public ZoneFailureDetector apply(final String input) { ClassLoader classLoader = ZoneFailureDetector.class.getClassLoader(); Maybe<ZoneFailureDetector> detector = Reflections.invokeConstructorFromArgs(classLoader, ZoneFailureDetector.class, input); if (detector.isPresent()) { return detector.get(); } else { throw new IllegalStateException("Failed to create ZoneFailureDetector "+input); } } });
@Override public Object call(ConfigBag params) { Entity target = resolveTarget().get(); return invokeEffectorNamed(target, effectorName, params); }
/** * @throws ImmediateSupplier.ImmediateUnsupportedException if cannot evaluate this in a timely manner */ public static Maybe<String> formatStringImmediately(final String spec, final Object ...args) { List<Object> resolvedArgs = Lists.newArrayList(); for (Object arg : args) { Maybe<?> argVal = resolveImmediately(arg); if (argVal.isAbsent()) return Maybe.Absent.castAbsent(argVal); resolvedArgs.add(argVal.get()); } return Maybe.of(String.format(spec, resolvedArgs.toArray())); }
/** As {@link #copyKey(ConfigBag, ConfigKey)} but allowing a different key name to be used when writing here. */ @Beta @SuppressWarnings("unchecked") public <T> ConfigBag copyKeyAs(ConfigBag source, ConfigKey<T> sourceKey, ConfigKey<T> targetKey) { Maybe<Object> sourceValue = source.getRawKeyMaybe(sourceKey, true); if (sourceValue.isPresent()) { put(targetKey, (T) sourceValue.get()); } return this; }
/** Finds a unique {@link MachineLocation} attached to the supplied entity * @throws IllegalStateException if there is not a unique such {@link SshMachineLocation} */ public static <T extends MachineLocation> T getMachine(Entity entity, Class<T> clazz) { try { return Machines.findUniqueMachineLocation(entity.getLocations(), clazz).get(); } catch (Exception e) { throw new IllegalStateException("Entity "+entity+" (in "+Tasks.current()+") requires a single " + clazz.getName() + ", but has "+entity.getLocations(), e); } }
protected MachineLocation getMachine() { Supplier<MachineLocation> supplier = config().get(MACHINE); if (supplier != null) { return supplier.get(); } else { return Locations.findUniqueMachineLocation(entity.getLocations()).get(); } }
private static void setExpectedState(Entity entity, Lifecycle state, boolean waitBrieflyForServiceUpIfRunning) { if (waitBrieflyForServiceUpIfRunning) { waitBrieflyForServiceUpIfStateIsRunning(entity, state); } ((EntityInternal)entity).sensors().set(Attributes.SERVICE_STATE_EXPECTED, new Lifecycle.Transition(state, new Date())); Maybe<Enricher> enricher = EntityAdjuncts.tryFindWithUniqueTag(entity.enrichers(), ComputeServiceState.DEFAULT_ENRICHER_UNIQUE_TAG); if (enricher.isPresent() && enricher.get() instanceof ComputeServiceState) { ((ComputeServiceState)enricher.get()).onEvent(null); } }
@Override @Deprecated public Maybe<Location> resolve(String spec, Boolean manage, Map locationFlags) { if (manage!=null) { locationFlags = MutableMap.copyOf(locationFlags); locationFlags.put(LocalLocationManager.CREATE_UNMANAGED, !manage); } Maybe<LocationSpec<? extends Location>> lSpec = getLocationSpec(spec, locationFlags); if (lSpec.isAbsent()) return (Maybe)lSpec; return Maybe.of((Location)mgmt.getLocationManager().createLocation(lSpec.get())); }
@Override public Iterable<URL> getResources(String name) { if (mgmt != null && isEntitledToSeeCatalogItem()) { Maybe<OsgiManager> osgi = ((ManagementContextInternal) mgmt).getOsgiManager(); if (osgi.isPresent() && hasBundles) { return osgi.get().getResources(name, getBundles()); } } return Collections.emptyList(); }
@Override protected void postLocalEvaluate(ConfigKey<?> key, BrooklynObject bo, Maybe<?> rawValue, Maybe<?> resolvedValue) { // TEMPORARY CODE // We're notifying of config-changed because currently persistence needs to know when the // attributeWhenReady is complete (so it can persist the result). // Long term, we'll just persist tasks properly so the call to onConfigChanged will go! if (rawValue.isPresent() && (rawValue.get() instanceof Task)) { ((EntityInternal)bo).getManagementSupport().getEntityChangeListener().onConfigChanged(key); } }
@Test public void testFindUniqueMachineLocation() throws Exception { SshMachineLocation l1 = create(sshMachineSpec); LocalhostMachineProvisioningLocation l2 = create(otherLocSpec); assertEquals(Machines.findUniqueMachineLocation(ImmutableList.of(l1, l2)).get(), l1); assertFalse(Machines.findUniqueMachineLocation(ImmutableList.of(l2)).isPresent()); }
@Test public void testFindUniqueMachineLocationOfType() throws Exception { SshMachineLocation l1 = create(sshMachineSpec); LocalhostMachineProvisioningLocation l2 = create(otherLocSpec); assertEquals(Machines.findUniqueMachineLocation(ImmutableList.of(l1, l2), SshMachineLocation.class).get(), l1); assertFalse(Machines.findUniqueMachineLocation(ImmutableList.of(l2), LocalhostMachine.class).isPresent()); }
private Bundle getBundle(ManagementContext mgmt, final String symbolicName) throws Exception { OsgiManager osgiManager = ((ManagementContextInternal)mgmt).getOsgiManager().get(); Framework framework = osgiManager.getFramework(); Maybe<Bundle> result = Osgis.bundleFinder(framework) .symbolicName(symbolicName) .find(); return result.get(); }
@Test public void testFindSubnetIpFromLocationWithoutPrivate() throws Exception { TestEntity entity = app.addChild(EntitySpec.create(TestEntity.class) .location(sshMachineWithoutPrivateSpec)); assertEquals(Machines.findSubnetIp(entity).get(), publicAddr); }
@Test public void testGetEffectorDeprecated() throws Exception { TestEntity entity2 = app.createAndManageChild(EntitySpec.create(TestEntity.class)); Effector<?> effector = entity2.getEntityType().getEffectorByName("myEffector").get(); Effector<?> effector2 = entity2.getEntityType().getEffectorByName("identityEffector").get(); assertEquals(effector.getName(), "myEffector"); assertEquals(effector2.getName(), "identityEffector"); }
@Test public void testGetConfigMapWithSubKeys() throws Exception { TestEntity entity = mgmt.getEntityManager().createEntity(EntitySpec.create(TestEntity.class) .configure(TestEntity.CONF_MAP_THING.subKey("mysub"), "myval")); assertEquals(entity.config().get(TestEntity.CONF_MAP_THING), ImmutableMap.of("mysub", "myval")); assertEquals(entity.config().getNonBlocking(TestEntity.CONF_MAP_THING).get(), ImmutableMap.of("mysub", "myval")); assertEquals(entity.config().get(TestEntity.CONF_MAP_THING.subKey("mysub")), "myval"); assertEquals(entity.config().getNonBlocking(TestEntity.CONF_MAP_THING.subKey("mysub")).get(), "myval"); }
@Override public void onEvent(SensorEvent<Object> event) { final Effector<?> eff = getEffectorNamed(getConfig(EFFECTOR)).get(); if (isBusySensorEnabled()) { final Object currentSensorValue = entity.sensors().get(sensor); setMoreUpdatesComing(event.getTimestamp(), event.getValue(), currentSensorValue); } invoke(eff, MutableMap.<String, Object>of()); }