brooklyn-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From s...@apache.org
Subject [4/6] brooklyn-server git commit: BROOKLYN-379: entity type includes OSGi prefix
Date Tue, 29 Nov 2016 08:21:53 GMT
BROOKLYN-379: entity type includes OSGi prefix

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

Branch: refs/heads/master
Commit: a72d1f20ed78504a9b690df1467465b781ac917a
Parents: cd9447a
Author: Aled Sage <aled.sage@gmail.com>
Authored: Mon Nov 7 17:11:45 2016 +0000
Committer: Aled Sage <aled.sage@gmail.com>
Committed: Fri Nov 25 23:28:19 2016 +0000

----------------------------------------------------------------------
 .../core/mgmt/persist/OsgiClassPrefixer.java    | 51 ++++++++++++++++++++
 .../core/mgmt/persist/XmlMementoSerializer.java | 15 ++----
 .../mgmt/rebind/dto/MementosGenerators.java     |  6 ++-
 3 files changed, 61 insertions(+), 11 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/a72d1f20/core/src/main/java/org/apache/brooklyn/core/mgmt/persist/OsgiClassPrefixer.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/brooklyn/core/mgmt/persist/OsgiClassPrefixer.java
b/core/src/main/java/org/apache/brooklyn/core/mgmt/persist/OsgiClassPrefixer.java
new file mode 100644
index 0000000..a76c02a
--- /dev/null
+++ b/core/src/main/java/org/apache/brooklyn/core/mgmt/persist/OsgiClassPrefixer.java
@@ -0,0 +1,51 @@
+/*
+ * 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 org.apache.brooklyn.core.mgmt.persist;
+
+import org.apache.brooklyn.core.mgmt.rebind.dto.MementosGenerators;
+import org.apache.brooklyn.util.core.ClassLoaderUtils;
+import org.apache.brooklyn.util.core.osgi.Osgis;
+import org.osgi.framework.Bundle;
+
+import com.google.common.base.Optional;
+
+/**
+ * For serialize/deserialize of persisted state, we need to prefix classes with their OSGi
bundle 
+ * (so we know which classloader to use when deserializing). In most cases, this can be done
when
+ * serializing the instances. However, in some cases the field of a memento is the String
representation  
+ * of the type (e.g. see (@link {@link org.apache.brooklyn.core.mgmt.rebind.dto.AbstractMemento#getType()}).
+ * In that situation, the code writing the field must include the prefix (e.g. see
+ * {@link MementosGenerators}.
+ */
+public class OsgiClassPrefixer {
+
+    private final ClassLoaderUtils whiteListRetriever;
+    
+    public OsgiClassPrefixer() {
+        whiteListRetriever = new ClassLoaderUtils(getClass());
+    }
+    
+    public Optional<String> getPrefix(Class<?> type) {
+        Optional<Bundle> bundle  = Osgis.getBundleOf(type);
+        if (bundle.isPresent() && !whiteListRetriever.isBundleWhiteListed(bundle.get()))
{
+            return Optional.of(bundle.get().getSymbolicName() + ":");
+        }
+        return Optional.absent();
+    }
+}

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/a72d1f20/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 15b3346..891e042 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
@@ -61,11 +61,9 @@ 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.osgi.Osgis;
 import org.apache.brooklyn.util.core.xstream.XmlSerializer;
 import org.apache.brooklyn.util.exceptions.Exceptions;
 import org.apache.brooklyn.util.text.Strings;
-import org.osgi.framework.Bundle;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -76,14 +74,11 @@ import com.thoughtworks.xstream.converters.MarshallingContext;
 import com.thoughtworks.xstream.converters.SingleValueConverter;
 import com.thoughtworks.xstream.converters.UnmarshallingContext;
 import com.thoughtworks.xstream.converters.reflection.ReflectionConverter;
-import com.thoughtworks.xstream.core.ClassLoaderReference;
 import com.thoughtworks.xstream.core.ReferencingMarshallingContext;
-import com.thoughtworks.xstream.core.util.Primitives;
 import com.thoughtworks.xstream.io.HierarchicalStreamReader;
 import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
 import com.thoughtworks.xstream.io.path.PathTrackingReader;
 import com.thoughtworks.xstream.mapper.CannotResolveClassException;
-import com.thoughtworks.xstream.mapper.DefaultMapper;
 import com.thoughtworks.xstream.mapper.Mapper;
 import com.thoughtworks.xstream.mapper.MapperWrapper;
 
@@ -407,11 +402,11 @@ public class XmlMementoSerializer<T> extends XmlSerializer<T>
implements Memento
     }
 
     public class OsgiClassnameMapper extends MapperWrapper {
-        private final ClassLoaderUtils whiteListRetriever;
+        private final OsgiClassPrefixer prefixer;
         
         OsgiClassnameMapper(MapperWrapper mapper) {
             super(mapper);
-            whiteListRetriever = new ClassLoaderUtils(getClass());
+            prefixer = new OsgiClassPrefixer();
         }
         
         @Override
@@ -420,9 +415,9 @@ public class XmlMementoSerializer<T> extends XmlSerializer<T>
implements Memento
             // For example the "outer class renaming stuff"?!
             String superResult = super.serializedClass(type);
             if (type != null && type.getName().equals(superResult)) {
-                Optional<Bundle> bundle  = Osgis.getBundleOf(type);
-                if (bundle.isPresent() && !whiteListRetriever.isBundleWhiteListed(bundle.get()))
{
-                    return bundle.get().getSymbolicName() + ":" + superResult;
+                Optional<String> prefix = prefixer.getPrefix(type);
+                if (prefix.isPresent()) {
+                    return prefix.get() + superResult;
                 }
             }
             return superResult;

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/a72d1f20/core/src/main/java/org/apache/brooklyn/core/mgmt/rebind/dto/MementosGenerators.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/brooklyn/core/mgmt/rebind/dto/MementosGenerators.java
b/core/src/main/java/org/apache/brooklyn/core/mgmt/rebind/dto/MementosGenerators.java
index 33d38a1..debc381 100644
--- a/core/src/main/java/org/apache/brooklyn/core/mgmt/rebind/dto/MementosGenerators.java
+++ b/core/src/main/java/org/apache/brooklyn/core/mgmt/rebind/dto/MementosGenerators.java
@@ -56,6 +56,7 @@ import org.apache.brooklyn.core.entity.EntityInternal;
 import org.apache.brooklyn.core.feed.AbstractFeed;
 import org.apache.brooklyn.core.location.internal.LocationInternal;
 import org.apache.brooklyn.core.mgmt.persist.BrooklynPersistenceUtils;
+import org.apache.brooklyn.core.mgmt.persist.OsgiClassPrefixer;
 import org.apache.brooklyn.core.mgmt.rebind.AbstractBrooklynObjectRebindSupport;
 import org.apache.brooklyn.core.mgmt.rebind.TreeUtils;
 import org.apache.brooklyn.core.objs.BrooklynTypes;
@@ -70,6 +71,7 @@ import org.slf4j.LoggerFactory;
 
 import com.google.common.annotations.Beta;
 import com.google.common.base.Function;
+import com.google.common.base.Optional;
 import com.google.common.base.Predicates;
 import com.google.common.collect.Sets;
 
@@ -441,11 +443,13 @@ public class MementosGenerators {
         if (Proxy.isProxyClass(instance.getClass())) {
             throw new IllegalStateException("Attempt to create memento from proxy "+instance+"
(would fail with wrong type)");
         }
+        OsgiClassPrefixer prefixer = new OsgiClassPrefixer();
+        Optional<String> typePrefix = prefixer.getPrefix(instance.getClass());
         
         builder.id = instance.getId();
         builder.displayName = instance.getDisplayName();
         builder.catalogItemId = instance.getCatalogItemId();
-        builder.type = instance.getClass().getName();
+        builder.type = (typePrefix.isPresent() ? typePrefix.get() : "") + instance.getClass().getName();
         builder.typeClass = instance.getClass();
         if (instance instanceof EntityAdjunct) {
             builder.uniqueTag = ((EntityAdjunct)instance).getUniqueTag();


Mime
View raw message