brooklyn-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From s...@apache.org
Subject [6/8] brooklyn-server git commit: Persistence hacks
Date Mon, 11 Jul 2016 12:45:52 GMT
Persistence hacks


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

Branch: refs/heads/master
Commit: a4930f02734c80846ae6c7a7469ead9a145ccd5b
Parents: c3dc6e8
Author: Svetoslav Neykov <svetoslav.neykov@cloudsoftcorp.com>
Authored: Wed Jul 6 17:37:19 2016 +0300
Committer: Aled Sage <aled.sage@gmail.com>
Committed: Sat Jul 9 10:58:36 2016 +0100

----------------------------------------------------------------------
 .../core/mgmt/persist/XmlMementoSerializer.java | 16 +++++++++++-
 .../core/mgmt/rebind/RebindIteration.java       | 26 +++++++++++++++++---
 .../brooklyn/util/core/ClassLoaderUtils.java    | 20 +++++++++------
 .../src/main/resources/etc/default.catalog.bom  |  2 +-
 4 files changed, 52 insertions(+), 12 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/a4930f02/core/src/main/java/org/apache/brooklyn/core/mgmt/persist/XmlMementoSerializer.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/brooklyn/core/mgmt/persist/XmlMementoSerializer.java
b/core/src/main/java/org/apache/brooklyn/core/mgmt/persist/XmlMementoSerializer.java
index e9085dc..58df79f 100644
--- a/core/src/main/java/org/apache/brooklyn/core/mgmt/persist/XmlMementoSerializer.java
+++ b/core/src/main/java/org/apache/brooklyn/core/mgmt/persist/XmlMementoSerializer.java
@@ -60,6 +60,7 @@ import org.apache.brooklyn.core.mgmt.rebind.dto.BasicLocationMemento;
 import org.apache.brooklyn.core.mgmt.rebind.dto.BasicPolicyMemento;
 import org.apache.brooklyn.core.mgmt.rebind.dto.MutableBrooklynMemento;
 import org.apache.brooklyn.core.sensor.BasicAttributeSensor;
+import org.apache.brooklyn.util.core.ClassLoaderUtils;
 import org.apache.brooklyn.util.core.xstream.XmlSerializer;
 import org.apache.brooklyn.util.exceptions.Exceptions;
 import org.apache.brooklyn.util.text.Strings;
@@ -94,10 +95,23 @@ public class XmlMementoSerializer<T> extends XmlSerializer<T>
implements Memento
         this(classLoader, DeserializingClassRenamesProvider.loadDeserializingClassRenames());
     }
     
+    private static class CustomClassLoader extends ClassLoader {
+        private ClassLoaderUtils loader;
+        private CustomClassLoader(ClassLoader cl) {
+            loader = new ClassLoaderUtils(cl);
+        }
+
+        @Override
+        protected Class<?> findClass(String name) throws ClassNotFoundException {
+            return loader.loadClass(name);
+        }
+        
+    }
+    
     public XmlMementoSerializer(ClassLoader classLoader, Map<String, String> deserializingClassRenames)
{
         super(deserializingClassRenames);
         this.classLoader = checkNotNull(classLoader, "classLoader");
-        xstream.setClassLoader(this.classLoader);
+        xstream.setClassLoader(new CustomClassLoader(this.classLoader));
         
         // old (deprecated in 070? or earlier) single-file persistence uses this keyword;
TODO remove soon in 080 ?
         xstream.alias("brooklyn", MutableBrooklynMemento.class);

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/a4930f02/core/src/main/java/org/apache/brooklyn/core/mgmt/rebind/RebindIteration.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/brooklyn/core/mgmt/rebind/RebindIteration.java
b/core/src/main/java/org/apache/brooklyn/core/mgmt/rebind/RebindIteration.java
index 6c0bd9d..1f1ac2e 100644
--- a/core/src/main/java/org/apache/brooklyn/core/mgmt/rebind/RebindIteration.java
+++ b/core/src/main/java/org/apache/brooklyn/core/mgmt/rebind/RebindIteration.java
@@ -91,10 +91,12 @@ import org.apache.brooklyn.core.objs.proxy.InternalPolicyFactory;
 import org.apache.brooklyn.core.policy.AbstractPolicy;
 import org.apache.brooklyn.util.collections.MutableList;
 import org.apache.brooklyn.util.collections.MutableMap;
+import org.apache.brooklyn.util.core.ClassLoaderUtils;
 import org.apache.brooklyn.util.core.flags.FlagUtils;
 import org.apache.brooklyn.util.exceptions.Exceptions;
 import org.apache.brooklyn.util.guava.Maybe;
 import org.apache.brooklyn.util.javalang.Reflections;
+import org.apache.brooklyn.util.javalang.Reflections.ReflectionNotFoundException;
 import org.apache.brooklyn.util.text.Strings;
 import org.apache.brooklyn.util.time.Duration;
 import org.apache.brooklyn.util.time.Time;
@@ -956,7 +958,7 @@ public abstract class RebindIteration {
             }
             
             try {
-                return new LoadedClass<T>((Class<T>)reflections.loadClass(jType),
catalogItemId);
+                return new LoadedClass<T>((Class<T>)loadClass(jType), catalogItemId);
             } catch (Exception e) {
                 Exceptions.propagateIfFatal(e);
                 LOG.warn("Unable to load "+jType+" using reflections; will try standard context");
@@ -980,11 +982,29 @@ public abstract class RebindIteration {
             }
         }
 
+        protected Class<?> loadClass(String jType) throws ClassNotFoundException {
+            try {
+            return reflections.loadClass(jType);
+            } catch (Exception e) {
+                Exceptions.propagateIfFatal(e);
+            }
+            return new ClassLoaderUtils(reflections.getClassLoader()).loadClass(jType);
+        }
+
+        @SuppressWarnings("unchecked")
+        public <T> Class<? extends T> loadClass(String classname, Class<T>
superType) {
+            try {
+                return (Class<? extends T>) loadClass(classname);
+            } catch (ClassNotFoundException e) {
+                throw Exceptions.propagate(e);
+            }
+        }
+
         /**
          * Constructs a new location, passing to its constructor the location id and all
of memento.getFlags().
          */
         protected Location newLocation(String locationId, String locationType) {
-            Class<? extends Location> locationClazz = reflections.loadClass(locationType,
Location.class);
+            Class<? extends Location> locationClazz = loadClass(locationType, Location.class);
 
             if (InternalFactory.isNewStyle(locationClazz)) {
                 // Not using loationManager.createLocation(LocationSpec) because don't want
init() to be called
@@ -1110,7 +1130,7 @@ public abstract class RebindIteration {
             String id = memento.getId();
             // catalog item subtypes are internal to brooklyn, not in osgi
             String itemType = checkNotNull(memento.getType(), "catalog item type of %s must
not be null in memento", id);
-            Class<? extends CatalogItem> clazz = reflections.loadClass(itemType, CatalogItem.class);
+            Class<? extends CatalogItem> clazz = loadClass(itemType, CatalogItem.class);
             return invokeConstructor(reflections, clazz, new Object[]{});
         }
 

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/a4930f02/core/src/main/java/org/apache/brooklyn/util/core/ClassLoaderUtils.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/brooklyn/util/core/ClassLoaderUtils.java b/core/src/main/java/org/apache/brooklyn/util/core/ClassLoaderUtils.java
index 644f07a..00fd05f 100644
--- a/core/src/main/java/org/apache/brooklyn/util/core/ClassLoaderUtils.java
+++ b/core/src/main/java/org/apache/brooklyn/util/core/ClassLoaderUtils.java
@@ -57,7 +57,7 @@ public class ClassLoaderUtils {
 
     // Class.forName gets the class loader from the calling class.
     // We don't have access to the same reflection API so need to pass it explicitly.
-    private final Class<?> callingClass;
+    private final ClassLoader classLoader;
     private final Entity entity;
     private final ManagementContext mgmt;
 
@@ -69,19 +69,25 @@ public class ClassLoaderUtils {
     }
 
     public ClassLoaderUtils(Class<?> callingClass) {
-        this.callingClass = callingClass;
+        this.classLoader = callingClass.getClassLoader();
+        this.entity = null;
+        this.mgmt = null;
+    }
+
+    public ClassLoaderUtils(ClassLoader cl) {
+        this.classLoader = cl;
         this.entity = null;
         this.mgmt = null;
     }
 
     public ClassLoaderUtils(Class<?> callingClass, Entity entity) {
-        this.callingClass = callingClass;
+        this.classLoader = callingClass.getClassLoader();
         this.entity = entity;
         this.mgmt = ((EntityInternal)entity).getManagementContext();
     }
 
     public ClassLoaderUtils(Class<?> callingClass, @Nullable ManagementContext mgmt)
{
-        this.callingClass = callingClass;
+        this.classLoader = callingClass.getClassLoader();
         this.entity = null;
         this.mgmt = mgmt;
     }
@@ -132,8 +138,8 @@ public class ClassLoaderUtils {
         }
 
         try {
-            // Used instead of callingClass.getClassLoader() as it could be null (only for
bootstrap classes)
-            return Class.forName(name, true, callingClass.getClassLoader());
+            // Used instead of callingClass.getClassLoader().loadClass(...) as it could be
null (only for bootstrap classes)
+            return Class.forName(name, true, classLoader);
         } catch (ClassNotFoundException e) {
         }
 
@@ -171,7 +177,7 @@ public class ClassLoaderUtils {
             }
             return SystemFrameworkLoader.get().loadClassFromBundle(className, bundle.get());
         } else {
-            return Class.forName(className);
+            return Class.forName(className, true, classLoader);
         }
     }
 

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/a4930f02/karaf/apache-brooklyn/src/main/resources/etc/default.catalog.bom
----------------------------------------------------------------------
diff --git a/karaf/apache-brooklyn/src/main/resources/etc/default.catalog.bom b/karaf/apache-brooklyn/src/main/resources/etc/default.catalog.bom
index 8184c6e..e1cfe99 100644
--- a/karaf/apache-brooklyn/src/main/resources/etc/default.catalog.bom
+++ b/karaf/apache-brooklyn/src/main/resources/etc/default.catalog.bom
@@ -7,7 +7,7 @@ brooklyn.catalog:
   items:
   - brooklyn.libraries:
     - name: org.apache.brooklyn.karaf-init
-      version: 0.10.0.SNAPSHOT # BROOKLYN_VERSION
+      version: "0.10.0.SNAPSHOT" # BROOKLYN_VERSION
     include: classpath://catalog-classes.bom
 
   - id: server


Mime
View raw message