private Object readResolve() { if (type != null && configKey == null) { return new BasicSpecParameter<T>(label, pinned, type, sensor); } else { return this; } }
/** * Adds the given list of {@link SpecParameter parameters} to the provided * {@link AbstractBrooklynObjectSpec spec}, and if spec has no parameters it * also generates a list from the spec * * @see EntitySpec#parameters(List) */ @Beta public static void initializeSpecWithExplicitParameters(AbstractBrooklynObjectSpec<?, ?> spec, List<? extends SpecParameter<?>> explicitParams, BrooklynClassLoadingContext loader) { // spec params list is empty if created from java (or there are none); non-empty if created from a parent entity spec // so if empty, we may need to populate (or it is no op), and if non-empty, we can skip if (spec.getParameters().isEmpty()) { spec.parametersAdd(BasicSpecParameter.fromSpec(loader.getManagementContext(), spec)); } // but now we need to filter non-reinheritable, and merge where params are extended/overridden spec.parametersReplace(resolveParameters(explicitParams, spec)); }
public static List<SpecParameter<?>> fromClass(ManagementContext mgmt, Class<?> type) { return ParseClassParameters.collectParameters(getImplementedBy(mgmt, type)); }
@Test public void testDebug() throws ClassNotFoundException { System.out.println(BasicSpecParameter.fromClass(mgmt, new ClassLoaderUtils(this.getClass()).loadClass("org.apache.brooklyn.entity.stock.BasicApplication"))); }
private SpecParameter<?> parseSpecParameterDefinition(Object def) { BrooklynClassLoadingContext loader = JavaBrooklynClassLoadingContext.create(mgmt); List<SpecParameter<?>> inputs = BasicSpecParameter.parseParameterDefinitionList(ImmutableList.of(def), null, loader); return Iterables.getOnlyElement(inputs); }
@Test public void testFullDefinition() { List<SpecParameter<?>> inputs = BasicSpecParameter.fromClass(mgmt, SpecParameterTestEntity.class); assertEquals(inputs.size(), 7); assertInput(inputs.get(0), "String Key", false, SpecParameterTestEntity.STRING_KEY); assertInput(inputs.get(1), "Integer Key", true, SpecParameterTestEntity.INTEGER_PINNED_KEY); assertInput(inputs.get(2), "Predicate Key", true, SpecParameterTestEntity.PREDICATE_PINNED_KEY); assertInput(inputs.get(3), "Hidden 1 Key", false, SpecParameterTestEntity.HIDDEN1_KEY); assertInput(inputs.get(4), "Hidden 2 Key", true, SpecParameterTestEntity.HIDDEN2_PINNED_KEY); assertInput(inputs.get(5), "unpinned1_key", false, SpecParameterTestEntity.UNPINNNED1_KEY); assertInput(inputs.get(6), "unpinned2_key", false, SpecParameterTestEntity.UNPINNNED2_KEY); }
@Test(dataProvider = "brooklynTypes") public void testParameters(Class<? extends BrooklynObject> testClass) { addCatalogItems( "brooklyn.catalog:", " id: " + SYMBOLIC_NAME, " version: " + TEST_VERSION, " itemType: " + inferItemType(testClass), " item:", " type: " + testClass.getName(), " brooklyn.parameters:", " - simple"); ConfigKey<String> SIMPLE_CONFIG = ConfigKeys.newStringConfigKey("simple"); SpecParameter<String> SIMPLE_PARAM = new BasicSpecParameter<>("simple", true, SIMPLE_CONFIG); AbstractBrooklynObjectSpec<?,?> spec = peekSpec(); assertTrue(Iterables.tryFind(spec.getParameters(), Predicates.<SpecParameter<?>>equalTo(SIMPLE_PARAM)).isPresent()); }
@Test(dataProvider = "brooklynTypes") public void testDefaultParameters(Class<? extends BrooklynObject> testClass) { addCatalogItems( "brooklyn.catalog:", " id: " + SYMBOLIC_NAME, " version: " + TEST_VERSION, " itemType: " + inferItemType(testClass), " item:", " type: "+ testClass.getName()); AbstractBrooklynObjectSpec<?, ?> spec = peekSpec(); assertEquals(ImmutableSet.copyOf(spec.getParameters()), ImmutableSet.copyOf(BasicSpecParameter.fromClass(mgmt(),testClass))); }
public static List<SpecParameter<?>> fromSpec(ManagementContext mgmt, AbstractBrooklynObjectSpec<?, ?> spec) { if (!spec.getParameters().isEmpty()) { return spec.getParameters(); } Class<?> type = null; if (spec instanceof EntitySpec) { EntitySpec<?> entitySpec = (EntitySpec<?>)spec; if (entitySpec.getImplementation() != null) { type = entitySpec.getImplementation(); } } if (type == null) { type = getImplementedBy(mgmt, spec.getType()); } return ParseClassParameters.collectParameters(getImplementedBy(mgmt, type)); }
/** used if yaml specifies a parameter, but possibly incomplete, and a spec supertype has a parameter */ @SuppressWarnings("unchecked") SpecParameter<?> resolveWithAncestor(SpecParameter<?> ancestor) { if (ancestor==null) return new BasicSpecParameter<>(getLabel(), isPinned(), getConfigKey(), getSensor()); return new BasicSpecParameter( hasLabelSet ? getLabel() : ancestor.getLabel(), hasPinnedSet ? isPinned() : ancestor.isPinned(), resolveWithAncestorConfigKey(ancestor.getConfigKey()), hasType ? getSensor() : ancestor.getSensor()); }
@Test public void testConfigInImplVisible() { Map<String, ConfigKey<?>> expectedKeys = ImmutableMap.<String, ConfigKey<?>>of( ConfigInImplParameterTestEntityImpl.SUGGESTED_VERSION.getName(), ConfigInImplParameterTestEntityImpl.SUGGESTED_VERSION, AbstractEntity.DEFAULT_DISPLAY_NAME.getName(), AbstractEntity.DEFAULT_DISPLAY_NAME); List<SpecParameter<?>> inputs = BasicSpecParameter.fromClass(mgmt, ConfigInImplParameterTestEntity.class); assertEquals(inputs.size(), expectedKeys.size()); for (SpecParameter<?> in : inputs) { ConfigKey<?> key = expectedKeys.get(in.getConfigKey().getName()); assertNotNull(key); assertInput(in, key.getName(), false, key); } }
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); }
@Test public void testUnresolvedCatalogItemParameters() { // Insert template which is not instantiatable during catalog addition due to // missing dependencies, but the spec can be created (the // dependencies are already parsed). addCatalogItems( "brooklyn.catalog:", " version: " + TEST_VERSION, " items:", " - id: " + SYMBOLIC_NAME, " itemType: template", " item:", " services:", " - type: basic-app", " - id: basic-app", " itemType: entity", " item:", " type: " + ConfigAppForTest.class.getName()); EntitySpec<? extends Application> spec = createAppSpec( "services:", "- type: " + ver(SYMBOLIC_NAME)); List<SpecParameter<?>> params = spec.getParameters(); assertEquals(params.size(), NUM_APP_DEFAULT_CONFIG_KEYS + ConfigAppForTest.NUM_CONFIG_KEYS_DEFINED_HERE, "params="+params); assertEquals(ImmutableSet.copyOf(params), ImmutableSet.copyOf(BasicSpecParameter.fromClass(mgmt(), ConfigAppForTest.class))); }
@Test public void testOsgiClassScanned() { TestResourceUnavailableException.throwIfResourceUnavailable(getClass(), OsgiTestResources.BROOKLYN_TEST_OSGI_ENTITIES_PATH); TestResourceUnavailableException.throwIfResourceUnavailable(getClass(), OsgiTestResources.BROOKLYN_TEST_MORE_ENTITIES_V2_PATH); addCatalogItems(CatalogScanOsgiTest.bomForLegacySiblingLibraries()); RegisteredType item = mgmt().getTypeRegistry().get(OsgiTestResources.BROOKLYN_TEST_MORE_ENTITIES_MORE_ENTITY); AbstractBrooklynObjectSpec<?,?> spec = createSpec(item); List<SpecParameter<?>> inputs = spec.getParameters(); if (inputs.isEmpty()) Assert.fail("no inputs (if you're in the IDE, mvn clean install may need to be run to rebuild osgi test JARs)"); Set<SpecParameter<?>> actual = ImmutableSet.copyOf(inputs); Set<SpecParameter<?>> expected = ImmutableSet.<SpecParameter<?>>of( new BasicSpecParameter<>("more_config", false, ConfigKeys.newStringConfigKey("more_config")), new BasicSpecParameter<>(AbstractEntity.DEFAULT_DISPLAY_NAME.getName(), false, AbstractEntity.DEFAULT_DISPLAY_NAME)); assertEquals(actual, expected); }
Class<Entity> entityType = (Class<Entity>)type; spec = EntitySpec.create(entityType) .parameters(BasicSpecParameter.fromClass(mgmt, entityType)); } else if (Policy.class.isAssignableFrom(type)) { @SuppressWarnings("unchecked")
@BeforeMethod(alwaysRun=true) @Override public void setUp() throws Exception{ super.setUp(); entity = (EntityInternal) app.createAndManageChild(EntitySpec.create(ConfigEntityForTesting.class) .parameters(ImmutableList.of(new BasicSpecParameter<>("spec config", true, SPEC_CONFIG)))); listener = new RecordingSensorEventListener<>(); app.subscriptions().subscribe(entity, AbstractEntity.CONFIG_KEY_ADDED, listener); app.subscriptions().subscribe(entity, AbstractEntity.CONFIG_KEY_REMOVED, listener); }
@Test public void testConfigUsesParameterDefaultValue() throws Exception { final ConfigKey<String> configKey = ConfigKeys.newStringConfigKey("testConfig"); ConfigKey<String> configParam = ConfigKeys.newStringConfigKey("testParam", "myDescription", "myDefaultConfigValue"); BrooklynDslDeferredSupplier<?> dsl = BrooklynDslCommon.config(configKey.getName()); Supplier<ConfigValuePair> valueSupplier = new Supplier<ConfigValuePair>() { @Override public ConfigValuePair get() { return new ConfigValuePair(BrooklynDslCommon.config("testParam"), "myDefaultConfigValue"); } }; new ConfigTestWorker(app, configKey, valueSupplier, dsl) .childSpec(EntitySpec.create(TestEntity.class).parameters(ImmutableList.of(new BasicSpecParameter<String>("myLabel", true, configParam)))) .run(); }
for (int i = 0; i < 100; i++) { ConfigKey<String> key = ConfigKeys.newStringConfigKey("myparam"+i); params.add(new BasicSpecParameter<String>("mylabel"+i, false, key)); config.put(key, "val"+i);
for (int i = 0; i < 100; i++) { ConfigKey<String> key = ConfigKeys.newStringConfigKey("myparam"+i); params.add(new BasicSpecParameter<String>("mylabel"+i, false, key)); config.put(key, "val"+i);