brooklyn-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From henev...@apache.org
Subject [08/45] git commit: Ground work to load classes from bundles in blueprints
Date Fri, 04 Jul 2014 09:50:50 GMT
Ground work to load classes from bundles in blueprints


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

Branch: refs/heads/master
Commit: 06c10c4318374009d1b8e0ed5b314d5481a40c7c
Parents: 79ebcc3
Author: Sam Corbett <sam.corbett@cloudsoftcorp.com>
Authored: Thu Jun 26 14:28:24 2014 +0100
Committer: Sam Corbett <sam.corbett@cloudsoftcorp.com>
Committed: Tue Jul 1 13:37:37 2014 +0100

----------------------------------------------------------------------
 .../java/brooklyn/event/feed/ssh/SshFeed.java   |  2 +-
 .../brooklyn/management/ha/OsgiManager.java     | 16 ++++++++++++++
 .../internal/LocalManagementContext.java        |  5 +++--
 .../internal/ManagementContextInternal.java     |  5 +++--
 .../NonDeploymentManagementContext.java         |  4 ++--
 .../src/main/java/brooklyn/util/osgi/Osgis.java | 15 +++++++------
 .../BrooklynAssemblyTemplateInstantiator.java   | 17 ++++++++++++---
 .../creation/BrooklynEntityClassResolver.java   | 23 +++++++++++++++++---
 8 files changed, 67 insertions(+), 20 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/06c10c43/core/src/main/java/brooklyn/event/feed/ssh/SshFeed.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/brooklyn/event/feed/ssh/SshFeed.java b/core/src/main/java/brooklyn/event/feed/ssh/SshFeed.java
index 7492f75..ab3df25 100644
--- a/core/src/main/java/brooklyn/event/feed/ssh/SshFeed.java
+++ b/core/src/main/java/brooklyn/event/feed/ssh/SshFeed.java
@@ -168,7 +168,7 @@ public class SshFeed extends AbstractFeed {
     
     // Treat as immutable once built
     private final SetMultimap<SshPollIdentifier, SshPollConfig<?>> polls = HashMultimap.<SshPollIdentifier,SshPollConfig<?>>create();
-    
+
     /** @deprecated since 0.7.0, use static convenience on {@link Locations} */
     @Deprecated
     public static SshMachineLocation getMachineOfEntity(Entity entity) {

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/06c10c43/core/src/main/java/brooklyn/management/ha/OsgiManager.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/brooklyn/management/ha/OsgiManager.java b/core/src/main/java/brooklyn/management/ha/OsgiManager.java
index 07400ec..c161190 100644
--- a/core/src/main/java/brooklyn/management/ha/OsgiManager.java
+++ b/core/src/main/java/brooklyn/management/ha/OsgiManager.java
@@ -9,6 +9,7 @@ import brooklyn.config.BrooklynServerConfig;
 import brooklyn.config.ConfigKey;
 import brooklyn.util.collections.MutableMap;
 import brooklyn.util.exceptions.Exceptions;
+import brooklyn.util.guava.Maybe;
 import brooklyn.util.os.Os;
 import brooklyn.util.osgi.Osgis;
 
@@ -45,4 +46,19 @@ public class OsgiManager {
         framework = null;
     }
 
+    // TODO: throws BundleException appropriate?
+    public void registerBundle(String bundleUrl) throws BundleException {
+        Osgis.install(framework, bundleUrl);
+    }
+
+    // TODO: Handle .get failing
+    public <T> Maybe<Class<T>> tryResolveClass(String bundleUrl, String
type) {
+        try {
+            Class<T> clazz = (Class<T>) Osgis.getBundle(framework, bundleUrl).get().loadClass(type);
+            return Maybe.of(clazz);
+        } catch (Exception e) {
+            Exceptions.propagateIfFatal(e);
+            return Maybe.absent(e);
+        }
+    }
 }

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/06c10c43/core/src/main/java/brooklyn/management/internal/LocalManagementContext.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/brooklyn/management/internal/LocalManagementContext.java b/core/src/main/java/brooklyn/management/internal/LocalManagementContext.java
index 0d3deb9..4b70799 100644
--- a/core/src/main/java/brooklyn/management/internal/LocalManagementContext.java
+++ b/core/src/main/java/brooklyn/management/internal/LocalManagementContext.java
@@ -36,6 +36,7 @@ import brooklyn.management.SubscriptionManager;
 import brooklyn.management.Task;
 import brooklyn.management.ha.OsgiManager;
 import brooklyn.util.exceptions.Exceptions;
+import brooklyn.util.guava.Maybe;
 import brooklyn.util.task.BasicExecutionContext;
 import brooklyn.util.task.BasicExecutionManager;
 import brooklyn.util.text.Strings;
@@ -255,9 +256,9 @@ public class LocalManagementContext extends AbstractManagementContext
{
     }
     
     @Override
-    public synchronized OsgiManager getOsgiManager() {
+    public synchronized Maybe<OsgiManager> getOsgiManager() {
         if (!isRunning()) throw new IllegalStateException("Management context no longer running");
-        return osgiManager;        
+        return Maybe.of(osgiManager);
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/06c10c43/core/src/main/java/brooklyn/management/internal/ManagementContextInternal.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/brooklyn/management/internal/ManagementContextInternal.java
b/core/src/main/java/brooklyn/management/internal/ManagementContextInternal.java
index fb50641..4707a7f 100644
--- a/core/src/main/java/brooklyn/management/internal/ManagementContextInternal.java
+++ b/core/src/main/java/brooklyn/management/internal/ManagementContextInternal.java
@@ -19,6 +19,7 @@ import brooklyn.location.Location;
 import brooklyn.management.ManagementContext;
 import brooklyn.management.Task;
 import brooklyn.management.ha.OsgiManager;
+import brooklyn.util.guava.Maybe;
 import brooklyn.util.task.TaskTags;
 
 public interface ManagementContextInternal extends ManagementContext {
@@ -59,9 +60,9 @@ public interface ManagementContextInternal extends ManagementContext {
 
     UsageManager getUsageManager();
     
-    /** returns OSGi manager, if available; may be null if OSGi not supported, e.g. in test
contexts
+    /** returns OSGi manager, if available; may be absent if OSGi not supported, e.g. in
test contexts
      * (but major contexts will support this) */
-    OsgiManager getOsgiManager();
+    Maybe<OsgiManager> getOsgiManager();
 
     InternalEntityFactory getEntityFactory();
     

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/06c10c43/core/src/main/java/brooklyn/management/internal/NonDeploymentManagementContext.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/brooklyn/management/internal/NonDeploymentManagementContext.java
b/core/src/main/java/brooklyn/management/internal/NonDeploymentManagementContext.java
index 61826ef..5c9278c 100644
--- a/core/src/main/java/brooklyn/management/internal/NonDeploymentManagementContext.java
+++ b/core/src/main/java/brooklyn/management/internal/NonDeploymentManagementContext.java
@@ -178,8 +178,8 @@ public class NonDeploymentManagementContext implements ManagementContextInternal
     }
     
     @Override
-    public OsgiManager getOsgiManager() {
-        return null;
+    public Maybe<OsgiManager> getOsgiManager() {
+        return Maybe.absent();
     }
 
     @Override

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/06c10c43/core/src/main/java/brooklyn/util/osgi/Osgis.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/brooklyn/util/osgi/Osgis.java b/core/src/main/java/brooklyn/util/osgi/Osgis.java
index 2cfc5a7..5edca46 100644
--- a/core/src/main/java/brooklyn/util/osgi/Osgis.java
+++ b/core/src/main/java/brooklyn/util/osgi/Osgis.java
@@ -13,6 +13,8 @@ import org.osgi.framework.BundleException;
 import org.osgi.framework.Constants;
 import org.osgi.framework.Version;
 import org.osgi.framework.launch.Framework;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 import brooklyn.util.ResourceUtils;
 import brooklyn.util.collections.MutableList;
@@ -33,13 +35,13 @@ import com.google.common.base.Predicates;
 @Beta
 public class Osgis {
 
+    private static final Logger LOG = LoggerFactory.getLogger(Osgis.class);
+
     public static List<Bundle> getBundlesByName(Framework framework, String symbolicName,
Predicate<Version> versionMatcher) {
         List<Bundle> result = MutableList.of();
         for (Bundle b: framework.getBundleContext().getBundles()) {
-            if (symbolicName.equals(b.getSymbolicName())) {
-                if (versionMatcher.apply(b.getVersion())) {
-                    result.add(b);
-                }
+            if (symbolicName.equals(b.getSymbolicName()) && versionMatcher.apply(b.getVersion()))
{
+                result.add(b);
             }
         }
         return result;
@@ -115,7 +117,7 @@ public class Osgis {
         try {
             framework.init();
             // nothing needs auto-loading, currently (and this needs a new dependency)
-//            AutoProcessor.process(configProps, m_fwk.getBundleContext());
+            // AutoProcessor.process(configProps, m_fwk.getBundleContext());
             framework.start();
         } catch (Exception e) {
             // framework bundle start exceptions are not interesting to caller...
@@ -128,12 +130,11 @@ public class Osgis {
      * using the {@link ResourceUtils} loader for this project (brooklyn core) */
     public static Bundle install(Framework framework, String url) throws BundleException
{
         Bundle bundle = framework.getBundleContext().getBundle(url);
-        if (bundle!=null) return bundle;
+        if (bundle != null) return bundle;
         
         // use our URL resolution so we get classpath items
         InputStream stream = ResourceUtils.create(Osgis.class).getResourceFromUrl(url);
         return framework.getBundleContext().installBundle(url, stream);
     }
-    
 
 }

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/06c10c43/usage/camp/src/main/java/io/brooklyn/camp/brooklyn/spi/creation/BrooklynAssemblyTemplateInstantiator.java
----------------------------------------------------------------------
diff --git a/usage/camp/src/main/java/io/brooklyn/camp/brooklyn/spi/creation/BrooklynAssemblyTemplateInstantiator.java
b/usage/camp/src/main/java/io/brooklyn/camp/brooklyn/spi/creation/BrooklynAssemblyTemplateInstantiator.java
index 4ec821f..a06d716 100644
--- a/usage/camp/src/main/java/io/brooklyn/camp/brooklyn/spi/creation/BrooklynAssemblyTemplateInstantiator.java
+++ b/usage/camp/src/main/java/io/brooklyn/camp/brooklyn/spi/creation/BrooklynAssemblyTemplateInstantiator.java
@@ -53,7 +53,7 @@ public class BrooklynAssemblyTemplateInstantiator implements AssemblyTemplateIns
         Application app = create(template, platform);
         Task<?> task = start(app, platform);
         log.info("CAMP created "+app+"; starting in "+task);
-        return ((BrooklynCampPlatform)platform).assemblies().get(app.getApplicationId());
+        return platform.assemblies().get(app.getApplicationId());
     }
 
     // note: based on BrooklynRestResourceUtils, but modified to not allow child entities
(yet)
@@ -65,6 +65,18 @@ public class BrooklynAssemblyTemplateInstantiator implements AssemblyTemplateIns
         BrooklynCatalog catalog = mgmt.getCatalog();
         CatalogItem<?> item = catalog.getCatalogItem(template.getId());
 
+        if (item == null) {
+            // This doesn't seem the most appropriate way to do this!
+            // Especially since YAML will only reach here if a services section was given
(so that
+            // PdpProcessor calls into BrooklynEntityMatcher [via applyMatchers] which sets
the
+            // instantiator to BrooklynAssemblyTemplateInstantiator). So services must be
given
+            // but will be disregarded.
+            Object customId = template.getCustomAttributes().get("id");
+            if (customId != null) {
+                item = catalog.getCatalogItem(customId.toString());
+            }
+        }
+
         if (item==null) {
             return createApplicationFromNonCatalogCampTemplate(template, platform);
         } else {
@@ -139,8 +151,7 @@ public class BrooklynAssemblyTemplateInstantiator implements AssemblyTemplateIns
     }
 
     private ManagementContext getBrooklynManagementContext(CampPlatform platform) {
-        ManagementContext mgmt = ((HasBrooklynManagementContext)platform).getBrooklynManagementContext();
-        return mgmt;
+        return ((HasBrooklynManagementContext)platform).getBrooklynManagementContext();
     }
     
     public Task<?> start(Application app, CampPlatform platform) {

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/06c10c43/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 fe560da..7e71bb5 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
@@ -9,6 +9,8 @@ import org.slf4j.LoggerFactory;
 
 import brooklyn.entity.Entity;
 import brooklyn.management.ManagementContext;
+import brooklyn.management.ha.OsgiManager;
+import brooklyn.management.internal.ManagementContextInternal;
 import brooklyn.util.guava.Maybe;
 
 /**
@@ -20,7 +22,7 @@ public class BrooklynEntityClassResolver {
     private static final Logger LOG = LoggerFactory.getLogger(BrooklynEntityClassResolver.class);
 
     /**
-     * Loads the class represented by {@link #entityType} with the given management context.
+     * 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
      */
@@ -36,7 +38,22 @@ public class BrooklynEntityClassResolver {
             LOG.warn("Found class {} on classpath but it is not assignable to {}", entityTypeName,
Entity.class);
             throw new IllegalStateException("Unable to load class "+ entityTypeName +" (extending
Entity) from catalogue or classpath: wrong type "+entityClazz.get());
         }
-        return (Class<T>) entityClazz.get();
+        return entityClazz.get();
+    }
+
+    /** 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) {
+        Maybe<OsgiManager> osgiManager = ((ManagementContextInternal) mgmt).getOsgiManager();
+        if (!osgiManager.isPresentAndNonNull()) {
+            LOG.debug("Asked to resolve class {} from bundle {} but osgi manager is unavailable
in context {}",
+                    new Object[]{entityTypeName, bundleUrl, mgmt});
+            return Maybe.absent();
+        }
+        Maybe<Class<T>> clazz = osgiManager.get().tryResolveClass(bundleUrl,
entityTypeName);
+        if (!clazz.isPresent() || !Entity.class.isAssignableFrom(clazz.get())) {
+            return Maybe.absent();
+        }
+        return clazz;
     }
 
     @SuppressWarnings("unchecked")
@@ -59,6 +76,6 @@ public class BrooklynEntityClassResolver {
             return Maybe.absent(new Throwable("Could not find "+typeName+" on classpath"));
         }
 
-        return Maybe.<Class<T>>of(clazz);
+        return Maybe.of(clazz);
     }
 }


Mime
View raw message