@Override public T apply(Maybe<T> input) { return input.or(value); } };
public Object getValueOrNullPreferringFlag() { return getFlagMaybeValue().or(getConfigKeyMaybeValue()).orNull(); } /** true if value is present for either flag or config key */
@Override public T apply(Maybe<T> input) { return input.or(value); } }
public Object getValueOrNullPreferringConfigKey() { return getConfigKeyMaybeValue().or(getFlagMaybeValue()).orNull(); } public Object getValueOrNullPreferringFlag() {
@Override public E apply(String input) { return tryCoerce(input, type).or(defaultValue); } }
protected static Collection<CatalogBundle> resolveWherePossible(ManagementContext mgmt, Collection<CatalogBundle> libraryBundles) { Collection<CatalogBundle> libraryBundlesResolved = MutableSet.of(); for (CatalogBundle b: libraryBundles) { libraryBundlesResolved.add(CatalogBundleDto.resolve(mgmt, b).or(b)); } return libraryBundlesResolved; }
@SuppressWarnings({ "unchecked" }) protected void setEntityLoadingConfig() { this.producer = getConfig(PRODUCER); this.fromHardcodedProducers= getConfig(FROM_HARDCODED_PRODUCERS); this.defaultMemberValue = (T) getConfig(DEFAULT_MEMBER_VALUE); this.fromMembers = Maybe.fromNullable(getConfig(FROM_MEMBERS)).or(fromMembers); this.fromChildren = Maybe.fromNullable(getConfig(FROM_CHILDREN)).or(fromChildren); this.entityFilter = (Predicate<? super Entity>) (getConfig(ENTITY_FILTER) == null ? Predicates.alwaysTrue() : getConfig(ENTITY_FILTER)); this.valueFilter = (Predicate<? super T>) (getConfig(VALUE_FILTER) == null ? getDefaultValueFilter() : getConfig(VALUE_FILTER)); setEntityLoadingTargetConfig(); }
public static WeightedParameter getFieldConfig(ConfigKey<?> config, Field configKeyField) { if (configKeyField == null) return null; CatalogConfig catalogConfig = configKeyField.getAnnotation(CatalogConfig.class); String label = config.getName(); Double priority = null; Boolean pinned = Boolean.FALSE; if (catalogConfig != null) { label = Maybe.fromNullable(catalogConfig.label()).or(config.getName()); priority = catalogConfig.priority(); pinned = catalogConfig.pinned(); } @SuppressWarnings({ "unchecked", "rawtypes" }) SpecParameter<?> param = new BasicSpecParameter(label, pinned, config); return new WeightedParameter(priority, param); }
vn!=null && vn.getVersionString()!=null ? vn.getVersionString() : getFirstAs(cm, String.class, "version").or(NO_VERSION));
@Test public void testRebindPreservesGetConfigWithDefault() throws Exception { MyEntity origE = origApp.createAndManageChild(EntitySpec.create(MyEntity.class)); assertNull(origE.getConfig(MyEntity.MY_CONFIG)); assertEquals(((EntityInternal)origE).config().getRaw(MyEntity.MY_CONFIG).or("mydefault"), "mydefault"); newApp = rebind(); MyEntity newE = (MyEntity) Iterables.find(newApp.getChildren(), Predicates.instanceOf(MyEntity.class)); assertNull(newE.getConfig(MyEntity.MY_CONFIG)); assertEquals(((EntityInternal)newE).config().getRaw(MyEntity.MY_CONFIG).or("mydefault"), "mydefault"); }
protected void registerPortMappings(KubernetesSshMachineLocation machine, Entity entity, Service service) { PortForwardManager portForwardManager = (PortForwardManager) getManagementContext().getLocationRegistry() .getLocationManaged(PortForwardManagerLocationResolver.PFM_GLOBAL_SPEC); List<ServicePort> ports = service.getSpec().getPorts(); String publicHostText = ((SshMachineLocation) machine).getSshHostAndPort().getHostText(); LOG.debug("Recording port-mappings for container {} of {}: {}", new Object[]{machine, this, ports}); for (ServicePort port : ports) { String protocol = port.getProtocol(); Integer targetPort = port.getTargetPort().getIntVal(); if (!"TCP".equalsIgnoreCase(protocol)) { LOG.debug("Ignoring port mapping {} for {} because only TCP is currently supported", port, machine); } else if (targetPort == null) { LOG.debug("Ignoring port mapping {} for {} because targetPort.intValue is null", port, machine); } else if (port.getNodePort() == null) { LOG.debug("Ignoring port mapping {} to {} because port.getNodePort() is null", targetPort, machine); } else { portForwardManager.associate(publicHostText, HostAndPort.fromParts(publicHostText, port.getNodePort()), machine, targetPort); AttributeSensor<Integer> sensor = Sensors.newIntegerSensor("kubernetes." + Strings.maybeNonBlank(port.getName()).or(targetPort.toString()) + ".port"); entity.sensors().set(sensor, targetPort); } } entity.enrichers().add(EnricherSpec.create(OnPublicNetworkEnricher.class).configure(OnPublicNetworkEnricher.MAP_MATCHING, "kubernetes.[a-zA-Z0-9][a-zA-Z0-9-_]*.port")); }
@Test public void testJsonyaMaybe() { Navigator<MutableMap<Object, Object>> m = europeMap(); Assert.assertEquals( m.at("europe", "spain", "barcelona").getMaybe().or("norealabc"), "norealabc" ); Assert.assertEquals(m.root().at("europe").getFocusMap().keySet(), MutableSet.of("uk", "france")); }
@Override protected String getInstallLabelExtraSalt() { return (String)((EntityInternal)getEntity()).config().getRaw(ConfigKeys.newStringConfigKey("salt")).or((String)null); } }
String masterNodeId = newState.getMasterNodeId(); ManagementNodeSyncRecord masterNodeDetails = newState.getManagementNodes().get(masterNodeId); LOG.info("Management node "+ownNodeId+" in "+managementContext.getManagementPlaneIdMaybe().or("<new-plane>")+" running as HA " + getInternalNodeState() + " autodetected" + (startMode == HighAvailabilityMode.HOT_STANDBY || startMode == HighAvailabilityMode.HOT_BACKUP ? " (will change to "+startMode+")" : "") break; case MASTER: LOG.info("Management node "+ownNodeId+" in "+managementContext.getManagementPlaneIdMaybe().or("<new-plane>")+" running as HA MASTER autodetected"); break; default: throw new IllegalStateException("Management node "+ownNodeId+" in "+managementContext.getManagementPlaneIdMaybe().or("<new-plane>")+" set to HA AUTO, encountered unexpected mode "+getInternalNodeState()); promoteToMaster(); if (existingMaster!=null) { LOG.info("Management node "+ownNodeId+" in "+managementContext.getManagementPlaneIdMaybe().or("<new-plane>")+" running as HA MASTER explicitly"); } else { LOG.info("Management node "+ownNodeId+" in "+managementContext.getManagementPlaneIdMaybe().or("<new-plane>")+" running as HA MASTER explicitly, stealing from "+existingMaster); + "to trigger a promotion, set a priority and demote the current master"); } else { LOG.info("Management node "+ownNodeId+" in "+managementContext.getManagementPlaneIdMaybe().or("<new-plane>")+" already running as HA MASTER, when set explicitly"); LOG.error("Management node "+ownNodeId+" in "+managementContext.getManagementPlaneIdMaybe().or("<new-plane>")+" detected no master when "+startMode+" requested and existing master required; failing."); throw new IllegalStateException("No existing master; cannot start as "+startMode); String message = "Management node "+ownNodeId+" in "+managementContext.getManagementPlaneIdMaybe().or("<new-plane>")+" running as HA "+getNodeState()+" ("; if (getNodeState().toString().equals(startMode.toString())) message += "explicitly requested";
if (immediate) { c = entity.config().getNonBlocking(ConfigKeys.newStringConfigKey("infinite_loop")).or("FAILED"); } else { c = entity.config().get(ConfigKeys.newStringConfigKey("infinite_loop"));
/** * For resolving a "simple" config key - i.e. where there's not custom logic inside a * {@link StructuredConfigKey} such as a {@link MapConfigKey}. For those, we'd need to do the * same as is in {@link #get(ConfigKey)}, but non-blocking! * See {@link #getNonBlockingResolvingStructuredKey(ConfigKey)}. */ protected <T> Maybe<T> getNonBlockingResolvingSimple(ConfigKey<T> key) { // TODO See AbstractConfigMapImpl.getConfigImpl, for how it looks up the "container" of the // key, so that it gets the right context entity etc. // getRaw returns Maybe(val) if the key was explicitly set (where val can be null) // or Absent if the config key was unset. Object unresolved = getRaw(key).or(key.getDefaultValue()); Maybe<Object> resolved = Tasks.resolving(unresolved) .as(Object.class) .immediately(true) .deep(true) .context(getContext()) .getMaybe(); if (resolved.isAbsent()) return Maybe.Absent.<T>castAbsent(resolved); // likely we don't need this coercion if we set as(key.getType()) above, // but that needs confirmation and quite a lot of testing return TypeCoercions.tryCoerce(resolved.get(), key.getTypeToken()); }
@Override public boolean apply(Object item, AssemblyTemplateConstructor atc) { if (!(item instanceof Service)) return false; Service svc = (Service)item; String type = svc.getServiceType(); for (ResolvableLink<PlatformComponentTemplate> t: platform.platformComponentTemplates().links()) { if (type.equals(t.getName())) { PlatformComponentTemplate pct = PlatformComponentTemplate.builder() .name(svc.getName()) .customAttribute("serviceType", type) .description(Maybe.fromNullable(svc.getDescription()).or(t.resolve().getDescription())) .build(); if (atc!=null) { atc.add(pct); if (instantiator!=null) atc.instantiator(instantiator); } return true; } } return false; }