Package org.osgi.framework

Examples of org.osgi.framework.BundleException


                {
                    th = ((java.security.PrivilegedActionException) th).getException();
                }

                // Rethrow all other exceptions as a BundleException.
                throw new BundleException(
                    "Activator start error in bundle " + bundle + ".",
                    BundleException.ACTIVATOR_ERROR, th);
            }
        }
        finally
View Full Code Here


            {
                throw new IllegalStateException("Cannot update an uninstalled bundle.");
            }
            else
            {
                throw new BundleException(
                    "Bundle " + bundle
                    + " cannot be update, since it is either starting or stopping.");
            }
        }

        // We must release the lock and close the input stream, so do both
        // in a finally block.
        try
        {
            // Variable to indicate whether bundle is active or not.
            Throwable rethrow = null;

            final int oldState = bundle.getState();

            // First get the update-URL from our header.
            String updateLocation = (String)
                ((BundleRevisionImpl) bundle.adapt(BundleRevision.class))
                    .getHeaders().get(Constants.BUNDLE_UPDATELOCATION);

            // If no update location specified, use original location.
            if (updateLocation == null)
            {
                updateLocation = bundle._getLocation();
            }

            // Stop the bundle if it is active, but do not change its
            // persistent state.
            if (oldState == Bundle.ACTIVE)
            {
                stopBundle(bundle, false);
            }

            try
            {
                // Revising the bundle creates a new revision, which modifies
                // the global state, so we need to acquire the global lock
                // before revising.
                boolean locked = acquireGlobalLock();
                if (!locked)
                {
                    throw new BundleException(
                        "Cannot acquire global lock to update the bundle.");
                }
                try
                {
                    // Try to revise.
                    boolean wasExtension = bundle.isExtension();
                    bundle.revise(updateLocation, is);

                    // Verify bundle revision.
                    try
                    {
                        Object sm = System.getSecurityManager();

                        if (sm != null)
                        {
                            ((SecurityManager) sm).checkPermission(
                                new AdminPermission(bundle, AdminPermission.LIFECYCLE));
                        }

                        // If this is an update from a normal to an extension bundle
                        // then attach the extension
                        if (!wasExtension && bundle.isExtension())
                        {
                            m_extensionManager.addExtensionBundle(this, bundle);
// TODO: REFACTOR - Perhaps we could move this into extension manager.
                            m_resolver.addRevision(m_extensionManager.getRevision());
// TODO: REFACTOR - Not clear why this is here. We should look at all of these steps more closely.
                            setBundleStateAndNotify(bundle, Bundle.RESOLVED);
                        }
                        else if (wasExtension)
                        {
                            setBundleStateAndNotify(bundle, Bundle.INSTALLED);
                        }
                    }
                    catch (Throwable ex)
                    {
                        try
                        {
                            bundle.rollbackRevise();
                        }
                        catch (Exception busted)
                        {
                            m_logger.log(
                                bundle, Logger.LOG_ERROR, "Unable to rollback.", busted);
                        }

                        throw ex;
                    }
                }
                finally
                {
                    // Always release the global lock.
                    releaseGlobalLock();
                }
            }
            catch (Throwable ex)
            {
                m_logger.log(
                    bundle, Logger.LOG_ERROR, "Unable to update the bundle.", ex);
                rethrow = ex;
            }

            // Set new state, mark as needing a refresh, and fire updated event
            // if successful.
            if (rethrow == null)
            {
                bundle.setLastModified(System.currentTimeMillis());

                if (!bundle.isExtension())
                {
                    setBundleStateAndNotify(bundle, Bundle.INSTALLED);
                }
                else
                {
                    m_extensionManager.startExtensionBundle(this, bundle);
                }

                fireBundleEvent(BundleEvent.UNRESOLVED, bundle);

                fireBundleEvent(BundleEvent.UPDATED, bundle);

                // Acquire global lock to check if we should auto-refresh.
                boolean locked = acquireGlobalLock();
                // If we did not get the global lock, then do not try to
                // auto-refresh.
                if (locked)
                {
                    try
                    {
                        if (!m_dependencies.hasDependents(bundle)
                            && !bundle.isExtension())
                        {
                            try
                            {
                                List<Bundle> list = new ArrayList<Bundle>(1);
                                list.add(bundle);
                                refreshPackages(list, null);
                            }
                            catch (Exception ex)
                            {
                                m_logger.log(bundle,
                                    Logger.LOG_ERROR,
                                    "Unable to immediately purge the bundle revisions.", ex);
                            }
                        }
                    }
                    finally
                    {
                        // Always release the global lock.
                        releaseGlobalLock();
                    }
                }
            }

            // If the old state was active, but the new revision is a fragment,
            // then mark the persistent state to inactive.
            if ((oldState == Bundle.ACTIVE)
                && Util.isFragment(bundle.adapt(BundleRevision.class)))
            {
                bundle.setPersistentStateInactive();
                m_logger.log(bundle, Logger.LOG_WARNING,
                    "Previously active bundle was updated to a fragment, resetting state to inactive: "
                    + bundle);
            }
            // Otherwise, restart the bundle if it was previously active,
            // but do not change its persistent state.
            else if (oldState == Bundle.ACTIVE)
            {
                startBundle(bundle, Bundle.START_TRANSIENT);
            }

            // If update failed, rethrow exception.
            if (rethrow != null)
            {
                if (rethrow instanceof AccessControlException)
                {
                    throw (AccessControlException) rethrow;
                }
                else if (rethrow instanceof BundleException)
                {
                    throw (BundleException) rethrow;
                }
                else
                {
                    throw new BundleException(
                        "Update of bundle " + bundle + " failed.", rethrow);
                }
            }
        }
        finally
View Full Code Here

            {
                throw new IllegalStateException("Cannot stop an uninstalled bundle.");
            }
            else
            {
                throw new BundleException(
                    "Bundle " + bundle
                    + " cannot be stopped since it is already stopping.");
            }
        }

        try
        {
            Throwable rethrow = null;

            // Set the bundle's persistent state to inactive if necessary.
            if (record)
            {
                bundle.setPersistentStateInactive();
            }

            // If the bundle is not persistently started, then we
            // need to reset the activation policy flag, since it
            // does not persist across persistent stops or transient
            // stops.
            if (!isBundlePersistentlyStarted(bundle))
            {
                bundle.setDeclaredActivationPolicyUsed(false);
            }

            // As per the OSGi spec, fragment bundles can not be stopped and must
            // throw a BundleException when there is an attempt to stop one.
            if (Util.isFragment(bundle.adapt(BundleRevision.class)))
            {
                throw new BundleException("Fragment bundles can not be stopped: " + bundle);
            }

            boolean wasActive = false;
            switch (bundle.getState())
            {
                case Bundle.UNINSTALLED:
                    throw new IllegalStateException("Cannot stop an uninstalled bundle.");
                case Bundle.STARTING:
                    if (bundle.isDeclaredActivationPolicyUsed()
                        && ((BundleRevisionImpl) bundle.adapt(BundleRevision.class))
                            .getDeclaredActivationPolicy() != BundleRevisionImpl.LAZY_ACTIVATION)
                    {
                        throw new BundleException(
                            "Stopping a starting or stopping bundle is currently not supported.");
                    }
                    break;
                case Bundle.STOPPING:
                    throw new BundleException(
                        "Stopping a starting or stopping bundle is currently not supported.");
                case Bundle.INSTALLED:
                case Bundle.RESOLVED:
                    return;
                case Bundle.ACTIVE:
                    wasActive = true;
                    break;
            }

            // At this point, no matter if the bundle's activation policy is
            // eager or deferred, we need to set the bundle's state to STOPPING
            // and fire the STOPPING event.
            setBundleStateAndNotify(bundle, Bundle.STOPPING);
            fireBundleEvent(BundleEvent.STOPPING, bundle);

            // If the bundle was active, then invoke the activator stop() method
            // or if we are stopping the system bundle.
            if ((wasActive) || (bundle.getBundleId() == 0))
            {
                try
                {
                    if (bundle.getActivator() != null)
                    {
                        m_secureAction.stopActivator(bundle.getActivator(), bundle._getBundleContext());
                    }
                }
                catch (Throwable th)
                {
                    m_logger.log(bundle, Logger.LOG_ERROR, "Error stopping bundle.", th);
                    rethrow = th;
                }
            }

            // Do not clean up after the system bundle since it will
            // clean up after itself.
            if (bundle.getBundleId() != 0)
            {
                // Clean up the bundle activator.
                bundle.setActivator(null);

                // Clean up the bundle context.
                // We invalidate this first to make sure it cannot be used
                // after stopping the activator.
                BundleContextImpl bci = (BundleContextImpl) bundle._getBundleContext();
                bci.invalidate();
                bundle.setBundleContext(null);

                // Unregister any services offered by this bundle.
                m_registry.unregisterServices(bundle);

                // Release any services being used by this bundle.
                m_registry.ungetServices(bundle);

                // The spec says that we must remove all event
                // listeners for a bundle when it is stopped.
                m_dispatcher.removeListeners(bci);

                setBundleStateAndNotify(bundle, Bundle.RESOLVED);

                // We still need to fire the STOPPED event, but we will do
                // it later so we can release the bundle lock.
            }

            // Throw activator error if there was one.
            if (rethrow != null)
            {
                // The spec says to expect BundleException or
                // SecurityException, so rethrow these exceptions.
                if (rethrow instanceof BundleException)
                {
                    throw (BundleException) rethrow;
                }
                else if ((System.getSecurityManager() != null) &&
                    (rethrow instanceof java.security.PrivilegedActionException))
                {
                    rethrow = ((java.security.PrivilegedActionException) rethrow).getException();
                }

                // Rethrow all other exceptions as a BundleException.
                throw new BundleException(
                    "Activator stop error in bundle " + bundle + ".", rethrow);
            }
        }
        finally
        {
View Full Code Here

            {
                throw new IllegalStateException("Cannot uninstall an uninstalled bundle.");
            }
            else
            {
                throw new BundleException(
                    "Bundle " + bundle
                    + " cannot be uninstalled since it is stopping.");
            }
        }
View Full Code Here

            // Acquire the global lock to create the bundle,
            // since this impacts the global state.
            boolean locked = acquireGlobalLock();
            if (!locked)
            {
                throw new BundleException(
                    "Unable to acquire the global lock to install the bundle.");
            }
            try
            {
                bundle = new BundleImpl(this, null, ba);

                // Extensions are handled as a special case.
                if (bundle.isExtension())
                {
                    m_extensionManager.addExtensionBundle(this, bundle);
                    m_resolver.addRevision(m_extensionManager.getRevision());
                }

                // Use a copy-on-write approach to add the bundle
                // to the installed maps.
                Map[] maps = new Map[] {
                    new HashMap<String, BundleImpl>(m_installedBundles[LOCATION_MAP_IDX]),
                    new TreeMap<Long, BundleImpl>(m_installedBundles[IDENTIFIER_MAP_IDX])
                };
                maps[LOCATION_MAP_IDX].put(bundle._getLocation(), bundle);
                maps[IDENTIFIER_MAP_IDX].put(new Long(bundle.getBundleId()), bundle);
                m_installedBundles = maps;
            }
            finally
            {
                // Always release the global lock.
                releaseGlobalLock();
            }
        }
        catch (Throwable ex)
        {
            if (ex instanceof BundleException)
            {
                throw (BundleException) ex;
            }
            else if (ex instanceof AccessControlException)
            {
                throw (AccessControlException) ex;
            }
            else
            {
                throw new BundleException("Could not create bundle object.", ex);
            }
        }

        if (bundle.isExtension())
        {
View Full Code Here

        {
            // Check to see if the framework is still running;
            if ((getState() == Bundle.STOPPING) ||
                (getState() == Bundle.UNINSTALLED))
            {
                throw new BundleException("The framework has been shutdown.");
            }

            // If bundle location is already installed, then
            // return it as required by the OSGi specification.
            existing = (BundleImpl) getBundle(location);
            if (existing == null)
            {
                // First generate an identifier for it.
                long id = getNextId();

                try
                {
                    // Add the bundle to the cache.
                    ba = m_cache.create(id, getInitialBundleStartLevel(), location, is);
                }
                catch (Exception ex)
                {
                    throw new BundleException(
                        "Unable to cache bundle: " + location, ex);
                }
                finally
                {
                    try
                    {
                        if (is != null) is.close();
                    }
                    catch (IOException ex)
                    {
                        m_logger.log(
                            Logger.LOG_ERROR,
                            "Unable to close input stream.", ex);
                    }
                }

                try
                {
                    // Acquire the global lock to create the bundle,
                    // since this impacts the global state.
                    boolean locked = acquireGlobalLock();
                    if (!locked)
                    {
                        throw new BundleException(
                            "Unable to acquire the global lock to install the bundle.");
                    }
                    try
                    {
                        bundle = new BundleImpl(this, origin, ba);
                    }
                    finally
                    {
                        // Always release the global lock.
                        releaseGlobalLock();
                    }

                    if (!bundle.isExtension())
                    {
                        Object sm = System.getSecurityManager();
                        if (sm != null)
                        {
                            ((SecurityManager) sm).checkPermission(
                                new AdminPermission(bundle, AdminPermission.LIFECYCLE));
                        }
                    }
                    else
                    {
                        m_extensionManager.addExtensionBundle(this, bundle);
                        m_resolver.addRevision(m_extensionManager.getRevision());
                    }
                }
                catch (Throwable ex)
                {
                    // Remove bundle from the cache.
                    try
                    {
                        if (bundle != null)
                        {
                            bundle.closeAndDelete();
                        }
                        else if (ba != null)
                        {
                            ba.closeAndDelete();
                        }
                    }
                    catch (Exception ex1)
                    {
                        m_logger.log(bundle,
                            Logger.LOG_ERROR,
                            "Could not remove from cache.", ex1);
                    }
                    if (ex instanceof BundleException)
                    {
                        throw (BundleException) ex;
                    }
                    else if (ex instanceof AccessControlException)
                    {
                        throw (AccessControlException) ex;
                    }
                    else
                    {
                        throw new BundleException("Could not create bundle object.", ex);
                    }
                }

                // Acquire global lock.
                boolean locked = acquireGlobalLock();
                if (!locked)
                {
                    // If the calling thread holds bundle locks, then we might not
                    // be able to get the global lock.
                    throw new IllegalStateException(
                        "Unable to acquire global lock to add bundle.");
                }
                try
                {
                    // Use a copy-on-write approach to add the bundle
                    // to the installed maps.
                    Map[] maps = new Map[] {
                        new HashMap<String, BundleImpl>(m_installedBundles[LOCATION_MAP_IDX]),
                        new TreeMap<Long, BundleImpl>(m_installedBundles[IDENTIFIER_MAP_IDX])
                    };
                    maps[LOCATION_MAP_IDX].put(location, bundle);
                    maps[IDENTIFIER_MAP_IDX].put(new Long(bundle.getBundleId()), bundle);
                    m_installedBundles = maps;
                }
                finally
                {
                    releaseGlobalLock();
                }

                if (bundle.isExtension())
                {
                    m_extensionManager.startExtensionBundle(this, bundle);
                }
            }
        }
        finally
        {
            // Always release install lock.
            releaseInstallLock(location);

            // Always try to close the input stream.
            try
            {
                if (is != null) is.close();
            }
            catch (IOException ex)
            {
                m_logger.log(bundle,
                    Logger.LOG_ERROR,
                    "Unable to close input stream.", ex);
                // Not much else we can do.
            }
        }

        if (existing != null)
        {
            Set<ServiceReference<org.osgi.framework.hooks.bundle.FindHook>> hooks =
                getHooks(org.osgi.framework.hooks.bundle.FindHook.class);
            if (!hooks.isEmpty())
            {
                Collection<Bundle> bundles = new ArrayList<Bundle>(1);
                bundles.add(existing);
                bundles = new ShrinkableCollection<Bundle>(bundles);
                for (ServiceReference<org.osgi.framework.hooks.bundle.FindHook> hook : hooks)
                {
                    org.osgi.framework.hooks.bundle.FindHook fh = getService(this, hook, false);
                    if (fh != null)
                    {
                        try
                        {
                            m_secureAction.invokeBundleFindHook(
                                fh, origin.getBundleContext(), bundles);
                        }
                        catch (Throwable th)
                        {
                            m_logger.doLog(
                                hook.getBundle(),
                                hook,
                                Logger.LOG_WARNING,
                                "Problem invoking bundle hook.",
                                th);
                        }
                    }
                }
                if (bundles.isEmpty())
                {
                    throw new BundleException(
                        "Bundle installation rejected by hook.",
                        BundleException.REJECTED_BY_HOOK);
                }
            }
        }
View Full Code Here

        catch (ResolveException ex)
        {
            if (ex.getRevision() != null)
            {
                Bundle b = ex.getRevision().getBundle();
                throw new BundleException(
                    "Unresolved constraint in bundle "
                    + b + ": " + ex.getMessage(), BundleException.RESOLVE_ERROR);
            }
            else
            {
                throw new BundleException(ex.getMessage(), BundleException.RESOLVE_ERROR);
            }
        }
    }
View Full Code Here

                clazz = ((BundleWiringImpl)
                    impl.adapt(BundleRevision.class).getWiring()).getClassByDelegation(className);
            }
            catch (ClassNotFoundException ex)
            {
                throw new BundleException("Not found: " + className, ex);
            }
            activator = (BundleActivator) clazz.newInstance();
        }

        return activator;
View Full Code Here

        {
            acquireBundleLock(bundle, Bundle.INSTALLED | Bundle.RESOLVED);
        }
        catch (IllegalStateException ex)
        {
            throw new BundleException(
                "Bundle state has changed unexpectedly during refresh.");
        }

        try
        {
View Full Code Here

                {
                    m_installRequestLock_Priority1.wait();
                }
                catch (InterruptedException ex)
                {
                    throw new BundleException("Unable to install, thread interrupted.");
                }
            }

            m_installRequestMap.put(location, location);
        }
View Full Code Here

TOP

Related Classes of org.osgi.framework.BundleException

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.