Search in sources :

Example 21 with PersistenceException

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

the class JobHandler method reschedule.

/**
     * Reschedule the job
     * Update the retry count and remove the started time.
     * @return <code>true</code> if rescheduling was successful, <code>false</code> otherwise.
     */
public boolean reschedule() {
    final ResourceResolver resolver = this.configuration.createResourceResolver();
    try {
        final Resource jobResource = resolver.getResource(job.getResourcePath());
        if (jobResource != null) {
            final ModifiableValueMap mvm = jobResource.adaptTo(ModifiableValueMap.class);
            mvm.put(Job.PROPERTY_JOB_RETRY_COUNT, job.getProperty(Job.PROPERTY_JOB_RETRY_COUNT, Integer.class));
            if (job.getProperty(Job.PROPERTY_RESULT_MESSAGE) != null) {
                mvm.put(Job.PROPERTY_RESULT_MESSAGE, job.getProperty(Job.PROPERTY_RESULT_MESSAGE));
            }
            mvm.remove(Job.PROPERTY_JOB_STARTED_TIME);
            mvm.put(JobImpl.PROPERTY_JOB_QUEUED, Calendar.getInstance());
            try {
                resolver.commit();
                return true;
            } catch (final PersistenceException pe) {
                this.configuration.getMainLogger().debug("Unable to update reschedule properties for job " + job.getId(), pe);
            }
        }
    } finally {
        resolver.close();
    }
    return false;
}
Also used : ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Resource(org.apache.sling.api.resource.Resource) PersistenceException(org.apache.sling.api.resource.PersistenceException) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap)

Example 22 with PersistenceException

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

the class JobHandler method persistJobProperties.

/**
     * Update the property of a job in the resource tree
     * @param propNames the property names to update
     * @return {@code true} if the update was successful.
     */
public boolean persistJobProperties(final String... propNames) {
    if (propNames != null) {
        final ResourceResolver resolver = this.configuration.createResourceResolver();
        try {
            final Resource jobResource = resolver.getResource(job.getResourcePath());
            if (jobResource != null) {
                final ModifiableValueMap mvm = jobResource.adaptTo(ModifiableValueMap.class);
                for (final String propName : propNames) {
                    final Object val = job.getProperty(propName);
                    if (val != null) {
                        if (val.getClass().isEnum()) {
                            mvm.put(propName, val.toString());
                        } else {
                            mvm.put(propName, val);
                        }
                    } else {
                        mvm.remove(propName);
                    }
                }
                resolver.commit();
                return true;
            } else {
                this.configuration.getMainLogger().debug("No job resource found at {}", job.getResourcePath());
            }
        } catch (final PersistenceException ignore) {
            this.configuration.getMainLogger().debug("Unable to persist properties", ignore);
        } finally {
            resolver.close();
        }
        return false;
    }
    return true;
}
Also used : ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Resource(org.apache.sling.api.resource.Resource) PersistenceException(org.apache.sling.api.resource.PersistenceException) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap)

Example 23 with PersistenceException

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

the class AbstractJobHandlingTest method cleanup.

public void cleanup() {
    // clean job area
    final ServiceReference<ResourceResolverFactory> ref = this.bc.getServiceReference(ResourceResolverFactory.class);
    final ResourceResolverFactory factory = this.bc.getService(ref);
    ResourceResolver resolver = null;
    try {
        resolver = factory.getAdministrativeResourceResolver(null);
        final Resource rsrc = resolver.getResource("/var/eventing");
        if (rsrc != null) {
            delete(rsrc);
            resolver.commit();
        }
    } catch (final LoginException le) {
    // ignore
    } catch (final PersistenceException e) {
    // ignore
    } catch (final Exception e) {
    // sometimes an NPE is thrown from the repository, as we
    // are in the cleanup, we can ignore this
    } finally {
        if (resolver != null) {
            resolver.close();
        }
    }
    // unregister all services
    for (final ServiceRegistration<?> reg : this.registrations) {
        reg.unregister();
    }
    this.registrations.clear();
    // remove all configurations
    try {
        final org.osgi.service.cm.Configuration[] cfgs = this.configAdmin.listConfigurations(null);
        if (cfgs != null) {
            for (final org.osgi.service.cm.Configuration c : cfgs) {
                try {
                    c.delete();
                } catch (final IOException io) {
                // ignore
                }
            }
        }
    } catch (final IOException io) {
    // ignore
    } catch (final InvalidSyntaxException e) {
    // ignore
    }
    this.sleep(1000);
}
Also used : Configuration(org.ops4j.pax.exam.Configuration) JobManagerConfiguration(org.apache.sling.event.impl.jobs.config.JobManagerConfiguration) Resource(org.apache.sling.api.resource.Resource) IOException(java.io.IOException) PersistenceException(org.apache.sling.api.resource.PersistenceException) BundleException(org.osgi.framework.BundleException) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException) IOException(java.io.IOException) LoginException(org.apache.sling.api.resource.LoginException) ResourceResolverFactory(org.apache.sling.api.resource.ResourceResolverFactory) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) PersistenceException(org.apache.sling.api.resource.PersistenceException) LoginException(org.apache.sling.api.resource.LoginException) InvalidSyntaxException(org.osgi.framework.InvalidSyntaxException)

Example 24 with PersistenceException

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

the class JobQueueImpl method removeAll.

/**
     * @see org.apache.sling.event.jobs.Queue#removeAll()
     */
@Override
public synchronized void removeAll() {
    final Set<String> topics = this.cache.getTopics();
    logger.debug("Removing all jobs for queue {} : {}", queueName, topics);
    if (!topics.isEmpty()) {
        final ResourceResolver resolver = this.services.configuration.createResourceResolver();
        try {
            final Resource baseResource = resolver.getResource(this.services.configuration.getLocalJobsPath());
            // sanity check - should never be null
            if (baseResource != null) {
                final BatchResourceRemover brr = new BatchResourceRemover();
                for (final String t : topics) {
                    final Resource topicResource = baseResource.getChild(t.replace('/', '.'));
                    if (topicResource != null) {
                        JobTopicTraverser.traverse(logger, topicResource, new JobTopicTraverser.JobCallback() {

                            @Override
                            public boolean handle(final JobImpl job) {
                                final Resource jobResource = topicResource.getResourceResolver().getResource(job.getResourcePath());
                                // sanity check
                                if (jobResource != null) {
                                    try {
                                        brr.delete(jobResource);
                                    } catch (final PersistenceException ignore) {
                                        logger.error("Unable to remove job " + job, ignore);
                                        topicResource.getResourceResolver().revert();
                                        topicResource.getResourceResolver().refresh();
                                    }
                                }
                                return true;
                            }
                        });
                    }
                }
                try {
                    resolver.commit();
                } catch (final PersistenceException ignore) {
                    logger.error("Unable to remove jobs", ignore);
                }
            }
        } finally {
            resolver.close();
        }
    }
}
Also used : BatchResourceRemover(org.apache.sling.event.impl.support.BatchResourceRemover) JobImpl(org.apache.sling.event.impl.jobs.JobImpl) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Resource(org.apache.sling.api.resource.Resource) PersistenceException(org.apache.sling.api.resource.PersistenceException) JobTopicTraverser(org.apache.sling.event.impl.jobs.JobTopicTraverser)

Example 25 with PersistenceException

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

the class JobSchedulerImpl method setSuspended.

/**
     * Change the suspended flag for a scheduled job
     * @param info The schedule info
     * @param flag The corresponding flag
     */
public void setSuspended(final ScheduledJobInfoImpl info, final boolean flag) {
    final ResourceResolver resolver = configuration.createResourceResolver();
    try {
        final StringBuilder sb = new StringBuilder(this.configuration.getScheduledJobsPath(true));
        sb.append(ResourceHelper.filterName(info.getName()));
        final String path = sb.toString();
        final Resource eventResource = resolver.getResource(path);
        if (eventResource != null) {
            final ModifiableValueMap mvm = eventResource.adaptTo(ModifiableValueMap.class);
            if (flag) {
                mvm.put(ResourceHelper.PROPERTY_SCHEDULE_SUSPENDED, Boolean.TRUE);
            } else {
                mvm.remove(ResourceHelper.PROPERTY_SCHEDULE_SUSPENDED);
            }
            resolver.commit();
        }
        if (flag) {
            this.stopScheduledJob(info);
        } else {
            this.startScheduledJob(info);
        }
    } catch (final PersistenceException pe) {
        // we ignore the exception if removing fails
        ignoreException(pe);
    } finally {
        resolver.close();
    }
}
Also used : ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Resource(org.apache.sling.api.resource.Resource) PersistenceException(org.apache.sling.api.resource.PersistenceException) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap)

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