@Override protected CatalogItemDtoAbstract<T, SpecT> configure(Map<?, ?> flags) { FlagUtils.setFieldsFromFlags(flags, this); return this; }
private <T extends Entity> T constructEntityImpl(Class<? extends T> clazz, EntitySpec<?> optionalSpec, Map<String, ?> optionalConstructorFlags, Optional<String> entityId) { T entity = construct(clazz, optionalSpec, optionalConstructorFlags); if (entityId.isPresent()) { FlagUtils.setFieldsFromFlags(ImmutableMap.of("id", entityId.get()), entity); } if (entity instanceof AbstractApplication) { FlagUtils.setFieldsFromFlags(ImmutableMap.of("mgmt", managementContext), entity); } managementContext.prePreManage(entity); ((AbstractEntity)entity).setManagementContext(managementContext); return entity; }
public static ProxySslConfig fromMap(Map<?,?> map) { Builder b = new Builder(); Map<?, ?> unused = FlagUtils.setFieldsFromFlags(map, b); if (!unused.isEmpty()) log.warn("Unused flags when populating "+b+" (ignoring): "+unused); return b.build(); }
@Test public void testNonImmutableField() { Foo f = new Foo(); FlagUtils.setFieldsFromFlags(ImmutableMap.of("w", 8), f); assertEquals(f.w, 8); FlagUtils.setFieldsFromFlags(ImmutableMap.of("w", 9), f); assertEquals(f.w, 9); }
@Test public void testCollectionCoercionOnSetFromFlags() { WithSpecialFieldTypes s = new WithSpecialFieldTypes(); Map<?,?> m = ImmutableMap.of("set", ImmutableSet.of(1)); FlagUtils.setFieldsFromFlags(m, s); assertEquals(s.set, ImmutableSet.of(1)); }
@Test public void testImmutableIntField() { Foo f = new Foo(); FlagUtils.setFieldsFromFlags(ImmutableMap.of("x", 8), f); assertEquals(f.x, 8); boolean succeededWhenShouldntHave = false; try { FlagUtils.setFieldsFromFlags(ImmutableMap.of("x", 9), f); succeededWhenShouldntHave = true; } catch (IllegalStateException e) { //expected } assertFalse(succeededWhenShouldntHave); assertEquals(f.x, 8); }
@Override protected void addConfig(RebindContext rebindContext, FeedMemento memento) { // TODO entity does config-lookup differently; the memento contains the config keys. // BasicEntityMemento.postDeserialize uses the injectTypeClass to call EntityTypes.getDefinedConfigKeys(clazz) ConfigBag configBag = ConfigBag.newInstance(memento.getConfig()); FlagUtils.setFieldsFromFlags(feed, configBag); FlagUtils.setAllConfigKeys(feed, configBag, false); }
@Override protected void addConfig(RebindContext rebindContext, EnricherMemento memento) { // TODO entity does config-lookup differently; the memento contains the config keys. // BasicEntityMemento.postDeserialize uses the injectTypeClass to call EntityTypes.getDefinedConfigKeys(clazz) // Note that the flags may have been set in the constructor; but some enrichers have no-arg constructors ConfigBag configBag = ConfigBag.newInstance(memento.getConfig()); FlagUtils.setFieldsFromFlags(enricher, configBag); FlagUtils.setAllConfigKeys(enricher, configBag, false); }
@Test public void testInetAddressCoercionOnSetFromFlags() { WithSpecialFieldTypes s = new WithSpecialFieldTypes(); Map<?,?> m = ImmutableMap.of("inet", "127.0.0.1"); FlagUtils.setFieldsFromFlags(m, s); assertEquals(s.inet.getHostAddress(), "127.0.0.1"); }
@Test public void testImmutableObjectField() { WithImmutableNonNullableObject o = new WithImmutableNonNullableObject(); FlagUtils.setFieldsFromFlags(ImmutableMap.of("a", "a", "b", "b"), o); assertEquals(o.a, "a"); assertEquals(o.b, "b"); FlagUtils.setFieldsFromFlags(ImmutableMap.of("a", "a2"), o); assertEquals(o.a, "a2"); boolean succeededWhenShouldntHave = false; try { FlagUtils.setFieldsFromFlags(ImmutableMap.of("b", "b2"), o); succeededWhenShouldntHave = true; } catch (IllegalStateException e) { //expected } assertFalse(succeededWhenShouldntHave); assertEquals(o.b, "b"); }
protected ManagedBundle newManagedBundle(ManagedBundleMemento memento) { ManagedBundle result = new BasicManagedBundle(memento.getSymbolicName(), memento.getVersion(), memento.getUrl()); FlagUtils.setFieldsFromFlags(ImmutableMap.of("id", memento.getId()), result); return result; }
@Test public void testCheckRequired() { WithImmutableNonNullableObject f = new WithImmutableNonNullableObject(); FlagUtils.setFieldsFromFlags(ImmutableMap.of("a", "a is a"), f); assertEquals(f.a, "a is a"); assertEquals(f.b, null); int exceptions = 0; try { FlagUtils.checkRequiredFields(f); } catch (IllegalStateException e) { exceptions++; } assertEquals(exceptions, 1); }
@Test public void testSetFieldsFromFlags() { Foo f = new Foo(); Map<?,?> m = MutableMap.of("w", 3, "x", 1, "y", 7, "z", 9); Map<?, ?> unused = FlagUtils.setFieldsFromFlags(m, f); assertEquals(f.w, 3); assertEquals(f.x, 1); assertEquals(f.yNotY, 7); assertEquals(unused, ImmutableMap.of("z", 9)); Map<?,?> m2 = FlagUtils.getFieldsWithValues(f); m.remove("z"); assertEquals(m2, m); }
@Test public void testSetConfigKeys() { FooCK f = new FooCK(); Map<?,?> unused = FlagUtils.setFieldsFromFlags(ImmutableMap.of("f1", 9, "ck1", "do-set", "ck2", "dont-set", "c3", "do-set"), f); assertEquals(f.bag.get(FooCK.CK1), "do-set"); assertEquals(f.bag.get(FooCK.CK3), "do-set"); assertEquals(f.f1, 9); assertEquals(f.bag.containsKey(FooCK.CK2), false); assertEquals(unused, ImmutableMap.of("ck2", "dont-set")); }
@Test public void testSetFromConfigKeys() { FooCK f = new FooCK(); Map<?, ?> unused = FlagUtils.setFieldsFromFlags(ImmutableMap.of(new BasicConfigKey<Integer>(Integer.class, "f1"), 9, "ck1", "do-set", "ck2", "dont-set"), f); assertEquals(f.bag.get(FooCK.CK1), "do-set"); assertEquals(f.f1, 9); assertEquals(f.bag.containsKey(FooCK.CK2), false); assertEquals(unused, ImmutableMap.of("ck2", "dont-set")); }
@Test public void testNonNullable() { WithImmutableNonNullableObject o = new WithImmutableNonNullableObject(); //allowed FlagUtils.setFieldsFromFlags(MutableMap.of("a", null), o); assertEquals(o.a, null); assertEquals(o.b, null); //not allowed boolean succeededWhenShouldntHave = false; try { FlagUtils.setFieldsFromFlags(MutableMap.of("b", null), o); succeededWhenShouldntHave = true; } catch (IllegalArgumentException e) { //expected } assertFalse(succeededWhenShouldntHave); assertEquals(o.b, null); }
@Override protected void addConfig(RebindContext rebindContext, PolicyMemento memento) { // TODO entity does config-lookup differently; the memento contains the config keys. // BasicEntityMemento.postDeserialize uses the injectTypeClass to call EntityTypes.getDefinedConfigKeys(clazz) Collection<ConfigKey<?>> configKeys = policy.getAdjunctType().getConfigKeys(); Map<?, ?> flags = memento.getConfig(); // First set the config keys that are known explicitly (including with deprecated names). flags = ConfigUtilsInternal.setAllConfigKeys(flags, configKeys, policy); // Note that the flags may have been set in the constructor; but non-legacy policies should have no-arg constructors if (!flags.isEmpty()) { ConfigBag configBag = ConfigBag.newInstance(flags); FlagUtils.setFieldsFromFlags(policy, configBag); FlagUtils.setAllConfigKeys(policy, configBag, false); } }
/** * Constructs a new enricher, passing to its constructor the enricher id and all of memento.getConfig(). */ protected Feed newFeed(FeedMemento memento) { String id = memento.getId(); LoadedClass<? extends Feed> loaded = load(Feed.class, memento); Class<? extends Feed> feedClazz = loaded.clazz; Feed feed; if (InternalFactory.isNewStyle(feedClazz)) { InternalPolicyFactory policyFactory = managementContext.getPolicyFactory(); feed = policyFactory.constructFeed(feedClazz); FlagUtils.setFieldsFromFlags(ImmutableMap.of("id", id), feed); ((AbstractFeed)feed).setManagementContext(managementContext); } else { throw new IllegalStateException("rebind of feed without no-arg constructor unsupported: id=" + id + "; type="+feedClazz); } setCatalogItemIds(feed, loaded.catalogItemId, loaded.searchPath); return feed; }
/** * Constructs a new location, passing to its constructor the location id and all of memento.getFlags(). */ protected Location newLocation(String locationId, String locationType) { Class<? extends Location> locationClazz = loadClass(locationType, Location.class); if (InternalFactory.isNewStyle(locationClazz)) { // Not using loationManager.createLocation(LocationSpec) because don't want init() to be called // TODO Need to rationalise this to move code into methods of InternalLocationFactory. // But note that we'll change all locations to be entities at some point! // See same code approach used in #newEntity(EntityMemento, Reflections) InternalLocationFactory locationFactory = managementContext.getLocationFactory(); Location location = locationFactory.constructLocation(locationClazz); FlagUtils.setFieldsFromFlags(ImmutableMap.of("id", locationId), location); managementContext.prePreManage(location); ((AbstractLocation)location).setManagementContext(managementContext); return location; } else { LOG.warn("Deprecated rebind of location without no-arg constructor; " + "this may not be supported in future versions: id=" + locationId + "; type="+locationType); // There are several possibilities for the constructor; find one that works. // Prefer passing in the flags because required for Application to set the management context // TODO Feels very hacky! Map<String,?> flags = MutableMap.of("id", locationId, "deferConstructionChecks", true); return invokeConstructor(reflections, locationClazz, new Object[] {flags}); } // note 'used' config keys get marked in BasicLocationRebindSupport }
@Override public void reconstruct(RebindContext rebindContext, CatalogItemMemento memento) { super.reconstruct(rebindContext, memento); FlagUtils.setFieldsFromFlags(MutableMap.builder() .put("symbolicName", memento.getSymbolicName()) .put("containingBundle", memento.getContainingBundle()) .put("javaType", memento.getJavaType()) .put("displayName", memento.getDisplayName()) .put("description", memento.getDescription()) .put("iconUrl", memento.getIconUrl()) .put("version", memento.getVersion()) .put("parameters", memento.getParameters()) .put("libraries", memento.getLibraries()) .put("planYaml", memento.getPlanYaml()) .put("deprecated", memento.isDeprecated()) .put("disabled", memento.isDisabled()) .build(), instance); }