com.merkle.oss.magnolia.powernode.RepositoryExceptionDelegator Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of magnolia-powernode Show documentation
Show all versions of magnolia-powernode Show documentation
compiles and bundles generated and base classes
package com.merkle.oss.magnolia.powernode;
import info.magnolia.jcr.RuntimeRepositoryException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.jcr.ItemNotFoundException;
import javax.jcr.PathNotFoundException;
import javax.jcr.RepositoryException;
import java.lang.invoke.MethodHandles;
import java.util.Optional;
public class RepositoryExceptionDelegator {
private static final Logger LOG = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
public void run(final NodeService.RepositoryRunnable runnable) {
getOrThrow(() -> {
runnable.run();
return null;
});
}
public T getOrThrow(final NodeService.RepositoryProvider provider) {
try {
return provider.get();
} catch (RepositoryException e) {
throw new RuntimeRepositoryException(e.getMessage(), e);
}
}
public Optional get(final NodeService.RepositoryProvider provider) {
try {
return Optional.ofNullable(provider.get());
} catch (PathNotFoundException | ItemNotFoundException e) {
return Optional.empty();
} catch (RuntimeRepositoryException e) {
return get(() -> {throw (RepositoryException)e.getCause();});
} catch (RepositoryException e) {
LOG.error("Failed to apply node function!", e);
return Optional.empty();
}
}
public interface RepositoryProvider {
T get() throws RepositoryException;
}
public interface RepositoryRunnable {
void run() throws RepositoryException;
}
}