domingo, abril 28, 2013

Git Bisect in Action

Today it is time for a screencast where I show an amazing git command, called git bisect. Git bisect command command uses git to find in which commit a bug was introduced. After that, I am sure you will wonder where it's been you whole career. Thanks Jason for showing me the way.

You can watch it in HD to see clearly each command.

We keep learning,

Siempre quise ir a L.A. Dejar un día esta ciudad. Cruzar el mar en tu compañía.  (Cadillac Solitario - Loquillo)

lunes, abril 22, 2013

NoSQLUnit 0.7.6 Released

NoSQLUnit is a JUnit extension to make writing unit and integration tests of systems that use NoSQL backend easier. Visit official page for more information.

In 0.7.6 release, next changes has been implemented:

We keep learning,

De dia vivire pensando en tu sonrisa, De noche las estrellas me acompañaran, Seras como un luz que alumbra en mi destino, Me voy pero te juro que mañana volvere (Un Beso Y Una Flor - Niño Bravo)


lunes, abril 15, 2013

Enjoy the magic of asciidoctor in java with asciidoctor-java-integration

The asciidoctor-java-integration is the official means of using Asciidoctor to render all your AsciiDoc documentation using Java instead of Ruby.


Since asciidoctor-java-integration is a standard jar file, the only thing you should do is add library into classpath.

Dependency declaration in Maven

    <version>${asciidoctor.version}</version>                   <1>
  1. As this library tracks the version of asciidoctor, you can use which every version of asciidoctor you prefer.


The core interface of asciidoctor-java-integration is Asciidoctor interface. It provides two methods for rendering asciidoc content, render and renderFile. Both of them returns a string with rendered content.

Also a factory method is provided to create an instance of Asciidoctor interface.

Creation of Asciidoctor interface

import static org.asciidoctor.Asciidoctor.Factory.create;
import org.asciidoctor.Asciidoctor;
Asciidoctor asciidoctor = create();

And then we can call render methods depending on our requirements.

Rendering a String

String rendered = asciidoctor.render("*This* is it.", Collections.EMPTY_MAP);

But also you can render the content of a file.

Rendering a File

String rendered = asciidoctor.renderFile("target/test-classes/rendersample.asciidoc", Collections.EMPTY_MAP);


Asciidoctor supports different kind of options, like in_place which renders the output inside a file, template_dir used to provide a directory of Tilt-compatible templates to be used instead of the default built-in templates, or for example attributes option where we can set key-value pairs of attributes that will be used within asciidoc document.

The second parameter of render methods are a java.util.Map where we can set all these options.

Example of using in_place Option and backend Attribute

Map<String, Object> attributes = new HashMap<String, Object>();
attributes.put("backend", "docbook");

Map<String, Object> options = new HashMap<String, Object>();
options.put("in_place", true);
options.put("attributes", attributes);

String render = asciidoctor.renderFile("target/test-classes/rendersample.asciidoc", options);

See that in previous example we have created a Map, where we have put the options and attributes (creating a Map too) required to render input as docbook and generate an output file.

But asciidoctor-java-integration also provides two builder classes to create these maps in a more readable form.

AttributesBuilder is provided for creating a map with required attributes set, and OptionsBuilder can be used for options. Previous example but using these classes looks like:

Example setting attributes and options

import static org.asciidoctor.AttributesBuilder.attributes;
import static org.asciidoctor.OptionsBuilder.options;


Map<String, Object> attributes = attributes().backend("docbook").asMap();
Map<String, Object> options = options().inPlace(true).attributes(attributes).asMap();

String render = asciidoctor.renderFile("target/test-classes/rendersample.asciidoc", options);



A utility class for searching all asciidoc files present in a root folder and all its subfolders is given. In fact it finds all files that end up with .asc, .asciidoc, .ad or .adoc. This class is AsciidocDirectoryWalker.

Example of finding all asciidoc

DirectoryWalker directoryWalker = new AsciidocDirectoryWalker("target/test-classes/src");
List<File> asciidocFiles = directoryWalker.scan();

We keep learning,
Cold, cold heart, Hard done by you, Some things look better, baby, Just passing through (Sacrifice - Elton John)

lunes, abril 01, 2013

Installing TomEE from Puppet

Apache TomEE is an all-Apache stack aimed at Java EE 6 Web Profile certification where Tomcat is top dog. It is the conjunction of Tomcat + Java EE

Puppet is a tool designed to manage the configuration of our systems declaratively. We only have to describe system resources and their state. This description is stored in the core-files of Puppet, which are called Puppet manifests.

In current post we are going to see how to define TomEE as Puppet resource, so it can be installed automatically in all computers that are managed by Puppet. I am using Ubuntu 12.04, but of course this could be adapted to your system.

Because TomEE is written in Java, we obviously need to ensure that a JDK is present in our system, for our example we are going to use OpenJDK 1.6. The way to install a package to our system is using Package resource, which uses underlying package manager to find, download and install it.

Let's create an init.pp manifest file and the first thing we are going to do is create an exec task which updates the package manager with a list of last packages available.

Then we can define a class that is in charge of installing the OpenJDK. A class in Puppet can be understood as a collection of resources that will be seen by Puppet as a unit.

It is pretty intuitive I guess, first of all we are ensuring that openjdk-6-jdk package is already present in system. If it is not installed, then Exec["update-package-list"] resource is executed, and finally the package manager installs OpenJDK into the system. 

After executing this part we can run java -version or javac -version without any problem, OpenJDK is there.

Next step is installing TomEE. Probably TomEE package is not in your distribution package repository in software package format (for example in case of Debian .deb). For this reason we need a different approach to the one followed by OpenJDK. We are going to download tar.gz file from TomEE site and uncompress it to an installation directory.

As with OpenJDK we are creating a class called tomee. First we are creating the directory where TomEE will be installed (tar command requires that destination directory should be already created). 

Then we are downloading TomEE from Apache site using wget command, to finally uncompressing it to already created directory.

Note the use of ->. Puppet does not guarantee the order of execution of defined resources, so you cannot infer "a priori" if TomEE will be downloaded first and then installed or vice versa. To define an order we are using -> operator which sets a priority between resources.

Now we have got Apache TomEE installed on computer, but obviously is not started and stopped automatically, you must execute /opt/tomee-1.5.1/bin/ to have TomEE available. Let's change this by using service resource. As its name suggests it registers as service an installed server. So inside tomee class define next service resource:

Keep an eye in two points of previous declaration, the first one is the provider/start/stop declarations, provider is set to init, which means we want to use the standard init-style service management, but for example you could use launchd (case of Mac OS X), upstart, windows (for Windows machines), ... see [] for more information. And because TomEE is not implemented as a Linux service by default (basically because has not been installed from a native package like .deb), we need to specify which command should be executed to start and stop TomEE

The second point is that service resource requires to have TomEE unpacked and OpenJDK installed, for this reason require attribute contains two declarations.

See full script here.

Final notes about example:
  • In Puppet, creates attribute inside exec task, is used to know if the resource should be executed or not (case that the file exists, exec will not be executed). In our case we are downloading TomEE in tmp directory. Most OS removes periodically this directory so it is a bad location for downloading it, but for this tutorial it works perfectly because I could re-execute the script every time as a new execution.
  • For simplifications we have add all content inside a single file, in your enterprise I suggest creating a TomEE module so you could share across all your projects.
  • TomEE version should be set as a variable/parameter/hiera so same class can be reused when new version of TomEE is released. 

We keep learning,

Ven al desfile de sirenas, de cuerpos bien cebados, sube al séptimo cielo (Ellos Las Prefieren Gordas - Orquesta Mondragon)


Donate If You Can and Find Post Useful