camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ggrzy...@apache.org
Subject [3/4] camel git commit: [CAMEL-10476] Provide initial configadmin configuration in camel:run scenario
Date Fri, 18 Nov 2016 16:58:33 GMT
[CAMEL-10476] Provide initial configadmin configuration in camel:run scenario

(cherry picked from commit 35a8fb65ce2f4feffe0b663d4ed48fdf00e98f44)


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

Branch: refs/heads/camel-2.17.x
Commit: d35f61c20cd0a14ebf4eaea3f4c5d050c6ffeb2e
Parents: a41e734
Author: Grzegorz Grzybek <gr.grzybek@gmail.com>
Authored: Fri Nov 18 17:04:55 2016 +0100
Committer: Grzegorz Grzybek <gr.grzybek@gmail.com>
Committed: Fri Nov 18 17:38:50 2016 +0100

----------------------------------------------------------------------
 .../test/blueprint/CamelBlueprintHelper.java    | 76 +++++++++++++++-----
 .../org/apache/camel/test/blueprint/Main.java   | 15 +++-
 .../apache/camel/test/blueprint/MainTest.java   | 56 ++++++++++++++-
 3 files changed, 127 insertions(+), 20 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/d35f61c2/components/camel-test-blueprint/src/main/java/org/apache/camel/test/blueprint/CamelBlueprintHelper.java
----------------------------------------------------------------------
diff --git a/components/camel-test-blueprint/src/main/java/org/apache/camel/test/blueprint/CamelBlueprintHelper.java
b/components/camel-test-blueprint/src/main/java/org/apache/camel/test/blueprint/CamelBlueprintHelper.java
index b291644..90f23bb 100644
--- a/components/camel-test-blueprint/src/main/java/org/apache/camel/test/blueprint/CamelBlueprintHelper.java
+++ b/components/camel-test-blueprint/src/main/java/org/apache/camel/test/blueprint/CamelBlueprintHelper.java
@@ -108,18 +108,38 @@ public final class CamelBlueprintHelper {
     public static BundleContext createBundleContext(String name, String descriptors, boolean
includeTestBundle,
                                                     String bundleFilter, String testBundleVersion,
String testBundleDirectives,
                                                     String[] ... configAdminPidFiles) throws
Exception {
+        return createBundleContext(name, descriptors, includeTestBundle,
+                bundleFilter, testBundleVersion, testBundleDirectives,
+                null,
+                configAdminPidFiles);
+    }
+
+    public static BundleContext createBundleContext(String name, String descriptors, boolean
includeTestBundle,
+                                                    String bundleFilter, String testBundleVersion,
String testBundleDirectives,
+                                                    ClassLoader loader,
+                                                    String[] ... configAdminPidFiles) throws
Exception {
         TinyBundle bundle = null;
+        TinyBundle configAdminInitBundle = null;
 
         if (includeTestBundle) {
             // add ourselves as a bundle
             bundle = createTestBundle(testBundleDirectives == null ? name : name + ';' +
testBundleDirectives,
-                    testBundleVersion, descriptors, configAdminPidFiles);
+                    testBundleVersion, descriptors);
+        }
+        if (configAdminPidFiles != null) {
+            configAdminInitBundle = createConfigAdminInitBundle(configAdminPidFiles);
         }
 
-        return createBundleContext(name, bundleFilter, bundle);
+        return createBundleContext(name, bundleFilter, bundle, configAdminInitBundle, loader);
     }
 
     public static BundleContext createBundleContext(String name, String bundleFilter, TinyBundle
bundle) throws Exception {
+        return createBundleContext(name, bundleFilter, bundle, null,null);
+    }
+
+    public static BundleContext createBundleContext(String name, String bundleFilter,
+                                                    TinyBundle bundle, TinyBundle configAdminInitBundle,
+                                                    ClassLoader loader) throws Exception
{
         // ensure felix-connect stores bundles in an unique target directory
         String uid = "" + System.currentTimeMillis();
         String tempDir = "target/bundles/" + uid;
@@ -133,12 +153,17 @@ public final class CamelBlueprintHelper {
 
         List<BundleDescriptor> bundles = new LinkedList<>();
 
+        if (configAdminInitBundle != null) {
+            String jarName = "configAdminInitBundle-" + uid + ".jar";
+            bundles.add(getBundleDescriptor("target/test-bundles/" + jarName, configAdminInitBundle));
+        }
+
         if (bundle != null) {
             String jarName = name.toLowerCase(Locale.ENGLISH) + "-" + uid + ".jar";
             bundles.add(getBundleDescriptor("target/test-bundles/" + jarName, bundle));
         }
 
-        List<BundleDescriptor> bundleDescriptors = getBundleDescriptors(bundleFilter);
+        List<BundleDescriptor> bundleDescriptors = getBundleDescriptors(bundleFilter,
loader);
         // let's put configadmin before blueprint.core
         int idx1 = -1;
         int idx2 = -1;
@@ -261,7 +286,7 @@ public final class CamelBlueprintHelper {
     }
 
     public static <T> T getOsgiService(BundleContext bundleContext, Class<T>
type, String filter, long timeout) {
-        ServiceTracker tracker = null;
+        ServiceTracker<T, T> tracker = null;
         try {
             String flt;
             if (filter != null) {
@@ -274,7 +299,7 @@ public final class CamelBlueprintHelper {
                 flt = "(" + Constants.OBJECTCLASS + "=" + type.getName() + ")";
             }
             Filter osgiFilter = FrameworkUtil.createFilter(flt);
-            tracker = new ServiceTracker(bundleContext, osgiFilter, null);
+            tracker = new ServiceTracker<T, T>(bundleContext, osgiFilter, null);
             tracker.open(true);
             // Note that the tracker is not closed to keep the reference
             // This is buggy, as the service reference may change i think
@@ -331,20 +356,20 @@ public final class CamelBlueprintHelper {
         if (runAndWait != null) {
             runAndWait.run();
         }
-        latch.await(CamelBlueprintHelper.DEFAULT_TIMEOUT, TimeUnit.MILLISECONDS);
+        boolean found = latch.await(CamelBlueprintHelper.DEFAULT_TIMEOUT, TimeUnit.MILLISECONDS);
         registration.unregister();
 
+        if (!found) {
+            throw new RuntimeException("Gave up waiting for BlueprintContainer from bundle
\"" + symbolicName + "\"");
+        }
+
         if (pThrowable[0] != null) {
             throw new RuntimeException(pThrowable[0].getMessage(), pThrowable[0]);
         }
     }
 
-    protected static TinyBundle createTestBundle(String name, String version, String descriptors,
String[] ... configAdminPidFiles) throws IOException {
+    protected static TinyBundle createConfigAdminInitBundle(String[] ... configAdminPidFiles)
throws IOException {
         TinyBundle bundle = TinyBundles.newBundle();
-        for (URL url : getBlueprintDescriptors(descriptors)) {
-            LOG.info("Using Blueprint XML file: " + url.getFile());
-            bundle.add("OSGI-INF/blueprint/blueprint-" + url.getFile().replace("/", "-"),
url);
-        }
         StringWriter configAdminInit = null;
         for (String[] configAdminPidFile : configAdminPidFiles) {
             if (configAdminPidFile == null) {
@@ -362,12 +387,28 @@ public final class CamelBlueprintHelper {
         bundle.add(Util.class);
         bundle.set("Manifest-Version", "2")
                 .set("Bundle-ManifestVersion", "2")
-                .set("Bundle-SymbolicName", name)
-                .set("Bundle-Version", version)
+                .set("Bundle-SymbolicName", "ConfigAdminInit")
+                .set("Bundle-Version", BUNDLE_VERSION)
                 .set("Bundle-Activator", TestBundleActivator.class.getName());
+
         if (configAdminInit != null) {
             bundle.set("X-Camel-Blueprint-ConfigAdmin-Init", configAdminInit.toString());
         }
+
+        return bundle;
+    }
+
+    protected static TinyBundle createTestBundle(String name, String version, String descriptors)
throws IOException {
+        TinyBundle bundle = TinyBundles.newBundle();
+        for (URL url : getBlueprintDescriptors(descriptors)) {
+            LOG.info("Using Blueprint XML file: " + url.getFile());
+            bundle.add("OSGI-INF/blueprint/blueprint-" + url.getFile().replace("/", "-"),
url);
+        }
+        bundle.set("Manifest-Version", "2")
+                .set("Bundle-ManifestVersion", "2")
+                .set("Bundle-SymbolicName", name)
+                .set("Bundle-Version", version);
+
         return bundle;
     }
 
@@ -401,8 +442,8 @@ public final class CamelBlueprintHelper {
      * @return List pointers to OSGi bundles.
      * @throws Exception If looking up the bundles fails.
      */
-    private static List<BundleDescriptor> getBundleDescriptors(final String bundleFilter)
throws Exception {
-        return new ClasspathScanner().scanForBundles(bundleFilter);
+    private static List<BundleDescriptor> getBundleDescriptors(final String bundleFilter,
ClassLoader loader) throws Exception {
+        return new ClasspathScanner().scanForBundles(bundleFilter, loader);
     }
 
     /**
@@ -414,10 +455,9 @@ public final class CamelBlueprintHelper {
      */
     protected static Collection<URL> getBlueprintDescriptors(String descriptors) throws
FileNotFoundException, MalformedURLException {
         List<URL> answer = new ArrayList<URL>();
-        String descriptor = descriptors;
-        if (descriptor != null) {
+        if (descriptors != null) {
             // there may be more resources separated by comma
-            Iterator<Object> it = ObjectHelper.createIterator(descriptor);
+            Iterator<Object> it = ObjectHelper.createIterator(descriptors);
             while (it.hasNext()) {
                 String s = (String) it.next();
                 LOG.trace("Resource descriptor: {}", s);

http://git-wip-us.apache.org/repos/asf/camel/blob/d35f61c2/components/camel-test-blueprint/src/main/java/org/apache/camel/test/blueprint/Main.java
----------------------------------------------------------------------
diff --git a/components/camel-test-blueprint/src/main/java/org/apache/camel/test/blueprint/Main.java
b/components/camel-test-blueprint/src/main/java/org/apache/camel/test/blueprint/Main.java
index a0be881..3cd8c30 100644
--- a/components/camel-test-blueprint/src/main/java/org/apache/camel/test/blueprint/Main.java
+++ b/components/camel-test-blueprint/src/main/java/org/apache/camel/test/blueprint/Main.java
@@ -42,6 +42,9 @@ public class Main extends MainSupport {
     private String configAdminPid;
     private String configAdminFileName;
 
+    // ClassLoader used to scan for bundles in CamelBlueprintHelper.createBundleContext()
+    private ClassLoader loader;
+
     public Main() {
 
         addOption(new ParameterOption("ac", "applicationContext",
@@ -141,8 +144,13 @@ public class Main extends MainSupport {
     }
 
     protected BundleContext createBundleContext(String name, String[] ... configAdminPidFiles)
throws Exception {
+        return createBundleContext(name, loader, configAdminPidFiles);
+    }
+
+    protected BundleContext createBundleContext(String name, ClassLoader loader, String[]
... configAdminPidFiles) throws Exception {
         return CamelBlueprintHelper.createBundleContext(name, descriptors, isIncludeSelfAsBundle(),
-                CamelBlueprintHelper.BUNDLE_FILTER, CamelBlueprintHelper.BUNDLE_VERSION,
null, configAdminPidFiles);
+                CamelBlueprintHelper.BUNDLE_FILTER, CamelBlueprintHelper.BUNDLE_VERSION,
null,
+                loader, configAdminPidFiles);
     }
 
     @Override
@@ -193,4 +201,9 @@ public class Main extends MainSupport {
     public void setConfigAdminFileName(String fileName) {
         this.configAdminFileName = fileName;
     }
+
+    public void setLoader(ClassLoader loader) {
+        this.loader = loader;
+    }
+
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/d35f61c2/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/MainTest.java
----------------------------------------------------------------------
diff --git a/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/MainTest.java
b/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/MainTest.java
index c7a093f..eb48a1c 100644
--- a/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/MainTest.java
+++ b/components/camel-test-blueprint/src/test/java/org/apache/camel/test/blueprint/MainTest.java
@@ -17,15 +17,30 @@
 package org.apache.camel.test.blueprint;
 
 
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.StringWriter;
+import java.net.URL;
+import java.net.URLClassLoader;
+import java.util.Arrays;
+
+import org.apache.aries.util.io.IOUtils;
 import org.apache.camel.ProducerTemplate;
+import org.apache.camel.util.FileUtil;
+import org.apache.felix.connect.felix.framework.util.Util;
 import org.junit.Test;
+import org.ops4j.io.FileUtils;
+import org.ops4j.pax.swissbox.tinybundles.core.TinyBundle;
+import org.ops4j.pax.swissbox.tinybundles.core.TinyBundles;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 
 
 public class MainTest {
-    
+
+    private static final String SYMBOLIC_NAME = "testMainWithoutIncludingTestBundle";
+
     @Test
     public void testMyMain() throws Exception {
         Main main = new Main();
@@ -48,4 +63,43 @@ public class MainTest {
         main.stop();
     }
 
+    @Test
+    public void testMainWithoutIncludingTestBundle() throws Exception {
+        TinyBundle bundle = TinyBundles.newBundle();
+        bundle.add("OSGI-INF/blueprint/camel.xml", getClass().getResourceAsStream("main-loadfile.xml"));
+        bundle.set("Manifest-Version", "2")
+                .set("Bundle-ManifestVersion", "2")
+                .set("Bundle-SymbolicName", SYMBOLIC_NAME)
+                .set("Bundle-Version", "1.0.0");
+        File tb = File.createTempFile(SYMBOLIC_NAME + "-", ".jar", new File("target"));
+        FileOutputStream out = new FileOutputStream(tb);
+        IOUtils.copy(bundle.build(), out);
+        out.close();
+
+        // simulate `camel:run` which is run after packaging the artifact, so a "bundle"
(location with
+        // META-INF/MANIFEST.MF) is detected in target/classes
+        URLClassLoader loader = new URLClassLoader(new URL[] { tb.toURI().toURL() }, getClass().getClassLoader());
+
+        Main main = new Main();
+        main.setLoader(loader);
+        // bundle name will be used as filter for blueprint container filter
+        main.setBundleName(SYMBOLIC_NAME);
+        // don't include test bundle (which is what `mvn camel:run` actually does)
+        main.setIncludeSelfAsBundle(false);
+        // don't setup the blueprint file here - it'll be picked up from a bundle on classpath
+        //main.setDescriptors("none!");
+        // set the configAdmin persistent id
+        main.setConfigAdminPid("stuff");
+        // set the configAdmin persistent file name
+        main.setConfigAdminFileName("src/test/resources/etc/stuff.cfg");
+        main.doStart();
+
+        ProducerTemplate template = main.getCamelTemplate();
+        assertNotNull("We should get the template here", template);
+
+        String result = template.requestBody("direct:start", "hello", String.class);
+        assertEquals("Get a wrong response", "Bye hello", result);
+        main.stop();
+    }
+
 }


Mime
View raw message