Search in sources :

Example 1 with Felix

use of org.apache.felix.framework.Felix in project felix by apache.

the class FrameworkService method doStart.

private void doStart() throws Exception {
    Felix tmp = new Felix(createConfig());
    tmp.start();
    this.felix = tmp;
    log("OSGi framework started", null);
}
Also used : Felix(org.apache.felix.framework.Felix)

Example 2 with Felix

use of org.apache.felix.framework.Felix in project felix by apache.

the class Junit4osgiPlugin method execute.

/**
 * Executes the plug-in.
 * @throws MojoFailureException when the test execution failed.
 * @see org.apache.maven.plugin.AbstractMojo#execute()
 */
public void execute() throws MojoFailureException {
    if (skip) {
        getLog().info("Tests are skipped");
        return;
    }
    List bundles = parseBundleList();
    bundles.addAll(getTestBundle());
    List activators = new ArrayList();
    m_logService = new LogServiceImpl();
    if (m_logEnable) {
        // Starts the log service if enabled
        activators.add(m_logService);
    } else {
        getLog().info("Log Service disabled");
    }
    activators.add(new Installer(m_pluginArtifacts, bundles, m_project, m_deployProjectArtifact));
    felixConf = new HashMap();
    felixConf.put("felix.systembundle.activators", activators);
    felixConf.put("org.osgi.framework.storage.clean", "onFirstInit");
    felixConf.put("ipojo.log.level", "WARNING");
    // Use a boot delagation to share classes between the host and the embedded Felix.
    // The cobertura package is used during code coverage collection
    // felixConf.put("org.osgi.framework.bootdelegation", "net.sourceforge.cobertura.coveragedata");
    felixConf.put("org.osgi.framework.system.packages.extra", "org.osgi.service.log;version=1.3, junit.framework;version=1.3");
    // felixConf.put("org.osgi.framework.system.packages.extra", "org.osgi.service.log, junit.framework");
    felixConf.put("org.osgi.framework.storage", m_targetDir.getAbsolutePath() + "/felix-cache");
    felixConf.put(Constants.FRAMEWORK_BUNDLE_PARENT, Constants.FRAMEWORK_BUNDLE_PARENT_FRAMEWORK);
    if (configuration != null) {
        felixConf.putAll(configuration);
    // Check boot delegation
    // String bd = (String) felixConf.get("org.osgi.framework.bootdelegation");
    // //            if (bd.indexOf("junit.framework") == -1) {
    // //                bd.concat(", junit.framework");
    // //            }
    // //            if (bd.indexOf("org.osgi.service.log") == -1) {
    // //                bd.concat(", org.osgi.service.log");
    // //            }
    // if (bd.indexOf("net.sourceforge.cobertura.coveragedata") == -1) {
    // bd.concat(", net.sourceforge.cobertura.coveragedata");
    // }
    }
    System.out.println("");
    System.out.println("-------------------------------------------------------");
    System.out.println(" T E S T S");
    System.out.println("-------------------------------------------------------");
    Felix felix = new Felix(felixConf);
    try {
        felix.start();
    } catch (BundleException e) {
        e.printStackTrace();
    }
    getLog().info("Felix started - Waiting for stability");
    waitForStability(felix.getBundleContext());
    getLog().info("Bundle Stability Reached - Waiting for runner service");
    Object runner = waitForRunnerService(felix.getBundleContext());
    if (runner == null) {
        throw new MojoFailureException("Cannot intialize the testing framework");
    }
    getLog().info("Runner Service available");
    invokeRun(runner, felix.getBundleContext());
    try {
        felix.stop();
        felix.waitForStop(5000);
        // Delete felix-cache
        File cache = new File(m_targetDir.getAbsolutePath() + "/felix-cache");
        cache.delete();
    } catch (Exception e) {
        getLog().error(e);
    }
    if (m_totalErrors > 0 || m_totalFailures > 0) {
        if (!testFailureIgnore) {
            throw new MojoFailureException("There are test failures. \n\n" + "Please refer to " + m_reportsDirectory.getAbsolutePath() + " for the individual test results.");
        } else {
            getLog().warn("There are test failures. \n\n" + "Please refer to " + m_reportsDirectory.getAbsolutePath() + " for the individual test results.");
        }
    }
}
Also used : LogServiceImpl(org.apache.felix.ipojo.junit4osgi.plugin.log.LogServiceImpl) HashMap(java.util.HashMap) Felix(org.apache.felix.framework.Felix) ArrayList(java.util.ArrayList) MojoFailureException(org.apache.maven.plugin.MojoFailureException) ArrayList(java.util.ArrayList) List(java.util.List) BundleException(org.osgi.framework.BundleException) JarFile(java.util.jar.JarFile) File(java.io.File) BundleException(org.osgi.framework.BundleException) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) MalformedURLException(java.net.MalformedURLException) MojoFailureException(org.apache.maven.plugin.MojoFailureException)

Example 3 with Felix

use of org.apache.felix.framework.Felix in project felix by apache.

the class Service method init.

public void init(InstallationLayout suppliedLayout, String[] args) throws Exception {
    if (!(suppliedLayout instanceof FelixLayout)) {
        this.layout = new FelixLayout(suppliedLayout);
    } else {
        this.layout = (FelixLayout) suppliedLayout;
    }
    configationProperties = readConfigProperties();
    instance = new Felix(new StringMap(configationProperties, false), null);
}
Also used : StringMap(org.apache.felix.framework.util.StringMap) Felix(org.apache.felix.framework.Felix)

Example 4 with Felix

use of org.apache.felix.framework.Felix in project Lucee by lucee.

the class CFMLEngineFactory method getFelix.

public Felix getFelix(final File cacheRootDir, Map<String, Object> config) throws BundleException {
    if (config == null)
        config = new HashMap<String, Object>();
    // Log Level
    // 1 = error, 2 = warning, 3 = information, and 4 = debug
    int logLevel = 1;
    String strLogLevel = getSystemPropOrEnvVar("felix.log.level", null);
    if (Util.isEmpty(strLogLevel))
        strLogLevel = (String) config.get("felix.log.level");
    if (!Util.isEmpty(strLogLevel)) {
        if ("0".equalsIgnoreCase(strLogLevel))
            logLevel = 0;
        else if ("error".equalsIgnoreCase(strLogLevel) || "1".equalsIgnoreCase(strLogLevel))
            logLevel = 1;
        else if ("warning".equalsIgnoreCase(strLogLevel) || "2".equalsIgnoreCase(strLogLevel))
            logLevel = 2;
        else if ("info".equalsIgnoreCase(strLogLevel) || "information".equalsIgnoreCase(strLogLevel) || "3".equalsIgnoreCase(strLogLevel))
            logLevel = 3;
        else if ("debug".equalsIgnoreCase(strLogLevel) || "4".equalsIgnoreCase(strLogLevel))
            logLevel = 4;
    }
    config.put("felix.log.level", "" + logLevel);
    // storage clean
    final String storageClean = (String) config.get(Constants.FRAMEWORK_STORAGE_CLEAN);
    if (Util.isEmpty(storageClean))
        config.put(Constants.FRAMEWORK_STORAGE_CLEAN, Constants.FRAMEWORK_STORAGE_CLEAN_ONFIRSTINIT);
    // parent classLoader
    final String parentClassLoader = (String) config.get(Constants.FRAMEWORK_BUNDLE_PARENT);
    if (Util.isEmpty(parentClassLoader))
        config.put(Constants.FRAMEWORK_BUNDLE_PARENT, Constants.FRAMEWORK_BUNDLE_PARENT_FRAMEWORK);
    else
        config.put(Constants.FRAMEWORK_BUNDLE_PARENT, BundleUtil.toFrameworkBundleParent(parentClassLoader));
    // felix.cache.rootdir
    boolean isNew = false;
    if (!cacheRootDir.exists()) {
        cacheRootDir.mkdirs();
        isNew = true;
    }
    if (cacheRootDir.isDirectory())
        config.put("felix.cache.rootdir", cacheRootDir.getAbsolutePath());
    if (logger != null)
        config.put("felix.log.logger", logger);
    // TODO felix.log.logger
    // remove any empty record, this can produce trouble
    {
        final Iterator<Entry<String, Object>> it = config.entrySet().iterator();
        Entry<String, Object> e;
        Object v;
        while (it.hasNext()) {
            e = it.next();
            v = e.getValue();
            if (v == null || v.toString().isEmpty())
                it.remove();
        }
    }
    final StringBuilder sb = new StringBuilder("loading felix with config:");
    final Iterator<Entry<String, Object>> it = config.entrySet().iterator();
    Entry<String, Object> e;
    while (it.hasNext()) {
        e = it.next();
        sb.append("\n- ").append(e.getKey()).append(':').append(e.getValue());
    }
    log(Logger.LOG_INFO, sb.toString());
    felix = new Felix(config);
    try {
        felix.start();
    } catch (BundleException be) {
        // this could be cause by an invalid felix cache, so we simply delete it and try again
        if (!isNew && "Error creating bundle cache.".equals(be.getMessage())) {
            Util.deleteContent(cacheRootDir, null);
        }
    }
    return felix;
}
Also used : ZipEntry(java.util.zip.ZipEntry) Entry(java.util.Map.Entry) JarEntry(java.util.jar.JarEntry) HashMap(java.util.HashMap) Felix(org.apache.felix.framework.Felix) Iterator(java.util.Iterator) BundleException(org.osgi.framework.BundleException)

Example 5 with Felix

use of org.apache.felix.framework.Felix in project Lucee by lucee.

the class BundleLoader method loadBundles.

/**
 * build (if necessary) a bundle and load it
 *
 * @param engFac
 * @param cacheRootDir
 * @param jarDirectory
 * @param rc
 * @param old
 * @return
 * @throws IOException
 * @throws BundleException
 */
public static BundleCollection loadBundles(final CFMLEngineFactory engFac, final File cacheRootDir, final File jarDirectory, final File rc, final BundleCollection old) throws IOException, BundleException {
    // TODO this should work in any case, but we should still improve this code
    final JarFile jf = new JarFile(rc);
    try {
        // Manifest
        final Manifest mani = jf.getManifest();
        if (mani == null)
            throw new IOException("lucee core [" + rc + "] is invalid, there is no META-INF/MANIFEST.MF File");
        final Attributes attrs = mani.getMainAttributes();
        // default properties
        final Properties defProp = loadDefaultProperties(jf);
        // Get data from Manifest and default.properties
        // Lucee Core Version
        // String rcv = unwrap(defProp.getProperty("lucee.core.version"));
        // if(Util.isEmpty(rcv)) throw new IOException("lucee core ["+rc+"] is invalid, no core version is defined in the {Lucee-Core}/default.properties File");
        // int version = CFMLEngineFactory.toInVersion(rcv);
        // read the config from default.properties
        final Map<String, Object> config = new HashMap<String, Object>();
        {
            final Iterator<Entry<Object, Object>> it = defProp.entrySet().iterator();
            Entry<Object, Object> e;
            String k;
            while (it.hasNext()) {
                e = it.next();
                k = (String) e.getKey();
                if (!k.startsWith("org.") && !k.startsWith("felix."))
                    continue;
                config.put(k, CFMLEngineFactorySupport.removeQuotes((String) e.getValue(), true));
            }
        }
        // close all bundles
        Felix felix;
        if (old != null) {
            removeBundlesEL(old);
            felix = old.felix;
            // stops felix (wait for it)
            BundleUtil.stop(felix, false);
            felix = engFac.getFelix(cacheRootDir, config);
        } else
            felix = engFac.getFelix(cacheRootDir, config);
        final BundleContext bc = felix.getBundleContext();
        // get bundle needed for that core
        final String rb = attrs.getValue("Require-Bundle");
        if (Util.isEmpty(rb))
            throw new IOException("lucee core [" + rc + "] is invalid, no Require-Bundle defintion found in the META-INF/MANIFEST.MF File");
        // get fragments needed for that core (Lucee specific Key)
        final String rbf = attrs.getValue("Require-Bundle-Fragment");
        // load Required/Available Bundles
        // Require-Bundle
        final Map<String, String> requiredBundles = readRequireBundle(rb);
        // Require-Bundle-Fragment
        final Map<String, String> requiredBundleFragments = readRequireBundle(rbf);
        final Map<String, File> availableBundles = loadAvailableBundles(jarDirectory);
        // deploys bundled bundles to bundle directory
        // deployBundledBundles(jarDirectory, availableBundles);
        // Add Required Bundles
        Entry<String, String> e;
        File f;
        String id;
        final List<Bundle> bundles = new ArrayList<Bundle>();
        Iterator<Entry<String, String>> it = requiredBundles.entrySet().iterator();
        while (it.hasNext()) {
            e = it.next();
            id = e.getKey() + "|" + e.getValue();
            f = availableBundles.get(id);
            // StringBuilder sb=new StringBuilder();
            if (f == null) {
            /*sb.append(id+"\n");
					Iterator<String> _it = availableBundles.keySet().iterator();
					while(_it.hasNext()){
						sb.append("- "+_it.next()+"\n");
					}
					throw new RuntimeException(sb.toString());*/
            }
            if (f == null)
                f = engFac.downloadBundle(e.getKey(), e.getValue(), null);
            bundles.add(BundleUtil.addBundle(engFac, bc, f, null));
        }
        // Add Required Bundle Fragments
        final List<Bundle> fragments = new ArrayList<Bundle>();
        it = requiredBundleFragments.entrySet().iterator();
        while (it.hasNext()) {
            e = it.next();
            id = e.getKey() + "|" + e.getValue();
            f = availableBundles.get(id);
            if (f == null)
                // if identification is not defined, it is loaded from the CFMLEngine
                f = engFac.downloadBundle(e.getKey(), e.getValue(), null);
            fragments.add(BundleUtil.addBundle(engFac, bc, f, null));
        }
        // Add Lucee core Bundle
        Bundle bundle;
        // bundles.add(bundle = BundleUtil.addBundle(engFac, bc, rc,null));
        bundle = BundleUtil.addBundle(engFac, bc, rc, null);
        // Start the bundles
        BundleUtil.start(engFac, bundles);
        BundleUtil.start(engFac, bundle);
        return new BundleCollection(felix, bundle, bundles);
    } finally {
        if (jf != null)
            try {
                jf.close();
            } catch (final IOException ioe) {
            }
    }
}
Also used : HashMap(java.util.HashMap) Bundle(org.osgi.framework.Bundle) Attributes(java.util.jar.Attributes) ArrayList(java.util.ArrayList) IOException(java.io.IOException) JarFile(java.util.jar.JarFile) Manifest(java.util.jar.Manifest) Properties(java.util.Properties) Entry(java.util.Map.Entry) ZipEntry(java.util.zip.ZipEntry) Felix(org.apache.felix.framework.Felix) Iterator(java.util.Iterator) JarFile(java.util.jar.JarFile) File(java.io.File) BundleContext(org.osgi.framework.BundleContext)

Aggregations

Felix (org.apache.felix.framework.Felix)7 HashMap (java.util.HashMap)3 BundleException (org.osgi.framework.BundleException)3 File (java.io.File)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 Iterator (java.util.Iterator)2 Entry (java.util.Map.Entry)2 Properties (java.util.Properties)2 JarFile (java.util.jar.JarFile)2 ZipEntry (java.util.zip.ZipEntry)2 BundleContext (org.osgi.framework.BundleContext)2 InputStream (java.io.InputStream)1 MalformedURLException (java.net.MalformedURLException)1 List (java.util.List)1 Attributes (java.util.jar.Attributes)1 JarEntry (java.util.jar.JarEntry)1 JarInputStream (java.util.jar.JarInputStream)1 Manifest (java.util.jar.Manifest)1 ServletContext (javax.servlet.ServletContext)1