Search in sources :

Example 6 with Resource

use of org.osgi.resource.Resource in project aries by apache.

the class InstallDependencies method install.

public void install(BasicSubsystem subsystem, BasicSubsystem parent, Coordination coordination) throws Exception {
    // Install dependencies first...
    List<Resource> dependencies = new ArrayList<Resource>(subsystem.getResource().getInstallableDependencies());
    Collections.sort(dependencies, new InstallResourceComparator());
    for (Resource dependency : dependencies) ResourceInstaller.newInstance(coordination, dependency, subsystem).install();
    for (Resource dependency : subsystem.getResource().getSharedDependencies()) {
        // parents be checked?
        if (parent == null || !dependency.equals(parent))
            ResourceInstaller.newInstance(coordination, dependency, subsystem).install();
    }
}
Also used : ArrayList(java.util.ArrayList) Resource(org.osgi.resource.Resource)

Example 7 with Resource

use of org.osgi.resource.Resource in project aries by apache.

the class StartAction method getBundles.

private static Collection<Bundle> getBundles(BasicSubsystem subsystem) {
    Collection<Resource> constituents = Activator.getInstance().getSubsystems().getConstituents(subsystem);
    ArrayList<Bundle> result = new ArrayList<Bundle>(constituents.size());
    for (Resource resource : constituents) {
        if (resource instanceof BundleRevision)
            result.add(((BundleRevision) resource).getBundle());
    }
    result.trimToSize();
    return result;
}
Also used : Bundle(org.osgi.framework.Bundle) Resource(org.osgi.resource.Resource) ArrayList(java.util.ArrayList) BundleRevision(org.osgi.framework.wiring.BundleRevision)

Example 8 with Resource

use of org.osgi.resource.Resource 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 9 with Resource

use of org.osgi.resource.Resource in project aries by apache.

the class ResolveContext method findProviders.

@Override
public List<Capability> findProviders(Requirement requirement) {
    ArrayList<Capability> capabilities = new ArrayList<Capability>();
    Resource resource = requirement.getResource();
    if (isResolved(resource) && Utils.isEffectiveResolve(requirement)) {
        processAlreadyResolvedResource(resource, requirement, capabilities);
    } else {
        installDependenciesOfRequirerIfNecessary(requirement);
        processNewlyResolvedResource(resource, requirement, capabilities);
    }
    capabilities.trimToSize();
    return capabilities;
}
Also used : HostedCapability(org.osgi.service.resolver.HostedCapability) Capability(org.osgi.resource.Capability) MissingCapability(org.apache.aries.subsystem.core.internal.DependencyCalculator.MissingCapability) ArrayList(java.util.ArrayList) Resource(org.osgi.resource.Resource)

Example 10 with Resource

use of org.osgi.resource.Resource in project aries by apache.

the class RawSubsystemResource method createFakeResource.

private static Resource createFakeResource(SubsystemManifest manifest) {
    Header<?> importServiceHeader = manifest.getHeaders().get(APPLICATION_IMPORT_SERVICE_HEADER);
    if (importServiceHeader == null) {
        return null;
    }
    List<Capability> modifiableCaps = new ArrayList<Capability>();
    final List<Capability> fakeCapabilities = Collections.unmodifiableList(modifiableCaps);
    Resource fakeResource = new Resource() {

        @Override
        public List<Capability> getCapabilities(String namespace) {
            if (namespace == null) {
                return fakeCapabilities;
            }
            List<Capability> results = new ArrayList<Capability>();
            for (Capability capability : fakeCapabilities) {
                if (namespace.equals(capability.getNamespace())) {
                    results.add(capability);
                }
            }
            return results;
        }

        @Override
        public List<Requirement> getRequirements(String namespace) {
            return Collections.emptyList();
        }
    };
    modifiableCaps.add(new OsgiIdentityCapability(fakeResource, Constants.ResourceTypeSynthesized, new Version(1, 0, 0), Constants.ResourceTypeSynthesized));
    Map<String, Map<String, String>> serviceImports = ManifestHeaderProcessor.parseImportString(importServiceHeader.getValue());
    for (Entry<String, Map<String, String>> serviceImport : serviceImports.entrySet()) {
        Collection<String> objectClasses = new ArrayList<String>(Arrays.asList(serviceImport.getKey()));
        String filter = serviceImport.getValue().get(IdentityNamespace.REQUIREMENT_FILTER_DIRECTIVE);
        BasicCapability.Builder capBuilder = new BasicCapability.Builder();
        capBuilder.namespace(ServiceNamespace.SERVICE_NAMESPACE);
        capBuilder.attribute(ServiceNamespace.CAPABILITY_OBJECTCLASS_ATTRIBUTE, objectClasses);
        if (filter != null)
            capBuilder.attributes(new HashMap<String, Object>(SimpleFilter.attributes(filter)));
        capBuilder.attribute("service.imported", "");
        capBuilder.resource(fakeResource);
        modifiableCaps.add(capBuilder.build());
    }
    return fakeResource;
}
Also used : Capability(org.osgi.resource.Capability) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Resource(org.osgi.resource.Resource) Requirement(org.osgi.resource.Requirement) Version(org.osgi.framework.Version) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

Resource (org.osgi.resource.Resource)197 Requirement (org.osgi.resource.Requirement)62 Capability (org.osgi.resource.Capability)61 ArrayList (java.util.ArrayList)54 List (java.util.List)37 HashMap (java.util.HashMap)31 File (java.io.File)27 Collection (java.util.Collection)24 ResourceBuilder (aQute.bnd.osgi.resource.ResourceBuilder)23 Wire (org.osgi.resource.Wire)22 Map (java.util.Map)21 CapReqBuilder (aQute.bnd.osgi.resource.CapReqBuilder)18 URI (java.net.URI)18 BndEditModel (aQute.bnd.build.model.BndEditModel)15 HashSet (java.util.HashSet)15 MockRegistry (test.lib.MockRegistry)15 Version (org.osgi.framework.Version)14 BundleRevision (org.osgi.framework.wiring.BundleRevision)14 Repository (org.osgi.service.repository.Repository)14 IdentityCapability (aQute.bnd.osgi.resource.ResourceUtils.IdentityCapability)13