Search in sources :

Example 61 with BundleException

use of org.osgi.framework.BundleException in project aries by apache.

the class BundleFrameworkFactoryImpl method createBundleFramework.

public BundleFramework createBundleFramework(BundleContext bc, BundleFrameworkConfiguration config) throws BundleException {
    BundleFramework framework = null;
    ServiceReference sr = bc.getServiceReference(CompositeBundleFactory.class.getName());
    if (sr != null) {
        CompositeBundleFactory cbf = (CompositeBundleFactory) bc.getService(sr);
        CompositeBundle compositeBundle = cbf.installCompositeBundle(config.getFrameworkProperties(), config.getFrameworkID(), config.getFrameworkManifest());
        framework = new BundleFrameworkImpl(compositeBundle);
    } else
        throw new BundleException("Failed to obtain framework factory service");
    return framework;
}
Also used : BundleFramework(org.apache.aries.application.management.spi.framework.BundleFramework) BundleException(org.osgi.framework.BundleException) CompositeBundleFactory(org.osgi.service.framework.CompositeBundleFactory) CompositeBundle(org.osgi.service.framework.CompositeBundle) ServiceReference(org.osgi.framework.ServiceReference)

Example 62 with BundleException

use of org.osgi.framework.BundleException in project aries by apache.

the class ApplicationContextManagerImpl method uninstall.

private void uninstall(ApplicationContextImpl app) {
    Set<Bundle> bundles = app.getApplicationContent();
    for (Bundle b : bundles) {
        try {
            b.uninstall();
        } catch (BundleException be) {
        // TODO ignoring this feels wrong, but I'm not sure how to communicate to the caller multiple failures. 
        }
    }
    app.setState(ApplicationState.UNINSTALLED);
}
Also used : Bundle(org.osgi.framework.Bundle) BundleException(org.osgi.framework.BundleException)

Example 63 with BundleException

use of org.osgi.framework.BundleException in project aries by apache.

the class AriesRepositoryGenerator method startFramework.

/**
   * Start OSGi framework and install the necessary bundles
   * @return
   * @throws BundleException
   */
public BundleContext startFramework() throws BundleException {
    ServiceLoader<FrameworkFactory> factoryLoader = ServiceLoader.load(FrameworkFactory.class, getClass().getClassLoader());
    Iterator<FrameworkFactory> factoryIterator = factoryLoader.iterator();
    //Map<String, String> osgiPropertyMap = new HashMap<String, String>();
    if (!!!factoryIterator.hasNext()) {
        System.out.println("Unable to locate the osgi jar");
    }
    try {
        FrameworkFactory frameworkFactory = factoryIterator.next();
        framework = frameworkFactory.newFramework(Collections.EMPTY_MAP);
    } catch (ServiceConfigurationError sce) {
        sce.printStackTrace();
    }
    framework.init();
    framework.start();
    // install the bundles in the current directory
    Collection<Bundle> installedBundles = new ArrayList<Bundle>();
    File bundleDir = new File(".");
    File[] jars = bundleDir.listFiles();
    try {
        for (File jar : jars) {
            if (jar.isFile() && (jar.getName().endsWith(".jar"))) {
                String location = URLDecoder.decode(jar.toURI().toURL().toExternalForm(), "UTF-8");
                if (shouldInstall(location, getIgnoreList())) {
                    installedBundles.add(framework.getBundleContext().installBundle("reference:" + location));
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    ServiceReference paRef = framework.getBundleContext().getServiceReference(PackageAdmin.class.getCanonicalName());
    if (paRef != null) {
        try {
            PackageAdmin admin = (PackageAdmin) framework.getBundleContext().getService(paRef);
            admin.resolveBundles(installedBundles.toArray(new Bundle[installedBundles.size()]));
        } finally {
            framework.getBundleContext().ungetService(paRef);
        }
    } else {
        System.out.println("Unable to find the service reference for package admin");
    }
    //loop through the list of installed bundles to start them
    for (Bundle bundle : installedBundles) {
        try {
            if (bundle.getHeaders().get(Constants.FRAGMENT_HOST) == null) {
                //start the bundle using its activiation policy
                bundle.start(Bundle.START_ACTIVATION_POLICY);
            } else {
            //nothing to start, we have got a fragment
            }
        } catch (BundleException e) {
            e.printStackTrace();
            continue;
        }
    }
    return framework.getBundleContext();
}
Also used : Bundle(org.osgi.framework.Bundle) ArrayList(java.util.ArrayList) ServiceConfigurationError(java.util.ServiceConfigurationError) BundleException(org.osgi.framework.BundleException) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) IOException(java.io.IOException) FrameworkFactory(org.osgi.framework.launch.FrameworkFactory) ServiceReference(org.osgi.framework.ServiceReference) PackageAdmin(org.osgi.service.packageadmin.PackageAdmin) BundleException(org.osgi.framework.BundleException) File(java.io.File)

Example 64 with BundleException

use of org.osgi.framework.BundleException in project aries by apache.

the class ApplicationContextManagerImpl method unbindBundleFrameworkManager.

public void unbindBundleFrameworkManager(BundleFrameworkManager bfm) {
    LOGGER.debug(LOG_ENTRY, "unbindBundleFrameworkManager", bfm);
    List<AriesApplicationContext> appContexts = new ArrayList<AriesApplicationContext>();
    synchronized (_appToContextMap) {
        appContexts.addAll(_appToContextMap.values());
    }
    for (AriesApplicationContext c : appContexts) {
        try {
            ((ApplicationContextImpl) c).close();
        } catch (BundleException e) {
            LOGGER.debug(LOG_EXCEPTION, e);
        }
    }
    LOGGER.debug(LOG_EXIT, "unbindBundleFrameworkManager");
}
Also used : AriesApplicationContext(org.apache.aries.application.management.AriesApplicationContext) ArrayList(java.util.ArrayList) BundleException(org.osgi.framework.BundleException)

Example 65 with BundleException

use of org.osgi.framework.BundleException in project aries by apache.

the class ApplicationContextManagerImpl method update.

public AriesApplicationContext update(AriesApplication app, DeploymentMetadata oldMetadata) throws UpdateException {
    ApplicationContextImpl oldCtx = _appToContextMap.get(app);
    if (oldCtx == null) {
        throw new IllegalArgumentException("AriesApplication " + app.getApplicationMetadata().getApplicationSymbolicName() + "/" + app.getApplicationMetadata().getApplicationVersion() + " cannot be updated because it is not installed");
    }
    uninstall(oldCtx);
    try {
        AriesApplicationContext newCtx = getApplicationContext(app);
        if (oldCtx.getApplicationState() == ApplicationState.ACTIVE) {
            newCtx.start();
        }
        return newCtx;
    } catch (BundleException e) {
        throw new UpdateException("Update failed: " + e.getMessage(), e, false, null);
    } catch (ManagementException e) {
        throw new UpdateException("Update failed: " + e.getMessage(), e, false, null);
    }
}
Also used : ManagementException(org.apache.aries.application.management.ManagementException) AriesApplicationContext(org.apache.aries.application.management.AriesApplicationContext) BundleException(org.osgi.framework.BundleException) UpdateException(org.apache.aries.application.management.UpdateException)

Aggregations

BundleException (org.osgi.framework.BundleException)99 Bundle (org.osgi.framework.Bundle)54 IOException (java.io.IOException)31 Test (org.junit.Test)19 File (java.io.File)15 ArrayList (java.util.ArrayList)13 InputStream (java.io.InputStream)10 FileInputStream (java.io.FileInputStream)9 BundleContext (org.osgi.framework.BundleContext)9 HashMap (java.util.HashMap)8 Map (java.util.Map)7 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)7 Hashtable (java.util.Hashtable)5 Manifest (java.util.jar.Manifest)5 ServiceReference (org.osgi.framework.ServiceReference)5 Version (org.osgi.framework.Version)5 BundleStartLevel (org.osgi.framework.startlevel.BundleStartLevel)5 LowDiskException (android.taobao.atlas.runtime.LowDiskException)4 TimeoutException (java.util.concurrent.TimeoutException)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)4