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");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();
}
}
|
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.
| Anchor | ||||
|---|---|---|---|---|
|
Providing Host Application Services
Providing services from the host application to bundles inside the embedded Felix framework instance follows the basic approach laid out in above. The main complication for providing a host application service to bundles is the fact that both the host application and the bundles must be using the same class definition for the service interface classes. Since the host application cannot import classes from a bundle, this means that the service interface classes must be accessible on the class path, typically as part of the host application itself. The host application then must export the service interface package via the system bundle so that bundles installed into the embedded framework instance can import it. This is achieved using the {org.osgi.framework.system.packages] configuration property previously presented.
Consider the follow simple property lookup service:
| Code Block |
|---|
package host.service.lookup;
public class Lookup
{
public Object lookup(String name);
}
|
This package is simply part of the host application, which is potentially packaged into a JAR file and started with the "java -jar" command. Assuming that we have a host application bundle activator as previously presented, the following code snippet shows how the host application could create an embedded version of the Felix framework and provide the property lookup service to installed bundles:
| Code Block |
|---|
package host.core;
import java.util.Map;
import java.util.HashMap;
import host.service.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();
// 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();
}
}
|
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.
...
Providing Host Application Services
...
| Anchor | ||||
|---|---|---|---|---|
|
...