Search in sources :

Example 21 with SubsystemException

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

the class LockingStrategy method lock.

public void lock(Collection<BasicSubsystem> subsystems) {
    Collection<BasicSubsystem> locked = new ArrayList<BasicSubsystem>(subsystems.size());
    try {
        while (locked.size() < subsystems.size()) {
            for (BasicSubsystem subsystem : subsystems) {
                if (!subsystem.stateChangeLock().tryLock()) {
                    unlock(locked);
                    locked.clear();
                    if (!condition.await(TRY_LOCK_TIME, TimeUnit.SECONDS)) {
                        throw new SubsystemException("Unable to acquire the state change lock in time: " + subsystem);
                    }
                    break;
                }
                locked.add(subsystem);
            }
        }
    } catch (InterruptedException e) {
        unlock(locked);
        throw new SubsystemException(e);
    }
}
Also used : SubsystemException(org.osgi.service.subsystem.SubsystemException) ArrayList(java.util.ArrayList)

Example 22 with SubsystemException

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

the class StartAction method run.

@Override
public Object run() {
    // Protect against re-entry now that cycles are supported.
    if (!Activator.getInstance().getLockingStrategy().set(State.STARTING, target)) {
        return null;
    }
    try {
        AffectedResources affectedResources;
        // If necessary, install the dependencies.
        if (State.INSTALLING.equals(target.getState()) && !Utils.isProvisionDependenciesInstall(target)) {
            // Acquire the global write lock while installing dependencies.
            Activator.getInstance().getLockingStrategy().writeLock();
            try {
                // We are now protected against installs, starts, stops, and uninstalls.
                // We need a separate coordination when installing 
                // dependencies because cleaning up the temporary export 
                // sharing policies must be done while holding the write lock.
                Coordination c = Utils.createCoordination(target);
                try {
                    installDependencies(target, c);
                    // Associated subsystems must be computed after all dependencies 
                    // are installed because some of the dependencies may be 
                    // subsystems. This is safe to do while only holding the read
                    // lock since we know that nothing can be added or removed.
                    affectedResources = computeAffectedResources(target);
                    for (BasicSubsystem subsystem : affectedResources.subsystems()) {
                        if (State.INSTALLING.equals(subsystem.getState()) && !Utils.isProvisionDependenciesInstall(subsystem)) {
                            installDependencies(subsystem, c);
                        }
                    }
                    // Downgrade to the read lock in order to prevent 
                    // installs and uninstalls but allow starts and stops.
                    Activator.getInstance().getLockingStrategy().readLock();
                } catch (Throwable t) {
                    c.fail(t);
                } finally {
                    // This will clean up the temporary export sharing
                    // policies. Must be done while holding the write lock.
                    c.end();
                }
            } finally {
                // Release the global write lock as soon as possible.
                Activator.getInstance().getLockingStrategy().writeUnlock();
            }
        } else {
            // Acquire the read lock in order to prevent installs and
            // uninstalls but allow starts and stops.
            Activator.getInstance().getLockingStrategy().readLock();
        }
        try {
            // and uninstalls.
            if (Restriction.INSTALL_ONLY.equals(restriction)) {
                return null;
            }
            // Compute associated subsystems here in case (1) they weren't
            // computed previously while holding the write lock or (2) they
            // were computed previously and more were subsequently added. 
            // This is safe to do while only holding the read lock since we
            // know that nothing can be added or removed.
            affectedResources = computeAffectedResources(target);
            // 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(affectedResources.subsystems());
            } finally {
                // Release the global mutual exclusion lock as soon as possible.
                Activator.getInstance().getLockingStrategy().unlock();
            }
            Coordination coordination = this.coordination;
            try {
                coordination = createCoordination();
                // We are now protected against other starts and stops of the affected subsystems.
                if (!isTargetStartable(instigator, requestor, target)) {
                    return null;
                }
                // Resolve if necessary.
                if (State.INSTALLED.equals(target.getState()))
                    resolve(instigator, target, target, coordination, affectedResources.subsystems());
                if (Restriction.RESOLVE_ONLY.equals(restriction))
                    return null;
                target.setState(State.STARTING);
                // Be sure to set the state back to RESOLVED if starting fails.
                coordination.addParticipant(new Participant() {

                    @Override
                    public void ended(Coordination coordination) throws Exception {
                    // Nothing.
                    }

                    @Override
                    public void failed(Coordination coordination) throws Exception {
                        target.setState(State.RESOLVED);
                    }
                });
                SubsystemContentHeader header = target.getSubsystemManifest().getSubsystemContentHeader();
                if (header != null)
                    Collections.sort(affectedResources.resources(), new StartResourceComparator(header));
                for (Resource resource : affectedResources.resources()) startResource(resource, coordination);
                target.setState(State.ACTIVE);
            } catch (Throwable t) {
                // We catch exceptions and fail the coordination here to
                // ensure we are still holding the state change locks when
                // the participant sets the state to RESOLVED.
                coordination.fail(t);
            } finally {
                try {
                    // of this start action.
                    if (coordination.getName().equals(Utils.computeCoordinationName(target))) {
                        coordination.end();
                    }
                } finally {
                    // Release the state change locks of affected subsystems.
                    Activator.getInstance().getLockingStrategy().unlock(affectedResources.subsystems());
                }
            }
        } finally {
            // Release the read lock.
            Activator.getInstance().getLockingStrategy().readUnlock();
        }
    } catch (CoordinationException e) {
        Throwable t = e.getCause();
        if (t == null) {
            throw new SubsystemException(e);
        }
        if (t instanceof SecurityException) {
            throw (SecurityException) t;
        }
        if (t instanceof SubsystemException) {
            throw (SubsystemException) t;
        }
        throw new SubsystemException(t);
    } finally {
        // Protection against re-entry no longer required.
        Activator.getInstance().getLockingStrategy().unset(State.STARTING, target);
    }
    return null;
}
Also used : Coordination(org.osgi.service.coordinator.Coordination) SubsystemException(org.osgi.service.subsystem.SubsystemException) Resource(org.osgi.resource.Resource) BundleException(org.osgi.framework.BundleException) SubsystemException(org.osgi.service.subsystem.SubsystemException) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) IOException(java.io.IOException) CoordinationException(org.osgi.service.coordinator.CoordinationException) SubsystemContentHeader(org.apache.aries.subsystem.core.archive.SubsystemContentHeader) Participant(org.osgi.service.coordinator.Participant) CoordinationException(org.osgi.service.coordinator.CoordinationException)

Example 23 with SubsystemException

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

the class SubsystemManifestValidator method validateAriesProvisionDependenciesDirective.

private static void validateAriesProvisionDependenciesDirective(BasicSubsystem subsystem) {
    AriesProvisionDependenciesDirective directive = subsystem.getAriesProvisionDependenciesDirective();
    BasicSubsystem parent = subsystem.getResource().getParents().iterator().next();
    AriesProvisionDependenciesDirective parentDirective = parent.getAriesProvisionDependenciesDirective();
    if (!directive.equals(parentDirective) && (subsystem.isFeature() || State.INSTALLING.equals(parent.getState()))) {
        throw new SubsystemException("The value of the " + AriesProvisionDependenciesDirective.NAME + " directive must be the same as the parent subsystem for features and implicitly installed subsystems.");
    }
}
Also used : SubsystemException(org.osgi.service.subsystem.SubsystemException) AriesProvisionDependenciesDirective(org.apache.aries.subsystem.core.archive.AriesProvisionDependenciesDirective)

Example 24 with SubsystemException

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

the class SubsystemManifestValidator method validate.

public static void validate(BasicSubsystem subsystem, SubsystemManifest manifest) {
    if (subsystem.getResource().getId() == 0) {
        return;
    }
    validatePreferredProviderHeader(manifest.getPreferredProviderHeader());
    validateAriesProvisionDependenciesDirective(subsystem);
    if (subsystem.isComposite()) {
        SubsystemContentHeader header = manifest.getSubsystemContentHeader();
        if (header == null) {
            return;
        }
        for (SubsystemContentHeader.Clause clause : header.getClauses()) {
            if (!clause.getVersionRange().isExact()) {
                throw new SubsystemException("Composite subsystem using version range for content: " + clause);
            }
        }
    } else if (subsystem.isFeature()) {
        SubsystemTypeHeader subsystemTypeHeader = manifest.getSubsystemTypeHeader();
        ProvisionPolicyDirective provisionPolicyDirective = subsystemTypeHeader.getProvisionPolicyDirective();
        if (provisionPolicyDirective.isAcceptDependencies()) {
            throw new SubsystemException("Feature subsystems may not declare a provision-policy of acceptDependencies");
        }
        if (manifest.getHeaders().get(SubsystemConstants.PREFERRED_PROVIDER) != null) {
            throw new SubsystemException("Feature subsystems may not declare a " + SubsystemConstants.PREFERRED_PROVIDER + " header");
        }
    }
}
Also used : SubsystemContentHeader(org.apache.aries.subsystem.core.archive.SubsystemContentHeader) ProvisionPolicyDirective(org.apache.aries.subsystem.core.archive.ProvisionPolicyDirective) SubsystemException(org.osgi.service.subsystem.SubsystemException) SubsystemTypeHeader(org.apache.aries.subsystem.core.archive.SubsystemTypeHeader)

Example 25 with SubsystemException

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

the class InstallTest method testLocationAsEmptyString.

@Test
public void testLocationAsEmptyString() throws Exception {
    try {
        Subsystem a = installSubsystemFromFile(getRootSubsystem(), new File(APPLICATION_A), "");
        try {
            BasicSubsystem basic = (BasicSubsystem) a;
            String location = basic.getLocation();
            assertEquals("Location value should be an empty string", "", location);
        } finally {
            uninstallSubsystemSilently(a);
        }
    } catch (SubsystemException e) {
        e.printStackTrace();
        fail("Subsystem should have installed");
    }
}
Also used : BasicSubsystem(org.apache.aries.subsystem.core.internal.BasicSubsystem) Subsystem(org.osgi.service.subsystem.Subsystem) SubsystemException(org.osgi.service.subsystem.SubsystemException) BasicSubsystem(org.apache.aries.subsystem.core.internal.BasicSubsystem) File(java.io.File) Test(org.junit.Test)

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