Search in sources :

Example 1 with SubsystemContentHeader

use of org.apache.aries.subsystem.core.archive.SubsystemContentHeader in project aries by apache.

the class Aries1426Test method testNoEffectOnSharingPolicy.

@Test
public void testNoEffectOnSharingPolicy() throws Exception {
    Subsystem applicationA = installSubsystemFromFile(APPLICATION_A);
    try {
        Map<String, String> headers = applicationA.getDeploymentHeaders();
        // There should be no subsystem Require-Capability header because
        // the Application-ImportService header included the only relevant
        // bundle clause.
        assertNull("Wrong Require-Capability", headers.get(Constants.REQUIRE_CAPABILITY));
        // There should be no subsystem Subsystem-ImportService header
        // because the Application-ImportService header included the only
        // relevant bundle clause.
        assertNull("Wrong Subsystem-ImportService", headers.get(SubsystemConstants.SUBSYSTEM_IMPORTSERVICE));
        org.apache.aries.subsystem.core.archive.Header<?> expected = new SubsystemContentHeader("bundle.a.jar;version=\"[0,0]\"");
        org.apache.aries.subsystem.core.archive.Header<?> actual = new SubsystemContentHeader(headers.get(SubsystemConstants.SUBSYSTEM_CONTENT));
        // The Subsystem-Content header should not include any synthesized
        // resources used to process Application-ImportService.
        assertEquals("Wrong Subsystem-Content", expected, actual);
        expected = new GenericHeader("Application-ImportService", "org.osgi.service.coordinator.Coordinator");
        actual = new GenericHeader("Application-ImportService", headers.get("Application-ImportService"));
        // The Application-ImportService header should be included in the
        // deployment manifest.
        assertEquals("Wrong Application-ImportService", expected, actual);
        BundleContext context = applicationA.getBundleContext();
        // The Coordinator service should not be visible to the application
        // region because Application-ImportService does not affect the
        // sharing policy and nothing outside of the subsystems
        // implementation made it visible.
        assertNull("Coordinator service should not be visible", context.getServiceReference(Coordinator.class));
    } finally {
        uninstallSubsystemSilently(applicationA);
    }
}
Also used : SubsystemContentHeader(org.apache.aries.subsystem.core.archive.SubsystemContentHeader) Subsystem(org.osgi.service.subsystem.Subsystem) GenericHeader(org.apache.aries.subsystem.core.archive.GenericHeader) Coordinator(org.osgi.service.coordinator.Coordinator) BundleContext(org.osgi.framework.BundleContext) Test(org.junit.Test) SubsystemTest(org.apache.aries.subsystem.itests.SubsystemTest)

Example 2 with SubsystemContentHeader

use of org.apache.aries.subsystem.core.archive.SubsystemContentHeader 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 3 with SubsystemContentHeader

use of org.apache.aries.subsystem.core.archive.SubsystemContentHeader in project aries by apache.

the class SubsystemResource method computeContentResources.

private void computeContentResources(SubsystemManifest manifest) throws BundleException, IOException, InvalidSyntaxException, URISyntaxException {
    SubsystemContentHeader contentHeader = manifest.getSubsystemContentHeader();
    if (contentHeader == null)
        return;
    for (SubsystemContentHeader.Clause clause : contentHeader.getClauses()) {
        Requirement requirement = clause.toRequirement(this);
        Resource resource = findContent(requirement);
        if (resource == null) {
            if (clause.isMandatory())
                throw new SubsystemException("A required content resource could not be found. This means the resource was either missing or not recognized as a supported resource format due to, for example, an invalid bundle manifest or blueprint XML file. Turn on debug logging for more information. The resource was: " + requirement);
            continue;
        }
        addContentResource(resource);
    }
}
Also used : SubsystemContentHeader(org.apache.aries.subsystem.core.archive.SubsystemContentHeader) RequireBundleRequirement(org.apache.aries.subsystem.core.archive.RequireBundleRequirement) Requirement(org.osgi.resource.Requirement) RequireCapabilityRequirement(org.apache.aries.subsystem.core.archive.RequireCapabilityRequirement) ImportPackageRequirement(org.apache.aries.subsystem.core.archive.ImportPackageRequirement) SubsystemImportServiceRequirement(org.apache.aries.subsystem.core.archive.SubsystemImportServiceRequirement) SubsystemException(org.osgi.service.subsystem.SubsystemException) Resource(org.osgi.resource.Resource)

Example 4 with SubsystemContentHeader

use of org.apache.aries.subsystem.core.archive.SubsystemContentHeader in project aries by apache.

the class RawSubsystemResource method computeRequirements.

private List<Requirement> computeRequirements(SubsystemManifest manifest) throws ResolutionException {
    if (isComposite(manifest)) {
        // Composites determine their own requirements.
        return manifest.toRequirements(this);
    }
    // Gather up all of the content resources for the subsystem.
    SubsystemContentHeader header = manifest.getSubsystemContentHeader();
    if (header == null) {
        // Empty subsystems (i.e. subsystems with no content) are allowed.
        return Collections.emptyList();
    }
    List<Requirement> requirements = header.toRequirements(this);
    List<Resource> resources = new ArrayList<Resource>(requirements.size());
    // TODO Do we need the system repository in here (e.g., for features)?
    // What about the preferred provider repository?
    // Search the local repository and service repositories for content.
    RepositoryServiceRepository serviceRepo = new RepositoryServiceRepository();
    // TODO Should we search the service repositories first, the assumption
    // being they will contain more current content than the subsystem
    // archive?
    CompositeRepository compositeRepo = new CompositeRepository(localRepository, serviceRepo);
    for (Requirement requirement : requirements) {
        Collection<Capability> capabilities = compositeRepo.findProviders(requirement);
        if (!capabilities.isEmpty()) {
            resources.add(capabilities.iterator().next().getResource());
        }
    }
    if (fakeImportServiceResource != null) {
        // Add the fake resource so the dependency calculator knows not to
        // return service requirements that are included in
        // Application-ImportService.
        resources.add(fakeImportServiceResource);
    }
    // dependencies not satisfied by the content resources themselves.
    return new DependencyCalculator(resources).calculateDependencies();
}
Also used : SubsystemContentHeader(org.apache.aries.subsystem.core.archive.SubsystemContentHeader) Requirement(org.osgi.resource.Requirement) Capability(org.osgi.resource.Capability) Resource(org.osgi.resource.Resource) ArrayList(java.util.ArrayList)

Example 5 with SubsystemContentHeader

use of org.apache.aries.subsystem.core.archive.SubsystemContentHeader in project aries by apache.

the class RawSubsystemResource method computeFileCapabilities.

private List<Capability> computeFileCapabilities(FileResource resource, IFile file, SubsystemManifest manifest) {
    SubsystemContentHeader ssch = manifest.getSubsystemContentHeader();
    if (ssch == null)
        return Collections.emptyList();
    for (Clause c : ssch.getClauses()) {
        Attribute er = c.getAttribute(ContentHandler.EMBEDDED_RESOURCE_ATTRIBUTE);
        if (er != null) {
            if (file.getName().equals(er.getValue())) {
                Map<String, Object> attrs = new HashMap<String, Object>();
                attrs.put(ContentHandler.EMBEDDED_RESOURCE_ATTRIBUTE, er.getValue());
                return Collections.<Capability>singletonList(new OsgiIdentityCapability(resource, c.getSymbolicName(), c.getVersionRange().getLeft(), c.getType(), attrs));
            }
        }
    }
    return Collections.emptyList();
}
Also used : SubsystemContentHeader(org.apache.aries.subsystem.core.archive.SubsystemContentHeader) Capability(org.osgi.resource.Capability) Attribute(org.apache.aries.subsystem.core.archive.Attribute) HashMap(java.util.HashMap) Clause(org.apache.aries.subsystem.core.archive.SubsystemContentHeader.Clause)

Aggregations

SubsystemContentHeader (org.apache.aries.subsystem.core.archive.SubsystemContentHeader)13 Resource (org.osgi.resource.Resource)8 SubsystemManifest (org.apache.aries.subsystem.core.archive.SubsystemManifest)4 Requirement (org.osgi.resource.Requirement)4 SubsystemException (org.osgi.service.subsystem.SubsystemException)4 ArrayList (java.util.ArrayList)3 Capability (org.osgi.resource.Capability)3 IOException (java.io.IOException)2 Iterator (java.util.Iterator)2 ImportPackageRequirement (org.apache.aries.subsystem.core.archive.ImportPackageRequirement)2 RequireBundleRequirement (org.apache.aries.subsystem.core.archive.RequireBundleRequirement)2 RequireCapabilityRequirement (org.apache.aries.subsystem.core.archive.RequireCapabilityRequirement)2 SubsystemImportServiceRequirement (org.apache.aries.subsystem.core.archive.SubsystemImportServiceRequirement)2 Region (org.eclipse.equinox.region.Region)2 BundleException (org.osgi.framework.BundleException)2 HashMap (java.util.HashMap)1 LinkedHashSet (java.util.LinkedHashSet)1 Attribute (org.apache.aries.subsystem.core.archive.Attribute)1 DeploymentManifest (org.apache.aries.subsystem.core.archive.DeploymentManifest)1 GenericHeader (org.apache.aries.subsystem.core.archive.GenericHeader)1