brooklyn-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From henev...@apache.org
Subject [13/45] git commit: Incorporate loading from bundles into BrooklynEntityClassResolver
Date Fri, 04 Jul 2014 09:50:55 GMT
Incorporate loading from bundles into BrooklynEntityClassResolver


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

Branch: refs/heads/master
Commit: 494291d8029d6e5fa02f1444a3e5d218ca3ef902
Parents: b0bd808
Author: Sam Corbett <sam.corbett@cloudsoftcorp.com>
Authored: Mon Jun 30 13:19:51 2014 +0100
Committer: Sam Corbett <sam.corbett@cloudsoftcorp.com>
Committed: Tue Jul 1 13:37:37 2014 +0100

----------------------------------------------------------------------
 .../java/brooklyn/catalog/BrooklynCatalog.java  |  2 ++
 .../internal/CatalogItemDtoAbstract.java        | 17 ++++++++--
 .../management/osgi/OsgiStandaloneTest.java     |  1 +
 .../creation/BrooklynEntityClassResolver.java   | 33 ++++++++++++++++++--
 .../spi/creation/BrooklynYamlTypeLoader.java    | 11 ++++---
 5 files changed, 54 insertions(+), 10 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/494291d8/api/src/main/java/brooklyn/catalog/BrooklynCatalog.java
----------------------------------------------------------------------
diff --git a/api/src/main/java/brooklyn/catalog/BrooklynCatalog.java b/api/src/main/java/brooklyn/catalog/BrooklynCatalog.java
index 622241f..720a2bc 100644
--- a/api/src/main/java/brooklyn/catalog/BrooklynCatalog.java
+++ b/api/src/main/java/brooklyn/catalog/BrooklynCatalog.java
@@ -13,6 +13,7 @@ public interface BrooklynCatalog {
     
     /** returns all items in the catalog */
     <T> Iterable<CatalogItem<T>> getCatalogItems();
+
     /** convenience for filtering items in the catalog; see CatalogPredicates for useful
filters */
     <T> Iterable<CatalogItem<T>> getCatalogItems(Predicate<? super CatalogItem<T>>
filter);
 
@@ -42,6 +43,7 @@ public interface BrooklynCatalog {
 
     /** adds a classpath entry which will be used by the 'manual' catalog */
     void addToClasspath(ClassLoader loader);
+
     /** adds a classpath entry which will be used by the 'manual' catalog */
     void addToClasspath(String url);
     

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/494291d8/core/src/main/java/brooklyn/catalog/internal/CatalogItemDtoAbstract.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/brooklyn/catalog/internal/CatalogItemDtoAbstract.java b/core/src/main/java/brooklyn/catalog/internal/CatalogItemDtoAbstract.java
index ad0fa71..043758b 100644
--- a/core/src/main/java/brooklyn/catalog/internal/CatalogItemDtoAbstract.java
+++ b/core/src/main/java/brooklyn/catalog/internal/CatalogItemDtoAbstract.java
@@ -1,11 +1,21 @@
 package brooklyn.catalog.internal;
 
-import javax.annotation.Nullable;
+import java.util.Collections;
+import java.util.List;
+
+import javax.annotation.Nonnull;
 
 import brooklyn.catalog.CatalogItem;
 
 public abstract class CatalogItemDtoAbstract<T> implements CatalogItem<T> {
-    
+
+    private static final CatalogItemContext EMPTY_CONTEXT = new CatalogItemContext() {
+        @Override
+        public List<String> getBundles() {
+            return Collections.emptyList();
+        }
+    };
+
     String id;
     String type;
     String name;
@@ -34,6 +44,7 @@ public abstract class CatalogItemDtoAbstract<T> implements CatalogItem<T>
{
         return iconUrl;
     }
 
+    @Nonnull
     @Override
     public CatalogItemContext getContext() {
         return context;
@@ -76,7 +87,7 @@ public abstract class CatalogItemDtoAbstract<T> implements CatalogItem<T>
{
         target.type = type;
         target.name = name;
         target.description = description;
-        target.context = context;
+        target.context = context != null ? context : EMPTY_CONTEXT;
         return target;
     }
 

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/494291d8/core/src/test/java/brooklyn/management/osgi/OsgiStandaloneTest.java
----------------------------------------------------------------------
diff --git a/core/src/test/java/brooklyn/management/osgi/OsgiStandaloneTest.java b/core/src/test/java/brooklyn/management/osgi/OsgiStandaloneTest.java
index 7ffaf2d..2461f27 100644
--- a/core/src/test/java/brooklyn/management/osgi/OsgiStandaloneTest.java
+++ b/core/src/test/java/brooklyn/management/osgi/OsgiStandaloneTest.java
@@ -110,6 +110,7 @@ public class OsgiStandaloneTest {
         Object result = aClass.getMethod("times", int.class).invoke(aInst, input);
         Assert.assertEquals(result, output);
     }
+
     protected void setAMultiplier(Bundle bundle, int newMultiplier) throws Exception {
         Assert.assertNotNull(bundle);
         Class<?> aClass = bundle.loadClass("brooklyn.test.osgi.TestA");

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/494291d8/usage/camp/src/main/java/io/brooklyn/camp/brooklyn/spi/creation/BrooklynEntityClassResolver.java
----------------------------------------------------------------------
diff --git a/usage/camp/src/main/java/io/brooklyn/camp/brooklyn/spi/creation/BrooklynEntityClassResolver.java
b/usage/camp/src/main/java/io/brooklyn/camp/brooklyn/spi/creation/BrooklynEntityClassResolver.java
index 7e71bb5..df096dc 100644
--- a/usage/camp/src/main/java/io/brooklyn/camp/brooklyn/spi/creation/BrooklynEntityClassResolver.java
+++ b/usage/camp/src/main/java/io/brooklyn/camp/brooklyn/spi/creation/BrooklynEntityClassResolver.java
@@ -2,6 +2,8 @@ package io.brooklyn.camp.brooklyn.spi.creation;
 
 import static com.google.common.base.Preconditions.checkNotNull;
 
+import java.util.Collections;
+import java.util.List;
 import java.util.NoSuchElementException;
 
 import org.slf4j.Logger;
@@ -27,9 +29,33 @@ public class BrooklynEntityClassResolver {
      * @throws java.lang.IllegalStateException if no class extending {@link Entity} is found
      */
     public static <T extends Entity> Class<T> resolveEntity(String entityTypeName,
ManagementContext mgmt) {
+        return resolveEntity(entityTypeName, mgmt, Collections.<String>emptyList());
+    }
+
+    /**
+     * Loads the class represented by entityTypeName with the given management context.
+     * Tries the context's catalogue first, then from its root classloader.
+     * @throws java.lang.IllegalStateException if no class extending {@link Entity} is found
+     */
+    public static <T extends Entity> Class<T> resolveEntity(String entityTypeName,
ManagementContext mgmt, List<String> context) {
         checkNotNull(mgmt, "management context");
-        Maybe<Class<T>> entityClazz = tryLoadEntityFromCatalogue(entityTypeName,
mgmt);
-        if (!entityClazz.isPresent()) entityClazz = tryLoadFromClasspath(entityTypeName,
mgmt);
+        Maybe<Class<T>> entityClazz = Maybe.absent();
+        // TODO: What if context not empty but class not found? Throw?
+        if (context != null) {
+            for (String bundleUrl : context) {
+                entityClazz = tryLoadEntityFromBundle(entityTypeName, bundleUrl, mgmt);
+                if (entityClazz.isPresent()) {
+                    break;
+                }
+            }
+        }
+
+        if (!entityClazz.isPresent()) {
+            entityClazz = tryLoadEntityFromCatalogue(entityTypeName, mgmt);
+        }
+        if (!entityClazz.isPresent()) {
+            entityClazz = tryLoadFromClasspath(entityTypeName, mgmt);
+        }
         if (!entityClazz.isPresent()) {
             LOG.warn("No catalog item for {} and could not load class directly; throwing",
entityTypeName);
             throw new IllegalStateException("Unable to load class "+ entityTypeName +" (extending
Entity) from catalogue or classpath: not found");
@@ -43,6 +69,7 @@ public class BrooklynEntityClassResolver {
 
     /** Tries to load the entity with the given class name from the given bundle. */
     public static <T extends Entity> Maybe<Class<T>> tryLoadEntityFromBundle(String
entityTypeName, String bundleUrl, ManagementContext mgmt) {
+        LOG.debug("Trying to resolve class {} from bundle {}", entityTypeName, bundleUrl);
         Maybe<OsgiManager> osgiManager = ((ManagementContextInternal) mgmt).getOsgiManager();
         if (!osgiManager.isPresentAndNonNull()) {
             LOG.debug("Asked to resolve class {} from bundle {} but osgi manager is unavailable
in context {}",
@@ -58,6 +85,7 @@ public class BrooklynEntityClassResolver {
 
     @SuppressWarnings("unchecked")
     public static <T extends Entity> Maybe<Class<T>> tryLoadEntityFromCatalogue(String
entityTypeName, ManagementContext mgmt) {
+        LOG.debug("Trying to resolve class {} from catalog", entityTypeName);
         try {
             return (Maybe<Class<T>>)(Maybe<?>) Maybe.<Class<? extends
Entity>>of(mgmt.getCatalog().loadClassByType(entityTypeName, Entity.class));
         } catch (NoSuchElementException e) {
@@ -68,6 +96,7 @@ public class BrooklynEntityClassResolver {
 
     @SuppressWarnings("unchecked")
     public static <T> Maybe<Class<T>> tryLoadFromClasspath(String typeName,
ManagementContext mgmt) {
+        LOG.debug("Trying to resolve class {} from classpath", typeName);
         Class<T> clazz;
         try {
             clazz = (Class<T>) mgmt.getCatalog().getRootClassLoader().loadClass(typeName);

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/494291d8/usage/camp/src/main/java/io/brooklyn/camp/brooklyn/spi/creation/BrooklynYamlTypeLoader.java
----------------------------------------------------------------------
diff --git a/usage/camp/src/main/java/io/brooklyn/camp/brooklyn/spi/creation/BrooklynYamlTypeLoader.java
b/usage/camp/src/main/java/io/brooklyn/camp/brooklyn/spi/creation/BrooklynYamlTypeLoader.java
index 77c8580..747e507 100644
--- a/usage/camp/src/main/java/io/brooklyn/camp/brooklyn/spi/creation/BrooklynYamlTypeLoader.java
+++ b/usage/camp/src/main/java/io/brooklyn/camp/brooklyn/spi/creation/BrooklynYamlTypeLoader.java
@@ -15,6 +15,7 @@ import brooklyn.util.config.ConfigBag;
 import brooklyn.util.exceptions.Exceptions;
 import brooklyn.util.guava.Maybe;
 import brooklyn.util.javalang.Reflections;
+import brooklyn.util.osgi.Osgis;
 
 import com.google.common.annotations.Beta;
 import com.google.common.base.Optional;
@@ -167,23 +168,23 @@ public abstract class BrooklynYamlTypeLoader {
         return loadClass(type, getTypeName().get(), factory.mgmt, factory.contextForLogging);
     }
 
-    /** 
+    /**
      * TODO in future will want OSGi-based resolver here (eg create from osgi:<bundle>:
prefix
      * would use that OSGi mechanism here
      */
     @SuppressWarnings("unchecked")
     private static <T> Class<T> loadClass(@Nullable Class<T> optionalSupertype,
String typeName, ManagementContext mgmt, Object otherContext) {
         try {
-            if (optionalSupertype!=null && Entity.class.isAssignableFrom(optionalSupertype))

+            if (optionalSupertype != null && Entity.class.isAssignableFrom(optionalSupertype))
                 return (Class<T>) BrooklynEntityClassResolver.<Entity>resolveEntity(typeName,
mgmt);
             else
                 return BrooklynEntityClassResolver.<T>tryLoadFromClasspath(typeName,
mgmt).get();
         } catch (Exception e) {
             Exceptions.propagateIfFatal(e);
-            log.warn("Unable to resolve "+typeName+" in spec "+otherContext);
+            log.warn("Unable to resolve " + typeName + " in spec " + otherContext);
             throw Exceptions.propagate(new IllegalStateException("Unable to resolve "
-                + (optionalSupertype!=null ? optionalSupertype.getSimpleName()+" " : "")
-                + "type '"+typeName+"'", e));
+                    + (optionalSupertype != null ? optionalSupertype.getSimpleName() + "
" : "")
+                    + "type '" + typeName + "'", e));
         }
     }
 


Mime
View raw message