Skip to content

Instantly share code, notes, and snippets.

View mpellegrini's full-sized avatar

Michael Pellegrini mpellegrini

View GitHub Profile

Keybase proof

I hereby claim:

  • I am mpellegrini on github.
  • I am mpellegrini (https://keybase.io/mpellegrini) on keybase.
  • I have a public key ASCJzHB0u-QGRLctjx9M5lAJx3bsi5zOGQMbNmH6GWJt-go

To claim this, I am signing this object:

@mpellegrini
mpellegrini / FunctionalVisitor.java
Created May 9, 2019 19:17
Example of Functional Visitor Pattern using Vavr
package com.michaelpellegrini.example.functional;
import io.vavr.Function1;
import io.vavr.collection.List;
import static io.vavr.API.$;
import static io.vavr.API.Case;
import static io.vavr.API.Match;
import static io.vavr.Predicates.instanceOf;
@mpellegrini
mpellegrini / RestEasyLogger.java
Last active May 19, 2020 07:55
JAX-RS Provider and RestEasy Interceptor to Log request / response messages
import org.jboss.resteasy.annotations.interception.ServerInterceptor;
import org.jboss.resteasy.core.ResourceMethod;
import org.jboss.resteasy.core.ServerResponse;
import org.jboss.resteasy.spi.Failure;
import org.jboss.resteasy.spi.HttpRequest;
import org.jboss.resteasy.spi.interception.MessageBodyWriterContext;
import org.jboss.resteasy.spi.interception.MessageBodyWriterInterceptor;
import org.jboss.resteasy.spi.interception.PreProcessInterceptor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@mpellegrini
mpellegrini / LocalDateBinder.java
Created May 27, 2017 09:33
JDBI v2 Bind Java LocalDate
public class LocalDateBinder implements Binder<Bind, LocalDate> {
@Override
public void bind(SQLStatement<?> stmt, Bind bind, LocalDate localDate) {
stmt.bind(bind.value(), localDate.format(DateTimeFormatter.ISO_LOCAL_DATE));
}
}
@mpellegrini
mpellegrini / DiscoverLdapServers.java
Created November 13, 2014 04:50
Get LDAP Servers Programmatically
import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.Attributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
import javax.naming.spi.NamingManager;
import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.Collection;
@mpellegrini
mpellegrini / programatic-ehcache.java
Created July 1, 2014 02:49
Creating Ehcache Programmatically
// Create a configuration
Configuration configuration = new Configuration();
//Create a CacheManager using defaults
CacheManager manager = CacheManager.create(configuration);
//Create a Cache specifying its configuration.
Cache testCache = new Cache(
new CacheConfiguration("test", maxElements)
.memoryStoreEvictionPolicy(MemoryStoreEvictionPolicy.LFU)
@mpellegrini
mpellegrini / web-fragment.xml
Last active August 29, 2015 13:57
Java EE Servlet 3.0 Web Fragments Descriptor
<?xml version="1.0" encoding="UTF-8"?>
<web-fragment version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-fragment_3_0.xsd">
<display-name>display-name</display-name>
<description>description</description>
</web-fragment>
// Better
public static boolean isEqual(byte[] a, byte[] b) {
if (a.length != b.length) {
return false;
}
int result = 0;
for (int i = 0; i < a.length; i++) {
result |= a[i] ^ b[i]
}
@mpellegrini
mpellegrini / beans.xml
Created March 25, 2014 12:27
Empty beans.xml marker resource for CDI support
<!--
For WAR files, use WEB-INF/beans.xml. In a Maven WAR module, this will be src/main/webapp/WEB-INF/beans.xml.
For EJB-JAR files or ordinary JAR files, use META-INF/beans.xml. In a Maven module, this will be src/main/resources/META-INF/beans.xml.
-->
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/beans_1_0.xsd">
@mpellegrini
mpellegrini / CDIEventExtension.java
Created March 25, 2014 12:22
An example CDI Extension that observes all events
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.enterprise.event.Observes;
import javax.enterprise.inject.spi.AfterBeanDiscovery;
import javax.enterprise.inject.spi.AfterDeploymentValidation;
import javax.enterprise.inject.spi.BeforeBeanDiscovery;
import javax.enterprise.inject.spi.BeforeShutdown;
import javax.enterprise.inject.spi.Extension;
import javax.enterprise.inject.spi.ProcessAnnotatedType;