Search in sources :

Example 36 with SubsystemException

use of org.osgi.service.subsystem.SubsystemException 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)

Example 37 with SubsystemException

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

the class BundleResourceInstaller method install.

public Resource install() {
    BundleRevision revision;
    if (resource instanceof BundleRevision) {
        revision = (BundleRevision) resource;
    } else if (resource instanceof BundleRevisionResource) {
        revision = ((BundleRevisionResource) resource).getRevision();
    } else {
        try {
            revision = installBundle();
        } catch (Exception e) {
            throw new SubsystemException(e);
        }
    }
    addReference(revision);
    addConstituent(new BundleConstituent(resource, revision));
    return revision;
}
Also used : SubsystemException(org.osgi.service.subsystem.SubsystemException) BundleRevision(org.osgi.framework.wiring.BundleRevision) SubsystemException(org.osgi.service.subsystem.SubsystemException)

Example 38 with SubsystemException

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

the class SubsystemGraph method add.

public synchronized void add(BasicSubsystem parent, BasicSubsystem child) {
    SubsystemWrapper parentWrap = new SubsystemWrapper(parent);
    SubsystemWrapper childWrap = new SubsystemWrapper(child);
    if (containsAncestor(childWrap, parentWrap))
        throw new SubsystemException("Cycle detected between '" + parentWrap + "' and '" + childWrap + "'");
    Collection<SubsystemWrapper> subsystems = adjacencyList.get(childWrap);
    if (subsystems == null) {
        subsystems = new HashSet<SubsystemWrapper>();
        adjacencyList.put(childWrap, subsystems);
    }
    subsystems = adjacencyList.get(parentWrap);
    if (subsystems == null) {
        subsystems = new HashSet<SubsystemWrapper>();
        adjacencyList.put(parentWrap, subsystems);
    }
    subsystems.add(childWrap);
}
Also used : SubsystemException(org.osgi.service.subsystem.SubsystemException)

Example 39 with SubsystemException

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

the class SubsystemResourceUninstaller method uninstallSubsystem.

private void uninstallSubsystem() {
    BasicSubsystem subsystem = (BasicSubsystem) resource;
    try {
        if (subsystem.getState().equals(Subsystem.State.RESOLVED))
            subsystem.setState(State.INSTALLED);
        subsystem.setState(State.UNINSTALLING);
        Throwable firstError = null;
        for (Resource resource : Activator.getInstance().getSubsystems().getResourcesReferencedBy(subsystem)) {
            // Don't uninstall the region context bundle here.
            if (Utils.isRegionContextBundle(resource))
                continue;
            try {
                ResourceUninstaller.newInstance(resource, subsystem).uninstall();
            } catch (Throwable t) {
                logger.error("An error occurred while uninstalling resource " + resource + " of subsystem " + subsystem, t);
                if (firstError == null)
                    firstError = t;
            }
        }
        subsystem.setState(State.UNINSTALLED);
        Activator activator = Activator.getInstance();
        activator.getSubsystemServiceRegistrar().unregister(subsystem);
        if (subsystem.isScoped()) {
            RegionContextBundleHelper.uninstallRegionContextBundle(subsystem);
            activator.getRegionDigraph().removeRegion(subsystem.getRegion());
        }
        if (firstError != null)
            throw new SubsystemException(firstError);
    } finally {
        // Let's be sure to always clean up the directory.
        IOUtils.deleteRecursive(subsystem.getDirectory());
    }
}
Also used : SubsystemException(org.osgi.service.subsystem.SubsystemException) Resource(org.osgi.resource.Resource)

Example 40 with SubsystemException

use of org.osgi.service.subsystem.SubsystemException 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)

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