DUE TO SPAM, SIGN-UP IS DISABLED. Goto Selfserve wiki signup and request an account.
...
| Code Block |
|---|
public class HostApplication
{
private HostActivator m_activator = null;
private Felix m_felix = null;
public HostApplication()
{
// Create a case-insensitive configuration property map.
Map configMap = new StringMap(false);
// Add core OSGi packages to be exported from the class path
// via the system bundle.
configMap.put(Constants.FRAMEWORK_SYSTEMPACKAGES,
"org.osgi.framework; version=1.3.0," +
"org.osgi.service.packageadmin; version=1.2.0," +
"org.osgi.service.startlevel; version=1.0.0," +
"org.osgi.service.url; version=1.0.0");
// Explicitly specify the directory to use for caching bundles.
configMap.put(BundleCache.CACHE_PROFILE_DIR_PROP, "cache");
try
{
// Now create an instance of the framework.
m_felix = new Felix();
// Create host activator;
m_activator = new HostActivator();
// Now start Felix instance with our config properties
// and activator.
m_felix.start(
new MutablePropertyResolverImpl(configMap),
m_activator);
}
catch (Exception ex)
{
System.err.println("Could not create framework: " + ex);
ex.printStackTrace();
}
}
public Bundle[] getInstalledBundles()
{
// Use the system bundle activator to gain external
// access to the set of installed bundles.
return m_activator.getContext().getBundles();
}
public void shutdownApplication()
{
// Shut down the felix framework when stopping the
// host application.
m_felix.shutdown();
}
}
|
Notice how the HostApplication.getInstalledBundles() method uses its activator instance to get access to the System Bundle's context in order to interact with the embedded Felix framework instance. This approach provides the foundation for all interaction between the host application and the embedded framework instance.
...
| Code Block |
|---|
package host.core; import java.util.Map; import java.util.HashMap; import host.service.lookup.Lookup; import org.apache.felix.framework.Felix; import org.apache.felix.framework.util.StringMap; import org.apache.felix.framework.util.MutablePropertyResolverImpl; import org.apache.felix.framework.cache.BundleCache; public class HostApplication { private HostActivator m_activator = null; private Felix m_felix = null; private Map m_lookupMap = new HashMap(); public HostApplication() { // Initialize the map for the property lookup service. m_lookupMap.put("name1", "value1"); m_lookupMap.put("name2", "value2"); m_lookupMap.put("name3", "value3"); m_lookupMap.put("name4", "value4"); // Create a case-insensitive configuration property map. Map configMap = new StringMap(false); // Add the host provided service interface package and the core OSGi // packages to be exported from the class path via the system bundle. configMap.put(Constants.FRAMEWORK_SYSTEMPACKAGES, "org.osgi.framework; version=1.3.0," + "org.osgi.service.packageadmin; version=1.2.0," + "org.osgi.service.startlevel; version=1.0.0," + "org.osgi.service.url; version=1.0.0," + "host.service.lookup; version=1.0.0"); // Explicitly specify the directory to use for caching bundles. configMap.put(BundleCache.CACHE_PROFILE_DIR_PROP, "cache"); try { // Now create an instance of the framework. m_felix = new Felix(); // Create host activator; m_activator = new HostActivator(m_lookupMap); // Now start Felix instance with our config properties // and activator. m_felix.start( new MutablePropertyResolverImpl(configMap), m_activator); } catch (Exception ex) { System.err.println("Could not create framework: " + ex); ex.printStackTrace(); } } } public void shutdownApplication() { // Shut down the felix framework when stopping the // host application. m_felix.shutdown(); } } |
Rather than having the Rather than having the host application bundle activator register the service, it is also possible for the the host application to simply get the bundle context from the bundle activator and register the service directly, but the presented approach is perhaps a little cleaner since it allows the host application to register/unregister the service when the system bundle starts/stops.
...
| Code Block |
|---|
package host.core; import java.util.Map; import host.service.command.lookupCommand; import org.apache.felix.framework.Felix; import org.apache.felix.framework.util.StringMap; import org.apache.felix.framework.util.MutablePropertyResolverImpl; import org.apache.felix.framework.cache.BundleCache; public class import org.osgi.util.tracker.ServiceTracker; public class HostApplication { private HostActivator m_activator = null; private Felix m_felix = null; private ServiceTracker m_tracker = null; public HostApplication() { // Create a case-insensitive configuration property map. Map configMap = new StringMap(false); // Add the hostbundle provided service interface package and the core OSGi // packages to be exported from the class path via the system bundle. configMap.put(Constants.FRAMEWORK_SYSTEMPACKAGES, "org.osgi.framework; version=1.3.0," + "org.osgi.service.packageadmin; version=1.2.0," + "org.osgi.service.startlevel; version=1.0.0," + "org.osgi.service.url; version=1.0.0," + "host.service.command; version=1.0.0"); // Explicitly specify the directory to use for caching bundles. configMap.put(BundleCache.CACHE_PROFILE_DIR_PROP, "cache"); try { // Now create an instance of the framework. m_felix = new Felix(); // Create host activator; m_activator = new HostActivator(m_lookupMap); // Now start Felix instance with our config properties // and activator. m_felix.start( new MutablePropertyResolverImpl(configMap), m_activator); } catch (Exception ex) { System.err.println("Could not create framework: " + ex); ex.printStackTrace(); } m_tracker = new } ServiceTracker( public boolean execute(String name, String commandline)m_activator.getContext(), Command.class.getName(), null); m_tracker.open(); } public boolean execute(String name, String commandline) { // See if any of the currently tracked command services // match the specified command name, if so then execute it. Object[] services = m_tracker.getServices(); for (int i = 0; (services != null) && (i < services.length); i++) { try { if (((Command) services[i]).getName().equals(name)) { return ((Command) services[i]).execute(commandline); } } catch (Exception ex) { // Since the services returned by the tracker could become // invalid at any moment, we will catch all exceptions, log // a message, and then ignore faulty services. System.err.println(ex); } } return false; } public void shutdownApplication() { // Shut down the felix framework when stopping the // host application. m_felix.shutdown(); } } |