Search in sources :

Example 41 with ResourceResolver

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

the class CheckTopologyTask method reassignJobsFromStoppedInstances.

/**
     * Reassign jobs from stopped instance.
     */
private void reassignJobsFromStoppedInstances() {
    if (caps.isLeader() && caps.isActive()) {
        this.logger.debug("Checking for stopped instances...");
        final ResourceResolver resolver = this.configuration.createResourceResolver();
        if (resolver != null) {
            try {
                final Resource jobsRoot = resolver.getResource(this.configuration.getAssginedJobsPath());
                this.logger.debug("Got jobs root {}", jobsRoot);
                // this resource should exist, but we check anyway
                if (jobsRoot != null) {
                    final Iterator<Resource> instanceIter = jobsRoot.listChildren();
                    while (caps.isActive() && instanceIter.hasNext()) {
                        final Resource instanceResource = instanceIter.next();
                        final String instanceId = instanceResource.getName();
                        if (!caps.isActive(instanceId)) {
                            logger.debug("Found stopped instance {}", instanceId);
                            assignJobs(instanceResource, true);
                        }
                    }
                }
            } finally {
                resolver.close();
            }
        }
    }
}
Also used : ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Resource(org.apache.sling.api.resource.Resource)

Example 42 with ResourceResolver

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

the class UpgradeTask method upgradeBridgedJobs.

/**
     * Upgrade bridged jobs.
     * In previous versions, bridged jobs were stored under a special topic.
     * This has changed, the jobs are now stored with their real topic.
     */
private void upgradeBridgedJobs() {
    final String path = configuration.getLocalJobsPath() + "/slingevent:eventadmin";
    final ResourceResolver resolver = configuration.createResourceResolver();
    if (resolver != null) {
        try {
            final Resource rootResource = resolver.getResource(path);
            if (rootResource != null) {
                upgradeBridgedJobs(rootResource);
            }
            if (caps.isLeader()) {
                final Resource unassignedRoot = resolver.getResource(configuration.getUnassignedJobsPath() + "/slingevent:eventadmin");
                if (unassignedRoot != null) {
                    upgradeBridgedJobs(unassignedRoot);
                }
            }
        } finally {
            resolver.close();
        }
    }
}
Also used : ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Resource(org.apache.sling.api.resource.Resource)

Example 43 with ResourceResolver

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

the class UpgradeTask method processJobsFromPreviousVersions.

/**
     * Handle jobs from previous versions (<= 3.1.4) by moving them to the unassigned area
     */
private void processJobsFromPreviousVersions() {
    final ResourceResolver resolver = configuration.createResourceResolver();
    if (resolver != null) {
        try {
            this.processJobsFromPreviousVersions(resolver.getResource(configuration.getPreviousVersionAnonPath()));
            this.processJobsFromPreviousVersions(resolver.getResource(configuration.getPreviousVersionIdentifiedPath()));
        } catch (final PersistenceException pe) {
            this.logger.warn("Problems moving jobs from previous version.", pe);
        } finally {
            resolver.close();
        }
    }
}
Also used : ResourceResolver(org.apache.sling.api.resource.ResourceResolver) PersistenceException(org.apache.sling.api.resource.PersistenceException)

Example 44 with ResourceResolver

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

the class CheckTopologyTask method assignJobs.

/**
     * Try to assign all jobs from the jobs root.
     * The jobs are stored by topic
     * @param jobsRoot The root of the jobs
     * @param unassign Whether to unassign the job if no instance is found.
     */
private void assignJobs(final Resource jobsRoot, final boolean unassign) {
    final ResourceResolver resolver = jobsRoot.getResourceResolver();
    final Iterator<Resource> topicIter = jobsRoot.listChildren();
    while (caps.isActive() && topicIter.hasNext()) {
        final Resource topicResource = topicIter.next();
        final String topicName = topicResource.getName().replace('.', '/');
        logger.debug("Found topic {}", topicName);
        // first check if there is an instance for these topics
        final List<InstanceDescription> potentialTargets = caps.getPotentialTargets(topicName);
        if (potentialTargets != null && potentialTargets.size() > 0) {
            final QueueConfigurationManager qcm = this.configuration.getQueueConfigurationManager();
            if (qcm == null) {
                break;
            }
            final QueueInfo info = qcm.getQueueInfo(topicName);
            logger.debug("Found queue {} for {}", info.queueConfiguration, topicName);
            JobTopicTraverser.traverse(this.logger, topicResource, new JobTopicTraverser.ResourceCallback() {

                @Override
                public boolean handle(final Resource rsrc) {
                    try {
                        final ValueMap vm = ResourceHelper.getValueMap(rsrc);
                        final String targetId = caps.detectTarget(topicName, vm, info);
                        if (targetId != null) {
                            final String newPath = configuration.getAssginedJobsPath() + '/' + targetId + '/' + topicResource.getName() + rsrc.getPath().substring(topicResource.getPath().length());
                            final Map<String, Object> props = new HashMap<>(vm);
                            props.put(Job.PROPERTY_JOB_QUEUE_NAME, info.queueName);
                            props.put(Job.PROPERTY_JOB_TARGET_INSTANCE, targetId);
                            props.remove(Job.PROPERTY_JOB_STARTED_TIME);
                            try {
                                ResourceHelper.getOrCreateResource(resolver, newPath, props);
                                resolver.delete(rsrc);
                                resolver.commit();
                                final String jobId = vm.get(ResourceHelper.PROPERTY_JOB_ID, String.class);
                                configuration.getAuditLogger().debug("REASSIGN OK {} : {}", targetId, jobId);
                            } catch (final PersistenceException pe) {
                                logger.warn("Unable to move unassigned job from " + rsrc.getPath() + " to " + newPath, pe);
                                resolver.refresh();
                                resolver.revert();
                            }
                        }
                    } catch (final InstantiationException ie) {
                        // something happened with the resource in the meantime
                        logger.warn("Unable to move unassigned job from " + rsrc.getPath(), ie);
                        resolver.refresh();
                        resolver.revert();
                    }
                    return caps.isActive();
                }
            });
        }
        // now unassign if there are still jobs
        if (caps.isActive() && unassign) {
            // we have to move everything to the unassigned area
            JobTopicTraverser.traverse(this.logger, topicResource, new JobTopicTraverser.ResourceCallback() {

                @Override
                public boolean handle(final Resource rsrc) {
                    try {
                        final ValueMap vm = ResourceHelper.getValueMap(rsrc);
                        final String newPath = configuration.getUnassignedJobsPath() + '/' + topicResource.getName() + rsrc.getPath().substring(topicResource.getPath().length());
                        final Map<String, Object> props = new HashMap<>(vm);
                        props.remove(Job.PROPERTY_JOB_QUEUE_NAME);
                        props.remove(Job.PROPERTY_JOB_TARGET_INSTANCE);
                        props.remove(Job.PROPERTY_JOB_STARTED_TIME);
                        try {
                            ResourceHelper.getOrCreateResource(resolver, newPath, props);
                            resolver.delete(rsrc);
                            resolver.commit();
                            final String jobId = vm.get(ResourceHelper.PROPERTY_JOB_ID, String.class);
                            configuration.getAuditLogger().debug("REUNASSIGN OK : {}", jobId);
                        } catch (final PersistenceException pe) {
                            logger.warn("Unable to unassigned job from " + rsrc.getPath() + " to " + newPath, pe);
                            resolver.refresh();
                            resolver.revert();
                        }
                    } catch (final InstantiationException ie) {
                        // something happened with the resource in the meantime
                        logger.warn("Unable to unassigned job from " + rsrc.getPath(), ie);
                        resolver.refresh();
                        resolver.revert();
                    }
                    return caps.isActive();
                }
            });
        }
    }
}
Also used : QueueInfo(org.apache.sling.event.impl.jobs.config.QueueConfigurationManager.QueueInfo) ValueMap(org.apache.sling.api.resource.ValueMap) Resource(org.apache.sling.api.resource.Resource) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) PersistenceException(org.apache.sling.api.resource.PersistenceException) QueueConfigurationManager(org.apache.sling.event.impl.jobs.config.QueueConfigurationManager) InstanceDescription(org.apache.sling.discovery.InstanceDescription) ValueMap(org.apache.sling.api.resource.ValueMap) HashMap(java.util.HashMap) Map(java.util.Map) JobTopicTraverser(org.apache.sling.event.impl.jobs.JobTopicTraverser)

Example 45 with ResourceResolver

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

the class DelegateRequestTest method setup.

@Before
public void setup() throws LoginException, PersistenceException {
    ResourceResolver adminResolver = null;
    try {
        adminResolver = rrFactory.getAdministrativeResourceResolver(null);
        Map<String, Object> properties = new HashMap<String, Object>();
        properties.put("text", "baseTESTValue");
        properties.put("other", "baseOther");
        properties.put(SlingConstants.NAMESPACE_PREFIX + ":" + SlingConstants.PROPERTY_RESOURCE_TYPE, "sling/delegate/base");
        ResourceUtil.getOrCreateResource(adminResolver, baseComponentPath, properties, null, false);
        properties.clear();
        properties.put("text", "extendedTESTValue");
        properties.put("other", "extendedOther");
        properties.put(SlingConstants.NAMESPACE_PREFIX + ":" + SlingConstants.PROPERTY_RESOURCE_TYPE, "sling/delegate/extended");
        ResourceUtil.getOrCreateResource(adminResolver, extendedComponentPath, properties, null, false);
        properties.clear();
        properties.put(SlingConstants.NAMESPACE_PREFIX + ":" + SlingConstants.PROPERTY_RESOURCE_SUPER_TYPE, "sling/delegate/base");
        ResourceUtil.getOrCreateResource(adminResolver, "/apps/sling/delegate/extended", properties, null, false);
        properties.clear();
        adminResolver.commit();
    } finally {
        if (adminResolver != null && adminResolver.isLive()) {
            adminResolver.close();
        }
    }
}
Also used : HashMap(java.util.HashMap) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Before(org.junit.Before)

Aggregations

ResourceResolver (org.apache.sling.api.resource.ResourceResolver)339 Resource (org.apache.sling.api.resource.Resource)168 Test (org.junit.Test)131 HashMap (java.util.HashMap)65 LoginException (org.apache.sling.api.resource.LoginException)53 PersistenceException (org.apache.sling.api.resource.PersistenceException)52 Session (javax.jcr.Session)31 ModifiableValueMap (org.apache.sling.api.resource.ModifiableValueMap)29 ValueMap (org.apache.sling.api.resource.ValueMap)27 SyntheticResource (org.apache.sling.api.resource.SyntheticResource)26 ArrayList (java.util.ArrayList)23 DistributionRequest (org.apache.sling.distribution.DistributionRequest)23 DistributionPackage (org.apache.sling.distribution.packaging.DistributionPackage)21 Map (java.util.Map)19 Before (org.junit.Before)19 IOException (java.io.IOException)17 NonExistingResource (org.apache.sling.api.resource.NonExistingResource)17 ChildResource (org.apache.sling.validation.model.ChildResource)17 HashSet (java.util.HashSet)16 ResourceResolverFactory (org.apache.sling.api.resource.ResourceResolverFactory)15