March 23, 2010

Setting environment variables with perl

Usually I work with different environments on different projects, this means that sometimes I have to change system variables and references in order to work in a different project. To avoid this repetitive task, I've created a few PERL scripts to make my life easier.

Changing environment variables

What I did was to create a script that receives as a parameter a file name, this file contains a set of environment variables to set. To make it even easier, the file has the format:
KEY
VALUE
Like this:
JAVA_HOME 
/opt/java/jdk1.6.0_12
JBOSS_HOME
/opt/java/4.2.3.GA/jboss-as\
Note: I still don't know a lot of PERL.

Script(file: my_setenv.pl ):
$filename = $ARGV[0];
print "Using filename $filename";

open FILE, "$filename" or die $!;

$iterator = 0;
$current_var = "";
while (my $line = ) {

 if( $iterator == 0 ){
  $current_var = $line;
  $iterator++;
 }else{
  print "Setting ENV -> $current_var = $line";
  $ENV{"$current_var"} = $line;  
  $current_var = "";
  $iterator = 0;
 }
}
close(FILE); 

print "Let's show all ENVIRONMENT variables.\n";
foreach $key (keys(%ENV)) {
    printf("%-10.10s: $ENV{$key}\n", $key);
}
Usage:
$> perl my_setenv.pl google_code_env.env

That's all.

March 18, 2010

Get metadata from JPA entities

I needed to extract a column length from a EJB-JPA Entity configured with annotations, this is how I did it:
import javax.persistence.Column;
import javax.persistence.Entity;

@Entity
public class Person {

   @Column(length=30)
   private String firstName;

   public static void main(String[] args) throws SecurityException,     NoSuchFieldException {
      Object person = new Person();
      println(person.getClass().getDeclaredField("firstName").getAnnotation(Column.class).length());
   }
}

Solution source

March 11, 2010

GIT - Working with a SVN repository

Git a SCM created by Linus Torvalds and currently mantained by Junio Hamano, is a distributed revision control system very simple and extremely powerful.

For those people who want to test this system (me included), and comes from a SVN environment, there are some utilities (included with GIT) to interact with SVN repositories.

In my case, I have a copy of a project fetched from the SVN repository, and for local development I use a branch called 'development'. When the development branch is stable, I merge this branch with the branch 'master', and then commit the changes to the SVN repository.

- Create and fetch from SVN repository
git svn init https://localhost/dev/svn/tiergen
git svn fetch -r HEAD
- Create a 'development' branch and start working with it.
git branch development
git checkout development
- After all changes are made in the development branch, let's merge thi with 'master'.
git checkout master
git merge development
- Finally, commit everything to the SVN repository.
git svn rebase # I always do this
git dcommit

March 1, 2010

Joda Time - Difference between dates

In order to calculate a difference between dates, I've found many problems:
  • Leap years.
  • Minute, seconds differences between months and years.
  • Months with different amount of days, etc.
 And I was not going to develop all this, right?. Somebody must have already done this, so, I started searching. The result was Joda-time, a very handy library that is being used in projects like JBoss Seam.

With this library, my work only was to use it, so, if I wanted to calculate date differences, I just have to: