Search in sources :

Example 11 with TaskResource

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

the class EntityResourceList method compact.

/**
     * Compact the resource group by removing uninstalled entries
     * @return <code>true</code> if another cycle should be started.
     */
public boolean compact() {
    synchronized (lock) {
        Collections.sort(this.resources);
        boolean startNewCycle = false;
        final List<TaskResource> toDelete = new ArrayList<TaskResource>();
        boolean first = true;
        for (final TaskResource r : resources) {
            if (r.getState() == ResourceState.UNINSTALLED || (!first && r.getState() == ResourceState.UNINSTALL)) {
                toDelete.add(r);
            }
            first = false;
        }
        if (!toDelete.isEmpty()) {
            // Avoid resources.remove(r) as the resource might have
            // changed since it was added, which causes it to compare()
            // differently and trip the TreeSet.remove() search.
            final Set<RegisteredResourceImpl> copy = new HashSet<RegisteredResourceImpl>(resources);
            for (final RegisteredResource r : toDelete) {
                copy.remove(r);
                this.cleanup(r);
                LOGGER.debug("Removing uninstalled from list: {}", r);
            }
            resources.clear();
            resources.addAll(copy);
            if (!this.isEmpty()) {
                startNewCycle = true;
            }
        }
        return startNewCycle;
    }
}
Also used : TaskResource(org.apache.sling.installer.api.tasks.TaskResource) RegisteredResource(org.apache.sling.installer.api.tasks.RegisteredResource) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet)

Example 12 with TaskResource

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

the class EntityResourceList method getActiveResource.

/**
     * @see org.apache.sling.installer.api.tasks.TaskResourceGroup#getActiveResource()
     */
@Override
public TaskResource getActiveResource() {
    synchronized (lock) {
        if (!resources.isEmpty()) {
            Collections.sort(this.resources);
            final TaskResource r = resources.get(0);
            if (r.getState() == ResourceState.INSTALL || r.getState() == ResourceState.UNINSTALL) {
                return r;
            }
        }
    }
    return null;
}
Also used : TaskResource(org.apache.sling.installer.api.tasks.TaskResource)

Example 13 with TaskResource

use of org.apache.sling.installer.api.tasks.TaskResource 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 14 with TaskResource

use of org.apache.sling.installer.api.tasks.TaskResource 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 15 with TaskResource

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

the class EntityResourceList method setFinishState.

@Override
public void setFinishState(ResourceState state, String error) {
    final TaskResource toActivate = getActiveResource();
    if (toActivate != null) {
        synchronized (lock) {
            if (toActivate.getState() == ResourceState.UNINSTALL && this.resources.size() > 1) {
                final TaskResource second = this.getNextActiveResource();
                // check for template
                if (second.getDictionary() != null && second.getDictionary().get(InstallableResource.RESOURCE_IS_TEMPLATE) != null) {
                    // second resource is a template! Do not install
                    ((RegisteredResourceImpl) second).setState(ResourceState.IGNORED, null);
                } else if (state == ResourceState.UNINSTALLED) {
                    // first resource got uninstalled, go back to second
                    if (second.getState() == ResourceState.IGNORED || second.getState() == ResourceState.INSTALLED) {
                        LOGGER.debug("Reactivating for next cycle: {}", second);
                        ((RegisteredResourceImpl) second).setState(ResourceState.INSTALL, null);
                    }
                } else {
                    // don't install as the first did not get uninstalled
                    if (second.getState() == ResourceState.INSTALL) {
                        String message = MessageFormat.format("The first resource '{0}' did not get uninstalled, therefore ignore this secondary resource in the uninstall group", toActivate.getEntityId());
                        LOGGER.debug(message);
                        ((RegisteredResourceImpl) second).setState(ResourceState.IGNORED, message);
                    }
                    // and now set resource to uninstalled
                    state = ResourceState.UNINSTALLED;
                }
            } else if (state == ResourceState.INSTALLED) {
                // make sure that no other resource has state INSTALLED
                if (this.resources.size() > 1) {
                    // to get the second item in the set we have to use an iterator!
                    final Iterator<RegisteredResourceImpl> i = this.resources.iterator();
                    // skip first
                    i.next();
                    while (i.hasNext()) {
                        final TaskResource rsrc = i.next();
                        if (rsrc.getState() == ResourceState.INSTALLED) {
                            ((RegisteredResourceImpl) rsrc).setState(ResourceState.INSTALL, null);
                        }
                    }
                }
            }
            ((RegisteredResourceImpl) toActivate).setState(state, error);
            if (state != ResourceState.INSTALLED) {
                // make sure to remove all install info attributes if the resource is not
                // installed anymore
                toActivate.setAttribute(TaskResource.ATTR_INSTALL_EXCLUDED, null);
                toActivate.setAttribute(TaskResource.ATTR_INSTALL_INFO, null);
            }
            // remove install info attributes on all other resources in the group
            final Iterator<RegisteredResourceImpl> tri = this.resources.iterator();
            // skip first
            tri.next();
            while (tri.hasNext()) {
                final TaskResource rsrc = tri.next();
                rsrc.setAttribute(TaskResource.ATTR_INSTALL_EXCLUDED, null);
                rsrc.setAttribute(TaskResource.ATTR_INSTALL_INFO, null);
            }
        }
        this.listener.onEvent(new InstallationEvent() {

            @Override
            public TYPE getType() {
                return TYPE.PROCESSED;
            }

            @Override
            public Object getSource() {
                return toActivate;
            }
        });
        if (state == ResourceState.UNINSTALLED) {
            this.cleanup(toActivate);
        }
    }
}
Also used : TaskResource(org.apache.sling.installer.api.tasks.TaskResource) Iterator(java.util.Iterator) InstallationEvent(org.apache.sling.installer.api.event.InstallationEvent)

Aggregations

TaskResource (org.apache.sling.installer.api.tasks.TaskResource)28 ChangeStateTask (org.apache.sling.installer.api.tasks.ChangeStateTask)9 InstallTask (org.apache.sling.installer.api.tasks.InstallTask)6 IOException (java.io.IOException)5 Version (org.osgi.framework.Version)5 InstallableResource (org.apache.sling.installer.api.InstallableResource)4 RegisteredResource (org.apache.sling.installer.api.tasks.RegisteredResource)4 Subsystem (org.osgi.service.subsystem.Subsystem)4 File (java.io.File)3 InputStream (java.io.InputStream)3 ArrayList (java.util.ArrayList)3 Test (org.junit.Test)3 LinkedList (java.util.LinkedList)2 List (java.util.List)2 TreeSet (java.util.TreeSet)2 OsgiInstaller (org.apache.sling.installer.api.OsgiInstaller)2 UpdateHandler (org.apache.sling.installer.api.UpdateHandler)2 InstallTaskFactory (org.apache.sling.installer.api.tasks.InstallTaskFactory)2 TaskResourceGroup (org.apache.sling.installer.api.tasks.TaskResourceGroup)2 TransformationResult (org.apache.sling.installer.api.tasks.TransformationResult)2