use of org.apache.sling.installer.api.tasks.InstallTask 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;
}
use of org.apache.sling.installer.api.tasks.InstallTask 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;
}
use of org.apache.sling.installer.api.tasks.InstallTask 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;
}
use of org.apache.sling.installer.api.tasks.InstallTask in project sling by apache.
the class OsgiInstallerImpl method getLowestStartLevel.
/**
* Get the lowest start level for the update operation
*/
private int getLowestStartLevel(final SortedSet<InstallTask> tasks, final StartLevel startLevel) {
final int currentStartLevel = startLevel.getStartLevel();
int startLevelToTarget = currentStartLevel;
for (final InstallTask task : tasks) {
if (task instanceof BundleUpdateTask) {
final Bundle b = ((BundleUpdateTask) task).getBundle();
if (b != null) {
try {
final int bundleStartLevel = startLevel.getBundleStartLevel(b) - 1;
if (bundleStartLevel < startLevelToTarget) {
startLevelToTarget = bundleStartLevel;
}
} catch (final IllegalArgumentException iae) {
// ignore - bundle is uninstalled
}
}
}
}
// check installer start level
final int ownStartLevel = startLevel.getBundleStartLevel(ctx.getBundle());
if (ownStartLevel > startLevelToTarget) {
// we don't want to disable ourselves
startLevelToTarget = ownStartLevel;
}
return startLevelToTarget;
}
use of org.apache.sling.installer.api.tasks.InstallTask in project sling by apache.
the class TaskOrderingTest method testMultipleRefreshAndStart.
@org.junit.Test
public void testMultipleRefreshAndStart() throws Exception {
int testIndex = 1;
final InstallTask[] tasksInOrder = { new BundleRemoveTask(getRegisteredResource("test:url"), null), new RefreshBundlesTask(null), new BundleStartTask(null, 0, null), new BundleStartTask(null, 1, null) };
taskSet.clear();
taskSet.add(tasksInOrder[3]);
taskSet.add(tasksInOrder[3]);
taskSet.add(new RefreshBundlesTask(null));
taskSet.add(tasksInOrder[2]);
taskSet.add(tasksInOrder[2]);
taskSet.add(tasksInOrder[1]);
taskSet.add(new RefreshBundlesTask(null));
taskSet.add(new RefreshBundlesTask(null));
taskSet.add(tasksInOrder[0]);
taskSet.add(tasksInOrder[3]);
taskSet.add(new RefreshBundlesTask(null));
taskSet.add(tasksInOrder[3]);
taskSet.add(tasksInOrder[2]);
taskSet.add(new RefreshBundlesTask(null));
taskSet.add(tasksInOrder[2]);
taskSet.add(tasksInOrder[1]);
taskSet.add(new RefreshBundlesTask(null));
taskSet.add(tasksInOrder[1]);
taskSet.add(new RefreshBundlesTask(null));
assertOrder(testIndex++, taskSet, tasksInOrder);
}
Aggregations