Skip to content

Instantly share code, notes, and snippets.

View garcia-jj's full-sized avatar

Otávio Garcia garcia-jj

  • Atlassian
View GitHub Profile
@garcia-jj
garcia-jj / GMailSender.java
Last active February 27, 2018 16:36
Sending email via GMail with Java SE environment
package br.com.otavio.mail.helper;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
@garcia-jj
garcia-jj / Reflecting.java
Last active August 29, 2015 14:05
Running Reflection vs Mirror vs Method Handler under a Intel x64 octa-core machine to get performance metrics
package ot;
import static java.lang.invoke.MethodHandles.publicLookup;
import static java.lang.invoke.MethodType.methodType;
import java.lang.invoke.MethodHandle;
import java.lang.reflect.Method;
import java.util.concurrent.TimeUnit;
import net.vidageek.mirror.dsl.Mirror;
@garcia-jj
garcia-jj / AbstractRepositoryTest.java
Last active August 29, 2015 14:04
Testes unitários de repositórios
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import org.junit.AfterClass;
import org.junit.BeforeClass;
public abstract class AbstractRepositoryTest {
protected static EntityManagerFactory emFactory;
@garcia-jj
garcia-jj / PersistentLocalDate.java
Last active August 29, 2015 14:00
JPA with java.time
/*
* Copyright 2014 Otávio S Garcia
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
@garcia-jj
garcia-jj / PasswordBasedEncryption.java
Last active August 29, 2015 13:58
Strong encryption with PBE
package foo;
import static java.lang.System.arraycopy;
import static java.util.Arrays.copyOf;
import static java.util.Arrays.copyOfRange;
import static java.util.Arrays.fill;
import java.security.GeneralSecurityException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
@garcia-jj
garcia-jj / bootprime.xhtml
Created March 15, 2014 03:28
Drawing bootstrap with primefaces
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:jsf="http://xmlns.jcp.org/jsf"
xmlns:f="http://xmlns.jcp.org/jsf/core"
xmlns:p="http://primefaces.org/ui">
<h:head>
<title>User Page</title>
<link href="//netdna.bootstrapcdn.com/bootstrap/3.1.0/css/bootstrap.min.css" rel="stylesheet"></link>
</h:head>
@garcia-jj
garcia-jj / Resources.java
Last active August 29, 2015 13:56
How to produce Java EE resources
import javax.annotation.Resource;
import javax.ejb.SessionContext;
import javax.enterprise.context.Dependent;
import javax.enterprise.inject.Produces;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
/**
* Responsible to produces some Java EE resources to make available into CDI beans.
*
@garcia-jj
garcia-jj / Page.java
Last active January 3, 2016 12:49
Pagination is really cool to avoid fuking the server performance :)
import static com.google.common.base.Objects.firstNonNull;
import java.io.Serializable;
/**
* Classe para prover paginação de resultados.
*
* @author Otávio Scherer Garcia
*/
public final class Page
@garcia-jj
garcia-jj / PrincipalHelper.java
Last active December 31, 2015 11:39
How to static ask for "who I am".
import javax.security.auth.Subject;
public class PrincipalHelper {
public static Principal main() {
Subject subject = (Subject) PolicyContext.getContext("javax.security.auth.Subject.container");
return (Principal) subject.getPublicCredentials().stream().findFirst().orElse(null);
}
}
@garcia-jj
garcia-jj / InvokerReflectionTest.java
Last active December 24, 2015 06:39
Testes de performance entre Reflection e Invoker do Java 7
package br.com.caelum.vraptor.benchmark;
import java.lang.invoke.MethodHandle;
import java.lang.reflect.Method;
import java.util.concurrent.TimeUnit;
import org.openjdk.jmh.annotations.BenchmarkMode;
import org.openjdk.jmh.annotations.GenerateMicroBenchmark;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Mode;