brooklyn-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sjcorb...@apache.org
Subject [1/4] git commit: Move out newClassLoadingContext out of the base API to a helper method.
Date Fri, 10 Oct 2014 11:27:02 GMT
Repository: incubator-brooklyn
Updated Branches:
  refs/heads/master ab0451175 -> c364331d7


Move out newClassLoadingContext out of the base API to a helper method.

Solving the following problems:
  * Doesn't need to be a base API, using only publicly available data
  * Wasn't really clear what we get depending on whether called from a CatalogItemDo, CatalogItemDtoAbstract,
with null or non-null mgmt argument, due to the recursive nature of the creation.
  * We were getting loads of nested sequential loaders due to the recursive nature of building
them.
  * Not possible to create a loader without access to an existing CatalogItem (which is the
trigger cause for the changes, preparing the ground for the next commit)


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

Branch: refs/heads/master
Commit: 48eb5cb10a50538161f04bdfc84045f09687aa5f
Parents: ab04511
Author: Svetoslav Neykov <svetoslav.neykov@cloudsoftcorp.com>
Authored: Wed Oct 1 14:02:52 2014 +0300
Committer: Svetoslav Neykov <svetoslav.neykov@cloudsoftcorp.com>
Committed: Fri Oct 10 13:48:38 2014 +0300

----------------------------------------------------------------------
 .../main/java/brooklyn/catalog/CatalogItem.java |  9 +--
 .../brooklyn/catalog/internal/CatalogDo.java    |  7 +-
 .../catalog/internal/CatalogItemDo.java         | 12 +--
 .../internal/CatalogItemDtoAbstract.java        | 31 ++------
 .../brooklyn/catalog/internal/CatalogUtils.java | 80 ++++++++++++++++++++
 .../BrooklynAssemblyTemplateInstantiator.java   |  3 +-
 .../BrooklynComponentTemplateResolver.java      |  3 +-
 .../rest/resources/CatalogResource.java         |  3 +-
 8 files changed, 96 insertions(+), 52 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/48eb5cb1/api/src/main/java/brooklyn/catalog/CatalogItem.java
----------------------------------------------------------------------
diff --git a/api/src/main/java/brooklyn/catalog/CatalogItem.java b/api/src/main/java/brooklyn/catalog/CatalogItem.java
index 319ee1f..3a6c2fb 100644
--- a/api/src/main/java/brooklyn/catalog/CatalogItem.java
+++ b/api/src/main/java/brooklyn/catalog/CatalogItem.java
@@ -19,18 +19,17 @@
 package brooklyn.catalog;
 
 import java.util.List;
+
 import javax.annotation.Nonnull;
 import javax.annotation.Nullable;
 
-import com.google.common.annotations.Beta;
-
 import brooklyn.basic.BrooklynObject;
 import brooklyn.entity.rebind.RebindSupport;
 import brooklyn.entity.rebind.Rebindable;
-import brooklyn.management.ManagementContext;
-import brooklyn.management.classloading.BrooklynClassLoadingContext;
 import brooklyn.mementos.CatalogItemMemento;
 
+import com.google.common.annotations.Beta;
+
 @Beta
 public interface CatalogItem<T,SpecT> extends BrooklynObject, Rebindable {
     
@@ -76,8 +75,6 @@ public interface CatalogItem<T,SpecT> extends BrooklynObject, Rebindable
{
     /** @return The underlying YAML for this item, if known */
     @Nullable public String getPlanYaml();
 
-    BrooklynClassLoadingContext newClassLoadingContext(final ManagementContext mgmt);
-
     @Override
     RebindSupport<CatalogItemMemento> getRebindSupport();
 }

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/48eb5cb1/core/src/main/java/brooklyn/catalog/internal/CatalogDo.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/brooklyn/catalog/internal/CatalogDo.java b/core/src/main/java/brooklyn/catalog/internal/CatalogDo.java
index 6abb670..bfbf799 100644
--- a/core/src/main/java/brooklyn/catalog/internal/CatalogDo.java
+++ b/core/src/main/java/brooklyn/catalog/internal/CatalogDo.java
@@ -29,8 +29,6 @@ import org.slf4j.LoggerFactory;
 
 import brooklyn.catalog.internal.CatalogClasspathDo.CatalogScanningModes;
 import brooklyn.management.ManagementContext;
-import brooklyn.management.classloading.BrooklynClassLoadingContext;
-import brooklyn.management.classloading.JavaBrooklynClassLoadingContext;
 import brooklyn.management.internal.ManagementContextInternal;
 import brooklyn.util.exceptions.Exceptions;
 import brooklyn.util.javalang.AggregateClassLoader;
@@ -346,8 +344,5 @@ public class CatalogDo {
         if (parent != null) return parent.getRootClassLoader();
         return getRecursiveClassLoader();
     }
-    
-    public BrooklynClassLoadingContext newClassLoadingContext() {
-        return new JavaBrooklynClassLoadingContext(mgmt, getRootClassLoader());
-    }
+
 }

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/48eb5cb1/core/src/main/java/brooklyn/catalog/internal/CatalogItemDo.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/brooklyn/catalog/internal/CatalogItemDo.java b/core/src/main/java/brooklyn/catalog/internal/CatalogItemDo.java
index 0471d55..1c0d276 100644
--- a/core/src/main/java/brooklyn/catalog/internal/CatalogItemDo.java
+++ b/core/src/main/java/brooklyn/catalog/internal/CatalogItemDo.java
@@ -24,8 +24,6 @@ import javax.annotation.Nullable;
 import brooklyn.catalog.CatalogItem;
 import brooklyn.entity.rebind.RebindSupport;
 import brooklyn.management.ManagementContext;
-import brooklyn.management.classloading.BrooklynClassLoadingContext;
-import brooklyn.management.classloading.BrooklynClassLoadingContextSequential;
 
 import com.google.common.base.Preconditions;
 
@@ -115,18 +113,10 @@ public class CatalogItemDo<T,SpecT> implements CatalogItem<T,SpecT>
{
         return javaClass;
     }
     
-    @Override
-    public BrooklynClassLoadingContext newClassLoadingContext(final ManagementContext mgmt)
{
-        BrooklynClassLoadingContextSequential result = new BrooklynClassLoadingContextSequential(mgmt);
-        result.add(itemDto.newClassLoadingContext(mgmt));
-        result.addSecondary(catalog.newClassLoadingContext());
-        return result;
-    }
-    
     @SuppressWarnings("unchecked")
     Class<? extends T> loadJavaClass(final ManagementContext mgmt) {
         if (javaClass!=null) return javaClass;
-        javaClass = (Class<T>)newClassLoadingContext(mgmt).loadClass(getJavaType());
+        javaClass = (Class<T>)CatalogUtils.newClassLoadingContext(mgmt, getLibraries(),
catalog.getRootClassLoader()).loadClass(getJavaType());
         return javaClass;
     }
 

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/48eb5cb1/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 4a9d5a6..e5fb139 100644
--- a/core/src/main/java/brooklyn/catalog/internal/CatalogItemDtoAbstract.java
+++ b/core/src/main/java/brooklyn/catalog/internal/CatalogItemDtoAbstract.java
@@ -20,27 +20,22 @@ package brooklyn.catalog.internal;
 
 import java.util.Map;
 import java.util.Set;
+
 import javax.annotation.Nonnull;
 import javax.annotation.Nullable;
 
-import com.google.common.collect.ImmutableSet;
-import com.google.common.collect.Iterables;
-import com.google.common.collect.Sets;
-
 import brooklyn.basic.AbstractBrooklynObject;
 import brooklyn.catalog.CatalogItem;
-import brooklyn.catalog.internal.BasicBrooklynCatalog.BrooklynLoaderTracker;
 import brooklyn.entity.rebind.BasicCatalogItemRebindSupport;
 import brooklyn.entity.rebind.RebindSupport;
-import brooklyn.management.ManagementContext;
-import brooklyn.management.classloading.BrooklynClassLoadingContext;
-import brooklyn.management.classloading.BrooklynClassLoadingContextSequential;
-import brooklyn.management.classloading.JavaBrooklynClassLoadingContext;
-import brooklyn.management.classloading.OsgiBrooklynClassLoadingContext;
 import brooklyn.mementos.CatalogItemMemento;
 import brooklyn.util.flags.FlagUtils;
 import brooklyn.util.flags.SetFromFlag;
 
+import com.google.common.collect.ImmutableSet;
+import com.google.common.collect.Iterables;
+import com.google.common.collect.Sets;
+
 public abstract class CatalogItemDtoAbstract<T, SpecT> extends AbstractBrooklynObject
implements CatalogItem<T, SpecT> {
 
     // TODO are ID and registeredType the same?
@@ -145,22 +140,6 @@ public abstract class CatalogItemDtoAbstract<T, SpecT> extends
AbstractBrooklynO
     }
 
     @Override
-    public BrooklynClassLoadingContext newClassLoadingContext(final ManagementContext mgmt)
{
-        BrooklynClassLoadingContextSequential result = new BrooklynClassLoadingContextSequential(mgmt);
-
-        if (getLibraries()!=null && getLibraries().getBundles()!=null &&
!getLibraries().getBundles().isEmpty())
-            // TODO getLibraries() should never be null but sometimes it is still
-            // e.g. run CatalogResourceTest without the above check
-            result.add(new OsgiBrooklynClassLoadingContext(mgmt, getLibraries().getBundles()));
-
-        BrooklynClassLoadingContext next = BrooklynLoaderTracker.getLoader();
-        if (next==null) next = JavaBrooklynClassLoadingContext.newDefault(mgmt);
-        result.add(next);
-
-        return result;
-    }
-
-    @Override
     public RebindSupport<CatalogItemMemento> getRebindSupport() {
         return new BasicCatalogItemRebindSupport(this);
     }

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/48eb5cb1/core/src/main/java/brooklyn/catalog/internal/CatalogUtils.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/brooklyn/catalog/internal/CatalogUtils.java b/core/src/main/java/brooklyn/catalog/internal/CatalogUtils.java
new file mode 100644
index 0000000..523b31d
--- /dev/null
+++ b/core/src/main/java/brooklyn/catalog/internal/CatalogUtils.java
@@ -0,0 +1,80 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package brooklyn.catalog.internal;
+
+import java.util.List;
+
+import javax.annotation.Nullable;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.base.Joiner;
+import com.google.common.base.Stopwatch;
+
+import brooklyn.catalog.CatalogItem;
+import brooklyn.catalog.CatalogItem.CatalogItemLibraries;
+import brooklyn.catalog.internal.BasicBrooklynCatalog.BrooklynLoaderTracker;
+import brooklyn.management.ManagementContext;
+import brooklyn.management.classloading.BrooklynClassLoadingContext;
+import brooklyn.management.classloading.BrooklynClassLoadingContextSequential;
+import brooklyn.management.classloading.JavaBrooklynClassLoadingContext;
+import brooklyn.management.classloading.OsgiBrooklynClassLoadingContext;
+import brooklyn.management.ha.OsgiManager;
+import brooklyn.management.internal.ManagementContextInternal;
+import brooklyn.util.guava.Maybe;
+import brooklyn.util.time.Time;
+
+public class CatalogUtils {
+    private static final Logger log = LoggerFactory.getLogger(CatalogUtils.class);
+
+    public static BrooklynClassLoadingContext newClassLoadingContext(ManagementContext mgmt,
CatalogItem<?, ?> item) {
+        CatalogItemLibraries libraries = item.getLibraries();
+        // TODO getLibraries() should never be null but sometimes it is still
+        // e.g. run CatalogResourceTest without the above check
+        if (libraries == null) {
+            log.debug("CatalogItemDtoAbstract.getLibraries() is null.", new Exception("Trace
for null CatalogItemDtoAbstract.getLibraries()"));
+        }
+        return newClassLoadingContext(mgmt, libraries);
+    }
+
+    public static BrooklynClassLoadingContext newClassLoadingContext(@Nullable ManagementContext
mgmt, CatalogItemLibraries libraries) {
+        return newClassLoadingContext(mgmt, libraries, mgmt.getCatalog().getRootClassLoader());
+    }
+
+    public static BrooklynClassLoadingContext newClassLoadingContext(@Nullable ManagementContext
mgmt, CatalogItemLibraries libraries, ClassLoader classLoader) {
+        BrooklynClassLoadingContextSequential result = new BrooklynClassLoadingContextSequential(mgmt);
+
+        if (libraries!=null) {
+            List<String> bundles = libraries.getBundles();
+            if (bundles!=null && !bundles.isEmpty()) {
+                result.add(new OsgiBrooklynClassLoadingContext(mgmt, bundles));
+            }
+        }
+
+        BrooklynClassLoadingContext loader = BrooklynLoaderTracker.getLoader();
+        if (loader != null) {
+            result.add(loader);
+        }
+
+        result.addSecondary(new JavaBrooklynClassLoadingContext(mgmt, classLoader));
+        return result;
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/48eb5cb1/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 5e1a8ca..ca8e750 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
@@ -42,6 +42,7 @@ import org.slf4j.LoggerFactory;
 import brooklyn.camp.brooklyn.api.AssemblyTemplateSpecInstantiator;
 import brooklyn.camp.brooklyn.api.HasBrooklynManagementContext;
 import brooklyn.catalog.CatalogItem;
+import brooklyn.catalog.internal.CatalogUtils;
 import brooklyn.catalog.internal.BasicBrooklynCatalog.BrooklynLoaderTracker;
 import brooklyn.config.BrooklynServerConfig;
 import brooklyn.entity.Application;
@@ -238,7 +239,7 @@ public class BrooklynAssemblyTemplateInstantiator implements AssemblyTemplateSpe
         
         String yaml = item.getPlanYaml();
         Reader input = new StringReader(yaml);
-        BrooklynClassLoadingContext itemLoader = item.newClassLoadingContext(mgmt);
+        BrooklynClassLoadingContext itemLoader = CatalogUtils.newClassLoadingContext(mgmt,
item);
         
         return resolveYamlSpec(mgmt, encounteredCatalogTypes, input, itemLoader);
     }

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/48eb5cb1/usage/camp/src/main/java/io/brooklyn/camp/brooklyn/spi/creation/BrooklynComponentTemplateResolver.java
----------------------------------------------------------------------
diff --git a/usage/camp/src/main/java/io/brooklyn/camp/brooklyn/spi/creation/BrooklynComponentTemplateResolver.java
b/usage/camp/src/main/java/io/brooklyn/camp/brooklyn/spi/creation/BrooklynComponentTemplateResolver.java
index 323e1a5..2e407da 100644
--- a/usage/camp/src/main/java/io/brooklyn/camp/brooklyn/spi/creation/BrooklynComponentTemplateResolver.java
+++ b/usage/camp/src/main/java/io/brooklyn/camp/brooklyn/spi/creation/BrooklynComponentTemplateResolver.java
@@ -37,6 +37,7 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
 import brooklyn.catalog.CatalogItem;
+import brooklyn.catalog.internal.CatalogUtils;
 import brooklyn.config.ConfigKey;
 import brooklyn.entity.Application;
 import brooklyn.entity.Entity;
@@ -218,7 +219,7 @@ public class BrooklynComponentTemplateResolver {
         
         if (item!=null) {
             // add additional bundles
-            loader = new BrooklynClassLoadingContextSequential(mgmt, item.newClassLoadingContext(mgmt),
loader);
+            loader = new BrooklynClassLoadingContextSequential(mgmt, CatalogUtils.newClassLoadingContext(mgmt,
item), loader);
             
             if (item.getJavaType() != null) {
                 typeName = item.getJavaType();

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/48eb5cb1/usage/rest-server/src/main/java/brooklyn/rest/resources/CatalogResource.java
----------------------------------------------------------------------
diff --git a/usage/rest-server/src/main/java/brooklyn/rest/resources/CatalogResource.java
b/usage/rest-server/src/main/java/brooklyn/rest/resources/CatalogResource.java
index e926064..53ef4cf 100644
--- a/usage/rest-server/src/main/java/brooklyn/rest/resources/CatalogResource.java
+++ b/usage/rest-server/src/main/java/brooklyn/rest/resources/CatalogResource.java
@@ -37,6 +37,7 @@ import brooklyn.catalog.CatalogItem;
 import brooklyn.catalog.CatalogPredicates;
 import brooklyn.catalog.internal.BasicBrooklynCatalog;
 import brooklyn.catalog.internal.CatalogDto;
+import brooklyn.catalog.internal.CatalogUtils;
 import brooklyn.entity.Entity;
 import brooklyn.entity.proxying.EntitySpec;
 import brooklyn.policy.Policy;
@@ -209,7 +210,7 @@ public class CatalogResource extends AbstractBrooklynRestResource implements
Cat
             
             MediaType mime = WebResourceUtils.getImageMediaTypeFromExtension(Files.getFileExtension(url));
             try {
-                Object content = ResourceUtils.create(result.newClassLoadingContext(mgmt())).getResourceFromUrl(url);
+                Object content = ResourceUtils.create(CatalogUtils.newClassLoadingContext(mgmt(),
result)).getResourceFromUrl(url);
                 return Response.ok(content, mime).build();
             } catch (Exception e) {
                 Exceptions.propagateIfFatal(e);


Mime
View raw message