Search in sources :

Example 16 with TaskResource

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

the class EntityResourceList method removeInternal.

boolean removeInternal(final String url) {
    boolean removed = false;
    synchronized (lock) {
        Collections.sort(this.resources);
        final Iterator<RegisteredResourceImpl> i = resources.iterator();
        boolean first = true;
        while (i.hasNext()) {
            final TaskResource r = i.next();
            if (r.getURL().equals(url)) {
                removed = true;
                if (first && (r.getState() == ResourceState.INSTALLED || r.getState() == ResourceState.INSTALL)) {
                    LOGGER.debug("Marking for uninstalling: {}", r);
                    ((RegisteredResourceImpl) r).setState(ResourceState.UNINSTALL, null);
                } else {
                    LOGGER.debug("Removing unused: {}", r);
                    i.remove();
                    this.cleanup(r);
                }
            }
            first = false;
        }
    }
    return removed;
}
Also used : TaskResource(org.apache.sling.installer.api.tasks.TaskResource)

Example 17 with TaskResource

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

the class EntityResourceList method addOrUpdate.

public void addOrUpdate(final RegisteredResourceImpl r) {
    synchronized (lock) {
        LOGGER.debug("Adding new resource: {}", r);
        Collections.sort(this.resources);
        // If an object with same url is already present, replace with the
        // new one which might have different attributes
        boolean first = true;
        boolean add = true;
        final Iterator<RegisteredResourceImpl> taskIter = this.resources.iterator();
        while (taskIter.hasNext()) {
            final TaskResource rr = taskIter.next();
            if (rr.getURL().equals(r.getURL())) {
                if (RegisteredResourceImpl.isSameResource((RegisteredResourceImpl) rr, r)) {
                    if (!rr.getDigest().equals(r.getDigest())) {
                        // same resource but different digest, we need to remove the file
                        LOGGER.debug("Cleanup duplicate resource: {}", r);
                        this.cleanup(r);
                    }
                    // same resource, just ignore the new one
                    add = false;
                } else {
                    if (first && rr.getState() == ResourceState.INSTALLED) {
                        // it's not the same, but the first one is installed, so uninstall
                        String message = MessageFormat.format("The first resource '{0}' got installed, therefore uninstall this secondary resource in this group", rr.getEntityId());
                        LOGGER.debug(message);
                        ((RegisteredResourceImpl) rr).setState(ResourceState.UNINSTALL, message);
                    } else {
                        LOGGER.debug("Cleanup obsolete resource: {}", rr);
                        taskIter.remove();
                        this.cleanup(rr);
                    }
                }
                break;
            }
            first = false;
        }
        if (add) {
            resources.add(r);
            Collections.sort(this.resources);
        }
    }
}
Also used : TaskResource(org.apache.sling.installer.api.tasks.TaskResource)

Example 18 with TaskResource

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

the class OsgiInstallerImpl method internalResourceRemoved.

/**
     * Handle external removal a resource
     * @see org.apache.sling.installer.api.ResourceChangeListener#resourceRemoved(java.lang.String, java.lang.String)
     */
private void internalResourceRemoved(final String resourceType, final String entityId) {
    String key = resourceType + ':' + entityId;
    synchronized (this.resourcesLock) {
        final EntityResourceList erl = this.persistentList.getEntityResourceList(key);
        logger.debug("Removed {} : {}", key, erl);
        // if this is not registered at all, we can simply ignore this
        if (erl != null) {
            final String resourceId = erl.getResourceId();
            key = resourceType + ':' + resourceId;
            final TaskResource tr = erl.getFirstResource();
            if (tr != null) {
                if (tr.getState() == ResourceState.IGNORED) {
                    // but only if it is not a template
                    if (tr.getDictionary() == null || tr.getDictionary().get(InstallableResource.RESOURCE_IS_TEMPLATE) == null) {
                        ((RegisteredResourceImpl) tr).setState(ResourceState.INSTALL, null);
                        this.persistentList.save();
                    }
                } else if (tr.getState() == ResourceState.UNINSTALLED) {
                // it has already been removed - nothing do to
                } else {
                    final UpdateHandler handler = findHandler(tr.getScheme());
                    if (handler == null) {
                        // set to ignored
                        String message = MessageFormat.format("No handler found to handle resource with scheme {0}", tr.getScheme());
                        logger.debug(message);
                        ((RegisteredResourceImpl) tr).setState(ResourceState.IGNORED, message);
                    } else {
                        // we don't need to check the result, we just check if a result is returned
                        if (handler.handleRemoval(resourceType, resourceId, tr.getURL()) != null) {
                            erl.setForceFinishState(ResourceState.UNINSTALLED, null);
                            erl.compact();
                        } else {
                            // set to ignored
                            String message = MessageFormat.format("No handler found to handle removal of resource with scheme {0}", tr.getScheme());
                            logger.debug(message);
                            ((RegisteredResourceImpl) tr).setState(ResourceState.IGNORED, message);
                        }
                    }
                    this.persistentList.save();
                }
            }
        }
    }
}
Also used : UpdateHandler(org.apache.sling.installer.api.UpdateHandler) TaskResource(org.apache.sling.installer.api.tasks.TaskResource)

Example 19 with TaskResource

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

the class OsgiInstallerImpl method internalResourceAddedOrUpdated.

/**
     * Handle external addition or update of a resource
     * @see org.apache.sling.installer.api.ResourceChangeListener#resourceAddedOrUpdated(java.lang.String, java.lang.String, java.io.InputStream, java.util.Dictionary, Map)
     */
private void internalResourceAddedOrUpdated(final String resourceType, final String entityId, final ResourceData data, final Dictionary<String, Object> dict, final Map<String, Object> attributes) {
    final String key = resourceType + ':' + entityId;
    final boolean persistChange = (attributes != null ? PropertiesUtil.toBoolean(attributes.get(ResourceChangeListener.RESOURCE_PERSIST), true) : true);
    try {
        boolean compactAndSave = false;
        boolean done = false;
        synchronized (this.resourcesLock) {
            final EntityResourceList erl = this.persistentList.getEntityResourceList(key);
            logger.debug("Added or updated {} : {}", key, erl);
            // we first check for update
            if (erl != null && erl.getFirstResource() != null) {
                // check digest for dictionaries
                final TaskResource tr = erl.getFirstResource();
                if (dict != null) {
                    final String digest = FileDataStore.computeDigest(dict);
                    if (tr.getDigest().equals(digest)) {
                        if (tr.getState() == ResourceState.INSTALLED) {
                            logger.debug("Resource did not change {}", key);
                        } else if (tr.getState() == ResourceState.INSTALL || tr.getState() == ResourceState.IGNORED) {
                            erl.setForceFinishState(ResourceState.INSTALLED, null);
                            compactAndSave = true;
                        }
                        done = true;
                    }
                }
                final UpdateHandler handler;
                if (!done && persistChange) {
                    handler = this.findHandler(tr.getScheme());
                    if (handler == null) {
                        logger.debug("No handler found to handle update of resource with scheme {}", tr.getScheme());
                    }
                } else {
                    handler = null;
                }
                if (!done && handler == null) {
                    compactAndSave = this.handleExternalUpdateWithoutWriteBack(erl);
                    done = true;
                }
                if (!done) {
                    final InputStream localIS = data.getInputStream();
                    try {
                        final UpdateResult result = (localIS == null ? handler.handleUpdate(resourceType, entityId, tr.getURL(), data.getDictionary(), attributes) : handler.handleUpdate(resourceType, entityId, tr.getURL(), localIS, attributes));
                        if (result != null) {
                            if (!result.getURL().equals(tr.getURL()) && !result.getResourceIsMoved()) {
                                // resource has been added!
                                final InternalResource internalResource = new InternalResource(result.getScheme(), result.getResourceId(), null, data.getDictionary(), (data.getDictionary() != null ? InstallableResource.TYPE_PROPERTIES : InstallableResource.TYPE_FILE), data.getDigest(result.getURL(), result.getDigest()), result.getPriority(), data.getDataFile(), null);
                                final RegisteredResource rr = this.persistentList.addOrUpdate(internalResource);
                                final TransformationResult transRes = new TransformationResult();
                                // use the old entity id
                                final int pos = erl.getResourceId().indexOf(':');
                                transRes.setId(erl.getResourceId().substring(pos + 1));
                                transRes.setResourceType(resourceType);
                                if (attributes != null) {
                                    transRes.setAttributes(attributes);
                                }
                                this.persistentList.transform(rr, new TransformationResult[] { transRes });
                                final EntityResourceList newGroup = this.persistentList.getEntityResourceList(key);
                                newGroup.setFinishState(ResourceState.INSTALLED, null);
                                newGroup.compact();
                            } else {
                                // resource has been updated or moved
                                ((RegisteredResourceImpl) tr).update(data.getDataFile(), data.getDictionary(), data.getDigest(result.getURL(), result.getDigest()), result.getPriority(), result.getURL());
                                erl.setForceFinishState(ResourceState.INSTALLED, null);
                            }
                            compactAndSave = true;
                        } else {
                            // handler does not persist
                            compactAndSave = this.handleExternalUpdateWithoutWriteBack(erl);
                        }
                    } finally {
                        if (localIS != null) {
                            // always close the input stream!
                            try {
                                localIS.close();
                            } catch (final IOException ignore) {
                            // ignore
                            }
                        }
                    }
                    done = true;
                }
            }
            if (!done) {
                // create
                final List<UpdateHandler> handlerList = this.updateHandlerTracker.getSortedServices();
                for (final UpdateHandler handler : handlerList) {
                    final InputStream localIS = data.getInputStream();
                    try {
                        final UpdateResult result = (localIS == null ? handler.handleUpdate(resourceType, entityId, null, data.getDictionary(), attributes) : handler.handleUpdate(resourceType, entityId, null, localIS, attributes));
                        if (result != null) {
                            final InternalResource internalResource = new InternalResource(result.getScheme(), result.getResourceId(), null, data.getDictionary(), (data.getDictionary() != null ? InstallableResource.TYPE_PROPERTIES : InstallableResource.TYPE_FILE), data.getDigest(result.getURL(), result.getDigest()), result.getPriority(), data.getDataFile(), null);
                            final RegisteredResource rr = this.persistentList.addOrUpdate(internalResource);
                            final TransformationResult transRes = new TransformationResult();
                            transRes.setId(entityId);
                            transRes.setResourceType(resourceType);
                            if (attributes != null) {
                                transRes.setAttributes(attributes);
                            }
                            this.persistentList.transform(rr, new TransformationResult[] { transRes });
                            final EntityResourceList newGroup = this.persistentList.getEntityResourceList(key);
                            newGroup.setFinishState(ResourceState.INSTALLED);
                            newGroup.compact();
                            compactAndSave = true;
                            done = true;
                            break;
                        }
                    } finally {
                        if (localIS != null) {
                            // always close the input stream!
                            try {
                                localIS.close();
                            } catch (final IOException ignore) {
                            // ignore
                            }
                        }
                    }
                }
                if (!done) {
                    logger.debug("No handler found to handle creation of resource {}", key);
                }
            }
            if (compactAndSave) {
                if (erl != null) {
                    erl.compact();
                }
                this.persistentList.save();
            }
        }
    } catch (final IOException ioe) {
        logger.error("Unable to handle resource add or update of " + key, ioe);
    }
}
Also used : UpdateHandler(org.apache.sling.installer.api.UpdateHandler) TaskResource(org.apache.sling.installer.api.tasks.TaskResource) InputStream(java.io.InputStream) RegisteredResource(org.apache.sling.installer.api.tasks.RegisteredResource) IOException(java.io.IOException) TransformationResult(org.apache.sling.installer.api.tasks.TransformationResult) UpdateResult(org.apache.sling.installer.api.UpdateResult)

Example 20 with TaskResource

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

the class OsgiInstallerImpl method mergeNewlyRegisteredResources.

private void mergeNewlyRegisteredResources() {
    synchronized (this.resourcesLock) {
        for (final Map.Entry<String, List<InternalResource>> entry : this.newResourcesSchemes.entrySet()) {
            final String scheme = entry.getKey();
            final List<InternalResource> registeredResources = entry.getValue();
            logger.debug("Processing set of new resources with scheme {}", scheme);
            // if they have been installed - remove resources with a different state
            for (final String entityId : this.persistentList.getEntityIds()) {
                final EntityResourceList group = this.persistentList.getEntityResourceList(entityId);
                final List<TaskResource> toRemove = new ArrayList<>();
                boolean first = true;
                for (final TaskResource r : group.listResources()) {
                    if (r.getScheme().equals(scheme)) {
                        logger.debug("Checking {}", r);
                        // search if we have a new entry with the same url
                        boolean found = false;
                        if (registeredResources != null) {
                            final Iterator<InternalResource> m = registeredResources.iterator();
                            while (!found && m.hasNext()) {
                                final InternalResource testResource = m.next();
                                found = testResource.getURL().equals(r.getURL());
                            }
                        }
                        if (!found) {
                            logger.debug("Resource {} seems to be removed.", r);
                            if (first && (r.getState() == ResourceState.INSTALLED || r.getState() == ResourceState.INSTALL)) {
                                ((RegisteredResourceImpl) r).setState(ResourceState.UNINSTALL, null);
                            } else {
                                toRemove.add(r);
                            }
                        }
                    }
                    first = false;
                }
                for (final TaskResource rr : toRemove) {
                    this.persistentList.remove(rr.getURL());
                }
            }
            if (registeredResources != null) {
                this.newResources.addAll(registeredResources);
            }
        }
        this.newResourcesSchemes.clear();
        this.mergeNewResources();
        printResources("Merged");
        // persist list
        this.persistentList.save();
    }
}
Also used : TaskResource(org.apache.sling.installer.api.tasks.TaskResource) ArrayList(java.util.ArrayList) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) Map(java.util.Map) HashMap(java.util.HashMap)

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