public static boolean isTrivial(Object v) { if (v instanceof Maybe) { if (!((Maybe<?>)v).isPresent()) return true; v = ((Maybe<?>) v).get(); } return v==null || (v instanceof Map && ((Map<?,?>)v).isEmpty()) || (v instanceof Collection && ((Collection<?>)v).isEmpty()) || (v instanceof CharSequence&& ((CharSequence)v).length() == 0); }
@Override public boolean apply(@Nullable RegisteredType item) { return RegisteredTypes.tryValidate(item, context).isPresent(); } }
@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); } } });
/** 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; }
@Override public T get() { Maybe<T> m = getMaybe(); if (m.isPresent()) return m.get(); if (returnDefaultOnGet) return defaultValue; return m.get(); }
public HashGenerator add(Entity entity, ConfigKey<?> key) { Maybe<?> value = ((ConfigurationSupportInternal)entity.config()).getNonBlocking(key); if (value.isPresent()) hash = hash*31 + (value.get()==null ? 0 : value.get().hashCode()); return this; }
private Maybe<T> coerceValue(Maybe<Object> valueMaybe, Class<T> type) { if (valueMaybe.isPresent()) { T coercedValue = TypeCoercions.coerce(valueMaybe.get(), type); return Maybe.of(coercedValue); } else { @SuppressWarnings("unchecked") Maybe<T> uncheckedValue = (Maybe<T>) valueMaybe; return uncheckedValue; } }
public String getUser() { if (!groovyTruth(user)) { if (config().getLocalRaw(SshTool.PROP_USER).isPresent()) { LOG.warn("User configuration for "+this+" set after deployment; deprecated behaviour may not be supported in future versions"); } return getConfig(SshTool.PROP_USER); } return user; }
@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(); }
@Test public void testMaybeGetNull() { Maybe<String> m = Maybe.ofAllowingNull(null); Assert.assertTrue(m.isPresent()); Assert.assertEquals(m.get(), null); }
@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); } }
public static Maybe<String> findSubnetIp(Iterable<? extends Location> ll) { // TODO Or if can't find MachineLocation, should we throw new IllegalStateException("Cannot find hostname for among "+ll); Maybe<MachineLocation> l = findUniqueMachineLocation(ll); return (l.isPresent()) ? Machines.getSubnetIp(l.get()) : Maybe.<String>absent(); }
@Test public void testMaybeAbsentMessageNoTraceDoesNotIncludeSource() { Maybe<Object> m = Maybe.absentNoTrace("nope"); Assert.assertFalse(m.isPresent()); Exception e = assertGetFailsContaining(m, "nope"); Assert.assertTrue(containsGetExplicitMethod(e), "Outer trace should be in explicit method"); Assert.assertFalse(containsMaybeMethod(e, "absentNoTrace"), "Outer trace should not be from 'absentNoTrace'"); Assert.assertNull(e.getCause()); }
@Test public void testFindEffector() { Maybe<Effector<?>> found = EffectorUtils.findEffector(ImmutableList.of(effector), "myEffector"); assertEquals(found.get(), effector); Maybe<Effector<?>> notFound = EffectorUtils.findEffector(ImmutableList.of(effector), "wrongEffectorName"); assertFalse(notFound.isPresent(), notFound.toString()); } }
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 public URL getResource(String name) { if (mgmt != null && isEntitledToSeeCatalogItem()) { Maybe<OsgiManager> osgi = ((ManagementContextInternal) mgmt).getOsgiManager(); if (osgi.isPresent() && hasBundles) { return osgi.get().getResource(name, getBundles()); } } return null; }
@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()); }
protected void failOnStep(ConfigKey<Boolean> latch) { if (((EntityInternal)entity).config().getRaw(latch).isPresent()) { DynamicTasks.queue("Failing task", new Runnable() { @Override public void run() { throw new IllegalStateException("forced fail"); } }); } }
@Test public void testLocalhostAllowedByDefault() { BrooklynProperties properties = BrooklynProperties.Factory.newEmpty(); properties.put("brooklyn.location.named.localhost_allowed", "localhost"); mgmt = LocalManagementContextForTests.newInstance(properties); Assert.assertTrue( findLocationMatching("localhost_allowed") ); Maybe<LocationSpec<?>> l = mgmt.getLocationRegistry().getLocationSpec("localhost_allowed"); Assert.assertTrue( l.isPresent(), "Should have resolved: "+l ); l.get(); }