Search in sources :

Example 1 with ChangeStateTask

use of org.apache.sling.installer.api.tasks.ChangeStateTask in project sling by apache.

the class PackageTransformer method createTask.

/**
     * @see org.apache.sling.installer.api.tasks.InstallTaskFactory#createTask(org.apache.sling.installer.api.tasks.TaskResourceGroup)
     */
@Override
public InstallTask createTask(final TaskResourceGroup toActivate) {
    final TaskResource resource = toActivate.getActiveResource();
    if (resource == null || !resource.getType().equals(RESOURCE_TYPE)) {
        return null;
    }
    // extract the package id
    final String id = (String) resource.getAttribute(ATTR_PCK_ID);
    final PackageId pkgId = PackageId.fromString(id);
    if (pkgId == null) {
        logger.error("Error during processing of {}: Package id is wrong/null.", resource);
        return new ChangeStateTask(toActivate, ResourceState.IGNORED);
    }
    if (resource.getState() == ResourceState.INSTALL) {
        return new InstallPackageTask(pkgId, toActivate);
    }
    return new UninstallPackageTask(pkgId, toActivate);
}
Also used : TaskResource(org.apache.sling.installer.api.tasks.TaskResource) PackageId(org.apache.jackrabbit.vault.packaging.PackageId) ChangeStateTask(org.apache.sling.installer.api.tasks.ChangeStateTask)

Example 2 with ChangeStateTask

use of org.apache.sling.installer.api.tasks.ChangeStateTask in project sling by apache.

the class UninstallSubsystemTask method execute.

@Override
public void execute(final InstallationContext ctx) {
    final TaskResource tr = this.getResource();
    ctx.log("Uninstalling subsystem from {}", tr);
    Subsystem subsystem = null;
    try {
        subsystem = this.bundleContext.getService(this.subsystemReference);
        if (subsystem != null) {
            subsystem.uninstall();
            ctx.addTaskToCurrentCycle(new ChangeStateTask(this.getResourceGroup(), ResourceState.UNINSTALLED));
            ctx.log("Uninstalled subsystem {}", subsystem);
        } else {
            ctx.log("Unable to uninstall subsystem {}.", tr);
            ctx.addTaskToCurrentCycle(new ChangeStateTask(this.getResourceGroup(), ResourceState.IGNORED));
        }
    } finally {
        if (subsystem != null) {
            this.bundleContext.ungetService(this.subsystemReference);
        }
    }
}
Also used : TaskResource(org.apache.sling.installer.api.tasks.TaskResource) Subsystem(org.osgi.service.subsystem.Subsystem) ChangeStateTask(org.apache.sling.installer.api.tasks.ChangeStateTask)

Example 3 with ChangeStateTask

use of org.apache.sling.installer.api.tasks.ChangeStateTask in project sling by apache.

the class BundleTaskCreatorTest method testDowngradeOfRemovedResource.

@Test
public void testDowngradeOfRemovedResource() throws IOException {
    final MockBundleResource[] r = { new MockBundleResource(SN, "1.0.0"), new MockBundleResource(SN, "1.1.0") };
    // Simulate V1.1 installed but resource is gone -> downgrade to 1.0
    r[1].setState(ResourceState.UNINSTALL);
    {
        final MockBundleTaskCreator c = new MockBundleTaskCreator();
        c.addBundleInfo(SN, "1.1.0", Bundle.ACTIVE);
        final SortedSet<InstallTask> s = getTasks(r, c);
        assertEquals("Expected two tasks", 2, s.size());
        final Iterator<InstallTask> i = s.iterator();
        final InstallTask first = i.next();
        assertTrue("Expected a ChangeStateTask:" + first, first instanceof ChangeStateTask);
        final InstallTask second = i.next();
        assertTrue("Expected a BundleRemoveTask", second instanceof BundleRemoveTask);
        final BundleRemoveTask t = (BundleRemoveTask) second;
        assertEquals("Remove should be to V1.1", r[1].getEntityId(), t.getResource().getEntityId());
    }
}
Also used : MockBundleResource(org.apache.sling.installer.core.impl.MockBundleResource) Iterator(java.util.Iterator) ChangeStateTask(org.apache.sling.installer.api.tasks.ChangeStateTask) SortedSet(java.util.SortedSet) InstallTask(org.apache.sling.installer.api.tasks.InstallTask) Test(org.junit.Test)

Example 4 with ChangeStateTask

use of org.apache.sling.installer.api.tasks.ChangeStateTask in project sling by apache.

the class StartSubsystemTask method execute.

@Override
public void execute(final InstallationContext ctx) {
    final TaskResource tr = this.getResource();
    ctx.log("Starting subsystem from {}", tr);
    this.subsystem.start();
    ctx.addTaskToCurrentCycle(new ChangeStateTask(this.getResourceGroup(), ResourceState.INSTALLED));
    ctx.log("Started subsystem {}", this.subsystem);
}
Also used : TaskResource(org.apache.sling.installer.api.tasks.TaskResource) ChangeStateTask(org.apache.sling.installer.api.tasks.ChangeStateTask)

Example 5 with ChangeStateTask

use of org.apache.sling.installer.api.tasks.ChangeStateTask in project sling by apache.

the class SubsystemInstaller method createTask.

/**
     * @see org.apache.sling.installer.api.tasks.InstallTaskFactory#createTask(org.apache.sling.installer.api.tasks.TaskResourceGroup)
     */
public InstallTask createTask(final TaskResourceGroup toActivate) {
    final InstallTask result;
    final TaskResource rsrc = toActivate.getActiveResource();
    if (rsrc.getType().equals(TYPE_SUBSYSTEM)) {
        // check if the required info is available
        final SubsystemInfo info = checkResource(toActivate);
        if (info == null) {
            // ignore as info is missing
            result = new ChangeStateTask(toActivate, ResourceState.IGNORED);
        } else {
            // search a subsystem with the symbolic name
            final ServiceReference<Subsystem> ref = this.getSubsystemReference(info.symbolicName);
            final Subsystem currentSubsystem = (ref != null ? this.bundleContext.getService(ref) : null);
            try {
                final Version newVersion = new Version(info.version);
                final Version oldVersion = (ref == null ? null : (Version) ref.getProperty("subsystem.version"));
                // Install
                if (rsrc.getState() == ResourceState.INSTALL) {
                    if (oldVersion != null) {
                        final int compare = oldVersion.compareTo(newVersion);
                        if (compare < 0) {
                            // installed version is lower -> update
                            result = new UpdateSubsystemTask(toActivate, this.bundleContext, ref, this.rootSubsystem);
                        } else if (compare == 0 && isSnapshot(newVersion)) {
                            // same version but snapshot -> update
                            result = new UpdateSubsystemTask(toActivate, this.bundleContext, ref, this.rootSubsystem);
                        } else if (compare == 0 && currentSubsystem != null && currentSubsystem.getState() != State.ACTIVE) {
                            // try to start the version
                            result = new StartSubsystemTask(toActivate, currentSubsystem);
                        } else {
                            logger.info("{} is not installed, subsystem with same or higher version is already installed: {}", info, newVersion);
                            result = new ChangeStateTask(toActivate, ResourceState.IGNORED);
                        }
                    } else {
                        result = new InstallSubsystemTask(toActivate, this.rootSubsystem);
                    }
                // Uninstall
                } else if (rsrc.getState() == ResourceState.UNINSTALL) {
                    if (oldVersion == null) {
                        logger.error("Nothing to uninstall. {} is currently not installed.", info);
                        result = new ChangeStateTask(toActivate, ResourceState.IGNORED);
                    } else {
                        final int compare = oldVersion.compareTo(newVersion);
                        if (compare == 0) {
                            result = new UninstallSubsystemTask(toActivate, this.bundleContext, ref);
                        } else {
                            logger.error("Nothing to uninstall. {} is currently not installed, different version is installed {}", info, oldVersion);
                            result = new ChangeStateTask(toActivate, ResourceState.IGNORED);
                        }
                    }
                } else {
                    result = null;
                }
            } finally {
                if (currentSubsystem != null) {
                    this.bundleContext.ungetService(ref);
                }
            }
        }
    } else {
        result = null;
    }
    return result;
}
Also used : TaskResource(org.apache.sling.installer.api.tasks.TaskResource) Version(org.osgi.framework.Version) Subsystem(org.osgi.service.subsystem.Subsystem) ChangeStateTask(org.apache.sling.installer.api.tasks.ChangeStateTask) InstallTask(org.apache.sling.installer.api.tasks.InstallTask)

Aggregations

ChangeStateTask (org.apache.sling.installer.api.tasks.ChangeStateTask)10 TaskResource (org.apache.sling.installer.api.tasks.TaskResource)9 InstallTask (org.apache.sling.installer.api.tasks.InstallTask)4 Subsystem (org.osgi.service.subsystem.Subsystem)4 Version (org.osgi.framework.Version)2 IOException (java.io.IOException)1 Hashtable (java.util.Hashtable)1 Iterator (java.util.Iterator)1 SortedSet (java.util.SortedSet)1 PackageId (org.apache.jackrabbit.vault.packaging.PackageId)1 InstallTaskFactory (org.apache.sling.installer.api.tasks.InstallTaskFactory)1 RegisteredResource (org.apache.sling.installer.api.tasks.RegisteredResource)1 ResourceTransformer (org.apache.sling.installer.api.tasks.ResourceTransformer)1 TaskResourceGroup (org.apache.sling.installer.api.tasks.TaskResourceGroup)1 TransformationResult (org.apache.sling.installer.api.tasks.TransformationResult)1 EntityResourceList (org.apache.sling.installer.core.impl.EntityResourceList)1 MockBundleResource (org.apache.sling.installer.core.impl.MockBundleResource)1 RegisteredResourceImpl (org.apache.sling.installer.core.impl.RegisteredResourceImpl)1 Util (org.apache.sling.installer.core.impl.Util)1 Before (org.junit.Before)1