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);
// Configure the Felix instance to be embedded.
configMap.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
{
// Create host activator;
m_activator = new HostActivator();
List list = new ArrayList();
list.add(m_activator);
// Now create an instance of the framework with
// our configuration properties and activator.
m_felix = new Felix(configMap, list);
// Now start Felix instance.
m_felix.start();
}
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.getBundles();
}
public void shutdownApplication()
{
// Shut down the felix framework when stopping the
// host application.
m_felix.shutdownstopAndWait();
}
}
|
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.List;
import java.util.ArrayList;
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.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);
// Configure the Felix instance to be embedded.
configMap.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
{
// Create host activator;
m_activator = new HostActivator(m_lookupMap);
List list = new ArrayList();
list.add(m_activator);
// Now create an instance of the framework with
// our configuration properties and activator.
m_felix = new Felix(configMap, list);
// Now start Felix instance.
m_felix.start();
}
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.shutdownstopAndWait();
}
}
|
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.List;
import java.util.ArrayList;
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.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
{
// Create host activator;
m_activator = new HostActivator(m_lookupMap);
List list = new ArrayList();
list.add(m_activator);
// Now create an instance of the framework with
// our configuration properties and activator.
m_felix = new Felix(configMap, list);
// Now start Felix instance.
m_felix.start();
}
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.shutdownstopAndWait();
}
}
|
The above example is overly simplistic with respect to concurrency issues and error conditions, but it demonstrates the overall approach for using bundle-provided services from the host application. Note, to compile the above code you will need to compile against the Felix framework and Felix OSGi compendium JAR files, since the ServiceTracker classes are included in the compendium JAR file, not the framework JAR file.
...