Search in sources :

Example 11 with LoginException

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

the class HeartbeatHandler method doCheckView.

/** Check whether the established view matches the reality, ie matches the
     * heartbeats
     */
@Override
protected void doCheckView() {
    super.doCheckView();
    ResourceResolver resourceResolver = null;
    try {
        resourceResolver = getResourceResolver();
        doCheckViewWith(resourceResolver);
    } catch (LoginException e) {
        logger.error("checkView: could not log in administratively: " + e, e);
    } catch (PersistenceException e) {
        logger.error("checkView: encountered a persistence exception during view check: " + e, e);
    } catch (RuntimeException e) {
        logger.error("checkView: encountered a runtime exception during view check: " + e, e);
    } finally {
        if (resourceResolver != null) {
            resourceResolver.close();
        }
    }
}
Also used : ResourceResolver(org.apache.sling.api.resource.ResourceResolver) PersistenceException(org.apache.sling.api.resource.PersistenceException) LoginException(org.apache.sling.api.resource.LoginException)

Example 12 with LoginException

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

the class HeartbeatHandler method resetLeaderElectionId.

/**
     * Hook that will cause a reset of the leaderElectionId
     * on next invocation of issueClusterLocalHeartbeat.
     * @return true if the leaderElectionId was reset - false if that was not
     * necessary as that happened earlier already and it has not propagated
     * yet to the ./clusterInstances in the meantime
     */
public boolean resetLeaderElectionId() {
    if (resetLeaderElectionId) {
        // resetting twice doesn't work
        return false;
    }
    resetLeaderElectionId = true;
    ResourceResolver resourceResolver = null;
    try {
        resourceResolver = getResourceResolver();
        if (resourceResolver != null) {
            newLeaderElectionId = newLeaderElectionId(resourceResolver);
            if (votingHandler != null) {
                logger.info("resetLeaderElectionId: set new leaderElectionId with votingHandler to: " + newLeaderElectionId);
                votingHandler.setLeaderElectionId(newLeaderElectionId);
            } else {
                logger.info("resetLeaderElectionId: no votingHandler, new leaderElectionId would be: " + newLeaderElectionId);
            }
        } else {
            logger.warn("resetLeaderElectionId: could not login, new leaderElectionId will be calculated upon next heartbeat only!");
        }
    } catch (LoginException e) {
        logger.error("resetLeaderElectionid: could not login: " + e, e);
    } finally {
        if (resourceResolver != null) {
            resourceResolver.close();
        }
    }
    return true;
}
Also used : ResourceResolver(org.apache.sling.api.resource.ResourceResolver) LoginException(org.apache.sling.api.resource.LoginException)

Example 13 with LoginException

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

the class DiscoveryServiceImpl method doUpdateProperties.

/**
     * Update the properties by inquiring the PropertyProvider's current values.
     * <p>
     * This method is invoked regularly by the heartbeatHandler.
     * The properties are stored in the repository under Config.getClusterInstancesPath()
     * and announced in the topology.
     * <p>
     * @see Config#getClusterInstancesPath()
     */
private void doUpdateProperties() {
    if (resourceResolverFactory == null) {
        // cannot update the properties then..
        logger.debug("doUpdateProperties: too early to update the properties. resourceResolverFactory not yet set.");
        return;
    } else {
        logger.debug("doUpdateProperties: updating properties now..");
    }
    final Map<String, String> newProps = new HashMap<String, String>();
    for (final ProviderInfo info : this.providerInfos) {
        info.refreshProperties();
        newProps.putAll(info.properties);
    }
    ResourceResolver resourceResolver = null;
    try {
        resourceResolver = resourceResolverFactory.getServiceResourceResolver(null);
        Resource myInstance = ResourceHelper.getOrCreateResource(resourceResolver, config.getClusterInstancesPath() + "/" + slingId + "/properties");
        // SLING-2879 - revert/refresh resourceResolver here to work
        // around a potential issue with jackrabbit in a clustered environment
        resourceResolver.revert();
        resourceResolver.refresh();
        final ModifiableValueMap myInstanceMap = myInstance.adaptTo(ModifiableValueMap.class);
        final Set<String> keys = new HashSet<String>(myInstanceMap.keySet());
        for (final String key : keys) {
            if (newProps.containsKey(key)) {
                // perfect
                continue;
            } else if (key.indexOf(":") != -1) {
                // ignore
                continue;
            } else {
                // remove
                myInstanceMap.remove(key);
            }
        }
        boolean anyChanges = false;
        for (final Entry<String, String> entry : newProps.entrySet()) {
            Object existingValue = myInstanceMap.get(entry.getKey());
            if (entry.getValue().equals(existingValue)) {
                // SLING-3389: dont rewrite the properties if nothing changed!
                if (logger.isDebugEnabled()) {
                    logger.debug("doUpdateProperties: unchanged: {}={}", entry.getKey(), entry.getValue());
                }
                continue;
            }
            if (logger.isDebugEnabled()) {
                logger.debug("doUpdateProperties: changed: {}={}", entry.getKey(), entry.getValue());
            }
            anyChanges = true;
            myInstanceMap.put(entry.getKey(), entry.getValue());
        }
        if (anyChanges) {
            resourceResolver.commit();
        }
    } catch (LoginException e) {
        logger.error("handleEvent: could not log in administratively: " + e, e);
        throw new RuntimeException("Could not log in to repository (" + e + ")", e);
    } catch (PersistenceException e) {
        logger.error("handleEvent: got a PersistenceException: " + e, e);
        throw new RuntimeException("Exception while talking to repository (" + e + ")", e);
    } finally {
        if (resourceResolver != null) {
            resourceResolver.close();
        }
    }
    logger.debug("doUpdateProperties: updating properties done.");
}
Also used : HashMap(java.util.HashMap) Resource(org.apache.sling.api.resource.Resource) ModifiableValueMap(org.apache.sling.api.resource.ModifiableValueMap) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) PersistenceException(org.apache.sling.api.resource.PersistenceException) LoginException(org.apache.sling.api.resource.LoginException) HashSet(java.util.HashSet)

Example 14 with LoginException

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

the class JobManagerConfiguration method createResourceResolver.

/**
     * Create a new resource resolver for reading and writing the resource tree.
     * The resolver needs to be closed by the client.
     * @return A resource resolver or {@code null} if the component is already deactivated.
     * @throws RuntimeException if the resolver can't be created.
     */
public ResourceResolver createResourceResolver() {
    ResourceResolver resolver = null;
    final ResourceResolverFactory factory = this.resourceResolverFactory;
    if (factory != null) {
        try {
            resolver = this.resourceResolverFactory.getServiceResourceResolver(null);
        } catch (final LoginException le) {
            logger.error("Unable to create new resource resolver: " + le.getMessage(), le);
            throw new RuntimeException(le);
        }
    }
    return resolver;
}
Also used : ResourceResolverFactory(org.apache.sling.api.resource.ResourceResolverFactory) ResourceResolver(org.apache.sling.api.resource.ResourceResolver) LoginException(org.apache.sling.api.resource.LoginException)

Example 15 with LoginException

use of org.apache.sling.api.resource.LoginException 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)

Aggregations

LoginException (org.apache.sling.api.resource.LoginException)61 ResourceResolver (org.apache.sling.api.resource.ResourceResolver)49 Resource (org.apache.sling.api.resource.Resource)34 PersistenceException (org.apache.sling.api.resource.PersistenceException)23 HashMap (java.util.HashMap)11 ModifiableValueMap (org.apache.sling.api.resource.ModifiableValueMap)8 RepositoryException (javax.jcr.RepositoryException)7 ArrayList (java.util.ArrayList)5 HashSet (java.util.HashSet)5 JsonException (javax.json.JsonException)4 ResourceResolverFactory (org.apache.sling.api.resource.ResourceResolverFactory)4 ValueMap (org.apache.sling.api.resource.ValueMap)4 IOException (java.io.IOException)3 Calendar (java.util.Calendar)3 Map (java.util.Map)3 SimpleCredentials (javax.jcr.SimpleCredentials)3 ServletException (javax.servlet.ServletException)3 InstanceDescription (org.apache.sling.discovery.InstanceDescription)3 Bundle (org.osgi.framework.Bundle)3 LinkedList (java.util.LinkedList)2