Search in sources :

Example 6 with UpdateResult

use of org.apache.sling.installer.api.UpdateResult in project sling by apache.

the class JcrInstaller method handleRemoval.

/**
     * @see org.apache.sling.installer.api.UpdateHandler#handleRemoval(java.lang.String, java.lang.String, java.lang.String)
     */
public UpdateResult handleRemoval(final String resourceType, final String id, final String url) {
    // get configuration
    final InstallerConfig cfg = this.getConfiguration();
    if (cfg == null || !cfg.isWriteBack()) {
        return null;
    }
    final int pos = url.indexOf(':');
    final String path = url.substring(pos + 1);
    // 0. Check protocol
    if (!url.startsWith(URL_SCHEME)) {
        logger.debug("Not removing unmanaged artifact from repository: {}", url);
        return null;
    }
    // 1. Is this a system configuration then don't delete
    final String[] rootPaths = cfg.getFolderNameFilter().getRootPaths();
    final String systemConfigRootPath = rootPaths[rootPaths.length - 1];
    if (path.startsWith(systemConfigRootPath)) {
        logger.debug("Not removing system artifact from repository at {}", path);
        return null;
    }
    // 2. Is this configuration provisioned by us
    boolean found = false;
    int lastSlash = path.lastIndexOf('/');
    while (!found && lastSlash > 1) {
        final String prefix = path.substring(0, lastSlash);
        if (cfg.getFolderNameFilter().getPriority(prefix) != -1) {
            found = true;
        } else {
            lastSlash = prefix.lastIndexOf('/');
        }
    }
    if (found) {
        // remove
        logger.debug("Removing artifact at {}", path);
        Session session = null;
        try {
            session = repository.loginService(/* subservice name */
            null, repository.getDefaultWorkspace());
            if (session.itemExists(path)) {
                session.getItem(path).remove();
                session.save();
            }
        } catch (final RepositoryException re) {
            logger.error("Unable to remove resource from " + path, re);
            return null;
        } finally {
            if (session != null) {
                session.logout();
            }
        }
        return new UpdateResult(url);
    }
    // not provisioned by us
    logger.debug("Not removing unmanaged artifact from repository at {}", path);
    return null;
}
Also used : RepositoryException(javax.jcr.RepositoryException) UpdateResult(org.apache.sling.installer.api.UpdateResult) Session(javax.jcr.Session)

Aggregations

UpdateResult (org.apache.sling.installer.api.UpdateResult)6 IOException (java.io.IOException)3 File (java.io.File)2 RepositoryException (javax.jcr.RepositoryException)2 Session (javax.jcr.Session)2 UpdateHandler (org.apache.sling.installer.api.UpdateHandler)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 FileOutputStream (java.io.FileOutputStream)1 InputStream (java.io.InputStream)1 Hashtable (java.util.Hashtable)1 CyclicBarrier (java.util.concurrent.CyclicBarrier)1 Node (javax.jcr.Node)1 InstallableResource (org.apache.sling.installer.api.InstallableResource)1 RegisteredResource (org.apache.sling.installer.api.tasks.RegisteredResource)1 TaskResource (org.apache.sling.installer.api.tasks.TaskResource)1 TransformationResult (org.apache.sling.installer.api.tasks.TransformationResult)1 Test (org.junit.Test)1