Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
EasyBeans Copyright (C) 2007 Bull S.A.S. Contact: easybeans@objectweb.org This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or any later version. This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA -------------------------------------------------------------------------- $Id: FelixFramework.java 3799 2008-08-06 12:50:51Z eyindanga $ --------------------------------------------------------------------------
 
 
 package org.ow2.util.maven.osgi.launcher.framework.felix;
 
 import java.io.File;
 import java.net.URL;
 import java.util.Map;
 
 import  org.apache.felix.framework.Felix;
 import  org.apache.felix.framework.cache.BundleCache;
 import  org.apache.felix.framework.util.FelixConstants;
 import  org.osgi.framework.Bundle;
 import  org.osgi.framework.BundleContext;
 import  org.osgi.framework.BundleException;
 import  org.osgi.framework.Constants;
Implementation of the Launcher for Apache Felix.

Author(s):
Florent BENOIT
 
 public class FelixFramework implements IFramework {

    
Logger.
 
     private static Log logger = LogFactory.getLog(FelixFramework.class);

    
List of Properties that will be extracted from the given property file.
 
     private static final String[] ALLOWED_PROPERTIES = new String[] {
         FelixConstants.FRAMEWORK_STARTLEVEL_PROP,
         FelixConstants.BUNDLE_STARTLEVEL_PROP,
         FelixConstants.LOG_LEVEL_PROP,
         Constants.FRAMEWORK_SYSTEMPACKAGES,
         Constants.FRAMEWORK_BOOTDELEGATION,
         BundleCache.CACHE_DIR_PROP
     };

    
Working directory.
 
     private File workingDirectory = null;

    
Map that will be used to configure Felix.
 
     private Map<StringStringconfigMap = null;

    
Link to the felix embedded object.
 
     private Felix felix = null;


    
Configure the Felix gateway by using the given configuration file.

Parameters:
config the URL of the configuration file to use
Throws:
FrameworkException if the configuration is not done
 
     public void configure(final URL configthrows FrameworkException {
         // No config, use default one.
         if (config == null) {
             setDefaultConfig();
             return;
        }
        // Use only given set of properties.
         = new HashMap<StringString>();
        // Open a connection on the given URL
        URLConnection urlConnection;
        try {
            urlConnection = config.openConnection();
        } catch (IOException e) {
            throw new FrameworkException("Cannot open the connection on the URL '" + config + "'."e);
        }
        urlConnection.setDefaultUseCaches(false);
        // Populate a properties object with the stream of the URL
        Properties prop = new Properties();
        InputStream is = null;
        try {
            is = urlConnection.getInputStream();
            prop.load(is);
        } catch (IOException e) {
            throw new FrameworkException("Cannot open the connection on the URL '" + config + "'."e);
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    .error("Cannot close the stream"e);
                }
            }
        }
        // Extract a given set of properties
        for (String propertyKey : ) {
            .put(propertyKeyprop.getProperty(propertyKey));
        }
    }

    
Set a default configuration.
    protected void setDefaultConfig() {
         = new HashMap<StringString>();
        .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");
        .put(BundleCache.CACHE_PROFILE_PROP, "felix-cache");
    }


    
Start the framework.

Throws:
FrameworkException if the framework can't be started.
    public void start() throws FrameworkException {
        File cache = new File(.getAbsolutePath(), .get(BundleCache.CACHE_DIR_PROP));
        // Cleanup
        if (cache.exists()) {
            FileUtils.delete(cache);
        }
        // Set the path of the cache
        .put(BundleCache.CACHE_PROFILE_DIR_PROP, cache.getAbsolutePath());
        // Configure the Felix instance to be embedded.
        // It avoids to call System.exit() at the end :)
        .put(FelixConstants.EMBEDDED_EXECUTION_PROP, "true");
        // Create Felix instance
         = new Felix(null);
        // Start it
        try {
            .start();
        } catch (BundleException e) {
            throw new FrameworkException("Cannot start Apache Felix"e);
        }
    }


    
Stop the framework.

Throws:
FrameworkException if the framework can't be stopped.
    public void stop() throws FrameworkException {
        // Cannot stop unknown felix object
        if ( == null) {
            throw new FrameworkException("No Apache Felix object configured");
        }
        // Stop felix instance
        try {
            .stop();
        } catch (BundleException e) {
            throw new FrameworkException("Cannot stop Apache Felix"e);
        }
    }

    
Gets the bundle context of the framework.

Returns:
the bundle context allowing to do some operations.
    public BundleContext getBundleContext() {
        if ( == null) {
            throw new IllegalStateException("Apache Felix is not yet started");
        }
        return .getBundleContext();
    }

    
Checks if the framework is running.

Returns:
true if the framework is running
    public boolean isRunning() {
        return  != null && .getState() == Bundle.ACTIVE;
    }


    
Sets the working directory.

Parameters:
workingDirectory the given directory used to store some files.
    public void setWorkingDirectory(final File workingDirectory) {
        this. = workingDirectory;
    }
New to GrepCode? Check out our FAQ X