public void setFinishState()

in src/main/java/org/apache/sling/installer/core/impl/EntityResourceList.java [248:327]


    public void setFinishState(ResourceState state, String error) {
        final TaskResource toActivate = getActiveResource();
        if ( toActivate != null ) {
            synchronized ( lock ) {
                if ( toActivate.getState() == ResourceState.UNINSTALL
                     && this.resources.size() > 1 ) {

                    final TaskResource second = this.getNextActiveResource();
                    // check for template
                    if ( second.getDictionary() != null
                         && second.getDictionary().get(InstallableResource.RESOURCE_IS_TEMPLATE) != null ) {
                        // second resource is a template! Do not install
                        ((RegisteredResourceImpl)second).setState(ResourceState.IGNORED, null);
                    } else if ( state == ResourceState.UNINSTALLED ) {
                        // first resource got uninstalled, go back to second
                        if (second.getState() == ResourceState.IGNORED || second.getState() == ResourceState.INSTALLED) {
                            LOGGER.debug("Reactivating for next cycle: {}", second);
                            ((RegisteredResourceImpl)second).setState(ResourceState.INSTALL, null);
                        }
                    } else {
                        // don't install as the first did not get uninstalled
                        if ( second.getState() == ResourceState.INSTALL ) {
                            String message = MessageFormat.format(
                                    "The first resource '{0}' did not get uninstalled, therefore ignore this secondary resource in the uninstall group",
                                    toActivate.getEntityId());
                            LOGGER.debug(message);
                            ((RegisteredResourceImpl) second).setState(ResourceState.IGNORED, message);
                        }
                        // and now set resource to uninstalled
                        state = ResourceState.UNINSTALLED;
                    }
                } else if ( state == ResourceState.INSTALLED ) {
                    // make sure that no other resource has state INSTALLED
                    if ( this.resources.size() > 1 ) {
                        // to get the second item in the set we have to use an iterator!
                        final Iterator<RegisteredResourceImpl> i = this.resources.iterator();
                        i.next(); // skip first
                        while ( i.hasNext() ) {
                            final TaskResource rsrc = i.next();
                            if ( rsrc.getState() == ResourceState.INSTALLED ) {
                                ((RegisteredResourceImpl)rsrc).setState(ResourceState.INSTALL, "Another resource with the same entity id but a higher version or priority or digest found (in that order, the latter only in case the version is a SNAPSHOT)!");
                            }
                        }
                    }

                }
                ((RegisteredResourceImpl)toActivate).setState(state, error);

                if ( state != ResourceState.INSTALLED ) {
                    // make sure to remove all install info attributes if the resource is not
                    // installed anymore
                    toActivate.setAttribute(TaskResource.ATTR_INSTALL_EXCLUDED, null);
                    toActivate.setAttribute(TaskResource.ATTR_INSTALL_INFO, null);
                }
                // remove install info attributes on all other resources in the group
                final Iterator<RegisteredResourceImpl> tri = this.resources.iterator();
                tri.next(); // skip first
                while ( tri.hasNext() ) {
                    final TaskResource rsrc = tri.next();
                    rsrc.setAttribute(TaskResource.ATTR_INSTALL_EXCLUDED, null);
                    rsrc.setAttribute(TaskResource.ATTR_INSTALL_INFO, null);
                }
            }
            this.listener.onEvent(new InstallationEvent() {

                @Override
                public TYPE getType() {
                    return TYPE.PROCESSED;
                }

                @Override
                public Object getSource() {
                    return toActivate;
                }
            });
            if ( state == ResourceState.UNINSTALLED ) {
                this.cleanup(toActivate);
            }
        }
    }