Skip to content

Instantly share code, notes, and snippets.

@rafaeltuelho
Created November 7, 2014 19:58
Show Gist options
  • Save rafaeltuelho/a8dd453a4093167bce17 to your computer and use it in GitHub Desktop.
Save rafaeltuelho/a8dd453a4093167bce17 to your computer and use it in GitHub Desktop.
OSGI Bundle Activator class to activate a Bundle inside a OSGI Container (eg: Apache Karaf)
package com.redhat.camel.java;
import org.apache.camel.impl.DefaultCamelContext;
import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
/**
* A Camel Application
*/
public class MainActivation implements BundleActivator{
private DefaultCamelContext camelContext;
private String bundleName;
@Override
public void start(BundleContext bundleContext) throws Exception {
camelContext = new DefaultCamelContext();
bundleName = bundleContext.getBundle().getSymbolicName();
camelContext.setName(bundleName);
camelContext.addRoutes(new MyRouteBuilder());
camelContext.start();
}
@Override
public void stop(BundleContext bundleContext) throws Exception {
camelContext.stop();
}
}
@rafaeltuelho
Copy link
Author

Use the maven bundle plugin to indicate to generate the Bundle OSGI JAR:

      <!-- OSGI Bundle Plugin -->
      <plugin>
        <groupId>org.apache.felix</groupId>
        <artifactId>maven-bundle-plugin</artifactId>
        <extensions>true</extensions>
        <configuration>
            <instructions>
                <Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
                <Bundle-Name>${project.artifactId}</Bundle-Name>
                <Bundle-Version>${project.version}</Bundle-Version>
                <Bundle-Activator>com.redhat.camel.java.MainActivation</Bundle-Activator>
                <Import-Package>
                    org.osgi.framework,
                    *;resolution:=optional
                </Import-Package>
                <DynamicImport-Package>*</DynamicImport-Package>
            </instructions>
        </configuration>
      </plugin>

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment