brooklyn-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From aleds...@apache.org
Subject [2/9] git commit: Fix JavaWebAppWithDslYamlRebindIntegrationTest
Date Fri, 31 Oct 2014 14:10:07 GMT
Fix JavaWebAppWithDslYamlRebindIntegrationTest

- support serialising/deserializing JavaBrooklynClassLoadingContext
  for “common case” of constructing with ManagementContext and no
  custom ClassLoader.


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

Branch: refs/heads/master
Commit: 84f522698ad74faa42d5d4d2ee3ef2d8f9c2780d
Parents: 92c1972
Author: Aled Sage <aled.sage@gmail.com>
Authored: Fri Oct 31 09:10:55 2014 +0000
Committer: Aled Sage <aled.sage@gmail.com>
Committed: Fri Oct 31 11:05:36 2014 +0000

----------------------------------------------------------------------
 .../brooklyn/catalog/internal/CatalogUtils.java |  2 +-
 .../JavaBrooklynClassLoadingContext.java        | 50 ++++++++++++++------
 .../main/java/brooklyn/util/ResourceUtils.java  |  4 +-
 3 files changed, 39 insertions(+), 17 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/84f52269/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
index 31a519e..e6a4a12 100644
--- a/core/src/main/java/brooklyn/catalog/internal/CatalogUtils.java
+++ b/core/src/main/java/brooklyn/catalog/internal/CatalogUtils.java
@@ -73,7 +73,7 @@ public class CatalogUtils {
             result.add(loader);
         }
 
-        result.addSecondary(new JavaBrooklynClassLoadingContext(mgmt, classLoader));
+        result.addSecondary(JavaBrooklynClassLoadingContext.create(mgmt, classLoader));
         return result;
     }
 

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/84f52269/core/src/main/java/brooklyn/management/classloading/JavaBrooklynClassLoadingContext.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/brooklyn/management/classloading/JavaBrooklynClassLoadingContext.java
b/core/src/main/java/brooklyn/management/classloading/JavaBrooklynClassLoadingContext.java
index ac58c79..2ecd229 100644
--- a/core/src/main/java/brooklyn/management/classloading/JavaBrooklynClassLoadingContext.java
+++ b/core/src/main/java/brooklyn/management/classloading/JavaBrooklynClassLoadingContext.java
@@ -18,34 +18,56 @@
  */
 package brooklyn.management.classloading;
 
-import java.net.URL;
+import static com.google.common.base.Preconditions.checkNotNull;
+import static com.google.common.base.Preconditions.checkState;
 
-import com.google.common.base.Objects;
+import java.net.URL;
 
 import brooklyn.management.ManagementContext;
 import brooklyn.util.exceptions.Exceptions;
 import brooklyn.util.guava.Maybe;
 
+import com.google.common.base.Objects;
+
 public class JavaBrooklynClassLoadingContext extends AbstractBrooklynClassLoadingContext
{
 
     private final ClassLoader loader;
 
-    public JavaBrooklynClassLoadingContext(ManagementContext mgmt, ClassLoader loader) {
-        super(mgmt);
-        this.loader = loader;
+    public static JavaBrooklynClassLoadingContext create(ClassLoader loader) {
+        return new JavaBrooklynClassLoadingContext(null, checkNotNull(loader, "loader"));
+    }
+    
+    /**
+     * At least one of mgmt or loader must not be null.
+     */
+    public static JavaBrooklynClassLoadingContext create(ManagementContext mgmt, ClassLoader
loader) {
+        checkState(mgmt != null || loader != null, "mgmt and loader must not both be null");
+        return new JavaBrooklynClassLoadingContext(mgmt, loader);
     }
     
     public static JavaBrooklynClassLoadingContext newDefault(ManagementContext mgmt) {
-        ClassLoader cl = null;
-        if (mgmt!=null) cl = mgmt.getCatalog().getRootClassLoader();
-        if (cl==null) cl = JavaBrooklynClassLoadingContext.class.getClassLoader();
-        return new JavaBrooklynClassLoadingContext(mgmt, cl);
+        return new JavaBrooklynClassLoadingContext(checkNotNull(mgmt, "mgmt"), null);
     }
 
+    private JavaBrooklynClassLoadingContext(ManagementContext mgmt, ClassLoader loader) {
+        super(mgmt);
+        this.loader = loader;
+    }
+    
+    // TODO Ugly workaround for rebind, where the classLoader cannot be serialized/deserialized.
+    // If we're supplying just mgmt (i.e. via {@link #newDefault(ManagementContext)} then
can retrieve
+    // class loader from that.
+    // Will not work if trying to serialize/deserialize an instance created with a specific
ClassLoader.
+    private ClassLoader getClassLoader() {
+        if (loader != null) return loader;
+        if (mgmt!=null) return mgmt.getCatalog().getRootClassLoader();
+        return JavaBrooklynClassLoadingContext.class.getClassLoader();
+    }
+    
     @SuppressWarnings({ "rawtypes", "unchecked" })
     public Maybe<Class<?>> tryLoadClass(String className) {
         try {
-            return (Maybe) Maybe.of(loader.loadClass(className));
+            return (Maybe) Maybe.of(getClassLoader().loadClass(className));
         } catch (Exception e) {
             Exceptions.propagateIfFatal(e);
             return Maybe.absent("Invalid class: "+className, e);
@@ -54,25 +76,25 @@ public class JavaBrooklynClassLoadingContext extends AbstractBrooklynClassLoadin
 
     @Override
     public String toString() {
-        return "java:"+loader;
+        return "java:"+getClassLoader();
     }
     
     @Override
     public int hashCode() {
-        return Objects.hashCode(super.hashCode(), loader);
+        return Objects.hashCode(super.hashCode(), getClassLoader());
     }
     
     @Override
     public boolean equals(Object obj) {
         if (!super.equals(obj)) return false;
         if (!(obj instanceof JavaBrooklynClassLoadingContext)) return false;
-        if (!Objects.equal(loader, ((JavaBrooklynClassLoadingContext)obj).loader)) return
false;
+        if (!Objects.equal(getClassLoader(), ((JavaBrooklynClassLoadingContext)obj).getClassLoader()))
return false;
         return true;
     }
 
     @Override
     public URL getResource(String name) {
-        return loader.getResource(name);
+        return getClassLoader().getResource(name);
     }
     
 }

http://git-wip-us.apache.org/repos/asf/incubator-brooklyn/blob/84f52269/core/src/main/java/brooklyn/util/ResourceUtils.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/brooklyn/util/ResourceUtils.java b/core/src/main/java/brooklyn/util/ResourceUtils.java
index ba704e0..072789e 100644
--- a/core/src/main/java/brooklyn/util/ResourceUtils.java
+++ b/core/src/main/java/brooklyn/util/ResourceUtils.java
@@ -146,7 +146,7 @@ public class ResourceUtils {
     }
 
     public ResourceUtils(ClassLoader loader, Object contextObject, String contextMessage)
{
-        this(new JavaBrooklynClassLoadingContext(null, loader), contextObject, contextMessage);
+        this(JavaBrooklynClassLoadingContext.create(loader), contextObject, contextMessage);
     }
     
     public ResourceUtils(BrooklynClassLoadingContext loader, Object contextObject, String
contextMessage) {
@@ -187,7 +187,7 @@ public class ResourceUtils {
         ManagementContext mgmt = null;
         BrooklynClassLoadingContext bl = BrooklynLoaderTracker.getLoader();
         if (bl!=null) mgmt = bl.getManagementContext();
-        return new JavaBrooklynClassLoadingContext(mgmt, loader);
+        return JavaBrooklynClassLoadingContext.create(mgmt, loader);
     }
     
     public BrooklynClassLoadingContext getLoader() {


Mime
View raw message