public static Entity catalogItemScopeRoot(Entity entity) { Entity root = entity; while (root.getParent() != null && root != root.getParent() && Objects.equal(root.getParent().getCatalogItemId(), root.getCatalogItemId())) { root = root.getParent(); } return root; }
public static void setCatalogItemIdOnAddition(Entity entity, BrooklynObject itemBeingAdded) { if (entity.getCatalogItemId()!=null) { if (itemBeingAdded.getCatalogItemId()==null) { if (log.isDebugEnabled()) BrooklynLogging.log(log, BrooklynLogging.levelDebugOrTraceIfReadOnly(entity), "Catalog item addition: "+entity+" from "+entity.getCatalogItemId()+" applying its catalog item ID to "+itemBeingAdded); final BrooklynObjectInternal addInternal = (BrooklynObjectInternal) itemBeingAdded; addInternal.setCatalogItemIdAndSearchPath(entity.getCatalogItemId(), entity.getCatalogItemIdSearchPath()); } else { if (!itemBeingAdded.getCatalogItemId().equals(entity.getCatalogItemId())) { // not a problem, but something to watch out for log.debug("Cross-catalog item detected: "+entity+" from "+entity.getCatalogItemId()+" has "+itemBeingAdded+" from "+itemBeingAdded.getCatalogItemId()); } } } else if (itemBeingAdded.getCatalogItemId()!=null) { if (log.isDebugEnabled()) BrooklynLogging.log(log, BrooklynLogging.levelDebugOrTraceIfReadOnly(entity), "Catalog item addition: "+entity+" without catalog item ID has "+itemBeingAdded+" from "+itemBeingAdded.getCatalogItemId()); } }
@Override public String getCatalogItemIdFromContext() { Task<?> currentTask = Tasks.current(); if (currentTask != null) { Entity contextEntity = BrooklynTaskTags.getContextEntity(currentTask); if (contextEntity != null) { return contextEntity.getCatalogItemId(); } } return null; }
@Test public void testCatalogItemIdInReferencedItems() throws Exception { String symbolicNameInner = "my.catalog.app.id.inner"; String symbolicNameOuter = "my.catalog.app.id.outer"; addCatalogItems( "brooklyn.catalog:", " version: " + TEST_VERSION, " items:", " - id: " + symbolicNameInner, " item: " + TestEntity.class.getName(), " - id: " + symbolicNameOuter, " item: " + symbolicNameInner); String yaml = "name: " + symbolicNameOuter + "\n" + "services: \n" + " - serviceType: "+ver(symbolicNameOuter); Entity app = createAndStartApplication(yaml); Entity entity = app.getChildren().iterator().next(); assertEquals(entity.getCatalogItemId(), ver(symbolicNameOuter)); assertEquals(entity.getCatalogItemIdSearchPath(), ImmutableList.of(ver(symbolicNameInner)), "should have just " + symbolicNameInner + " in search path"); deleteCatalogEntity(symbolicNameInner); deleteCatalogEntity(symbolicNameOuter); }
lb.put("iconUrl", URI.create(entityUri + "/icon")); if (entity.getCatalogItemId() != null) { String versionedId = entity.getCatalogItemId(); URI catalogUri; String symbolicName = CatalogUtils.getSymbolicNameFromVersionedId(versionedId); return new EntitySummary(entity.getId(), entity.getDisplayName(), type, entity.getCatalogItemId(), lb.build());
@Test public void testCatalogItemIdInReferencedItems() throws Exception { TestResourceUnavailableException.throwIfResourceUnavailable(getClass(), OsgiStandaloneTest.BROOKLYN_TEST_OSGI_ENTITIES_PATH); String symbolicNameInner = "my.catalog.app.id.inner"; String symbolicNameOuter = "my.catalog.app.id.outer"; addCatalogItems( "brooklyn.catalog:", " version: " + TEST_VERSION, " items:", " - id: " + symbolicNameInner, " name: My Catalog App", " description: My description", " icon_url: classpath://path/to/myicon.jpg", " brooklyn.libraries:", " - url: " + OsgiStandaloneTest.BROOKLYN_TEST_OSGI_ENTITIES_URL, " item: " + SIMPLE_ENTITY_TYPE, " - id: " + symbolicNameOuter, " item: " + symbolicNameInner); String yaml = "name: " + symbolicNameOuter + "\n" + "services: \n" + " - serviceType: " + ver(symbolicNameOuter); Entity app = createAndStartApplication(yaml); Entity entity = app.getChildren().iterator().next(); assertEquals(entity.getCatalogItemId(), ver(symbolicNameOuter)); assertEquals(entity.getCatalogItemIdSearchPath(), ImmutableList.of(ver(symbolicNameInner)), "should have just " + symbolicNameInner + " in search path"); deleteCatalogEntity(symbolicNameInner); deleteCatalogEntity(symbolicNameOuter); } @Test
@Test public void testMoreEntityV1() throws Exception { TestResourceUnavailableException.throwIfResourceUnavailable(getClass(), "/brooklyn/osgi/brooklyn-test-osgi-more-entities_0.1.0.jar"); addCatalogItems(getLocalResource("more-entity-v1-osgi-catalog.yaml")); RegisteredType item = mgmt().getTypeRegistry().get("more-entity"); Assert.assertNotNull(item); Assert.assertEquals(item.getVersion(), "1.0"); Assert.assertTrue(RegisteredTypePredicates.IS_ENTITY.apply(item)); Assert.assertEquals(item.getLibraries().size(), 1); Entity app = createAndStartApplication("services: [ { type: 'more-entity:1.0' } ]"); Entity moreEntity = Iterables.getOnlyElement(app.getChildren()); Assert.assertEquals(moreEntity.getCatalogItemId(), "more-entity:1.0"); OsgiVersionMoreEntityTest.assertV1EffectorCall(moreEntity); OsgiVersionMoreEntityTest.assertV1MethodCall(moreEntity); }
@Test public void testMoreEntitiesV1() throws Exception { TestResourceUnavailableException.throwIfResourceUnavailable(getClass(), BROOKLYN_TEST_MORE_ENTITIES_V1_PATH); RegisteredType c2 = addMoreEntityV1(mgmt, TEST_VERSION); // test load and instantiate Entity me = addItemFromCatalog(c2); Assert.assertEquals(me.getCatalogItemId(), CatalogUtils.getVersionedId(OsgiTestResources.BROOKLYN_TEST_MORE_ENTITIES_MORE_ENTITY, TEST_VERSION)); assertV1MethodCall(me); assertV1EffectorCall(me); // test adding a child gets the right type; this time by entity parent hierarchy @SuppressWarnings({ "unchecked" }) Entity me2 = me.addChild( mgmt.getTypeRegistry().createSpec(c2, null, EntitySpec.class) ); Assert.assertEquals(me2.getCatalogItemId(), CatalogUtils.getVersionedId(OsgiTestResources.BROOKLYN_TEST_MORE_ENTITIES_MORE_ENTITY, TEST_VERSION)); }
@Test public void testMoreEntitiesV2() throws Exception { TestResourceUnavailableException.throwIfResourceUnavailable(getClass(), BROOKLYN_TEST_MORE_ENTITIES_V2_PATH); RegisteredType c2 = addCatalogItemWithTypeAsName( OsgiTestResources.BROOKLYN_TEST_MORE_ENTITIES_MORE_ENTITY, TEST_VERSION, BROOKLYN_TEST_MORE_ENTITIES_V2_URL, BROOKLYN_TEST_OSGI_ENTITIES_URL); // test load and instantiate Entity me = addItemFromCatalog(c2); Assert.assertEquals(me.getCatalogItemId(), CatalogUtils.getVersionedId(OsgiTestResources.BROOKLYN_TEST_MORE_ENTITIES_MORE_ENTITY, TEST_VERSION)); assertV2MethodCall(me); assertV2EffectorCall(me); Assert.assertEquals(me.policies().size(), 1, "Wrong number of policies: "+MutableList.copyOf(me.policies())); String catalogItemId = Iterables.getOnlyElement( me.policies() ).getCatalogItemId(); Assert.assertNotNull(catalogItemId); // allow either me's bundle (more) or the actual source bundle Assert.assertTrue(catalogItemId.equals(me.getCatalogItemId()) || catalogItemId.startsWith("brooklyn-test-osgi-entities")); }
public static BrooklynClassLoadingContext getClassLoadingContext(Entity entity) { ManagementContext mgmt = ((EntityInternal)entity).getManagementContext(); String catId = entity.getCatalogItemId(); if (Strings.isBlank(catId)) return JavaBrooklynClassLoadingContext.create(mgmt); Maybe<RegisteredType> cat = RegisteredTypes.tryValidate(mgmt.getTypeRegistry().get(catId), RegisteredTypeLoadingContexts.spec(Entity.class)); if (cat.isNull()) { log.warn("Cannot load "+catId+" to get classloader for "+entity+"; will try with standard loader, but might fail subsequently"); return JavaBrooklynClassLoadingContext.create(mgmt); } return newClassLoadingContext(mgmt, cat.get(), JavaBrooklynClassLoadingContext.create(mgmt)); }
@Test public void testMoreEntityV2() throws Exception { TestResourceUnavailableException.throwIfResourceUnavailable(getClass(), "/brooklyn/osgi/brooklyn-test-osgi-more-entities_0.2.0.jar"); TestResourceUnavailableException.throwIfResourceUnavailable(getClass(), "/brooklyn/osgi/brooklyn-test-osgi-entities.jar"); addCatalogItems(getLocalResource("more-entity-v2-osgi-catalog.yaml")); Entity app = createAndStartApplication("services: [ { type: 'more-entity:1.0' } ]"); Entity moreEntity = Iterables.getOnlyElement(app.getChildren()); Assert.assertEquals(moreEntity.getCatalogItemId(), "more-entity:1.0"); OsgiVersionMoreEntityTest.assertV2EffectorCall(moreEntity); OsgiVersionMoreEntityTest.assertV2MethodCall(moreEntity); Assert.assertEquals(moreEntity.policies().size(), 1, "wrong policies: "+moreEntity.policies()); Policy policy = Iterables.getOnlyElement(moreEntity.policies()); // it was loaded from the java so should have the base more-entity catalog id Assert.assertEquals(policy.getCatalogItemId(), "more-entity:1.0"); }
/** TODO we get warnings from {@link BrooklynEntityMatcher#extractValidConfigFlagsOrKeys}; * if we passed the correct loader at that point we could avoid those warnings. */ @Test public void testMoreEntityV1WithPolicy() throws Exception { TestResourceUnavailableException.throwIfResourceUnavailable(getClass(), "/brooklyn/osgi/brooklyn-test-osgi-more-entities_0.1.0.jar"); TestResourceUnavailableException.throwIfResourceUnavailable(getClass(), "/brooklyn/osgi/brooklyn-test-osgi-entities.jar"); addCatalogItems(getLocalResource("simple-policy-osgi-catalog.yaml")); addCatalogItems(getLocalResource("more-entity-v1-with-policy-osgi-catalog.yaml")); Entity app = createAndStartApplication("services: [ { type: 'more-entity:1.0' } ]"); Entity moreEntity = Iterables.getOnlyElement(app.getChildren()); Assert.assertEquals(moreEntity.getCatalogItemId(), "more-entity:1.0"); Assert.assertEquals(moreEntity.policies().size(), 1, "wrong policies: "+moreEntity.policies()); Policy policy = Iterables.getOnlyElement(moreEntity.policies()); // it was loaded by yaml w ref to catalog, so should have the simple-policy catalog-id Assert.assertEquals(policy.getCatalogItemId(), "simple-policy:1.0"); }
assertEquals(newEntity.getCatalogItemId(), appSymbolicName+":"+appVersion); assertEquals(newPolicy.getId(), origPolicy.getId()); assertEquals(entity2.getCatalogItemId(), appSymbolicName+":"+appVersion);
serviceState, iconUrl, entity.getCatalogItemId(), children, groupIds,
@Test public void testMoreEntitiesV1Policy() throws Exception { TestResourceUnavailableException.throwIfResourceUnavailable(getClass(), BROOKLYN_TEST_MORE_ENTITIES_V1_PATH); TestResourceUnavailableException.throwIfResourceUnavailable(getClass(), BROOKLYN_TEST_OSGI_ENTITIES_PATH); RegisteredType c2 = addCatalogItemWithTypeAsName( OsgiTestResources.BROOKLYN_TEST_MORE_ENTITIES_MORE_ENTITY, TEST_VERSION, BROOKLYN_TEST_MORE_ENTITIES_V1_URL); // test load and instantiate Entity me = addItemFromCatalog(c2); RegisteredType cp = addCatalogItemWithTypeAsName( OsgiTestResources.BROOKLYN_TEST_OSGI_ENTITIES_SIMPLE_POLICY, TEST_VERSION, BROOKLYN_TEST_OSGI_ENTITIES_URL); me.policies().add(getPolicySpec(cp)); Assert.assertEquals(me.policies().size(), 1, "Wrong number of policies: "+MutableList.copyOf(me.policies())); String catalogItemId = Iterables.getOnlyElement( me.policies() ).getCatalogItemId(); Assert.assertNotNull(catalogItemId); // must be the actual source bundle Assert.assertFalse(catalogItemId.equals(me.getCatalogItemId()), "catalog item id is: "+catalogItemId); Assert.assertTrue(catalogItemId.equals(CatalogUtils.getVersionedId(OsgiTestResources.BROOKLYN_TEST_OSGI_ENTITIES_SIMPLE_POLICY, TEST_VERSION)), "catalog item id is: "+catalogItemId); }
private void doTestLongReferenceSequence() throws Exception { // adds a0, a1 extending a0, a2 extending a1, ... a9 extending a8 // osgi rebind of types can fail because bundles are restored in any order // and dependencies might not yet be installed; // ensure items are added first without validation, then validating for (int i = 0; i<10; i++) { addCatalogItems( "brooklyn.catalog:", " id: a" + i, " version: 1", " itemType: entity", " item:", " type: " + (i==0 ? BasicEntity.class.getName() : "a" + (i-1))); } origApp = (StartableApplication) createAndStartApplication("services: [ { type: a9 } ]"); rebind(); Entity child = Iterables.getOnlyElement( newApp.getChildren() ); Asserts.assertTrue(child instanceof BasicEntity); Asserts.assertEquals(child.getCatalogItemId(), "a9:1"); }
Assert.assertEquals(entity1.getCatalogItemId(), "item-from-library:1.0"); assertCanFindMessages( entity1 );
@Test public void testMoreEntityV2AutoscanWithClasspath() throws Exception { TestResourceUnavailableException.throwIfResourceUnavailable(getClass(), "/brooklyn/osgi/brooklyn-test-osgi-more-entities_0.2.0.jar"); TestResourceUnavailableException.throwIfResourceUnavailable(getClass(), "/brooklyn/osgi/brooklyn-test-osgi-entities.jar"); addCatalogItems(getLocalResource("more-entities-osgi-catalog-scan.yaml")); log.info("autoscan for osgi found catalog items: "+Strings.join(mgmt().getCatalog().getCatalogItems(), ", ")); RegisteredType item = mgmt().getTypeRegistry().get("more-entity"); Assert.assertNotNull(item); Assert.assertEquals(item.getVersion(), "2.0.test"); Assert.assertTrue(RegisteredTypePredicates.IS_ENTITY.apply(item)); // this refers to the java item, where the libraries are defined item = mgmt().getTypeRegistry().get("org.apache.brooklyn.test.osgi.entities.more.MoreEntity"); Assert.assertEquals(item.getVersion(), "2.0.test_java"); Assert.assertEquals(item.getLibraries().size(), 2); Entity app = createAndStartApplication("services: [ { type: 'more-entity:2.0.test' } ]"); Entity moreEntity = Iterables.getOnlyElement(app.getChildren()); Assert.assertEquals(moreEntity.getCatalogItemId(), "more-entity:2.0.test"); OsgiVersionMoreEntityTest.assertV2EffectorCall(moreEntity); OsgiVersionMoreEntityTest.assertV2MethodCall(moreEntity); }
assertEquals(entity2.getCatalogItemId(), appSymbolicName+":"+appVersion);
@Test public void testCatalogBomFromBundleWithManualManifest() throws Exception { bm.setDefaultClassForLoading(getClass()); File jf = bm.createJarFromClasspathDir("osgi/catalog-bundle-1"); jf = bm.copyRemoving(jf, MutableSet.of(JarFile.MANIFEST_NAME)); String customName = "catalog-bundle-1-manual-"+Identifiers.makeRandomId(4); jf = bm.copyAddingManifest(jf, MutableMap.of( "Manifest-Version", "2.0", "Bundle-SymbolicName", customName, "Bundle-Version", "0.0.0.SNAPSHOT")); Assert.assertTrue(bm.hasOsgiManifest(jf)); installBundle(jf); assertHasBasic1(); Entity basic1 = assertBasic1DeploysAndHasSensor(); RegisteredType item = mgmt().getTypeRegistry().get( basic1.getCatalogItemId() ); Collection<OsgiBundleWithUrl> libs = item.getLibraries(); Asserts.assertSize(libs, 1); Assert.assertEquals(Iterables.getOnlyElement(libs).getSymbolicName(), customName); }