Search in sources :

Example 1 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 2 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)

Example 3 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 4 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 5 with TaskResource

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

the class DeployPckTask method execute.

@Override
public void execute(final InstallationContext ctx) {
    final TaskResource tr = this.getResource();
    // get and check symbolic name
    final String symbolicName = (String) tr.getAttribute(DeploymentPackageInstaller.DEPLOYMENTPACKAGE_SYMBOLICMAME);
    if (symbolicName == null) {
        logger.error("Resource {} has no symbolic name - ignoring.", tr);
        this.getResourceGroup().setFinishState(ResourceState.IGNORED);
        return;
    }
    // get package if available
    final DeploymentPackage dp = this.deploymentAdmin.getDeploymentPackage(symbolicName);
    if (tr.getState() == ResourceState.INSTALL) {
        InputStream is = null;
        try {
            is = tr.getInputStream();
            if (is == null) {
                // something went wrong
                logger.error("Resource {} does not provide an input stream!", tr);
                this.getResourceGroup().setFinishState(ResourceState.IGNORED);
            } else {
                final Version newVersion = new Version((String) tr.getAttribute(DeploymentPackageInstaller.DEPLOYMENTPACKAGE_VERSION));
                // check version
                if (dp != null) {
                    final int compare = dp.getVersion().compareTo(newVersion);
                    if (compare < 0) {
                        // installed version is lower -> update
                        this.deploymentAdmin.installDeploymentPackage(is);
                        ctx.log("Installed deployment package {} : {}", symbolicName, newVersion);
                        this.getResourceGroup().setFinishState(ResourceState.INSTALLED);
                    } else if (compare >= 0) {
                        logger.debug("Deployment package " + symbolicName + " " + newVersion + " is not installed, package with higher or same version is already installed.");
                    }
                } else {
                    this.deploymentAdmin.installDeploymentPackage(is);
                    ctx.log("Installed deployment package {} : {}", symbolicName, newVersion);
                    this.getResourceGroup().setFinishState(ResourceState.INSTALLED);
                }
            }
        } catch (final DeploymentException e) {
            logger.error("Unable to install deployment package {} from resource {}", symbolicName, tr);
            this.getResourceGroup().setFinishState(ResourceState.IGNORED);
        } catch (final IOException ioe) {
            logger.error("Unable to install deployment package {} from resource {}", symbolicName, tr);
            this.getResourceGroup().setFinishState(ResourceState.IGNORED);
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException ignore) {
                }
            }
        }
    } else {
        // uninstall
        if (dp != null) {
            try {
                dp.uninstall();
            } catch (final DeploymentException e) {
                logger.error("Unable to uninstall deployment package {} from resource {}", symbolicName, tr);
            }
        } else {
            logger.info("Unable to find deployment package with symbolic name {} for uninstalling.", symbolicName);
        }
        this.getResourceGroup().setFinishState(ResourceState.UNINSTALLED);
    }
}
Also used : TaskResource(org.apache.sling.installer.api.tasks.TaskResource) Version(org.osgi.framework.Version) InputStream(java.io.InputStream) DeploymentPackage(org.osgi.service.deploymentadmin.DeploymentPackage) DeploymentException(org.osgi.service.deploymentadmin.DeploymentException) IOException(java.io.IOException)

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