Search in sources :

Example 31 with SubsystemException

use of org.osgi.service.subsystem.SubsystemException in project aries by apache.

the class BasicSubsystem method removedContent.

synchronized void removedContent(Collection<DeployedContentHeader.Clause> content) {
    DeploymentManifest manifest = getDeploymentManifest();
    DeployedContentHeader header = manifest.getDeployedContentHeader();
    if (header == null)
        return;
    Collection<DeployedContentHeader.Clause> clauses = new ArrayList<DeployedContentHeader.Clause>(header.getClauses());
    for (Iterator<DeployedContentHeader.Clause> i = clauses.iterator(); i.hasNext(); ) if (content.contains(i.next())) {
        i.remove();
        break;
    }
    DeploymentManifest.Builder builder = new DeploymentManifest.Builder();
    for (Entry<String, Header<?>> entry : manifest.getHeaders().entrySet()) {
        if (DeployedContentHeader.NAME.equals(entry.getKey()))
            continue;
        builder.header(entry.getValue());
    }
    if (!clauses.isEmpty())
        builder.header(new DeployedContentHeader(clauses));
    try {
        setDeploymentManifest(builder.build());
    } catch (Exception e) {
        throw new SubsystemException(e);
    }
}
Also used : DeploymentManifest(org.apache.aries.subsystem.core.archive.DeploymentManifest) SubsystemException(org.osgi.service.subsystem.SubsystemException) DeployedContentHeader(org.apache.aries.subsystem.core.archive.DeployedContentHeader) ArrayList(java.util.ArrayList) URISyntaxException(java.net.URISyntaxException) BundleException(org.osgi.framework.BundleException) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) ResolutionException(org.osgi.service.resolver.ResolutionException) SubsystemException(org.osgi.service.subsystem.SubsystemException) IOException(java.io.IOException) SubsystemTypeHeader(org.apache.aries.subsystem.core.archive.SubsystemTypeHeader) SubsystemContentHeader(org.apache.aries.subsystem.core.archive.SubsystemContentHeader) ProvisionResourceHeader(org.apache.aries.subsystem.core.archive.ProvisionResourceHeader) AriesSubsystemParentsHeader(org.apache.aries.subsystem.core.archive.AriesSubsystemParentsHeader) Header(org.apache.aries.subsystem.core.archive.Header) DeployedContentHeader(org.apache.aries.subsystem.core.archive.DeployedContentHeader)

Example 32 with SubsystemException

use of org.osgi.service.subsystem.SubsystemException in project aries by apache.

the class BasicSubsystem method setState.

void setState(State value) {
    if (logger.isDebugEnabled())
        logger.debug("Setting state of subsystem {} to {}", getSymbolicName(), value);
    State state = getState();
    if (value.equals(state)) {
        if (logger.isDebugEnabled())
            logger.debug("Requested state {} equals current state {}", value, state);
        return;
    }
    try {
        if (logger.isDebugEnabled())
            logger.debug("Setting the deployment manifest...");
        synchronized (this) {
            setDeploymentManifest(new DeploymentManifest.Builder().manifest(getDeploymentManifest()).state(value).build());
        }
    } catch (Exception e) {
        throw new SubsystemException(e);
    }
    Activator.getInstance().getSubsystemServiceRegistrar().update(this);
    synchronized (this) {
        if (logger.isDebugEnabled())
            logger.debug("Notifying all waiting for state change of subsystem {}", getSymbolicName());
        notifyAll();
    }
}
Also used : SubsystemException(org.osgi.service.subsystem.SubsystemException) URISyntaxException(java.net.URISyntaxException) BundleException(org.osgi.framework.BundleException) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) ResolutionException(org.osgi.service.resolver.ResolutionException) SubsystemException(org.osgi.service.subsystem.SubsystemException) IOException(java.io.IOException)

Example 33 with SubsystemException

use of org.osgi.service.subsystem.SubsystemException in project aries by apache.

the class StartAction method resolve.

private static void resolve(BasicSubsystem instigator, BasicSubsystem target, BasicSubsystem subsystem, Coordination coordination, Collection<BasicSubsystem> subsystems) {
    emitResolvingEvent(subsystem);
    try {
        // actually doing the resolution work.
        if (!subsystem.isRoot()) {
            setExportIsolationPolicy(subsystem, coordination);
            resolveSubsystems(instigator, target, coordination, subsystems);
            resolveBundles(subsystem);
        }
        emitResolvedEvent(subsystem);
    } catch (Throwable t) {
        subsystem.setState(State.INSTALLED);
        if (t instanceof SubsystemException)
            throw (SubsystemException) t;
        throw new SubsystemException(t);
    }
}
Also used : SubsystemException(org.osgi.service.subsystem.SubsystemException)

Example 34 with SubsystemException

use of org.osgi.service.subsystem.SubsystemException in project aries by apache.

the class StopAction method run.

@Override
public Object run() {
    // Protect against re-entry now that cycles are supported.
    if (!Activator.getInstance().getLockingStrategy().set(State.STOPPING, target)) {
        return null;
    }
    try {
        // We are now protected against re-entry.
        // Acquire the global read lock to prevent installs and uninstalls
        // but allow starts and stops.
        Activator.getInstance().getLockingStrategy().readLock();
        try {
            // We are now protected against installs and uninstalls.
            checkRoot();
            // Compute affected subsystems. This is safe to do while only 
            // holding the read lock since we know that nothing can be added 
            // or removed.
            LinkedHashSet<BasicSubsystem> subsystems = new LinkedHashSet<BasicSubsystem>();
            subsystems.add(target);
            List<Resource> resources = new ArrayList<Resource>(Activator.getInstance().getSubsystems().getResourcesReferencedBy(target));
            for (Resource resource : resources) {
                if (resource instanceof BasicSubsystem) {
                    subsystems.add((BasicSubsystem) resource);
                }
            }
            // Acquire the global mutual exclusion lock while acquiring the
            // state change locks of affected subsystems.
            Activator.getInstance().getLockingStrategy().lock();
            try {
                // We are now protected against cycles.
                // Acquire the state change locks of affected subsystems.
                Activator.getInstance().getLockingStrategy().lock(subsystems);
            } finally {
                // Release the global mutual exclusion lock as soon as possible.
                Activator.getInstance().getLockingStrategy().unlock();
            }
            try {
                // We are now protected against other starts and stops of the affected subsystems.
                State state = target.getState();
                if (EnumSet.of(State.INSTALLED, State.INSTALLING, State.RESOLVED).contains(state)) {
                    // apache-aries-provision-dependencies:=resolve.
                    return null;
                } else if (EnumSet.of(State.INSTALL_FAILED, State.UNINSTALLED).contains(state)) {
                    throw new IllegalStateException("Cannot stop from state " + state);
                }
                target.setState(State.STOPPING);
                SubsystemContentHeader header = target.getSubsystemManifest().getSubsystemContentHeader();
                if (header != null) {
                    Collections.sort(resources, new StartResourceComparator(target.getSubsystemManifest().getSubsystemContentHeader()));
                    Collections.reverse(resources);
                }
                for (Resource resource : resources) {
                    // Don't stop the region context bundle.
                    if (Utils.isRegionContextBundle(resource))
                        continue;
                    try {
                        stopResource(resource);
                    } catch (Exception e) {
                        logger.error("An error occurred while stopping resource " + resource + " of subsystem " + target, e);
                    }
                }
                // TODO Can we automatically assume it actually is resolved?
                target.setState(State.RESOLVED);
                try {
                    synchronized (target) {
                        target.setDeploymentManifest(new DeploymentManifest(target.getDeploymentManifest(), null, target.isAutostart(), target.getSubsystemId(), SubsystemIdentifier.getLastId(), target.getLocation(), false, false));
                    }
                } catch (Exception e) {
                    throw new SubsystemException(e);
                }
            } finally {
                // Release the state change locks of affected subsystems.
                Activator.getInstance().getLockingStrategy().unlock(subsystems);
            }
        } finally {
            // Release the read lock.
            Activator.getInstance().getLockingStrategy().readUnlock();
        }
    } finally {
        // Protection against re-entry no longer required.
        Activator.getInstance().getLockingStrategy().unset(State.STOPPING, target);
    }
    return null;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) DeploymentManifest(org.apache.aries.subsystem.core.archive.DeploymentManifest) SubsystemException(org.osgi.service.subsystem.SubsystemException) ArrayList(java.util.ArrayList) Resource(org.osgi.resource.Resource) SubsystemException(org.osgi.service.subsystem.SubsystemException) IOException(java.io.IOException) BundleException(org.osgi.framework.BundleException) SubsystemContentHeader(org.apache.aries.subsystem.core.archive.SubsystemContentHeader) State(org.osgi.service.subsystem.Subsystem.State)

Example 35 with SubsystemException

use of org.osgi.service.subsystem.SubsystemException in project aries by apache.

the class RepositoryServiceRepository method findProviders.

@SuppressWarnings("unchecked")
public Collection<Capability> findProviders(Requirement requirement) {
    Set<Capability> result = new HashSet<Capability>();
    ServiceReference<?>[] references;
    try {
        references = context.getAllServiceReferences("org.osgi.service.repository.Repository", null);
        if (references == null)
            return result;
    } catch (InvalidSyntaxException e) {
        throw new IllegalStateException(e);
    }
    for (ServiceReference<?> reference : references) {
        Object repository = context.getService(reference);
        if (repository == null)
            continue;
        try {
            // Reflection is used here to allow the service to work with a mixture of
            // Repository services implementing different versions of the API.
            Class<?> clazz = repository.getClass();
            Class<?> repoInterface = null;
            while (clazz != null && repoInterface == null) {
                for (Class<?> intf : clazz.getInterfaces()) {
                    if (Repository.class.getName().equals(intf.getName())) {
                        // Compare interfaces by name so that we can work with different versions of the
                        // interface.
                        repoInterface = intf;
                        break;
                    }
                }
                clazz = clazz.getSuperclass();
            }
            if (repoInterface == null) {
                continue;
            }
            Map<Requirement, Collection<Capability>> map;
            try {
                Method method = repoInterface.getMethod("findProviders", Collection.class);
                map = (Map<Requirement, Collection<Capability>>) method.invoke(repository, Collections.singleton(requirement));
            } catch (Exception e) {
                throw new SubsystemException(e);
            }
            Collection<Capability> capabilities = map.get(requirement);
            if (capabilities == null)
                continue;
            result.addAll(capabilities);
        } finally {
            context.ungetService(reference);
        }
    }
    return result;
}
Also used : Capability(org.osgi.resource.Capability) SubsystemException(org.osgi.service.subsystem.SubsystemException) Method(java.lang.reflect.Method) SubsystemException(org.osgi.service.subsystem.SubsystemException) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) ServiceReference(org.osgi.framework.ServiceReference) Requirement(org.osgi.resource.Requirement) Repository(org.osgi.service.repository.Repository) Collection(java.util.Collection) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) HashSet(java.util.HashSet)

Aggregations

SubsystemException (org.osgi.service.subsystem.SubsystemException)61 Subsystem (org.osgi.service.subsystem.Subsystem)39 Test (org.junit.Test)32 SubsystemTest (org.apache.aries.subsystem.itests.SubsystemTest)23 AriesSubsystem (org.apache.aries.subsystem.AriesSubsystem)19 SubsystemArchiveBuilder (org.apache.aries.subsystem.itests.util.SubsystemArchiveBuilder)15 Bundle (org.osgi.framework.Bundle)11 BundleArchiveBuilder (org.apache.aries.subsystem.itests.util.BundleArchiveBuilder)7 Resource (org.osgi.resource.Resource)6 IOException (java.io.IOException)5 ArrayList (java.util.ArrayList)5 SubsystemContentHeader (org.apache.aries.subsystem.core.archive.SubsystemContentHeader)5 BundleException (org.osgi.framework.BundleException)5 Requirement (org.osgi.resource.Requirement)5 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)3 BundleRevision (org.osgi.framework.wiring.BundleRevision)3 Repository (org.osgi.service.repository.Repository)3 File (java.io.File)2 URISyntaxException (java.net.URISyntaxException)2 DeploymentManifest (org.apache.aries.subsystem.core.archive.DeploymentManifest)2