Search in sources :

Example 6 with TaskResource

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

Example 7 with TaskResource

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

the class RegisteredResourceTest method testConfigEntity.

//    @org.junit.Test
public void testConfigEntity() throws Exception {
    final InstallableResource i = new InstallableResource("test:/foo/someconfig", null, new Hashtable<String, Object>(), null, null, null);
    final TaskResource r = create(i);
    assertNull("RegisteredResource must not have bundle symbolic name", r.getAttribute(Constants.BUNDLE_SYMBOLICNAME));
    assertEquals("RegisteredResource entity ID must match", "config:someconfig", r.getEntityId());
}
Also used : TaskResource(org.apache.sling.installer.api.tasks.TaskResource) InstallableResource(org.apache.sling.installer.api.InstallableResource)

Example 8 with TaskResource

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

the class RegisteredResourceTest method testBundleManifest.

@org.junit.Test
public void testBundleManifest() throws Exception {
    final File f = getTestBundle("testbundle-1.0.jar");
    final InstallableResource i = new InstallableResource("test:" + f.getAbsolutePath(), new FileInputStream(f), null, f.getName(), null, null);
    final TaskResource r = create(i);
    assertNotNull("RegisteredResource must have bundle symbolic name", r.getAttribute(Constants.BUNDLE_SYMBOLICNAME));
    assertEquals("RegisteredResource entity ID must match", "bundle:osgi-installer-testbundle", r.getEntityId());
}
Also used : TaskResource(org.apache.sling.installer.api.tasks.TaskResource) InstallableResource(org.apache.sling.installer.api.InstallableResource) File(java.io.File) FileInputStream(java.io.FileInputStream) Test(org.junit.Test)

Example 9 with TaskResource

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

the class OsgiInstallerImpl method getInstallationState.

/**
     * @see org.apache.sling.installer.api.info.InfoProvider#getInstallationState()
     */
@Override
public InstallationState getInstallationState() {
    synchronized (this.resourcesLock) {
        final InstallationState state = new InstallationState() {

            private final List<ResourceGroup> activeResources = new ArrayList<>();

            private final List<ResourceGroup> installedResources = new ArrayList<>();

            private final List<RegisteredResource> untransformedResources = new ArrayList<>();

            @Override
            public List<ResourceGroup> getActiveResources() {
                return activeResources;
            }

            @Override
            public List<ResourceGroup> getInstalledResources() {
                return installedResources;
            }

            @Override
            public List<RegisteredResource> getUntransformedResources() {
                return untransformedResources;
            }

            @Override
            public String toString() {
                return "InstallationState[active resources: " + this.activeResources + ", installed resources: " + this.installedResources + ", untransformed resources: " + this.untransformedResources + "]";
            }
        };
        for (final String entityId : this.persistentList.getEntityIds()) {
            if (!this.persistentList.isSpecialEntityId(entityId)) {
                final EntityResourceList group = this.persistentList.getEntityResourceList(entityId);
                final String alias = group.getAlias();
                final List<Resource> resources = new ArrayList<>();
                boolean first = true;
                boolean isActive = false;
                for (final TaskResource tr : group.getResources()) {
                    final ResourceState resourceState = tr.getState();
                    if (first) {
                        if (resourceState == ResourceState.INSTALL || resourceState == ResourceState.UNINSTALL) {
                            isActive = true;
                        }
                        first = false;
                    }
                    resources.add(new Resource() {

                        @Override
                        public String getScheme() {
                            return tr.getScheme();
                        }

                        @Override
                        public String getURL() {
                            return tr.getURL();
                        }

                        @Override
                        public String getType() {
                            return tr.getType();
                        }

                        @Override
                        public InputStream getInputStream() throws IOException {
                            return tr.getInputStream();
                        }

                        @Override
                        public Dictionary<String, Object> getDictionary() {
                            return tr.getDictionary();
                        }

                        @Override
                        public String getDigest() {
                            return tr.getDigest();
                        }

                        @Override
                        public int getPriority() {
                            return tr.getPriority();
                        }

                        @Override
                        public String getEntityId() {
                            return tr.getEntityId();
                        }

                        @Override
                        public ResourceState getState() {
                            return resourceState;
                        }

                        @Override
                        public Version getVersion() {
                            return tr.getVersion();
                        }

                        @Override
                        public long getLastChange() {
                            return ((RegisteredResourceImpl) tr).getLastChange();
                        }

                        @Override
                        public Object getAttribute(final String key) {
                            return tr.getAttribute(key);
                        }

                        @Override
                        @CheckForNull
                        public String getError() {
                            return tr.getError();
                        }

                        @Override
                        public String toString() {
                            return "resource[entityId=" + getEntityId() + ", scheme=" + getScheme() + ", url=" + getURL() + ", type=" + getType() + ", error=" + getError() + ", state=" + getState() + ", version=" + getVersion() + ", lastChange=" + getLastChange() + ", priority=" + getPriority() + ", digest=" + getDigest() + "]";
                        }
                    });
                }
                final ResourceGroup rg = new ResourceGroup() {

                    @Override
                    public List<Resource> getResources() {
                        return resources;
                    }

                    @Override
                    public String getAlias() {
                        return alias;
                    }

                    @Override
                    public String toString() {
                        return "group[" + resources + "]";
                    }
                };
                if (isActive) {
                    state.getActiveResources().add(rg);
                } else {
                    state.getInstalledResources().add(rg);
                }
            }
        }
        Collections.sort(state.getActiveResources(), COMPARATOR);
        Collections.sort(state.getInstalledResources(), COMPARATOR);
        state.getUntransformedResources().addAll(this.persistentList.getUntransformedResources());
        return state;
    }
}
Also used : Dictionary(java.util.Dictionary) TaskResource(org.apache.sling.installer.api.tasks.TaskResource) InputStream(java.io.InputStream) RegisteredResource(org.apache.sling.installer.api.tasks.RegisteredResource) ArrayList(java.util.ArrayList) TaskResource(org.apache.sling.installer.api.tasks.TaskResource) Resource(org.apache.sling.installer.api.info.Resource) RegisteredResource(org.apache.sling.installer.api.tasks.RegisteredResource) InstallableResource(org.apache.sling.installer.api.InstallableResource) IOException(java.io.IOException) InstallationState(org.apache.sling.installer.api.info.InstallationState) Version(org.osgi.framework.Version) CheckForNull(javax.annotation.CheckForNull) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) ResourceState(org.apache.sling.installer.api.tasks.ResourceState) TaskResourceGroup(org.apache.sling.installer.api.tasks.TaskResourceGroup) ResourceGroup(org.apache.sling.installer.api.info.ResourceGroup)

Example 10 with TaskResource

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

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