DUE TO SPAM, SIGN-UP IS DISABLED. Goto Selfserve wiki signup and request an account.
...
- Defining some configuration properties.
- Creating an instance of
org.apache.felix.framework.Felixwith configuration properties. - Invoking the
org.apache.felix.framework.Felix.start()method with the configuration properties.
The only configuration properties that are actually required to start Felix are ones that tell it where/how to locate the bundle cache profile directory where installed bundles will be cached. Felix' bundle cache implementation allows you to configure the location where bundles are cached using configuration properties. At a minimum, either a bundle cache profile name or directory must be specified; see the bundle cache document for more detailed information on configuring the bundle cache.
...
| Code Block |
|---|
public static void main(String[] argv) throws Exception
{
// (1) Load system properties.
Main.loadSystemProperties();
// (2) Read configuration properties.
Properties configProps = Main.loadConfigProperties();
// (3) Copy framework properties from the system properties.
Main.copySystemProperties(configProps);
// (4) See if the profile name property was specified.
String profileName = configProps.getProperty(BundleCache.CACHE_PROFILE_PROP);
// (4) See if the profile directory property was specified.
String profileDirName = configProps.getProperty(BundleCache.CACHE_PROFILE_DIR_PROP);
// Print welcome banner.
System.out.println("\nWelcome to Felix.");
System.out.println("=================\n");
// (5) If no profile or profile directory is specified in the
// properties, then ask for a profile name.
if ((profileName == null) && (profileDirName == null))
{
System.out.print("Enter profile name: ");
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
try
{
profileName = in.readLine();
}
catch (IOException ex)
{
System.err.println("Could not read input.");
System.exit(-1);
}
System.out.println("");
if (profileName.length() != 0)
{
configProps.setProperty(BundleCache.CACHE_PROFILE_PROP, profileName);
}
}
// (6) A profile directory or name must be specified.
if ((profileDirName == null) && (profileName.length() == 0))
{
System.err.println("You must specify a profile name or directory.");
System.exit(-1);
}
try
{
// (7) Now create an instance of the framework.
m_felix = new Felix();
m_felix.start(
new MutablePropertyResolverImpl(new StringMap(configProps, false), null),;
nullm_felix.start();
}
catch (Exception ex)
{
System.err.println("Could not create framework: " + ex);
ex.printStackTrace();
System.exit(-1);
}
}
|
...
- Load any system properties specified in the
system.propertiesfile; this file is typically located in theconf/directory of the Felix installation directory, but it can be specified directly using thefelix.system.propertiessystem property. This file is not needed to launch Felix and is provided merely for convenience when system properties must be specified. The file is a standard Java properties file, but it also supports property substitution using${<property-name} syntax. Property substitution can be nested; only system properties will be used for substitution. - Load any configuration properties specified in the
config.propertiesfile; this file is typically located in theconf/directory of the Felix installation directory, but it can be specified directly using thefelix.config.propertiessystem property. This file is used to configure the Felix instance created by the launcher. The file is a standard Java properties file, but it also supports property substitution using "${<property-name}" syntax. Property substitution can be nested; configuration and system properties will be used for substitution with configuration properties having precedence. - For convenience, any configuration properties that are set as system properties will be copied into the set of configuration properties to provide an easy way to add to or override configuration properties specified in the
config.propertiesfile. - Try to load the profile name or profile directory configuration properties. At least one of these must be specified so that the bundle cache knows where to save installed bundles.
- If either the profile name or profile directory configuration property has not been specified, then ask the user to specify a profile name and add it to the current set of configuration properties.
- Error if there is no profile name or profile directory.
- Create the Felix instance and call
start(), passing in the configuration properties, and callstart().
The framework is not active until the start() method is called. If no shell bundles are specified in the config.properties file or if there is difficulty locating the shell bundles that are specified, then it will appear as if the framework is hung, but it is actually running without any way to interact with it since the shell bundles provide the only means of interaction.
...
| Code Block |
|---|
package example;
import java.util.Map;
import org.osgi.framework.Constants;
import org.apache.felix.framework.Felix;
import org.apache.felix.framework.cache.BundleCache;
import org.apache.felix.framework.util.MutablePropertyResolverImpl;
import org.apache.felix.framework.util.StringMap;
import org.apache.felix.framework.util.FelixConstants;
public class Main
{
private static Felix m_felix = null;
public static void main(String[] argv) throws Exception
{
// Print welcome banner.
System.out.println("\nWelcome to Felix.");
System.out.println("=================\n");
Map configMap = new StringMap(false);
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");
configMap.put(FelixConstants.AUTO_START_PROP + ".1",
"file:bundle/org.apache.felix.shell-0.8.0-SNAPSHOT.jar " +
"file:bundle/org.apache.felix.shell.tui-0.8.0-SNAPSHOT.jar");
configMap.put(BundleCache.CACHE_PROFILE_DIR_PROP, "cache");
try
{
// Now create an instance of the framework.
m_felix = new Felix(configMap, null);
m_felix.start();
}
newcatch MutablePropertyResolverImpl(configMapException ex),
{
null);
System.err.println("Could not create }
catch (Exception ex)
{
System.err.println("Could not create framework: framework: " + ex);
ex.printStackTrace();
System.exit(-1);
}
}
}
|
...
| Code Block |
|---|
m_felix = new Felix(configMap, null);
m_felix.start(
new MutablePropertyResolverImpl(configMap),
null);
|
The last steps create the framework instance and start it. The configuration property map is converted to an instance of a PropertyResolver before being passed into the Felix.start() method.
...
| 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
{
// NowCreate create an instance of the framework.host activator;
m_felixactivator = new FelixHostActivator();
List //list Create= hostnew activatorArrayList();
list.add(m_activator = new HostActivator();
// Now startcreate Felixan instance withof ourthe configframework propertieswith
// our config properties and activator.
m_felix.start(
= new Felix(configMap, list);
new MutablePropertyResolverImpl(configMap),
// Now start Felix.
m_activatorfelix.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.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.
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
{
// NowCreate create an instance of the framework.host activator;
m_felixactivator = new FelixHostActivator(m_lookupMap);
List //list Create= hostnew activatorArrayList();
list.add(m_activator = new HostActivator(m_lookupMap);
// Now startcreate Felixan instance withof ourthe configframework propertieswith
// our config properties and activator.
m_felix.start(
= new Felix(configMap, list);
new MutablePropertyResolverImpl(configMap),
// Now start Felix.
m_activatorfelix.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.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// ofCreate thehost framework.activator;
m_felixactivator = new FelixHostActivator(m_lookupMap);
List //list Create= hostnew activatorArrayList();
list.add(m_activator = new HostActivator(m_lookupMap);
// Now startcreate Felixan instance withof ourthe configframework propertieswith
// our config properties and activator.
m_felix.start(
= new Felix(configMap, list);
new MutablePropertyResolverImpl(configMap),
// Now start Felix.
m_activatorfelix.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.shutdown();
}
}
|
...