NamedStringTag ownerEntityTag = BrooklynTags.findFirst(BrooklynTags.OWNER_ENTITY_ID, loc.tags().getTags()); if (ownerEntityTag != null) { if (!getId().equals(ownerEntityTag.getContents())) {
tags().addTag(BrooklynTags.newIconUrlTag((String) flags.remove(BrooklynConfigKeys.ICON_URL.getName())));
loc.tags().addTags(spec.getTags());
pol.tags().addTags(spec.getTags());
enricher.tags().addTags(spec.getTags());
assertNotNull(origItem, "Item not in catalog (might be using new type registry where adding tags to items isn't supported as items come from BOM)"); final String tag = "tag1"; origItem.tags().addTag(tag); assertTrue(origItem.tags().containsTag(tag)); origManagementContext.getCatalog().persist(origItem); assertTrue(newItem.tags().containsTag(tag)); newItem.tags().removeTag(tag);
private static void populateBrooklynObjectMementoBuilder(BrooklynObject instance, AbstractMemento.Builder<?> builder) { if (Proxy.isProxyClass(instance.getClass())) { throw new IllegalStateException("Attempt to create memento from proxy "+instance+" (would fail with wrong type)"); } OsgiClassPrefixer prefixer = new OsgiClassPrefixer(); Optional<String> typePrefix = prefixer.getPrefix(instance.getClass()); builder.id = instance.getId(); builder.displayName = instance.getDisplayName(); builder.catalogItemId = instance.getCatalogItemId(); builder.searchPath = instance.getCatalogItemIdSearchPath(); builder.type = (typePrefix.isPresent() ? typePrefix.get() : "") + instance.getClass().getName(); builder.typeClass = instance.getClass(); if (instance instanceof EntityAdjunct) { builder.uniqueTag = ((EntityAdjunct)instance).getUniqueTag(); } for (Object tag : instance.tags().getTags()) { builder.tags.add(tag); } // CatalogItems return empty support, so this is safe even through they don't support relations for (RelationshipType<?,? extends BrooklynObject> relationship: instance.relations().getRelationshipTypes()) { @SuppressWarnings({ "unchecked", "rawtypes" }) Set relations = instance.relations().getRelations((RelationshipType)relationship); Set<String> relationIds = Sets.newLinkedHashSet(); for (Object r: relations) relationIds.add( ((BrooklynObject)r).getId() ); builder.relations.put(relationship.getRelationshipTypeName(), relationIds); } }
@Test public void testRebindTags() throws Exception { final Entity entity = createAndStartApplication("services:", "- type: " + BasicApplication.class.getName(), " brooklyn.tags:", " - 1", " - hi world", " - $brooklyn:object:", " type: " + TagObject.class.getName()); Assert.assertTrue(entity.tags().getTags().contains(1)); Assert.assertTrue(entity.tags().getTags().contains("hi world")); Assert.assertTrue(Iterables.any(entity.tags().getTags(), new Predicate<Object>() { @Override public boolean apply(Object input) { return input instanceof TagObject; } })); rebind(); final Entity newEntity = mgmt().getEntityManager().getEntity(entity.getId()); Assert.assertTrue(newEntity.tags().getTags().contains(1)); Assert.assertTrue(newEntity.tags().getTags().contains("hi world")); Assert.assertTrue(Iterables.any(newEntity.tags().getTags(), new Predicate<Object>() { @Override public boolean apply(Object input) { return input instanceof TagObject; } })); }
/** @deprecated since it was introduced in 0.9.0; for backwards compatibility only, may be removed at any point */ @Deprecated public static RegisteredType of(CatalogItem<?, ?> item) { if (item==null) return null; TypeImplementationPlan impl = null; if (item.getPlanYaml()!=null) { impl = new BasicTypeImplementationPlan(null, item.getPlanYaml()); } else if (item.getJavaType()!=null) { impl = new JavaClassNameTypeImplementationPlan(item.getJavaType()); } else { throw new IllegalStateException("Unsupported catalog item "+item+" when trying to create RegisteredType"); } BasicRegisteredType type = (BasicRegisteredType) spec(item.getSymbolicName(), item.getVersion(), impl, item.getCatalogItemJavaType()); type.containingBundle = item.getContainingBundle(); type.displayName = item.getDisplayName(); type.description = item.getDescription(); type.iconUrl = item.getIconUrl(); type.disabled = item.isDisabled(); type.deprecated = item.isDeprecated(); if (item.getLibraries()!=null) type.bundles.addAll(item.getLibraries()); // aliases aren't on item if (item.tags()!=null) type.tags.addAll(item.tags().getTags()); if (item.getCatalogItemType()==CatalogItemType.TEMPLATE) { type.tags.add(BrooklynTags.CATALOG_TEMPLATE); } // these things from item we ignore: javaType, specType, registeredTypeName ... return type; }
@Test public void testBuiltEntity() { final CatalogEntityItemDto catalogItem = CatalogItemBuilder.newEntity(symbolicName, version) .javaType(javaType) .displayName(displayName) .description(description) .iconUrl(iconUrl) .deprecated(deprecated) .disabled(disabled) .plan(plan) .libraries(libraries) .tag(tag) .build(); assertEquals(catalogItem.getSymbolicName(), symbolicName); assertEquals(catalogItem.getVersion(), version); assertEquals(catalogItem.getJavaType(), javaType); assertEquals(catalogItem.getDisplayName(), displayName); assertEquals(catalogItem.getIconUrl(), iconUrl); assertEquals(catalogItem.isDeprecated(), deprecated); assertEquals(catalogItem.isDisabled(), disabled); assertEquals(catalogItem.getPlanYaml(), plan); assertEquals(catalogItem.getLibraries(), libraries); assertEquals(catalogItem.tags().getTags().size(), 1); assertTrue(catalogItem.tags().getTags().contains(tag)); } }
entity.tags().addTags(spec.getTags()); addSpecParameters(spec, theEntity.getMutableEntityType());
/** @deprecated since 0.12.0 use {@link RegisteredType} methods instead */ @Deprecated @SuppressWarnings({ "unchecked", "rawtypes" }) public static CatalogItemSummary catalogItemSummary(BrooklynRestResourceUtils b, CatalogItem item, UriBuilder ub) { try { switch (item.getCatalogItemType()) { case TEMPLATE: case ENTITY: return catalogEntitySummary(b, item, ub); case POLICY: return catalogPolicySummary(b, item, ub); case ENRICHER: return catalogEnricherSummary(b, item, ub); case LOCATION: return catalogLocationSummary(b, item, ub); default: log.warn("Unexpected catalog item type when getting self link (supplying generic item): "+item.getCatalogItemType()+" "+item); } } catch (Exception e) { Exceptions.propagateIfFatal(e); log.warn("Invalid item in catalog when converting REST summaries (supplying generic item), at "+item+": "+e, e); } return new CatalogItemSummary(item.getSymbolicName(), item.getVersion(), item.getContainingBundle(), item.getDisplayName(), item.getJavaType(), item.getCatalogItemType().toString(), item.getPlanYaml(), item.getDescription(), tidyIconLink(b, item, item.getIconUrl(), ub), item.tags().getTags(), item.isDeprecated(), makeLinks(item, ub)); }
/** augments the given item with annotations and class data for the given class, then adds to catalog * @deprecated since 0.7.0 the classpath DO is replaced by libraries */ @Deprecated public CatalogItem<?,?> addCatalogEntry(CatalogItemDtoAbstract<?,?> item, Class<?> c) { Catalog catalogAnnotation = c.getAnnotation(Catalog.class); item.setSymbolicName(c.getName()); item.setJavaType(c.getName()); item.setDisplayName(firstNonEmpty(c.getSimpleName(), c.getName())); if (catalogAnnotation!=null) { item.setDisplayName(firstNonEmpty(catalogAnnotation.name(), item.getDisplayName())); item.setDescription(firstNonEmpty(catalogAnnotation.description())); item.setIconUrl(firstNonEmpty(catalogAnnotation.iconUrl())); } if (item instanceof CatalogEntityItemDto || item instanceof CatalogTemplateItemDto) { item.tags().addTag(BrooklynTags.newTraitsTag(ClassUtils.getAllInterfaces(c))); } if (log.isTraceEnabled()) log.trace("adding to catalog: "+c+" (from catalog "+catalog+")"); catalog.addEntry(item); return item; }
@Test public void testRestoresConfig() throws Exception { origApp.enrichers().add(EnricherSpec.create(MyEnricher.class) .displayName("My Enricher") .uniqueTag("tagU") .tag("tag1").tag("tag2") .configure(MyEnricher.MY_CONFIG_WITH_SETFROMFLAG_NO_SHORT_NAME, "myVal for with setFromFlag noShortName") .configure(MyEnricher.MY_CONFIG_WITH_SETFROMFLAG_WITH_SHORT_NAME, "myVal for setFromFlag withShortName") .configure(MyEnricher.MY_CONFIG_WITHOUT_SETFROMFLAG, "myVal for witout setFromFlag")); newApp = rebind(); MyEnricher newEnricher = (MyEnricher) Iterables.getOnlyElement(newApp.enrichers()); assertEquals(newEnricher.getDisplayName(), "My Enricher"); assertEquals(newEnricher.getUniqueTag(), "tagU"); assertEquals(newEnricher.tags().getTags(), MutableSet.of("tagU", "tag1", "tag2")); assertEquals(newEnricher.getConfig(MyEnricher.MY_CONFIG_WITH_SETFROMFLAG_NO_SHORT_NAME), "myVal for with setFromFlag noShortName"); assertEquals(newEnricher.getConfig(MyEnricher.MY_CONFIG_WITH_SETFROMFLAG_WITH_SHORT_NAME), "myVal for setFromFlag withShortName"); assertEquals(newEnricher.getConfig(MyEnricher.MY_CONFIG_WITHOUT_SETFROMFLAG), "myVal for witout setFromFlag"); }
@Test public void testRestoresConfig() throws Exception { origApp.policies().add(PolicySpec.create(MyPolicy.class) .displayName("My Policy") .uniqueTag("tagU") .tag("tag1").tag("tag2") .configure(MyPolicy.MY_CONFIG_WITH_SETFROMFLAG_NO_SHORT_NAME, "myVal for with setFromFlag noShortName") .configure(MyPolicy.MY_CONFIG_WITH_SETFROMFLAG_WITH_SHORT_NAME, "myVal for setFromFlag withShortName") .configure(MyPolicy.MY_CONFIG_WITHOUT_SETFROMFLAG, "myVal for witout setFromFlag")); newApp = rebind(); MyPolicy newPolicy = (MyPolicy) Iterables.getOnlyElement(newApp.policies()); assertEquals(newPolicy.getDisplayName(), "My Policy"); assertEquals(newPolicy.getUniqueTag(), "tagU"); assertEquals(newPolicy.tags().getTags(), MutableSet.of("tagU", "tag1", "tag2")); assertEquals(newPolicy.getConfig(MyPolicy.MY_CONFIG_WITH_SETFROMFLAG_NO_SHORT_NAME), "myVal for with setFromFlag noShortName"); assertEquals(newPolicy.getConfig(MyPolicy.MY_CONFIG_WITH_SETFROMFLAG_WITH_SHORT_NAME), "myVal for setFromFlag withShortName"); assertEquals(newPolicy.getConfig(MyPolicy.MY_CONFIG_WITHOUT_SETFROMFLAG), "myVal for witout setFromFlag"); }
/** @deprecated since 0.12.0 use {@link RegisteredType} methods instead */ @Deprecated public static CatalogEnricherSummary catalogEnricherSummary(BrooklynRestResourceUtils b, CatalogItem<? extends Enricher,EnricherSpec<?>> item, UriBuilder ub) { final Set<EnricherConfigSummary> config = Sets.newLinkedHashSet(); try{ final EnricherSpec<?> spec = (EnricherSpec<?>) b.getCatalog().peekSpec(item); for (final SpecParameter<?> input : spec.getParameters()){ config.add(EntityTransformer.enricherConfigSummary(input)); } }catch (Exception e) { Exceptions.propagateIfFatal(e); log.trace("Unable to create policy spec for "+item+": "+e, e); } return new CatalogEnricherSummary(item.getSymbolicName(), item.getVersion(), item.getContainingBundle(), item.getDisplayName(), item.getJavaType(), item.getCatalogItemType().toString(), item.getPlanYaml(), item.getDescription(), tidyIconLink(b, item, item.getIconUrl(), ub), config, item.tags().getTags(), item.isDeprecated(), makeLinks(item, ub)); }
@Test public void testBrooklynCampObjectTags() throws Exception { final Entity app = createAndStartApplication( "services:", "- type: " + BasicApplication.class.getName(), " brooklyn.tags:", " - tag1", " - $brooklyn:object:", " type: " + TagsTestObject.class.getName()); assertTrue(Iterables.any(app.tags().getTags(), new Predicate<Object>() { @Override public boolean apply(@Nullable Object input) { return input instanceof TagsTestObject; } })); assertTrue(app.tags().getTags().contains("tag1")); }
/** @deprecated since 0.12.0 use {@link RegisteredType} methods instead */ @Deprecated public static CatalogPolicySummary catalogPolicySummary(BrooklynRestResourceUtils b, CatalogItem<? extends Policy,PolicySpec<?>> item, UriBuilder ub) { final Set<PolicyConfigSummary> config = Sets.newLinkedHashSet(); try{ final PolicySpec<?> spec = (PolicySpec<?>) b.getCatalog().peekSpec(item); for (final SpecParameter<?> input : spec.getParameters()){ config.add(EntityTransformer.policyConfigSummary(input)); } }catch (Exception e) { Exceptions.propagateIfFatal(e); log.trace("Unable to create policy spec for "+item+": "+e, e); } return new CatalogPolicySummary(item.getSymbolicName(), item.getVersion(), item.getContainingBundle(), item.getDisplayName(), item.getJavaType(), item.getCatalogItemType().toString(), item.getPlanYaml(), item.getDescription(), tidyIconLink(b, item, item.getIconUrl(), ub), config, item.tags().getTags(), item.isDeprecated(), makeLinks(item, ub)); }
@Test public void testConcurrentAddTag() throws Exception { final int NUM_TASKS = Math.min(500 * Runtime.getRuntime().availableProcessors(), 1000); List<ListenableFuture<?>> futures = Lists.newArrayList(); List<Integer> tags = Lists.newArrayList(); for (int i = 0; i < NUM_TASKS; i++) { final int val = i; ListenableFuture<?> future = executor.submit(new Runnable() { @Override public void run() { entity.tags().addTag(val); }}); futures.add(future); tags.add(val); } Futures.allAsList(futures).get(); Asserts.assertEqualsIgnoringOrder(entity.tags().getTags(), tags); }
@Test public void testCreateGetContainsAndRemoveTags() throws Exception { entity = app.createAndManageChild(EntitySpec.create(TestEntity.class) .tag(2) .addInitializer(EntityInitializers.addingTags("foo"))); entity.tags().addTag(app); Assert.assertTrue(entity.tags().containsTag(app)); Assert.assertTrue(entity.tags().containsTag("foo")); Assert.assertTrue(entity.tags().containsTag(2)); Assert.assertFalse(entity.tags().containsTag("bar")); Assert.assertEquals(entity.tags().getTags(), MutableSet.of(app, "foo", 2)); entity.tags().removeTag("foo"); Assert.assertFalse(entity.tags().containsTag("foo")); Assert.assertTrue(entity.tags().containsTag(entity.getParent())); Assert.assertFalse(entity.tags().containsTag(entity)); entity.tags().removeTag(2); Assert.assertEquals(entity.tags().getTags(), MutableSet.of(app)); }