All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.merkle.oss.magnolia.powernode.RepositoryExceptionDelegator Maven / Gradle / Ivy

There is a newer version: 2.1.1
Show newest version
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;
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy