brooklyn-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sjcorb...@apache.org
Subject [1/2] incubator-brooklyn git commit: Load resources from entities' catalog item loader.
Date Wed, 14 Jan 2015 19:00:59 GMT
Repository: incubator-brooklyn
Updated Branches:
  refs/heads/master 97db77428 -> b67d58479


Load resources from entities' catalog item loader.


Project: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/commit/571e0473
Tree: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/tree/571e0473
Diff: http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/diff/571e0473

Branch: refs/heads/master
Commit: 571e04738a3cd1f710a4b1e06ccec5b0d1bfe77d
Parents: b2474e5
Author: Svetoslav Neykov <svetoslav.neykov@cloudsoftcorp.com>
Authored: Wed Jan 14 14:59:56 2015 +0200
Committer: Svetoslav Neykov <svetoslav.neykov@cloudsoftcorp.com>
Committed: Wed Jan 14 15:51:11 2015 +0200

----------------------------------------------------------------------
 .../internal/AbstractManagementContext.java     | 21 +++++++++++++++-----
 .../brooklyn/catalog/CatalogYamlEntityTest.java | 18 ++++++++++++++++-
 2 files changed, 33 insertions(+), 6 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/571e0473/core/src/main/java/brooklyn/management/internal/AbstractManagementContext.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/brooklyn/management/internal/AbstractManagementContext.java
b/core/src/main/java/brooklyn/management/internal/AbstractManagementContext.java
index ab156b6..6f08ef0 100644
--- a/core/src/main/java/brooklyn/management/internal/AbstractManagementContext.java
+++ b/core/src/main/java/brooklyn/management/internal/AbstractManagementContext.java
@@ -35,8 +35,10 @@ import org.slf4j.LoggerFactory;
 
 import brooklyn.basic.BrooklynObject;
 import brooklyn.catalog.BrooklynCatalog;
+import brooklyn.catalog.CatalogItem;
 import brooklyn.catalog.CatalogLoadMode;
 import brooklyn.catalog.internal.BasicBrooklynCatalog;
+import brooklyn.catalog.internal.CatalogUtils;
 import brooklyn.config.BrooklynProperties;
 import brooklyn.config.BrooklynServerConfig;
 import brooklyn.config.StringConfigMap;
@@ -113,14 +115,23 @@ public abstract class AbstractManagementContext implements ManagementContextInte
     }
 
     static {
-        // ensure that if ResourceUtils is given an entity as context,
-        // we use the catalog class loader (e.g. to resolve classpath URLs)
         ResourceUtils.addClassLoaderProvider(new Function<Object, BrooklynClassLoadingContext>()
{
             @Override
             public BrooklynClassLoadingContext apply(@Nullable Object input) {
-                // TODO for entities, this should get its originating catalog item's loader
-                if (input instanceof EntityInternal)
-                    return apply(((EntityInternal)input).getManagementSupport());
+                if (input instanceof EntityInternal) {
+                    EntityInternal internal = (EntityInternal)input;
+                    if (internal.getCatalogItemId() != null) {
+                        CatalogItem<?, ?> item = CatalogUtils.getCatalogItemOptionalVersion(internal.getManagementContext(),
internal.getCatalogItemId());
+                        if (item != null) {
+                            return CatalogUtils.newClassLoadingContext(internal.getManagementContext(),
item);
+                        } else {
+                            log.error("Can't find catalog item " + internal.getCatalogItemId()
+
+                                    " used for instantiating entity " + internal +
+                                    ". Falling back to application classpath.");
+                        }
+                    }
+                    return apply(internal.getManagementSupport());
+                }
                 
                 if (input instanceof EntityManagementSupport)
                     return apply(((EntityManagementSupport)input).getManagementContext());

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/571e0473/usage/camp/src/test/java/io/brooklyn/camp/brooklyn/catalog/CatalogYamlEntityTest.java
----------------------------------------------------------------------
diff --git a/usage/camp/src/test/java/io/brooklyn/camp/brooklyn/catalog/CatalogYamlEntityTest.java
b/usage/camp/src/test/java/io/brooklyn/camp/brooklyn/catalog/CatalogYamlEntityTest.java
index cb804a5..42658e1 100644
--- a/usage/camp/src/test/java/io/brooklyn/camp/brooklyn/catalog/CatalogYamlEntityTest.java
+++ b/usage/camp/src/test/java/io/brooklyn/camp/brooklyn/catalog/CatalogYamlEntityTest.java
@@ -21,10 +21,12 @@ package io.brooklyn.camp.brooklyn.catalog;
 import static org.testng.Assert.assertEquals;
 import static org.testng.Assert.assertTrue;
 import static org.testng.Assert.fail;
-
 import brooklyn.test.TestResourceUnavailableException;
+import brooklyn.util.ResourceUtils;
 import io.brooklyn.camp.brooklyn.AbstractYamlTest;
 
+import java.io.InputStream;
+import java.net.URL;
 import java.util.Collection;
 
 import org.testng.Assert;
@@ -362,6 +364,20 @@ public class CatalogYamlEntityTest extends AbstractYamlTest {
         Object spec = catalog.createSpec(item);
         Assert.assertNotNull(spec);
     }
+    
+    @Test
+    public void testLoadResourceFromBundle() throws Exception {
+        String id = "resource.test";
+        addCatalogOSGiEntity(id, SIMPLE_ENTITY_TYPE);
+        String yaml =
+                "services: \n" +
+                "  - serviceType: "+ver(id);
+        Entity app = createAndStartApplication(yaml);
+        Entity simpleEntity = Iterables.getOnlyElement(app.getChildren());
+        InputStream icon = new ResourceUtils(simpleEntity).getResourceFromUrl("classpath:/brooklyn/osgi/tests/icon.gif");
+        assertTrue(icon != null);
+        icon.close();
+    }
 
     private void registerAndLaunchAndAssertSimpleEntity(String symbolicName, String serviceType)
throws Exception {
         addCatalogOSGiEntity(symbolicName, serviceType);


Mime
View raw message