Search in sources :

Example 26 with Coordination

use of org.osgi.service.coordinator.Coordination in project aries by apache.

the class SubsystemResourceInstaller method installAriesSubsystem.

private BasicSubsystem installAriesSubsystem(final BasicSubsystem subsystem) throws Exception {
    addChild(subsystem);
    addReference(subsystem);
    addConstituent(subsystem);
    addSubsystem(subsystem);
    installRegionContextBundle(subsystem);
    // The first event for RESOLVED (i.e. persisted) subsystems is emitted later.
    if (State.INSTALLING.equals(subsystem.getState())) {
        Activator.getInstance().getSubsystemServiceRegistrar().register(subsystem, this.subsystem);
        coordination.addParticipant(new Participant() {

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

            @Override
            public void failed(Coordination coordination) throws Exception {
                subsystem.setState(State.INSTALL_FAILED);
                subsystem.uninstall();
            }
        });
    }
    Comparator<Resource> comparator = new InstallResourceComparator();
    // Install dependencies first if appropriate...
    if (!subsystem.isRoot() && Utils.isProvisionDependenciesInstall(subsystem)) {
        new InstallDependencies().install(subsystem, this.subsystem, coordination);
    }
    // Simulate installation of shared content so that necessary relationships are established.
    for (Resource content : subsystem.getResource().getSharedContent()) {
        ResourceInstaller.newInstance(coordination, content, subsystem).install();
    }
    // Now take care of the installable content.
    if (State.INSTALLING.equals(subsystem.getState())) {
        List<Resource> installableContent = new ArrayList<Resource>(subsystem.getResource().getInstallableContent());
        Collections.sort(installableContent, comparator);
        for (Resource content : installableContent) ResourceInstaller.newInstance(coordination, content, subsystem).install();
    }
    // in which case an INSTALLED event must be propagated.
    if (State.INSTALLING.equals(subsystem.getState()) && Utils.isProvisionDependenciesInstall(subsystem)) {
        subsystem.setState(State.INSTALLED);
    } else {
        // This is a persisted subsystem in the RESOLVED state. Emit the first service event.
        Activator.getInstance().getSubsystemServiceRegistrar().register(subsystem, this.subsystem);
    }
    return subsystem;
}
Also used : Participant(org.osgi.service.coordinator.Participant) Coordination(org.osgi.service.coordinator.Coordination) Resource(org.osgi.resource.Resource) ArrayList(java.util.ArrayList)

Example 27 with Coordination

use of org.osgi.service.coordinator.Coordination in project aries by apache.

the class Subsystems method getRootSubsystem.

public synchronized BasicSubsystem getRootSubsystem() {
    if (root == null) {
        File file = Activator.getInstance().getBundleContext().getDataFile("");
        File[] fileArray = file.listFiles();
        List<File> fileList = new ArrayList<File>(Arrays.asList(fileArray));
        Collections.sort(fileList, new Comparator<File>() {

            @Override
            public int compare(File file1, File file2) {
                String name1 = file1.getName();
                String name2 = file2.getName();
                return Long.valueOf(name1).compareTo(Long.valueOf(name2));
            }
        });
        if (fileList.isEmpty()) {
            // There are no persisted subsystems, including root.
            SubsystemResource resource;
            try {
                resource = new SubsystemResource(file);
            } catch (SubsystemException e) {
                throw e;
            } catch (Exception e) {
                throw new SubsystemException(e);
            }
            Coordination coordination = Utils.createCoordination();
            try {
                root = new BasicSubsystem(resource);
                // TODO This initialization is a bit brittle. The root subsystem
                // must be gotten before anything else will be able to use the
                // graph. At the very least, throw IllegalStateException where
                // appropriate.
                graph = new SubsystemGraph(root);
                ResourceInstaller.newInstance(coordination, root, root).install();
                populateRootSubsystem(root, coordination);
            } catch (Exception e) {
                coordination.fail(e);
            } finally {
                coordination.end();
            }
        } else {
            // There are persisted subsystems.
            Coordination coordination = Utils.createCoordination();
            Collection<BasicSubsystem> subsystems = new ArrayList<BasicSubsystem>(fileList.size());
            try {
                for (File f : fileList) {
                    BasicSubsystem s = new BasicSubsystem(f);
                    if (State.UNINSTALLED.equals(s.getState())) {
                        // left over cache, delete this
                        IOUtils.deleteRecursive(f);
                    } else {
                        subsystems.add(s);
                        addSubsystem(s);
                    }
                }
                root = getSubsystemById(0);
                SubsystemIdentifier.setLastId(Long.parseLong(root.getDeploymentManifest().getHeaders().get(DeploymentManifest.ARIESSUBSYSTEM_LASTID).getValue()));
                graph = new SubsystemGraph(root);
                ResourceInstaller.newInstance(coordination, root, root).install();
                populateRootSubsystem(root, coordination);
            } catch (Exception e) {
                coordination.fail(e);
            } finally {
                coordination.end();
            }
        }
    }
    return root;
}
Also used : Coordination(org.osgi.service.coordinator.Coordination) SubsystemException(org.osgi.service.subsystem.SubsystemException) ArrayList(java.util.ArrayList) SubsystemException(org.osgi.service.subsystem.SubsystemException) File(java.io.File)

Example 28 with Coordination

use of org.osgi.service.coordinator.Coordination in project aries by apache.

the class StartAction method setExportIsolationPolicy.

private static void setExportIsolationPolicy(final BasicSubsystem subsystem, Coordination coordination) throws InvalidSyntaxException {
    if (!subsystem.isComposite())
        return;
    final Region from = ((BasicSubsystem) subsystem.getParents().iterator().next()).getRegion();
    final Region to = subsystem.getRegion();
    RegionFilterBuilder builder = from.getRegionDigraph().createRegionFilterBuilder();
    setExportIsolationPolicy(builder, subsystem.getDeploymentManifest().getExportPackageHeader(), subsystem);
    setExportIsolationPolicy(builder, subsystem.getDeploymentManifest().getProvideCapabilityHeader(), subsystem);
    setExportIsolationPolicy(builder, subsystem.getDeploymentManifest().getSubsystemExportServiceHeader(), subsystem);
    RegionFilter regionFilter = builder.build();
    if (regionFilter.getSharingPolicy().isEmpty())
        return;
    if (logger.isDebugEnabled())
        logger.debug("Establishing region connection: from=" + from + ", to=" + to + ", filter=" + regionFilter);
    try {
        from.connectRegion(to, regionFilter);
    } catch (BundleException e) {
        // been set. Bad assumption?
        return;
    }
    coordination.addParticipant(new Participant() {

        @Override
        public void ended(Coordination coordination) throws Exception {
            // It may be necessary to rollback the export sharing policy
            // even when the coordination did not fail. For example, this
            // might have been a subsystem whose export sharing policy was
            // set just in case it offered dependencies for some other
            // subsystem.
            unsetExportIsolationPolicyIfNecessary();
        }

        @Override
        public void failed(Coordination coordination) throws Exception {
        // Nothing to do because a coordination is always ended.
        }

        private void unsetExportIsolationPolicyIfNecessary() throws BundleException, InvalidSyntaxException {
            if (!EnumSet.of(State.INSTALLING, State.INSTALLED).contains(subsystem.getState())) {
                // does not require a rollback.
                return;
            }
            // The subsystem is either INSTALLING or INSTALLED and therefore
            // requires a rollback since the export sharing policy must only
            // be set upon entering the RESOLVED state.
            RegionUpdater updater = new RegionUpdater(from, to);
            updater.addRequirements(null);
        }
    });
}
Also used : RegionFilter(org.eclipse.equinox.region.RegionFilter) Participant(org.osgi.service.coordinator.Participant) Coordination(org.osgi.service.coordinator.Coordination) Region(org.eclipse.equinox.region.Region) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) BundleException(org.osgi.framework.BundleException) RegionFilterBuilder(org.eclipse.equinox.region.RegionFilterBuilder) 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)

Example 29 with Coordination

use of org.osgi.service.coordinator.Coordination in project aries by apache.

the class StartAction method startBundleResource.

private void startBundleResource(Resource resource, Coordination coordination) throws BundleException {
    if (target.isRoot())
        // root region.
        return;
    if (Utils.isRegionContextBundle(resource))
        // The region context bundle was persistently started elsewhere.
        return;
    final Bundle bundle = ((BundleRevision) resource).getBundle();
    if ((bundle.getState() & (Bundle.STARTING | Bundle.ACTIVE)) != 0)
        return;
    if (logger.isDebugEnabled()) {
        int bundleStartLevel = bundle.adapt(BundleStartLevel.class).getStartLevel();
        Bundle systemBundle = Activator.getInstance().getBundleContext().getBundle(0);
        int fwStartLevel = systemBundle.adapt(FrameworkStartLevel.class).getStartLevel();
        logger.debug("StartAction: starting bundle " + bundle.getSymbolicName() + " " + bundle.getVersion().toString() + " bundleStartLevel=" + bundleStartLevel + " frameworkStartLevel=" + fwStartLevel);
    }
    bundle.start(Bundle.START_TRANSIENT | Bundle.START_ACTIVATION_POLICY);
    if (logger.isDebugEnabled()) {
        logger.debug("StartAction: bundle " + bundle.getSymbolicName() + " " + bundle.getVersion().toString() + " started correctly");
    }
    if (coordination == null)
        return;
    coordination.addParticipant(new Participant() {

        public void ended(Coordination coordination) throws Exception {
        // noop
        }

        public void failed(Coordination coordination) throws Exception {
            bundle.stop();
        }
    });
}
Also used : BundleStartLevel(org.osgi.framework.startlevel.BundleStartLevel) Participant(org.osgi.service.coordinator.Participant) Coordination(org.osgi.service.coordinator.Coordination) Bundle(org.osgi.framework.Bundle) BundleRevision(org.osgi.framework.wiring.BundleRevision) FrameworkStartLevel(org.osgi.framework.startlevel.FrameworkStartLevel) 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)

Example 30 with Coordination

use of org.osgi.service.coordinator.Coordination in project aries by apache.

the class InstallAction method run.

@Override
public BasicSubsystem run() {
    // Doesn't appear to be any need of protecting against re-entry in the
    // case of installation.
    BasicSubsystem result = null;
    // Acquire the global write lock to prevent all other operations until
    // the installation is complete. There is no need to hold any other locks.
    Activator.getInstance().getLockingStrategy().writeLock();
    try {
        State state = parent.getState();
        if (State.INSTALLING.equals(state)) {
            throw new SubsystemException("A child subsystem may not be installed while the parent is in the INSTALLING state");
        }
        // Initialization of a null coordination must be privileged and,
        // therefore, occur in the run() method rather than in the constructor.
        Coordination coordination = Utils.createCoordination(parent);
        try {
            TargetRegion region = new TargetRegion(parent);
            SubsystemResource ssr = new SubsystemResource(location, content, parent, coordination);
            result = Activator.getInstance().getSubsystems().getSubsystemByLocation(location);
            if (result != null) {
                if (!region.contains(result))
                    throw new SubsystemException("Location already exists but existing subsystem is not part of target region: " + location);
                if (!(result.getSymbolicName().equals(ssr.getSubsystemManifest().getSubsystemSymbolicNameHeader().getSymbolicName()) && result.getVersion().equals(ssr.getSubsystemManifest().getSubsystemVersionHeader().getVersion()) && result.getType().equals(ssr.getSubsystemManifest().getSubsystemTypeHeader().getType())))
                    throw new SubsystemException("Location already exists but symbolic name, version, and type are not the same: " + location);
            } else {
                result = (BasicSubsystem) region.find(ssr.getSubsystemManifest().getSubsystemSymbolicNameHeader().getSymbolicName(), ssr.getSubsystemManifest().getSubsystemVersionHeader().getVersion());
                if (result != null) {
                    if (!result.getType().equals(ssr.getSubsystemManifest().getSubsystemTypeHeader().getType()))
                        throw new SubsystemException("Subsystem already exists in target region but has a different type: " + location);
                } else {
                    result = new BasicSubsystem(ssr, deploymentManifest);
                }
            }
            checkLifecyclePermission(result);
            return (BasicSubsystem) ResourceInstaller.newInstance(coordination, result, parent).install();
        } catch (Throwable t) {
            coordination.fail(t);
        } finally {
            try {
                coordination.end();
            } catch (CoordinationException e) {
                Throwable t = e.getCause();
                if (t instanceof SubsystemException)
                    throw (SubsystemException) t;
                if (t instanceof SecurityException)
                    throw (SecurityException) t;
                throw new SubsystemException(t);
            }
        }
    } finally {
        // Release the global write lock.
        Activator.getInstance().getLockingStrategy().writeUnlock();
    }
    return result;
}
Also used : Coordination(org.osgi.service.coordinator.Coordination) CoordinationException(org.osgi.service.coordinator.CoordinationException) State(org.osgi.service.subsystem.Subsystem.State) SubsystemException(org.osgi.service.subsystem.SubsystemException)

Aggregations

Coordination (org.osgi.service.coordinator.Coordination)30 CoordinationException (org.osgi.service.coordinator.CoordinationException)11 Participant (org.osgi.service.coordinator.Participant)7 SubsystemException (org.osgi.service.subsystem.SubsystemException)7 IOException (java.io.IOException)5 BundleException (org.osgi.framework.BundleException)5 InvalidSyntaxException (org.osgi.framework.InvalidSyntaxException)5 Coordinator (org.osgi.service.coordinator.Coordinator)4 ArrayList (java.util.ArrayList)3 Resource (org.osgi.resource.Resource)3 ResolutionException (org.osgi.service.resolver.ResolutionException)2 ReporterLogService (aQute.bnd.deployer.repository.ReporterLogService)1 BndResolver (biz.aQute.resolve.BndResolver)1 ResolveProcess (biz.aQute.resolve.ResolveProcess)1 ResolverLogger (biz.aQute.resolve.ResolverLogger)1 File (java.io.File)1 URISyntaxException (java.net.URISyntaxException)1 EnumSet (java.util.EnumSet)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1