Search in sources :

Example 1 with InstallTask

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

the class OsgiInstallerImpl method doExecuteTasks.

/**
     * Execute all tasks
     * @param tasks The tasks to executed.
     * @return The action to perform after the execution.
     */
private ACTION doExecuteTasks(final SortedSet<InstallTask> tasks) {
    if (!tasks.isEmpty()) {
        final InstallationContext ctx = new InstallationContext() {

            @SuppressWarnings("deprecation")
            @Override
            public void addTaskToNextCycle(final InstallTask t) {
                logger.warn("Deprecated method addTaskToNextCycle was called. Task will be executed in this cycle instead: {}", t);
                synchronized (tasks) {
                    tasks.add(t);
                }
            }

            @Override
            public void addTaskToCurrentCycle(final InstallTask t) {
                logger.debug("Adding {}task to current cycle: {}", t.isAsynchronousTask() ? "async " : "", t);
                synchronized (tasks) {
                    tasks.add(t);
                }
            }

            @SuppressWarnings("deprecation")
            @Override
            public void addAsyncTask(final InstallTask t) {
                if (t.isAsynchronousTask()) {
                    logger.warn("Deprecated method addAsyncTask was called: {}", t);
                    this.addTaskToCurrentCycle(t);
                } else {
                    logger.warn("Deprecated method addAsyncTask is called with non async task(!): {}", t);
                    this.addTaskToCurrentCycle(new AsyncWrapperInstallTask(t));
                }
            }

            @Override
            public void log(final String message, final Object... args) {
                auditLogger.info(message, args);
            }

            @Override
            public void asyncTaskFailed(final InstallTask t) {
                // persist all changes and retry restart
                // remove attribute
                logger.debug("asyncTaskFailed: {}", t);
                if (t.getResource() != null) {
                    t.getResource().setAttribute(InstallTask.ASYNC_ATTR_NAME, null);
                }
                persistentList.save();
                synchronized (resourcesLock) {
                    if (!active) {
                        logger.debug("Restarting background thread from asyncTaskFailed");
                        active = true;
                        startBackgroundThread();
                    } else {
                        logger.debug("active={}, no need to restart background thread", active);
                    }
                }
            }
        };
        while (this.active && !tasks.isEmpty()) {
            InstallTask task = null;
            synchronized (tasks) {
                task = tasks.first();
                tasks.remove(task);
            }
            // async tasks are executed "immediately"
            if (task.isAsynchronousTask()) {
                logger.debug("Executing async task: {}", task);
                // set attribute
                final Integer oldValue;
                if (task.getResource() != null) {
                    oldValue = (Integer) task.getResource().getAttribute(InstallTask.ASYNC_ATTR_NAME);
                    final Integer newValue;
                    if (oldValue == null) {
                        newValue = 1;
                    } else {
                        newValue = oldValue + 1;
                    }
                    task.getResource().setAttribute(InstallTask.ASYNC_ATTR_NAME, newValue);
                } else {
                    oldValue = null;
                }
                // save new state
                this.cleanupInstallableResources();
                final InstallTask aSyncTask = task;
                final String threadName = "BackgroundTaskThread" + backgroundTaskCounter.incrementAndGet();
                final Thread t = new Thread(threadName) {

                    @Override
                    public void run() {
                        logger.debug("Starting background thread {} to execute {}", Thread.currentThread().getName(), aSyncTask);
                        try {
                            Thread.sleep(2000L);
                        } catch (final InterruptedException ie) {
                        // ignore
                        }
                        // reset attribute
                        if (aSyncTask.getResource() != null) {
                            aSyncTask.getResource().setAttribute(InstallTask.ASYNC_ATTR_NAME, oldValue);
                        }
                        aSyncTask.execute(ctx);
                        logger.debug("Background thread {} ends", Thread.currentThread().getName());
                    }
                };
                t.start();
                return ACTION.SHUTDOWN;
            }
            try {
                logger.debug("Executing task: {}", task);
                task.execute(ctx);
            } catch (final Throwable t) {
                logger.error("Uncaught exception during task execution!", t);
            }
        }
        // save new state
        final boolean newCycle = this.cleanupInstallableResources();
        if (newCycle) {
            return ACTION.CYCLE;
        }
    }
    return ACTION.SLEEP;
}
Also used : InstallationContext(org.apache.sling.installer.api.tasks.InstallationContext) InstallTask(org.apache.sling.installer.api.tasks.InstallTask)

Example 2 with InstallTask

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

the class OsgiInstallerImpl method computeTasks.

/**
     * Compute OSGi tasks based on our resources, and add to supplied list of tasks.
     */
private SortedSet<InstallTask> computeTasks() {
    final SortedSet<InstallTask> tasks = new TreeSet<>();
    // Walk the list of entities, and create appropriate OSGi tasks for each group
    final List<InstallTaskFactory> services = this.factoryTracker.getSortedServices();
    if (services.size() > 0) {
        for (final String entityId : this.persistentList.getEntityIds()) {
            final EntityResourceList group = this.persistentList.getEntityResourceList(entityId);
            // Check the first resource in each group
            final TaskResource toActivate = group.getActiveResource();
            if (toActivate != null) {
                final InstallTask task = getTask(services, group);
                if (task != null) {
                    tasks.add(task);
                }
            }
        }
    }
    return tasks;
}
Also used : InstallTaskFactory(org.apache.sling.installer.api.tasks.InstallTaskFactory) TaskResource(org.apache.sling.installer.api.tasks.TaskResource) TreeSet(java.util.TreeSet) InstallTask(org.apache.sling.installer.api.tasks.InstallTask)

Example 3 with InstallTask

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

the class TaskOrderingTest method testMultipleConfigAndBundles.

@org.junit.Test
public void testMultipleConfigAndBundles() throws Exception {
    int testIndex = 1;
    final InstallTask[] tasksInOrder = { new BundleInstallTask(getRegisteredResource("test:someURIa.nothing"), null), new BundleInstallTask(getRegisteredResource("test:someURIb.nothing"), null), new RefreshBundlesTask(null), new BundleStartTask(null, 0, null) };
    taskSet.clear();
    for (int i = tasksInOrder.length - 1; i >= 0; i--) {
        taskSet.add(tasksInOrder[i]);
    }
    assertOrder(testIndex++, taskSet, tasksInOrder);
    taskSet.clear();
    for (int i = 0; i < tasksInOrder.length; i++) {
        taskSet.add(tasksInOrder[i]);
    }
    assertOrder(testIndex++, taskSet, tasksInOrder);
}
Also used : BundleInstallTask(org.apache.sling.installer.core.impl.tasks.BundleInstallTask) BundleStartTask(org.apache.sling.installer.core.impl.tasks.BundleStartTask) InstallTask(org.apache.sling.installer.api.tasks.InstallTask) BundleInstallTask(org.apache.sling.installer.core.impl.tasks.BundleInstallTask) RefreshBundlesTask(org.apache.sling.installer.core.impl.tasks.RefreshBundlesTask)

Example 4 with InstallTask

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

the class TaskOrderingTest method testBasicOrdering.

@org.junit.Test
public void testBasicOrdering() throws Exception {
    int testIndex = 1;
    final InstallTask[] tasksInOrder = { new BundleRemoveTask(getRegisteredResource("test:url"), null), new BundleInstallTask(getRegisteredResource("test:url"), null), new BundleUpdateTask(getRegisteredResource("test:url"), null), new RefreshBundlesTask(null), new BundleStartTask(null, 0, null) };
    taskSet.clear();
    taskSet.add(tasksInOrder[4]);
    taskSet.add(tasksInOrder[3]);
    taskSet.add(tasksInOrder[2]);
    taskSet.add(tasksInOrder[1]);
    taskSet.add(tasksInOrder[0]);
    assertOrder(testIndex++, taskSet, tasksInOrder);
    taskSet.clear();
    taskSet.add(tasksInOrder[0]);
    taskSet.add(tasksInOrder[1]);
    taskSet.add(tasksInOrder[2]);
    taskSet.add(tasksInOrder[3]);
    taskSet.add(tasksInOrder[4]);
    assertOrder(testIndex++, taskSet, tasksInOrder);
    taskSet.clear();
    taskSet.add(tasksInOrder[3]);
    taskSet.add(tasksInOrder[2]);
    taskSet.add(tasksInOrder[0]);
    taskSet.add(tasksInOrder[4]);
    taskSet.add(tasksInOrder[1]);
    assertOrder(testIndex++, taskSet, tasksInOrder);
    taskSet.clear();
    taskSet.add(tasksInOrder[4]);
    taskSet.add(tasksInOrder[0]);
    taskSet.add(tasksInOrder[2]);
    taskSet.add(tasksInOrder[3]);
    taskSet.add(tasksInOrder[1]);
    assertOrder(testIndex++, taskSet, tasksInOrder);
}
Also used : BundleInstallTask(org.apache.sling.installer.core.impl.tasks.BundleInstallTask) BundleUpdateTask(org.apache.sling.installer.core.impl.tasks.BundleUpdateTask) BundleStartTask(org.apache.sling.installer.core.impl.tasks.BundleStartTask) BundleRemoveTask(org.apache.sling.installer.core.impl.tasks.BundleRemoveTask) InstallTask(org.apache.sling.installer.api.tasks.InstallTask) BundleInstallTask(org.apache.sling.installer.core.impl.tasks.BundleInstallTask) RefreshBundlesTask(org.apache.sling.installer.core.impl.tasks.RefreshBundlesTask)

Example 5 with InstallTask

use of org.apache.sling.installer.api.tasks.InstallTask 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)

Aggregations

InstallTask (org.apache.sling.installer.api.tasks.InstallTask)13 TaskResource (org.apache.sling.installer.api.tasks.TaskResource)6 ChangeStateTask (org.apache.sling.installer.api.tasks.ChangeStateTask)4 BundleInstallTask (org.apache.sling.installer.core.impl.tasks.BundleInstallTask)4 BundleStartTask (org.apache.sling.installer.core.impl.tasks.BundleStartTask)4 RefreshBundlesTask (org.apache.sling.installer.core.impl.tasks.RefreshBundlesTask)3 TreeSet (java.util.TreeSet)2 EntityResourceList (org.apache.sling.installer.core.impl.EntityResourceList)2 MockBundleResource (org.apache.sling.installer.core.impl.MockBundleResource)2 BundleRemoveTask (org.apache.sling.installer.core.impl.tasks.BundleRemoveTask)2 BundleUpdateTask (org.apache.sling.installer.core.impl.tasks.BundleUpdateTask)2 Test (org.junit.Test)2 Version (org.osgi.framework.Version)2 Iterator (java.util.Iterator)1 SortedSet (java.util.SortedSet)1 InstallTaskFactory (org.apache.sling.installer.api.tasks.InstallTaskFactory)1 InstallationContext (org.apache.sling.installer.api.tasks.InstallationContext)1 RegisteredResourceImpl (org.apache.sling.installer.core.impl.RegisteredResourceImpl)1 Util (org.apache.sling.installer.core.impl.Util)1 Bundle (org.osgi.framework.Bundle)1