Search in sources :

Example 6 with PersistenceException

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

the class CustomConfigurationPersistenceStrategy method getOrCreateResource.

private Resource getOrCreateResource(ResourceResolver resourceResolver, String path, Map<String, Object> properties) {
    try {
        Resource resource = ResourceUtil.getOrCreateResource(resourceResolver, path, DEFAULT_RESOURCE_TYPE, DEFAULT_RESOURCE_TYPE, false);
        replaceProperties(resource, properties);
        return resource;
    } catch (PersistenceException ex) {
        throw new ConfigurationPersistenceException("Unable to persist configuration to " + path, ex);
    }
}
Also used : Resource(org.apache.sling.api.resource.Resource) ConfigurationPersistenceException(org.apache.sling.caconfig.spi.ConfigurationPersistenceException) PersistenceException(org.apache.sling.api.resource.PersistenceException) ConfigurationPersistenceException(org.apache.sling.caconfig.spi.ConfigurationPersistenceException)

Example 7 with PersistenceException

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

the class CustomConfigurationPersistenceStrategy2 method deleteConfiguration.

@Override
public boolean deleteConfiguration(ResourceResolver resourceResolver, String configResourcePath) {
    Resource resource = resourceResolver.getResource(configResourcePath);
    if (resource != null) {
        try {
            resourceResolver.delete(resource);
        } catch (PersistenceException ex) {
            throw new ConfigurationPersistenceException("Unable to delete configuration at " + configResourcePath, ex);
        }
    }
    commit(resourceResolver);
    return true;
}
Also used : Resource(org.apache.sling.api.resource.Resource) ConfigurationPersistenceException(org.apache.sling.caconfig.spi.ConfigurationPersistenceException) PersistenceException(org.apache.sling.api.resource.PersistenceException) ConfigurationPersistenceException(org.apache.sling.caconfig.spi.ConfigurationPersistenceException)

Example 8 with PersistenceException

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

the class AnnouncementRegistryImpl method listAnnouncementsInSameCluster.

@Override
public synchronized Collection<Announcement> listAnnouncementsInSameCluster(final ClusterView localClusterView) {
    logger.debug("listAnnouncementsInSameCluster: start. localClusterView: {}", localClusterView);
    if (localClusterView == null) {
        throw new IllegalArgumentException("clusterView must not be null");
    }
    ResourceResolver resourceResolver = null;
    final Collection<Announcement> incomingAnnouncements = new LinkedList<Announcement>();
    final InstanceDescription localInstance = getLocalInstanceDescription(localClusterView);
    try {
        resourceResolver = resourceResolverFactory.getServiceResourceResolver(null);
        Resource clusterInstancesResource = ResourceHelper.getOrCreateResource(resourceResolver, config.getClusterInstancesPath());
        Iterator<Resource> it0 = clusterInstancesResource.getChildren().iterator();
        while (it0.hasNext()) {
            Resource aClusterInstanceResource = it0.next();
            final String instanceId = aClusterInstanceResource.getName();
            logger.debug("listAnnouncementsInSameCluster: handling clusterInstance: {}", instanceId);
            if (localInstance != null && localInstance.getSlingId().equals(instanceId)) {
                // this is the local instance then - which we serve from the cache only
                logger.debug("listAnnouncementsInSameCluster: matched localInstance, filling with cache: {}", instanceId);
                fillWithCachedAnnouncements(incomingAnnouncements);
                continue;
            }
            //TODO: add ClusterView.contains(instanceSlingId) for convenience to next api change
            if (!contains(localClusterView, instanceId)) {
                logger.debug("listAnnouncementsInSameCluster: instance is not in my view, ignoring: {}", instanceId);
                // (corresponds to earlier expiry-handling)
                continue;
            }
            final Resource announcementsResource = aClusterInstanceResource.getChild("announcements");
            if (announcementsResource == null) {
                logger.debug("listAnnouncementsInSameCluster: instance has no announcements: {}", instanceId);
                continue;
            }
            logger.debug("listAnnouncementsInSameCluster: instance has announcements: {}", instanceId);
            Iterator<Resource> it = announcementsResource.getChildren().iterator();
            Announcement topologyAnnouncement;
            while (it.hasNext()) {
                Resource anAnnouncement = it.next();
                topologyAnnouncement = Announcement.fromJSON(anAnnouncement.adaptTo(ValueMap.class).get("topologyAnnouncement", String.class));
                logger.debug("listAnnouncementsInSameCluster: found announcement: {}", topologyAnnouncement);
                incomingAnnouncements.add(topologyAnnouncement);
            // SLING-3389: no longer check for expired announcements -
            // instead make use of the fact that this instance
            // has a clusterView and that every live instance
            // is responsible of cleaning up expired announcements
            // with the repository
            }
        }
    // since SLING-3389 this method does only read operations, hence
    // no commit necessary anymore - close happens in below finally block
    } catch (LoginException e) {
        logger.error("listAnnouncementsInSameCluster: could not log in administratively: " + e, e);
        throw new RuntimeException("Could not log in to repository (" + e + ")", e);
    } catch (PersistenceException e) {
        logger.error("listAnnouncementsInSameCluster: got a PersistenceException: " + e, e);
        throw new RuntimeException("Exception while talking to repository (" + e + ")", e);
    } catch (JsonException e) {
        logger.error("listAnnouncementsInSameCluster: got a JSONException: " + e, e);
        throw new RuntimeException("Exception while converting json (" + e + ")", e);
    } finally {
        if (resourceResolver != null) {
            resourceResolver.close();
        }
    }
    if (logger.isDebugEnabled()) {
        logger.debug("listAnnouncementsInSameCluster: result: " + incomingAnnouncements.size());
    }
    return incomingAnnouncements;
}
Also used : JsonException(javax.json.JsonException) ValueMap(org.apache.sling.api.resource.ValueMap) Resource(org.apache.sling.api.resource.Resource) LinkedList(java.util.LinkedList) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) PersistenceException(org.apache.sling.api.resource.PersistenceException) LoginException(org.apache.sling.api.resource.LoginException) InstanceDescription(org.apache.sling.discovery.InstanceDescription)

Example 9 with PersistenceException

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

the class AnnouncementRegistryImpl method unregisterAnnouncement.

@Override
public synchronized void unregisterAnnouncement(final String ownerId) {
    if (ownerId == null || ownerId.length() == 0) {
        throw new IllegalArgumentException("ownerId must not be null or empty");
    }
    // remove from the cache - even if there's an error afterwards
    ownAnnouncementsCache.remove(ownerId);
    if (resourceResolverFactory == null) {
        logger.error("unregisterAnnouncement: resourceResolverFactory is null");
        return;
    }
    ResourceResolver resourceResolver = null;
    try {
        resourceResolver = resourceResolverFactory.getServiceResourceResolver(null);
        final String path = config.getClusterInstancesPath() + "/" + slingId + "/announcements/" + ownerId;
        final Resource announcementsResource = resourceResolver.getResource(path);
        if (announcementsResource != null) {
            resourceResolver.delete(announcementsResource);
            resourceResolver.commit();
        }
    } catch (LoginException e) {
        logger.error("unregisterAnnouncement: could not log in administratively: " + e, e);
        throw new RuntimeException("Could not log in to repository (" + e + ")", e);
    } catch (PersistenceException e) {
        logger.error("unregisterAnnouncement: got a PersistenceException: " + e, e);
        throw new RuntimeException("Exception while talking to repository (" + e + ")", e);
    } finally {
        if (resourceResolver != null) {
            resourceResolver.close();
        }
    }
}
Also used : ResourceResolver(org.apache.sling.api.resource.ResourceResolver) Resource(org.apache.sling.api.resource.Resource) PersistenceException(org.apache.sling.api.resource.PersistenceException) LoginException(org.apache.sling.api.resource.LoginException)

Example 10 with PersistenceException

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

the class DistributedEventReceiver method cleanUpObsoleteInstances.

private void cleanUpObsoleteInstances() {
    final Set<String> slingIds = this.instances;
    if (slingIds != null) {
        this.instances = null;
        this.logger.debug("Checking for old instance trees for distributed events.");
        ResourceResolver resolver = null;
        try {
            resolver = this.resourceResolverFactory.getServiceResourceResolver(null);
            final Resource baseResource = resolver.getResource(this.rootPath);
            // sanity check - should never be null
            if (baseResource != null) {
                final ResourceHelper.BatchResourceRemover brr = ResourceHelper.getBatchResourceRemover(50);
                final Iterator<Resource> iter = baseResource.listChildren();
                while (iter.hasNext()) {
                    final Resource rootResource = iter.next();
                    if (!slingIds.contains(rootResource.getName())) {
                        brr.delete(rootResource);
                    }
                }
                // final commit for outstanding deletes
                resolver.commit();
            }
        } catch (final PersistenceException pe) {
            // in the case of an error, we just log this as a warning
            this.logger.warn("Exception during job resource tree cleanup.", pe);
        } catch (final LoginException ignore) {
            this.ignoreException(ignore);
        } finally {
            if (resolver != null) {
                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) LoginException(org.apache.sling.api.resource.LoginException)

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