Search in sources :

Example 6 with ChangeStateTask

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

the class ConfigTaskCreator method createTask.

/**
     * Create a task to install or uninstall a configuration.
     *
	 * @see org.apache.sling.installer.api.tasks.InstallTaskFactory#createTask(org.apache.sling.installer.api.tasks.TaskResourceGroup)
	 */
@Override
public InstallTask createTask(final TaskResourceGroup group) {
    final TaskResource toActivate = group.getActiveResource();
    if (!toActivate.getType().equals(InstallableResource.TYPE_CONFIG)) {
        return null;
    }
    final InstallTask result;
    if (toActivate.getState() == ResourceState.UNINSTALL) {
        // if this is an uninstall, check if we have to install an older version
        // in this case we should do an update instead of uninstall/install (!)
        final TaskResource second = group.getNextActiveResource();
        if (second != null && (second.getState() == ResourceState.IGNORED || second.getState() == ResourceState.INSTALLED || second.getState() == ResourceState.INSTALL) && (second.getDictionary() == null || second.getDictionary().get(InstallableResource.RESOURCE_IS_TEMPLATE) == null)) {
            result = new ChangeStateTask(group, ResourceState.UNINSTALLED);
        } else {
            result = new ConfigRemoveTask(group, this.configAdmin);
        }
    } else {
        result = new ConfigInstallTask(group, this.configAdmin);
    }
    return result;
}
Also used : TaskResource(org.apache.sling.installer.api.tasks.TaskResource) ChangeStateTask(org.apache.sling.installer.api.tasks.ChangeStateTask) InstallTask(org.apache.sling.installer.api.tasks.InstallTask)

Example 7 with ChangeStateTask

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

the class BundleTaskCreator method createTask.

/**
     * Create a bundle task - install, update or remove
     *
	 * @see org.apache.sling.installer.api.tasks.InstallTaskFactory#createTask(org.apache.sling.installer.api.tasks.TaskResourceGroup)
	 */
@Override
public InstallTask createTask(final TaskResourceGroup resourceList) {
    // quick check of the resource type.
    final TaskResource toActivate = resourceList.getActiveResource();
    if (toActivate.getType().equals(PersistentResourceList.RESTART_ACTIVE_BUNDLES_TYPE)) {
        return new RestartActiveBundlesTask(resourceList, this.taskSupport);
    }
    if (!toActivate.getType().equals(InstallableResource.TYPE_BUNDLE)) {
        return null;
    }
    // check if symbolic name and version is provided in the attributes
    if (toActivate.getAttribute(Constants.BUNDLE_SYMBOLICNAME) == null) {
        final Util.BundleHeaders headers = Util.readBundleHeaders(toActivate, logger);
        if (headers == null) {
            String message = MessageFormat.format("Resource of type bundle {0} is not really a bundle - manifest entries are missing.", toActivate);
            logger.info(message);
            return new ChangeStateTask(resourceList, ResourceState.IGNORED, message);
        }
        toActivate.setAttribute(Constants.BUNDLE_SYMBOLICNAME, headers.symbolicName);
        toActivate.setAttribute(Constants.BUNDLE_VERSION, headers.version);
        if (headers.activationPolicy != null) {
            toActivate.setAttribute(Constants.BUNDLE_ACTIVATIONPOLICY, headers.activationPolicy);
        }
    }
    final String symbolicName = (String) toActivate.getAttribute(Constants.BUNDLE_SYMBOLICNAME);
    final boolean isInstallerCoreBundle = this.bundleContext.getBundle().getSymbolicName().equals(symbolicName);
    // Uninstall
    final InstallTask result;
    if (toActivate.getState() == ResourceState.UNINSTALL) {
        // find the info with the exact version
        final BundleInfo info = this.getBundleInfo(symbolicName, (String) toActivate.getAttribute(Constants.BUNDLE_VERSION));
        // Remove corresponding bundle if present and if we installed it
        if (info != null) {
            // if this is an uninstall, check if we have to install an older version
            // in this case we should do an update instead of uninstall/install (!)
            Iterator<TaskResource> candidatesIt = ((EntityResourceList) resourceList).getActiveResourceIterator();
            TaskResource second = null;
            while (candidatesIt != null && second == null && candidatesIt.hasNext()) {
                TaskResource candidate = candidatesIt.next();
                boolean sameVersion = toActivate.getVersion().equals(candidate.getVersion());
                if (!sameVersion) {
                    if (bundleBlacklist.isBlacklisted(symbolicName, candidate.getVersion())) {
                        String message = MessageFormat.format("Uninstalling blacklisted bundle {0} found at {1}", symbolicName, candidate.getURL());
                        logger.info(message);
                        // blacklisted candidates should be uninstalled to no longer be taken into account anymore
                        ((RegisteredResourceImpl) candidate).setState(ResourceState.UNINSTALL, message);
                    } else {
                        second = candidate;
                    }
                }
            }
            if (second != null && (second.getState() == ResourceState.IGNORED || second.getState() == ResourceState.INSTALLED || second.getState() == ResourceState.INSTALL)) {
                second.setAttribute(FORCE_INSTALL_VERSION, info.version.toString());
                BundleUtil.clearBundleStart(second);
                logger.debug("Detected downgrade of bundle {}", symbolicName);
                result = new ChangeStateTask(resourceList, ResourceState.UNINSTALLED, null);
            } else {
                // prevent uninstalling the installer itself!
                if (isInstallerCoreBundle) {
                    logger.debug("Prevent completely uninstalling installer bundle {}", symbolicName);
                    result = new ChangeStateTask(resourceList, ResourceState.UNINSTALLED, null);
                } else {
                    result = new BundleRemoveTask(resourceList, this.taskSupport);
                }
            }
        } else {
            logger.debug("Bundle {}:{} is not installed anymore - nothing to remove.", symbolicName, toActivate.getAttribute(Constants.BUNDLE_VERSION));
            result = new ChangeStateTask(resourceList, ResourceState.UNINSTALLED, null);
        }
    // Install
    } else {
        // check for installer and system update
        final Integer asyncTaskCounter = (Integer) toActivate.getAttribute(InstallTask.ASYNC_ATTR_NAME);
        if (asyncTaskCounter != null) {
            if (isInstallerCoreBundle) {
                result = new InstallerBundleUpdateTask(resourceList, this.taskSupport);
            } else {
                // system bundle
                result = new ChangeStateTask(resourceList, ResourceState.INSTALLED, null, new String[] { InstallTask.ASYNC_ATTR_NAME }, null);
            }
        } else {
            final Version newVersion = new Version((String) toActivate.getAttribute(Constants.BUNDLE_VERSION));
            if (bundleBlacklist.isBlacklisted(symbolicName, newVersion)) {
                String message = MessageFormat.format("Ignoring blacklisted bundle {0} found at {1}", symbolicName, toActivate.getURL());
                logger.info(message);
                result = new ChangeStateTask(resourceList, ResourceState.IGNORED, message);
            } else {
                // for install and update, we want the bundle with the
                // highest version
                final BundleInfo info = this.getBundleInfo(symbolicName, null);
                // check if we should start the bundle as we installed it in the previous run
                if (info == null) {
                    // bundle is not installed yet: install
                    result = new BundleInstallTask(resourceList, this.taskSupport);
                } else if (BundleUtil.isBundleStart(toActivate)) {
                    result = new BundleStartTask(resourceList, info.id, this.taskSupport);
                } else {
                    boolean doUpdate = false;
                    final int compare = info.version.compareTo(newVersion);
                    if (compare < 0) {
                        // installed version is lower -> update
                        doUpdate = true;
                    } else if (compare > 0) {
                        final String forceVersion = (String) toActivate.getAttribute(FORCE_INSTALL_VERSION);
                        if (forceVersion != null && info.version.compareTo(new Version(forceVersion)) == 0) {
                            doUpdate = true;
                        } else {
                            logger.debug("Bundle " + info.symbolicName + " " + newVersion + " is not installed, bundle with higher version is already installed.");
                        }
                    } else if (compare == 0 && BundleInfo.isSnapshot(newVersion)) {
                        // installed, same version but SNAPSHOT
                        doUpdate = true;
                    }
                    if (doUpdate) {
                        logger.debug("Scheduling update of {}", toActivate);
                        // check if this is the system bundle
                        if (Constants.SYSTEM_BUNDLE_SYMBOLICNAME.equals(symbolicName)) {
                            result = new SystemBundleUpdateTask(resourceList, this.taskSupport);
                        // check if this is a installer update
                        } else if (isInstallerCoreBundle) {
                            result = new InstallerBundleUpdateTask(resourceList, this.taskSupport);
                        } else {
                            result = new BundleUpdateTask(resourceList, this.taskSupport);
                        }
                    } else if (compare == 0 && (isInstallerCoreBundle || Constants.SYSTEM_BUNDLE_SYMBOLICNAME.equals(symbolicName))) {
                        // the installer core bundle / system bundle has been updated, just set state
                        result = new ChangeStateTask(resourceList, ResourceState.INSTALLED, "This is the system bundle, therefore nothing was actually done!");
                    } else {
                        String message = MessageFormat.format("Nothing to install for {0}, same or newer version {1} already installed.", toActivate, newVersion);
                        logger.debug(message);
                        result = new ChangeStateTask(resourceList, ResourceState.IGNORED, message);
                    }
                }
            }
        }
        toActivate.setAttribute(FORCE_INSTALL_VERSION, null);
    }
    return result;
}
Also used : TaskResource(org.apache.sling.installer.api.tasks.TaskResource) Util(org.apache.sling.installer.core.impl.Util) InstallTask(org.apache.sling.installer.api.tasks.InstallTask) RegisteredResourceImpl(org.apache.sling.installer.core.impl.RegisteredResourceImpl) Version(org.osgi.framework.Version) ChangeStateTask(org.apache.sling.installer.api.tasks.ChangeStateTask) EntityResourceList(org.apache.sling.installer.core.impl.EntityResourceList)

Example 8 with ChangeStateTask

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

the class InstallSubsystemTask method execute.

@Override
public void execute(final InstallationContext ctx) {
    final TaskResource tr = this.getResource();
    ctx.log("Installing new subsystem from {}", tr);
    try {
        final Subsystem sub = this.rootSubsystem.install(tr.getURL(), tr.getInputStream());
        ctx.addTaskToCurrentCycle(new StartSubsystemTask(this.getResourceGroup(), sub));
        ctx.log("Installed new subsystem {}", sub);
    } catch (final IOException e) {
        ctx.log("Unable to install subsystem {} : {}", tr, e);
        ctx.addTaskToCurrentCycle(new ChangeStateTask(this.getResourceGroup(), ResourceState.IGNORED));
    }
}
Also used : TaskResource(org.apache.sling.installer.api.tasks.TaskResource) Subsystem(org.osgi.service.subsystem.Subsystem) ChangeStateTask(org.apache.sling.installer.api.tasks.ChangeStateTask) IOException(java.io.IOException)

Example 9 with ChangeStateTask

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

the class UpdateSubsystemTask method execute.

@Override
public void execute(final InstallationContext ctx) {
    final TaskResource tr = this.getResource();
    ctx.log("Updating subsystem from {}", tr);
    Subsystem subsystem = null;
    try {
        subsystem = this.bundleContext.getService(this.subsystemReference);
        if (subsystem != null) {
            subsystem.stop();
            subsystem.uninstall();
            ctx.addTaskToCurrentCycle(new InstallSubsystemTask(this.getResourceGroup(), this.rootSubsystem));
        } else {
            ctx.log("Unable to update 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 10 with ChangeStateTask

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

the class UpdateHandlerTest method setUp.

@Before
public void setUp() {
    setupInstaller();
    serviceRegistrations.clear();
    final Dictionary<String, Object> props = new Hashtable<String, Object>();
    props.put(Constants.SERVICE_RANKING, 1000);
    serviceRegistrations.add(this.bundleContext.registerService(ResourceTransformer.class, new ResourceTransformer() {

        public TransformationResult[] transform(final RegisteredResource resource) {
            final int lastDot = resource.getURL().lastIndexOf('.');
            final int lastSlash = resource.getURL().lastIndexOf('/');
            if (resource.getURL().substring(lastDot + 1).equals(TYPE)) {
                final String id = resource.getURL().substring(lastSlash + 1, lastDot);
                final TransformationResult tr = new TransformationResult();
                tr.setId(id);
                tr.setResourceType(TYPE);
                return new TransformationResult[] { tr };
            }
            return null;
        }
    }, props));
    serviceRegistrations.add(this.bundleContext.registerService(InstallTaskFactory.class, new InstallTaskFactory() {

        public InstallTask createTask(final TaskResourceGroup toActivate) {
            final TaskResource tr = toActivate.getActiveResource();
            if (tr != null && tr.getEntityId().startsWith(TYPE)) {
                if (tr.getState() == ResourceState.INSTALL) {
                    installed.put(tr.getEntityId(), tr.getDictionary());
                    return new ChangeStateTask(toActivate, ResourceState.INSTALLED);
                } else {
                    installed.remove(tr.getEntityId());
                    return new ChangeStateTask(toActivate, ResourceState.UNINSTALLED);
                }
            }
            return null;
        }
    }, props));
}
Also used : InstallTaskFactory(org.apache.sling.installer.api.tasks.InstallTaskFactory) TaskResource(org.apache.sling.installer.api.tasks.TaskResource) Hashtable(java.util.Hashtable) RegisteredResource(org.apache.sling.installer.api.tasks.RegisteredResource) TaskResourceGroup(org.apache.sling.installer.api.tasks.TaskResourceGroup) ChangeStateTask(org.apache.sling.installer.api.tasks.ChangeStateTask) ResourceTransformer(org.apache.sling.installer.api.tasks.ResourceTransformer) TransformationResult(org.apache.sling.installer.api.tasks.TransformationResult) Before(org.junit.Before)

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