Search in sources :

Example 31 with PersistenceException

use of org.apache.sling.api.resource.PersistenceException in project sling by apache.

the class FindUnfinishedJobsTask method initTopic.

/**
     * Initialize a topic and update all jobs from that topic.
     * Reset started time and increase retry count of unfinished jobs
     * @param topicResource The topic resource
     */
private void initTopic(final Resource topicResource) {
    logger.debug("Initializing topic {}...", topicResource.getName());
    JobTopicTraverser.traverse(logger, topicResource, new JobTopicTraverser.JobCallback() {

        @Override
        public boolean handle(final JobImpl job) {
            if (job.getProcessingStarted() != null) {
                logger.debug("Found unfinished job {}", job.getId());
                job.retry();
                try {
                    final Resource jobResource = topicResource.getResourceResolver().getResource(job.getResourcePath());
                    // sanity check
                    if (jobResource != null) {
                        final ModifiableValueMap mvm = jobResource.adaptTo(ModifiableValueMap.class);
                        mvm.remove(Job.PROPERTY_JOB_STARTED_TIME);
                        mvm.put(Job.PROPERTY_JOB_RETRY_COUNT, job.getRetryCount());
                        if (job.getProperty(JobImpl.PROPERTY_JOB_QUEUED, Calendar.class) == null) {
                            mvm.put(JobImpl.PROPERTY_JOB_QUEUED, Calendar.getInstance());
                        }
                        jobResource.getResourceResolver().commit();
                    }
                } catch (final PersistenceException ignore) {
                    logger.error("Unable to update unfinished job " + job, ignore);
                }
            } else if (job.getProperty(JobImpl.PROPERTY_JOB_QUEUED, Calendar.class) == null) {
                logger.debug("Found job without queued date {}", job.getId());
                try {
                    final Resource jobResource = topicResource.getResourceResolver().getResource(job.getResourcePath());
                    // sanity check
                    if (jobResource != null) {
                        final ModifiableValueMap mvm = jobResource.adaptTo(ModifiableValueMap.class);
                        mvm.put(JobImpl.PROPERTY_JOB_QUEUED, Calendar.getInstance());
                        jobResource.getResourceResolver().commit();
                    }
                } catch (final PersistenceException ignore) {
                    logger.error("Unable to update queued date for job " + job.getId(), ignore);
                }
            }
            return true;
        }
    });
    logger.debug("Topic {} initialized", topicResource.getName());
}
Also used : JobImpl(org.apache.sling.event.impl.jobs.JobImpl) Calendar(java.util.Calendar) Resource(org.apache.sling.api.resource.Resource) PersistenceException(org.apache.sling.api.resource.PersistenceException) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap) JobTopicTraverser(org.apache.sling.event.impl.jobs.JobTopicTraverser)

Example 32 with PersistenceException

use of org.apache.sling.api.resource.PersistenceException in project sling by apache.

the class ResourceBuilderImpl method resource.

@Override
public ResourceBuilder resource(String path, Map<String, Object> properties) {
    Resource r = null;
    final String parentPath;
    final String fullPath;
    boolean absolutePath = isAbsolutePath(path);
    if (absolutePath) {
        parentPath = ResourceUtil.getParent(path);
        fullPath = path;
    } else {
        checkRelativePath(path);
        parentPath = parentPath(path);
        fullPath = currentParent.getPath() + "/" + path;
    }
    final Resource myParent = ensureResourceExists(parentPath);
    try {
        r = currentParent.getResourceResolver().getResource(fullPath);
        if (r == null) {
            r = currentParent.getResourceResolver().create(myParent, ResourceUtil.getName(fullPath), properties);
        } else {
            // Resource exists, set our properties
            final ModifiableValueMap mvm = r.adaptTo(ModifiableValueMap.class);
            if (mvm == null) {
                throw new IllegalStateException("Cannot modify properties of " + r.getPath());
            }
            for (Map.Entry<String, Object> e : properties.entrySet()) {
                mvm.put(e.getKey(), e.getValue());
            }
        }
    } catch (PersistenceException pex) {
        throw new RuntimeException("PersistenceException while creating Resource " + fullPath, pex);
    }
    if (r == null) {
        throw new RuntimeException("Failed to get or create resource " + fullPath);
    } else if (hierarchyMode || absolutePath) {
        return cloneResourceBuilder(r, this.intermediatePrimaryType, true);
    }
    return this;
}
Also used : Resource(org.apache.sling.api.resource.Resource) PersistenceException(org.apache.sling.api.resource.PersistenceException) ValueMap(org.apache.sling.api.resource.ValueMap) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap) Map(java.util.Map) HashMap(java.util.HashMap) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap)

Example 33 with PersistenceException

use of org.apache.sling.api.resource.PersistenceException in project sling by apache.

the class ResourceBuilderImpl method ensureResourceExists.

/** Create a Resource at the specified path if none exists yet,
     *  using the current intermediate primary type. "Stolen" from
     *  the sling-mock module's ContentBuilder class.
     *  @param path Resource path
     *  @return Resource at path (existing or newly created)
     */
protected final Resource ensureResourceExists(String path) {
    if (path == null || path.length() == 0 || path.equals("/")) {
        return resourceResolver.getResource("/");
    }
    Resource resource = resourceResolver.getResource(path);
    if (resource != null) {
        return resource;
    }
    String parentPath = ResourceUtil.getParent(path);
    String name = ResourceUtil.getName(path);
    Resource parentResource = ensureResourceExists(parentPath);
    try {
        resource = resourceResolver.create(parentResource, name, MapArgsConverter.toMap(JCR_PRIMARYTYPE, intermediatePrimaryType));
        return resource;
    } catch (PersistenceException ex) {
        throw new RuntimeException("Unable to create intermediate resource at " + path, ex);
    }
}
Also used : Resource(org.apache.sling.api.resource.Resource) PersistenceException(org.apache.sling.api.resource.PersistenceException)

Example 34 with PersistenceException

use of org.apache.sling.api.resource.PersistenceException in project sling by apache.

the class JcrResourceProvider method delete.

@Override
public void delete(@Nonnull final ResolveContext<JcrProviderState> ctx, @Nonnull final Resource resource) throws PersistenceException {
    // try to adapt to Item
    Item item = resource.adaptTo(Item.class);
    try {
        if (item == null) {
            final String jcrPath = resource.getPath();
            if (jcrPath == null) {
                logger.debug("delete: {} maps to an empty JCR path", resource.getPath());
                throw new PersistenceException("Unable to delete resource", null, resource.getPath(), null);
            }
            item = ctx.getProviderState().getSession().getItem(jcrPath);
        }
        item.remove();
    } catch (final RepositoryException e) {
        throw new PersistenceException("Unable to delete resource", e, resource.getPath(), null);
    }
}
Also used : Item(javax.jcr.Item) PersistenceException(org.apache.sling.api.resource.PersistenceException) RepositoryException(javax.jcr.RepositoryException)

Example 35 with PersistenceException

use of org.apache.sling.api.resource.PersistenceException in project sling by apache.

the class JcrResourceProvider method move.

@Override
public boolean move(@Nonnull final ResolveContext<JcrProviderState> ctx, final String srcAbsPath, final String destAbsPath) throws PersistenceException {
    final String srcNodePath = srcAbsPath;
    final String dstNodePath = destAbsPath + '/' + ResourceUtil.getName(srcAbsPath);
    try {
        ctx.getProviderState().getSession().move(srcNodePath, dstNodePath);
        return true;
    } catch (final RepositoryException e) {
        throw new PersistenceException("Unable to move resource to " + destAbsPath, e, srcAbsPath, null);
    }
}
Also used : PersistenceException(org.apache.sling.api.resource.PersistenceException) RepositoryException(javax.jcr.RepositoryException)

Aggregations

PersistenceException (org.apache.sling.api.resource.PersistenceException)146 Resource (org.apache.sling.api.resource.Resource)102 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)63 ModifiableValueMap (org.apache.sling.api.resource.ModifiableValueMap)37 HashMap (java.util.HashMap)35 LoginException (org.apache.sling.api.resource.LoginException)32 RepositoryException (javax.jcr.RepositoryException)27 ValueMap (org.apache.sling.api.resource.ValueMap)23 Node (javax.jcr.Node)18 Map (java.util.Map)15 Calendar (java.util.Calendar)14 IOException (java.io.IOException)13 ArrayList (java.util.ArrayList)7 ByteArrayInputStream (java.io.ByteArrayInputStream)6 InputStream (java.io.InputStream)6 ConfigurationPersistenceException (org.apache.sling.caconfig.spi.ConfigurationPersistenceException)6 InstanceDescription (org.apache.sling.discovery.InstanceDescription)6 QueueInfo (org.apache.sling.event.impl.jobs.config.QueueConfigurationManager.QueueInfo)6 Test (org.junit.Test)5 Session (javax.jcr.Session)4