@PreDestroy public void stop() { server.ifPresent(Throwing.consumer(JettyHttpServer::stop).sneakyThrow()); }
public static <T> ConsumerChainer<T> consumer( final ThrowingConsumer<T> consumer) { return new ConsumerChainer<>(consumer); }
@Override public Consumer<T> sneakyThrow() { return t -> { try { throwing.doAccept(t); } catch (Error | RuntimeException e) { throw e; } catch (Throwable throwable) { throw doSneakyThrow(throwable); } }; }
@Override public <E extends RuntimeException> ThrowingConsumer<T> orThrow( final Class<E> exclass) { return t -> { try { throwing.doAccept(t); } catch (Error | RuntimeException e) { throw e; } catch (Throwable throwable) { throw rethrow(exclass, throwable); } }; }
@Override public <E extends RuntimeException> ThrowingConsumer<T> orThrow( final Class<E> exclass) { return t -> { try { throwing.doAccept(t); } catch (Error | RuntimeException e) { throw e; } catch (Throwable throwable) { throw rethrow(exclass, throwable); } }; }
@Override public void configure(HierarchicalConfiguration config) throws ConfigurationException { server.ifPresent(Throwing.consumer(JettyHttpServer::start).sneakyThrow()); }
public static <T> ConsumerChainer<T> consumer( final ThrowingConsumer<T> consumer) { return new ConsumerChainer<>(consumer); }
@Override public Consumer<T> sneakyThrow() { return t -> { try { throwing.doAccept(t); } catch (Error | RuntimeException e) { throw e; } catch (Throwable throwable) { throw doSneakyThrow(throwable); } }; }
public void updateReferences(Map<String, String> headers, MailboxSession session) throws MailboxException { Optional<String> inReplyToId = Optional.ofNullable(headers.get(RFC2822Headers.IN_REPLY_TO)); Optional<String> forwardedId = Optional.ofNullable(headers.get(X_FORWARDED_ID_HEADER)); inReplyToId.ifPresent(Throwing.consumer((String id) -> updateAnswered(id, session)).sneakyThrow()); forwardedId.ifPresent(Throwing.consumer((String id) -> updateForwarded(id, session)).sneakyThrow()); }
@Override public ConsumerChainer<T> orTryWith(final ThrowingConsumer<T> other) { final ThrowingConsumer<T> consumer = t -> { try { throwing.doAccept(t); } catch (Error | RuntimeException e) { throw e; } catch (Throwable ignored) { other.doAccept(t); } }; return new ConsumerChainer<>(consumer); }
private void doStop() { try { MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer(); registeredKeys.forEach(Throwing.consumer(key -> mBeanServer.unregisterMBean(new ObjectName(key)))); registeredKeys.clear(); jmxConnectorServer.stop(); restrictingRMISocketFactory.getSockets() .forEach(Throwing.consumer(ServerSocket::close) .sneakyThrow()); } catch (Exception e) { throw new RuntimeException(e); } }
@Override public ConsumerChainer<T> orTryWith(final ThrowingConsumer<T> other) { final ThrowingConsumer<T> consumer = t -> { try { throwing.doAccept(t); } catch (Error | RuntimeException e) { throw e; } catch (Throwable ignored) { other.doAccept(t); } }; return new ConsumerChainer<>(consumer); }
protected void addConfiguredDomains(List<Domain> domains) { domains.stream() .filter(Throwing.predicate((Domain domain) -> !containsDomainInternal(domain)).sneakyThrow()) .forEach(Throwing.consumer(this::addDomain).sneakyThrow()); }
@Override public void removeAll() throws MessagingException { ImmutableList.copyOf(list()) .forEach(Throwing.<MailKey>consumer(this::remove).sneakyThrow()); } }
@Override public void setGlobalMaxMessageCount(SerializableQuotaValue<QuotaCount> maxGlobalMessageCount) throws MailboxException { maxGlobalMessageCount.toValue(QuotaCount::count, QuotaCount.unlimited()) .ifPresent(Throwing.consumer(maxQuotaManager::setGlobalMaxMessage).sneakyThrow()); }
@Override public void setGlobalMaxStorage(SerializableQuotaValue<QuotaSize> maxGlobalSize) throws MailboxException { maxGlobalSize.toValue(QuotaSize::size, QuotaSize.unlimited()) .ifPresent(Throwing.consumer(maxQuotaManager::setGlobalMaxStorage).sneakyThrow()); } }
public void deleteMail(MailRepositoryPath path, MailKey mailKey) throws MailRepositoryStore.MailRepositoryStoreException, MessagingException { getRepositories(path) .forEach(Throwing.consumer((MailRepository repository) -> repository.remove(mailKey)).sneakyThrow()); }
@Override public void service(Mail mail) throws MessagingException { if (mail.getAttribute(attribute) == null) { return; } try { getCalendarMap(mail) .values() .stream() .findAny() .ifPresent(Throwing.<Calendar>consumer(calendar -> writeToHeaders(calendar, mail)) .sneakyThrow()); } catch (ClassCastException e) { LOGGER.error("Received a mail with {} not being an ICAL object for mail {}", attribute, mail.getName(), e); } }
private void store(Mail mail, MailRepositoryUrl url) throws MessagingException { try { Optional<MailRepository> mailRepository = retrieveRepository(url); if (!mailRepository.isPresent()) { LOGGER.warn("'{}' mail repository does not exist and will not be created. Mail {} will not be stored in it.", url, mail.getName()); } ThrowingConsumer<MailRepository> storingConsumer = repository -> repository.store(mail); mailRepository.ifPresent(Throwing.consumer(storingConsumer).sneakyThrow()); } catch (MailRepositoryStore.MailRepositoryStoreException e) { throw new MessagingException("Error while selecting url " + url, e); } }
@Override public void service(Mail mail) throws MessagingException { MimeMessage message = mail.getMessage(); // Invoke SpamAssassin connection and scan the message SpamAssassinInvoker sa = new SpamAssassinInvoker(metricFactory, spamdHost, spamdPort); mail.getRecipients() .forEach( Throwing.consumer((MailAddress recipient) -> querySpamAssassin(mail, message, sa, recipient)) .sneakyThrow()); }