karaf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From cschnei...@apache.org
Subject karaf-decanter git commit: [KARAF-4546] Allow configs for decanter in spring
Date Wed, 01 Jun 2016 11:05:59 GMT
Repository: karaf-decanter
Updated Branches:
  refs/heads/master 939d43b65 -> 07201b77d


[KARAF-4546] Allow configs for decanter in spring


Project: http://git-wip-us.apache.org/repos/asf/karaf-decanter/repo
Commit: http://git-wip-us.apache.org/repos/asf/karaf-decanter/commit/07201b77
Tree: http://git-wip-us.apache.org/repos/asf/karaf-decanter/tree/07201b77
Diff: http://git-wip-us.apache.org/repos/asf/karaf-decanter/diff/07201b77

Branch: refs/heads/master
Commit: 07201b77dd0b79efb7a9778b86dc3e9ae76cdf82
Parents: 939d43b
Author: Christian Schneider <chris@die-schneider.net>
Authored: Wed Jun 1 13:05:43 2016 +0200
Committer: Christian Schneider <chris@die-schneider.net>
Committed: Wed Jun 1 13:05:43 2016 +0200

----------------------------------------------------------------------
 pom.xml                                         |  5 --
 spring-boot-starter-decanter/.gitignore         |  1 +
 spring-boot-starter-decanter/etc/test.cfg       |  0
 spring-boot-starter-decanter/pom.xml            | 12 +++
 .../karaf/decanter/boot/DecanterConnect.java    | 38 +++-------
 .../decanter/boot/DecanterRegistryFactory.java  | 77 ++++++++++++++------
 .../resources/META-INF/decanter.bundles.default |  1 +
 .../boot/DecanterRegistryFactoryTest.java       | 15 ++++
 .../src/test/resources/logback.xml              |  2 +-
 9 files changed, 95 insertions(+), 56 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/karaf-decanter/blob/07201b77/pom.xml
----------------------------------------------------------------------
diff --git a/pom.xml b/pom.xml
index 57d5b7e..91de60c 100644
--- a/pom.xml
+++ b/pom.xml
@@ -359,11 +359,6 @@
 
         <!-- Test dependencies -->
         <dependency>
-            <groupId>org.slf4j</groupId>
-            <artifactId>slf4j-simple</artifactId>
-            <scope>test</scope>
-        </dependency>
-        <dependency>
             <groupId>junit</groupId>
             <artifactId>junit</artifactId>
             <scope>test</scope>

http://git-wip-us.apache.org/repos/asf/karaf-decanter/blob/07201b77/spring-boot-starter-decanter/.gitignore
----------------------------------------------------------------------
diff --git a/spring-boot-starter-decanter/.gitignore b/spring-boot-starter-decanter/.gitignore
index b83d222..6884ba0 100644
--- a/spring-boot-starter-decanter/.gitignore
+++ b/spring-boot-starter-decanter/.gitignore
@@ -1 +1,2 @@
 /target/
+bundle*

http://git-wip-us.apache.org/repos/asf/karaf-decanter/blob/07201b77/spring-boot-starter-decanter/etc/test.cfg
----------------------------------------------------------------------
diff --git a/spring-boot-starter-decanter/etc/test.cfg b/spring-boot-starter-decanter/etc/test.cfg
new file mode 100644
index 0000000..e69de29

http://git-wip-us.apache.org/repos/asf/karaf-decanter/blob/07201b77/spring-boot-starter-decanter/pom.xml
----------------------------------------------------------------------
diff --git a/spring-boot-starter-decanter/pom.xml b/spring-boot-starter-decanter/pom.xml
index f0f9c07..781335f 100644
--- a/spring-boot-starter-decanter/pom.xml
+++ b/spring-boot-starter-decanter/pom.xml
@@ -31,6 +31,13 @@
             <version>${project.version}</version>
         </dependency>
         <dependency>
+            <groupId>org.apache.karaf.decanter.appender</groupId>
+            <artifactId>
+                org.apache.karaf.decanter.appender.log
+            </artifactId>
+            <version>${project.version}</version>
+        </dependency>
+        <dependency>
             <groupId>org.apache.karaf.decanter.marshaller</groupId>
             <artifactId>
                 org.apache.karaf.decanter.marshaller.json
@@ -51,6 +58,11 @@
             <artifactId>org.apache.felix.configadmin</artifactId>
             <version>1.8.8</version>
         </dependency>
+        <dependency>
+            <groupId>org.apache.felix</groupId>
+            <artifactId>org.apache.felix.fileinstall</artifactId>
+            <version>3.5.4</version>
+        </dependency>
         
         <dependency>
             <groupId>org.springframework.boot</groupId>

http://git-wip-us.apache.org/repos/asf/karaf-decanter/blob/07201b77/spring-boot-starter-decanter/src/main/java/org/apache/karaf/decanter/boot/DecanterConnect.java
----------------------------------------------------------------------
diff --git a/spring-boot-starter-decanter/src/main/java/org/apache/karaf/decanter/boot/DecanterConnect.java
b/spring-boot-starter-decanter/src/main/java/org/apache/karaf/decanter/boot/DecanterConnect.java
index dd2dba8..8a851e3 100644
--- a/spring-boot-starter-decanter/src/main/java/org/apache/karaf/decanter/boot/DecanterConnect.java
+++ b/spring-boot-starter-decanter/src/main/java/org/apache/karaf/decanter/boot/DecanterConnect.java
@@ -18,16 +18,10 @@
  */
 package org.apache.karaf.decanter.boot;
 
-import java.io.IOException;
-import java.util.Dictionary;
-import java.util.Hashtable;
-
 import javax.annotation.PreDestroy;
 
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.BundleException;
-import org.osgi.service.cm.Configuration;
-import org.osgi.service.cm.ConfigurationAdmin;
 import org.osgi.service.event.EventAdmin;
 import org.osgi.util.tracker.ServiceTracker;
 
@@ -37,31 +31,17 @@ public class DecanterConnect {
 
     public DecanterConnect() throws Exception {
         registryContext = new DecanterRegistryFactory().create();
-        Dictionary<String, String> kafka = new Hashtable<>();
-        kafka.put("bootstrap.servers", "kafka:9092");
-        configure(registryContext, "org.apache.karaf.decanter.appender.kafka", kafka);
-        injectEventAdmin(registryContext);
-    }
-
-    private static void configure(BundleContext context, String pid, Dictionary<String,
String> properties)
-        throws IOException {
-        ServiceTracker<ConfigurationAdmin, ConfigurationAdmin> tracker = new ServiceTracker<>(context,
-                                                                                        
     ConfigurationAdmin.class,
-                                                                                        
     null);
-        tracker.open();
-        ConfigurationAdmin configAdmin = tracker.getService();
-        Configuration config = configAdmin.getConfiguration(pid);
-        config.update(properties);
-        tracker.close();
+        LogbackDecanterAppender.setDispatcher(getService(registryContext, EventAdmin.class));
     }
 
-    private static void injectEventAdmin(BundleContext context) {
-        ServiceTracker<EventAdmin, EventAdmin> tracker = new ServiceTracker<>(context,
EventAdmin.class,
-                                                                              null);
-        tracker.open();
-        EventAdmin eventAdmin = tracker.getService();
-        LogbackDecanterAppender.setDispatcher(eventAdmin);
-        tracker.close();
+    private <S>S getService(BundleContext context, Class<S> serviceClazz) {
+        ServiceTracker<S, S> tracker = new ServiceTracker<S, S>(context, serviceClazz,
null);
+        try {
+            tracker.open();
+            return tracker.getService();
+        } finally {
+            tracker.close();
+        }
     }
 
     @PreDestroy

http://git-wip-us.apache.org/repos/asf/karaf-decanter/blob/07201b77/spring-boot-starter-decanter/src/main/java/org/apache/karaf/decanter/boot/DecanterRegistryFactory.java
----------------------------------------------------------------------
diff --git a/spring-boot-starter-decanter/src/main/java/org/apache/karaf/decanter/boot/DecanterRegistryFactory.java
b/spring-boot-starter-decanter/src/main/java/org/apache/karaf/decanter/boot/DecanterRegistryFactory.java
index b0a9561..42b689b 100644
--- a/spring-boot-starter-decanter/src/main/java/org/apache/karaf/decanter/boot/DecanterRegistryFactory.java
+++ b/spring-boot-starter-decanter/src/main/java/org/apache/karaf/decanter/boot/DecanterRegistryFactory.java
@@ -20,16 +20,18 @@ package org.apache.karaf.decanter.boot;
 
 import static org.apache.felix.connect.launch.PojoServiceRegistryFactory.BUNDLE_DESCRIPTORS;
 
+import java.io.BufferedReader;
 import java.io.IOException;
-import java.net.URI;
+import java.io.InputStream;
+import java.io.InputStreamReader;
 import java.net.URISyntaxException;
-import java.net.URL;
 import java.nio.charset.Charset;
-import java.nio.file.Files;
-import java.nio.file.Paths;
+import java.util.ArrayList;
 import java.util.HashMap;
+import java.util.HashSet;
 import java.util.List;
 import java.util.ServiceLoader;
+import java.util.Set;
 
 import org.apache.felix.connect.launch.BundleDescriptor;
 import org.apache.felix.connect.launch.ClasspathScanner;
@@ -38,40 +40,75 @@ import org.osgi.framework.BundleContext;
 import org.osgi.framework.Constants;
 import org.osgi.framework.FrameworkUtil;
 
+/**
+ * Creates a felix connect instance from a list of bundle symbolic names provided in a file.
+ * All listed bundles must be present in the project classpath.
+ */
 public class DecanterRegistryFactory {
     private static final String BUNDLES_CONFIG = "META-INF/decanter.bundles";
     private static final String BUNDLES_CONFIG_DEFAULT = "META-INF/decanter.bundles.default";
 
     public BundleContext create() throws Exception {
-        ServiceLoader<PojoServiceRegistryFactory> loader = ServiceLoader
-            .load(PojoServiceRegistryFactory.class);
-        PojoServiceRegistryFactory srFactory = loader.iterator().next();
+        setSysPropDefault("felix.fileinstall.dir", "etc");
+        setSysPropDefault("felix.fileinstall.noInitialDelay", "true");
+        PojoServiceRegistryFactory srFactory = ServiceLoader
+            .load(PojoServiceRegistryFactory.class).iterator().next();
         HashMap<String, Object> pojoSrConfig = new HashMap<>();
         pojoSrConfig.put(BUNDLE_DESCRIPTORS, getBundles());
         return srFactory.newPojoServiceRegistry(pojoSrConfig).getBundleContext();
     }
+    
+    private void setSysPropDefault(String key, String defaultValue) {
+        if (System.getProperty(key) == null) {
+            System.setProperty(key, defaultValue);
+        }
+    }
 
     List<BundleDescriptor> getBundles() throws URISyntaxException, IOException, Exception
{
-        URI bundleURL = getURI(BUNDLES_CONFIG);
-        if (bundleURL == null) {
-            bundleURL = getURI(BUNDLES_CONFIG_DEFAULT);
+        InputStream is = getStream(BUNDLES_CONFIG);
+        if (is == null) {
+            is = getStream(BUNDLES_CONFIG_DEFAULT);
         }
-        List<String> bundleNames = Files.readAllLines(Paths.get(bundleURL), Charset.forName("utf-8"));
+        List<String> bundleNames = readLines(is);
         String filter = getBundleFilter(bundleNames);
         FrameworkUtil.createFilter(filter);
         List<BundleDescriptor> bundles = new ClasspathScanner().scanForBundles(filter);
+        assertAllPresent(bundles, new HashSet<String>(bundleNames));
         //printNames(bundles);
         return bundles;
     }
 
-    private URI getURI(String path) throws URISyntaxException {
-        ClassLoader loader = this.getClass().getClassLoader();
-        URL url = loader.getResource(path); 
-        return url == null ? null : url.toURI();
+    private List<String> readLines(InputStream is) throws IOException {
+        try (
+            BufferedReader reader = new BufferedReader(new InputStreamReader(is, Charset.forName("utf-8")))
+            ) {
+            List<String> lines = new ArrayList<>();
+            String line;
+            while ((line = reader.readLine()) != null) {
+                lines.add(line);
+            }
+            return lines;
+        }
+        
+        
     }
-    
-    
 
+    private void assertAllPresent(List<BundleDescriptor> bundles, Set<String>
bundleNames) {
+        Set<String> bundlesPresent = new HashSet<>();
+        for (BundleDescriptor bundle : bundles) {
+            bundlesPresent.add(getSymbolicName(bundle)); 
+        }
+        for (String expected : bundleNames) {
+            if (!bundlesPresent.contains(expected)) {
+                throw new RuntimeException("Bundle " + expected + " was not loaded");
+            }
+        }
+    }
+
+    private InputStream getStream(String path) throws URISyntaxException {
+        return this.getClass().getClassLoader().getResourceAsStream(path);
+    }
+    
     static String getBundleFilter(List<String> bundles) {
         StringBuilder joined = new StringBuilder();
         joined.append("(|(Bundle-SymbolicName=");
@@ -87,9 +124,7 @@ public class DecanterRegistryFactory {
         return joined.toString();
     }
 
-    private void printNames(List<BundleDescriptor> bundles) {
-        for (BundleDescriptor desc : bundles) {
-            System.out.println(desc.getHeaders().get(Constants.BUNDLE_SYMBOLICNAME));
-        }
+    private String getSymbolicName(BundleDescriptor desc) {
+        return desc.getHeaders().get(Constants.BUNDLE_SYMBOLICNAME);
     }
 }

http://git-wip-us.apache.org/repos/asf/karaf-decanter/blob/07201b77/spring-boot-starter-decanter/src/main/resources/META-INF/decanter.bundles.default
----------------------------------------------------------------------
diff --git a/spring-boot-starter-decanter/src/main/resources/META-INF/decanter.bundles.default
b/spring-boot-starter-decanter/src/main/resources/META-INF/decanter.bundles.default
index 473ee88..d9f5b9b 100644
--- a/spring-boot-starter-decanter/src/main/resources/META-INF/decanter.bundles.default
+++ b/spring-boot-starter-decanter/src/main/resources/META-INF/decanter.bundles.default
@@ -2,6 +2,7 @@ org.apache.felix.connect
 org.apache.felix.scr
 org.apache.felix.eventadmin
 org.apache.felix.configadmin
+org.apache.felix.fileinstall
 org.apache.karaf.decanter.api
 org.apache.karaf.decanter.marshaller.json
 org.apache.karaf.decanter.appender.log

http://git-wip-us.apache.org/repos/asf/karaf-decanter/blob/07201b77/spring-boot-starter-decanter/src/test/java/org/apache/karaf/decanter/boot/DecanterRegistryFactoryTest.java
----------------------------------------------------------------------
diff --git a/spring-boot-starter-decanter/src/test/java/org/apache/karaf/decanter/boot/DecanterRegistryFactoryTest.java
b/spring-boot-starter-decanter/src/test/java/org/apache/karaf/decanter/boot/DecanterRegistryFactoryTest.java
index 27c4f5b..d5010ea 100644
--- a/spring-boot-starter-decanter/src/test/java/org/apache/karaf/decanter/boot/DecanterRegistryFactoryTest.java
+++ b/spring-boot-starter-decanter/src/test/java/org/apache/karaf/decanter/boot/DecanterRegistryFactoryTest.java
@@ -18,11 +18,16 @@
  */
 package org.apache.karaf.decanter.boot;
 
+import java.io.IOException;
 import java.util.Arrays;
 
+import org.junit.Assert;
 import org.junit.Test;
 import org.osgi.framework.BundleContext;
 import org.osgi.framework.FrameworkUtil;
+import org.osgi.framework.ServiceReference;
+import org.osgi.service.cm.Configuration;
+import org.osgi.service.cm.ConfigurationAdmin;
 
 public class DecanterRegistryFactoryTest {
 
@@ -36,6 +41,16 @@ public class DecanterRegistryFactoryTest {
     public void testCreate() throws Exception {
         DecanterRegistryFactory factory = new DecanterRegistryFactory();
         BundleContext context = factory.create();
+        checkConfigPresent(context);
         context.getBundle().stop();
     }
+
+    private void checkConfigPresent(BundleContext context)
+        throws IOException {
+        ServiceReference<ConfigurationAdmin> sref = context.getServiceReference(ConfigurationAdmin.class);
+        ConfigurationAdmin configAdmin = context.getService(sref);
+        Configuration config = configAdmin.getConfiguration("test");
+        Assert.assertEquals("myvalue", config.getProperties().get("mykey"));
+        context.ungetService(sref);
+    }
 }

http://git-wip-us.apache.org/repos/asf/karaf-decanter/blob/07201b77/spring-boot-starter-decanter/src/test/resources/logback.xml
----------------------------------------------------------------------
diff --git a/spring-boot-starter-decanter/src/test/resources/logback.xml b/spring-boot-starter-decanter/src/test/resources/logback.xml
index e6740be..8cb6e18 100644
--- a/spring-boot-starter-decanter/src/test/resources/logback.xml
+++ b/spring-boot-starter-decanter/src/test/resources/logback.xml
@@ -14,7 +14,7 @@
     </encoder>
   </appender>
 
-  <root level="DEBUG">
+  <root level="INFO">
     <appender-ref ref="STDOUT" />
     <appender-ref ref="DECANTER" />
   </root>


Mime
View raw message