DUE TO SPAM, SIGN-UP IS DISABLED. Goto Selfserve wiki signup and request an account.
...
...
Embedding Felix into a host application is a simple way to provide a sophisticated extensibility mechanism (i.e., plugin system) to the host application. Embedding Felix is very similar to launching Felix as described above, the main difference is that the host application typically wants to interact with the framework instance and/or installed bundles/services from the outside. This is fairly easy to achieve with Felix, but there are some subtle issues to understand. This section presents the mechanisms for embedding Felix into a host application and the issues in doing so.
| Anchor | ||||
|---|---|---|---|---|
|
Host/Felix Interaction
|
Embedded Execution Configuration Property
When a Felix instance is embedded in a host application, the host application must inform the Felix instance that it is embedded. To do this, the host application sets the "felix.embedded.execution" configuration property to "true"; this can be accomplished in the same way that all configuration properties are set, i.e., passing it in to the Felix constructor via a property resolver. This property specifically controls whether or not the Felix instance will shutdown the JVM (i.e., call System.exit() when the framework is shutdown.
| Anchor | ||||
|---|---|---|---|---|
|
Host/Felix Interaction
In the section on launching Felix above, the start() method was used to start a Felix framework instance executing. The start() method accepts two arguments, the first being the configuration properties for the framework, the In the section on launching Felix above, the start() method was used to start a Felix framework instance executing. The start() method accepts two arguments, the first being the configuration properties for the framework, the second being a list of bundle activator instances. These bundle activator instances provide the foundation for how the host application interacts with the Felix framework.
...
| 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);
// Configure Createthe aFelix case-insensitiveinstance configurationto propertybe mapembedded.
Map configMap = new StringMap(false.put(FelixConstants.EMBEDDED_EXECUTION_PROP, "true");
// 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();
}
}
|
...
| 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.FelixConstants; 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 a case-insensitive configuration property map. Map configMap = new StringMap(false); // Configure the Felix instance to be embedded. Map configMap = new StringMap(false.put(FelixConstants.EMBEDDED_EXECUTION_PROP, "true"); // 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(); } } |
...
| Code Block |
|---|
package host.core; import java.util.Map; import host.service.command.Command; import org.apache.felix.framework.Felix; import org.apache.felix.framework.util.FelixConstants; import org.apache.felix.framework.util.StringMap; import org.apache.felix.framework.util.MutablePropertyResolverImpl; import org.apache.felix.framework.cache.BundleCache; 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); // Configure the Felix instance to be embedded. configMap.put(FelixConstants.EMBEDDED_EXECUTION_PROP, "true"); // Add the bundle 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( 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(); } } |
...