Skip to content

Commit

Permalink
[CAMEL-10476] Provide initial configadmin configuration in camel:run …
Browse files Browse the repository at this point in the history
…scenario

(cherry picked from commit 35a8fb6)
  • Loading branch information
grgrzybek committed Nov 18, 2016
1 parent 3e096a5 commit e6bf836
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -113,18 +113,38 @@ public static BundleContext createBundleContext(String name, String descriptors,
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;
Expand All @@ -138,12 +158,17 @@ public static BundleContext createBundleContext(String name, String bundleFilter

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;
Expand Down Expand Up @@ -266,7 +291,7 @@ public static <T> T getOsgiService(BundleContext bundleContext, Class<T> type, S
}

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) {
Expand All @@ -279,7 +304,7 @@ public static <T> T getOsgiService(BundleContext bundleContext, Class<T> type, S
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
Expand Down Expand Up @@ -336,20 +361,20 @@ public void blueprintEvent(BlueprintEvent event) {
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) {
Expand All @@ -367,12 +392,28 @@ protected static TinyBundle createTestBundle(String name, String version, String
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;
}

Expand Down Expand Up @@ -406,8 +447,8 @@ private static Collection<ServiceReference> asCollection(ServiceReference[] refe
* @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);
}

/**
Expand All @@ -419,10 +460,9 @@ private static List<BundleDescriptor> getBundleDescriptors(final String bundleFi
*/
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -142,8 +145,13 @@ protected BundleContext createBundleContext() throws Exception {
}

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
Expand Down Expand Up @@ -194,4 +202,9 @@ public String getConfigAdminFileName() {
public void setConfigAdminFileName(String fileName) {
this.configAdminFileName = fileName;
}

public void setLoader(ClassLoader loader) {
this.loader = loader;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -48,4 +63,43 @@ public void testMyMain() throws Exception {
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();
}

}

0 comments on commit e6bf836

Please sign in to comment.