brooklyn-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From s...@apache.org
Subject [1/2] brooklyn-server git commit: Allow a user to add or override deserialize configuration in karaf via a etc/deserializingClassRenames.properties file
Date Wed, 04 Jan 2017 14:18:14 GMT
Repository: brooklyn-server
Updated Branches:
  refs/heads/master a74cc061c -> 812e07911


Allow a user to add or override deserialize configuration in karaf via a etc/deserializingClassRenames.properties
file


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

Branch: refs/heads/master
Commit: a34e8ab59a0e09a3c83150c4732df2269dbba5fd
Parents: 4e69fef
Author: Thomas Bouron <thomas.bouron@cloudsoftcorp.com>
Authored: Thu Dec 15 14:04:13 2016 +0000
Committer: Thomas Bouron <thomas.bouron@cloudsoftcorp.com>
Committed: Wed Jan 4 13:58:00 2017 +0000

----------------------------------------------------------------------
 .../mgmt/persist/ClasspathConfigLoader.java     |  72 ++++++++++
 .../core/mgmt/persist/ConfigLoader.java         |  25 ++++
 .../DeserializingClassRenamesProvider.java      |  91 +++++++------
 .../launcher/osgi/OsgiConfigLoader.java         | 133 +++++++++++++++++++
 .../resources/OSGI-INF/blueprint/blueprint.xml  |  15 ++-
 5 files changed, 293 insertions(+), 43 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/a34e8ab5/core/src/main/java/org/apache/brooklyn/core/mgmt/persist/ClasspathConfigLoader.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/brooklyn/core/mgmt/persist/ClasspathConfigLoader.java
b/core/src/main/java/org/apache/brooklyn/core/mgmt/persist/ClasspathConfigLoader.java
new file mode 100644
index 0000000..229e8d0
--- /dev/null
+++ b/core/src/main/java/org/apache/brooklyn/core/mgmt/persist/ClasspathConfigLoader.java
@@ -0,0 +1,72 @@
+/*
+ * 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 java.io.IOException;
+import java.io.InputStream;
+import java.util.Enumeration;
+import java.util.Map;
+import java.util.Properties;
+
+import org.apache.brooklyn.util.core.ResourceUtils;
+import org.apache.brooklyn.util.exceptions.Exceptions;
+import org.apache.brooklyn.util.stream.Streams;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.collect.ImmutableMap;
+import com.google.common.collect.Maps;
+
+/**
+ * Loads the class-renames from the configuration file on the classpath.
+ *
+ * @see {@link #DESERIALIZING_CLASS_RENAMES_PROPERTIES_PATH}
+ */
+public class ClasspathConfigLoader implements ConfigLoader {
+    private static final Logger LOG = LoggerFactory.getLogger(ClasspathConfigLoader.class);
+    private static final String DESERIALIZING_CLASS_RENAMES_PROPERTIES_PATH = "classpath://org/apache/brooklyn/core/mgmt/persist/deserializingClassRenames.properties";
+
+
+    @Override
+    public Map<String, String> load() {
+        try {
+            InputStream resource = new ResourceUtils(DeserializingClassRenamesProvider.class).getResourceFromUrl(DESERIALIZING_CLASS_RENAMES_PROPERTIES_PATH);
+
+            try {
+                Properties props = new Properties();
+                props.load(resource);
+
+                Map<String, String> result = Maps.newLinkedHashMap();
+                for (Enumeration<?> iter = props.propertyNames(); iter.hasMoreElements();
) {
+                    String key = (String) iter.nextElement();
+                    String value = props.getProperty(key);
+                    result.put(key, value);
+                }
+                return result;
+            } catch (IOException e) {
+                throw Exceptions.propagate(e);
+            } finally {
+                Streams.closeQuietly(resource);
+            }
+        } catch (Exception e) {
+            LOG.warn("Failed to load class-renames from " + DESERIALIZING_CLASS_RENAMES_PROPERTIES_PATH
+ " (continuing)", e);
+            return ImmutableMap.<String, String>of();
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/a34e8ab5/core/src/main/java/org/apache/brooklyn/core/mgmt/persist/ConfigLoader.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/brooklyn/core/mgmt/persist/ConfigLoader.java b/core/src/main/java/org/apache/brooklyn/core/mgmt/persist/ConfigLoader.java
new file mode 100644
index 0000000..5a75227
--- /dev/null
+++ b/core/src/main/java/org/apache/brooklyn/core/mgmt/persist/ConfigLoader.java
@@ -0,0 +1,25 @@
+/*
+ * 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 java.util.Map;
+
+public interface ConfigLoader {
+    public Map<String, String> load();
+}

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/a34e8ab5/core/src/main/java/org/apache/brooklyn/core/mgmt/persist/DeserializingClassRenamesProvider.java
----------------------------------------------------------------------
diff --git a/core/src/main/java/org/apache/brooklyn/core/mgmt/persist/DeserializingClassRenamesProvider.java
b/core/src/main/java/org/apache/brooklyn/core/mgmt/persist/DeserializingClassRenamesProvider.java
index 9c8c056..5753a14 100644
--- a/core/src/main/java/org/apache/brooklyn/core/mgmt/persist/DeserializingClassRenamesProvider.java
+++ b/core/src/main/java/org/apache/brooklyn/core/mgmt/persist/DeserializingClassRenamesProvider.java
@@ -18,67 +18,74 @@
  */
 package org.apache.brooklyn.core.mgmt.persist;
 
-import java.io.IOException;
-import java.io.InputStream;
-import java.util.Enumeration;
+import java.util.List;
 import java.util.Map;
-import java.util.Properties;
 
-import org.apache.brooklyn.util.core.ResourceUtils;
-import org.apache.brooklyn.util.exceptions.Exceptions;
+import org.apache.brooklyn.util.collections.MutableMap;
 import org.apache.brooklyn.util.javalang.Reflections;
-import org.apache.brooklyn.util.stream.Streams;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 
 import com.google.common.annotations.Beta;
-import com.google.common.collect.ImmutableMap;
-import com.google.common.collect.Maps;
+import com.google.common.collect.Lists;
 
+/*
+ * This provider keeps a cache of the class-renames, which is lazily populated (see {@link
#cache}.
+ * Calling {@link #reset()} will set this cache to null, causing it to be reloaded next time
+ * it is requested.
+ *
+ * Loading the cache involves iterating over the {@link #loaders}, returning the union of
+ * the results from {@link Loader#load()}.
+ *
+ * Initially, the only loader is the basic {@link ClasspathConfigLoader}.
+ *
+ * However, when running in karaf the {@link OsgiConfigLoader} will be instantiated and added.
+ * See karaf/init/src/main/resources/OSGI-INF/blueprint/blueprint.xml
+ */
 @Beta
 public class DeserializingClassRenamesProvider {
+    private static final Logger LOG = LoggerFactory.getLogger(DeserializingClassRenamesProvider.class);
 
-    public static final String DESERIALIZING_CLASS_RENAMES_PROPERTIES_PATH = "classpath://org/apache/brooklyn/core/mgmt/persist/deserializingClassRenames.properties";
+    private static final List<ConfigLoader> loaders = Lists.newCopyOnWriteArrayList();
+    static {
+        loaders.add(new ClasspathConfigLoader());
+    }
     
     private static volatile Map<String, String> cache;
-    
+
+    public static List<ConfigLoader> getLoaders() {
+        return loaders;
+    }
+
     @Beta
     public static Map<String, String> loadDeserializingClassRenames() {
-        // Double-checked locking - got to use volatile or some such!
-        if (cache == null) {
-            synchronized (DeserializingClassRenamesProvider.class) {
-                if (cache == null) {
-                    cache = loadDeserializingClassRenamesCache();
+        synchronized (DeserializingClassRenamesProvider.class) {
+            if (cache == null) {
+                MutableMap.Builder<String, String> builder = MutableMap.<String,
String>builder();
+                for (ConfigLoader loader : loaders) {
+                    builder.putAll(loader.load());
                 }
+                cache = builder.build();
+                LOG.info("Class-renames cache loaded, size {}", cache.size());
             }
-        }
-        return cache;
-    }
-    
-    private static Map<String, String> loadDeserializingClassRenamesCache() {
-        InputStream resource = new ResourceUtils(DeserializingClassRenamesProvider.class).getResourceFromUrl(DESERIALIZING_CLASS_RENAMES_PROPERTIES_PATH);
-        if (resource != null) {
-            try {
-                Properties props = new Properties();
-                props.load(resource);
-                
-                Map<String, String> result = Maps.newLinkedHashMap();
-                for (Enumeration<?> iter = props.propertyNames(); iter.hasMoreElements();)
{
-                    String key = (String) iter.nextElement();
-                    String value = props.getProperty(key);
-                    result.put(key, value);
-                }
-                return result;
-            } catch (IOException e) {
-                throw Exceptions.propagate(e);
-            } finally {
-                Streams.closeQuietly(resource);
-            }
-        } else {
-            return ImmutableMap.<String, String>of();
+            return cache;
         }
     }
 
+    /**
+     * Handles inner classes, where the outer class has been renamed. For example:
+     * 
+     * {@code findMappedName("com.example.MyFoo$MySub")} will return {@code com.example.renamed.MyFoo$MySub},
if
+     * the renamed contains {@code com.example.MyFoo: com.example.renamed.MyFoo}.
+     */
     @Beta
     public static String findMappedName(String name) {
-        return Reflections.findMappedNameAndLog(loadDeserializingClassRenames(), name);
+        return Reflections.findMappedNameAndLog(DeserializingClassRenamesProvider.loadDeserializingClassRenames(),
name);
+    }
+
+    public static void reset() {
+        synchronized (DeserializingClassRenamesProvider.class) {
+            cache = null;
+        }
     }
 }

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/a34e8ab5/karaf/init/src/main/java/org/apache/brooklyn/launcher/osgi/OsgiConfigLoader.java
----------------------------------------------------------------------
diff --git a/karaf/init/src/main/java/org/apache/brooklyn/launcher/osgi/OsgiConfigLoader.java
b/karaf/init/src/main/java/org/apache/brooklyn/launcher/osgi/OsgiConfigLoader.java
new file mode 100644
index 0000000..5ae6f17
--- /dev/null
+++ b/karaf/init/src/main/java/org/apache/brooklyn/launcher/osgi/OsgiConfigLoader.java
@@ -0,0 +1,133 @@
+/*
+ * 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.launcher.osgi;
+
+import java.io.IOException;
+import java.util.Dictionary;
+import java.util.Enumeration;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.brooklyn.core.mgmt.persist.ConfigLoader;
+import org.apache.brooklyn.core.mgmt.persist.DeserializingClassRenamesProvider;
+import org.apache.brooklyn.util.collections.MutableMap;
+import org.apache.brooklyn.util.exceptions.Exceptions;
+import org.osgi.framework.Constants;
+import org.osgi.framework.InvalidSyntaxException;
+import org.osgi.service.cm.Configuration;
+import org.osgi.service.cm.ConfigurationAdmin;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.collect.ImmutableList;
+import com.google.common.collect.ImmutableMap;
+
+/**
+ * Loads the class-renames from the OSGi configuration file: {@code org.apache.brooklyn.classrename.cfg}.
+ *
+ * Only public for OSGi instantiation - treat as an internal class, which may change in
+ * future releases.
+ *
+ * See http://stackoverflow.com/questions/18844987/creating-a-blueprint-bean-from-an-inner-class:
+ * we unfortunately need to include {@code !org.apache.brooklyn.core.mgmt.persist.DeserializingClassRenamesProvider}
+ * in the Import-Package, as the mvn plugin gets confused due to the use of this inner class
+ * within the blueprint.xml.
+ *
+ * @see {@link #KARAF_DESERIALIZING_CLASS_RENAMES_PROPERTIES}
+ */
+public class OsgiConfigLoader implements ConfigLoader {
+    private static final Logger LOG = LoggerFactory.getLogger(OsgiConfigLoader.class);
+    private static final List<String> EXCLUDED_KEYS = ImmutableList.of("service.pid",
"felix.fileinstall.filename");
+    private static final String KARAF_DESERIALIZING_CLASS_RENAMES_PROPERTIES = "org.apache.brooklyn.classrename";
+
+    private ConfigurationAdmin configAdmin;
+
+    public OsgiConfigLoader() {
+        LOG.trace("OsgiConfigLoader instance created");
+    }
+
+    // For injection as OSGi bean
+    public void setConfigAdmin(ConfigurationAdmin configAdmin) {
+        this.configAdmin = configAdmin;
+    }
+
+    // Called by OSGi
+    public void init() {
+        LOG.trace("DeserializingClassRenamesProvider.OsgiConfigLoader.init: registering loader");
+        DeserializingClassRenamesProvider.getLoaders().add(this);
+        DeserializingClassRenamesProvider.reset();
+    }
+
+    // Called by OSGi
+    public void destroy() {
+        LOG.trace("DeserializingClassRenamesProvider.OsgiConfigLoader.destroy: unregistering
loader");
+        boolean removed = DeserializingClassRenamesProvider.getLoaders().remove(this);
+        if (removed) {
+            DeserializingClassRenamesProvider.reset();
+        }
+    }
+
+    // Called by OSGi when configuration changes
+    public void updateProperties(Map properties) {
+        LOG.debug("DeserializingClassRenamesProvider.OsgiConfigLoader.updateProperties: clearing
cache, so class-renames will be reloaded");
+        DeserializingClassRenamesProvider.reset();
+    }
+
+    @Override
+    public Map<String, String> load() {
+        if (configAdmin == null) {
+            LOG.warn("No OSGi configuration-admin available - cannot load {}.cfg", KARAF_DESERIALIZING_CLASS_RENAMES_PROPERTIES);
+            return ImmutableMap.of();
+        }
+
+        String filter = '(' + Constants.SERVICE_PID + '=' + KARAF_DESERIALIZING_CLASS_RENAMES_PROPERTIES
+ ')';
+        Configuration[] configs;
+
+        try {
+            configs = configAdmin.listConfigurations(filter);
+        } catch (InvalidSyntaxException | IOException e) {
+            LOG.info("Cannot list OSGi configurations");
+            throw Exceptions.propagate(e);
+        }
+
+        final MutableMap<String, String> map = MutableMap.of();
+        if (configs != null) {
+            for (Configuration config : configs) {
+                LOG.debug("Reading OSGi configuration from {}; bundleLocation={}", config.getPid(),
config.getBundleLocation());
+                map.putAll(dictToMap(config.getProperties()));
+            }
+        } else {
+            LOG.info("No OSGi configuration found for {}.cfg", KARAF_DESERIALIZING_CLASS_RENAMES_PROPERTIES);
+        }
+
+        return map;
+    }
+
+    private Map<String, String> dictToMap(Dictionary<String, Object> props) {
+        Map<String, String> mapProps = MutableMap.of();
+        Enumeration<String> keyEnum = props.keys();
+        while (keyEnum.hasMoreElements()) {
+            String key = keyEnum.nextElement();
+            if (!EXCLUDED_KEYS.contains(key)) {
+                mapProps.put(key, (String) props.get(key));
+            }
+        }
+        return mapProps;
+    }
+}

http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/a34e8ab5/karaf/init/src/main/resources/OSGI-INF/blueprint/blueprint.xml
----------------------------------------------------------------------
diff --git a/karaf/init/src/main/resources/OSGI-INF/blueprint/blueprint.xml b/karaf/init/src/main/resources/OSGI-INF/blueprint/blueprint.xml
index 2262c20..5bac6cd 100644
--- a/karaf/init/src/main/resources/OSGI-INF/blueprint/blueprint.xml
+++ b/karaf/init/src/main/resources/OSGI-INF/blueprint/blueprint.xml
@@ -47,10 +47,23 @@ limitations under the License.
         </cm:default-properties>
     </cm:property-placeholder>
 
+    <bean id="deserializingClassRenamesProvider"
+          class="org.apache.brooklyn.launcher.osgi.OsgiConfigLoader"
+          init-method="init"
+          destroy-method="destroy">
+
+        <property name="configAdmin" ref="configAdmin" />
+
+        <cm:managed-properties persistent-id="org.apache.brooklyn.classrename"
+                               update-method="updateProperties"
+                               update-strategy="component-managed" />
+    </bean>
+
     <bean id="launcher"
           class="org.apache.brooklyn.launcher.osgi.OsgiLauncher"
           init-method="init"
-          destroy-method="destroy">
+          destroy-method="destroy"
+          depends-on="deserializingClassRenamesProvider">
 
         <property name="brooklynVersion" ref="brooklynVersion" />
 


Mime
View raw message