java2scala #1: Introduction

This is the very first post of a new series in which I’ll try to introduce scala to an audience supposed to be a quite expert and very busy Java programmer. I don’t want to discuss here if scala will be the next success language in favour of Java (even if I agree with some opinions about that [1] [2]). I’d like to just suppose you:

  • Are a quite expert Java developer. So you can read and understand in few seconds not too complex java samples.
  • You think a polyglot programmer is a must nowadays: see my opinion and what a friend of mine said about that.
  • You are very interested in scala language and would get in touch with it
  • You are terrible busy and/or prefer to learn from examples than from a book. Of course at some stage of your learning a good book would be a must. Don’t worry there are many good book about scala. As said if you are busy some samples comparing a well known java with scala could help you to get a first idea about scala.

At this point you have probably already got what I have in mind: a post series (maybe a long one) with a set of samples written in java and in scala doing the same things. I think it could be a good way for an expert java developer to compare the language, understand similarities, difference and totally new features of the language. Of course I’ll try to help you to get all these points with some opportune comment in my posts.

I’m trying to make your life easier. For example what I don’t like (from my java developer point of view) of all books article and tutorials about scala (and more generally speaking about all new languages around) is that all samples are written to run in console and suppose to demonstrate everything with the console output. Of course it is the easiest way to try something in the new language and (don’t misunderstand me) scala console is great to play with the language. But it isn’t what a java developer is used to. We are used to an IDE to edit our code, a build framework to build/package/resolve dependencies, unit tests to better understand and verify code, a version control system to keep track of our code changes and their history. And I’m trying to present java2scala samples with no console, but a complete environment where you will feel at home:

java2scala samples will be not only a series of blog posts, but also a project:

  • Using Eclipse and the scala plugin as IDE
  • Using maven as build environment (with scala-maven-plugin and m2eclipse integration)
  • All samples will always have its own unit tests (both for java and scala sample)
  • The project is hosted on github using git as SCM and having some great good feature of github like direct download of last committed code.
  • All samples will have a blog post describing every sample details and difference and features of both languages presented in the samples. I’ll try to keep my comment as minimal as possible, hoping sample code can explain itself. I’ll just try to point your attention to some crucial aspect of samples presented, but , of course, feel free to ask.

The project will be always up to date to the last blog posts (at least master branch) and will contain 2 maven modules: java samples and scala samples. Of course both module will have same samples (and their unit tests) in the same packages written in the two different languages.

All what you have to do is to download (or much better “git clone git://github.com/maeste/java2scalaSamples.git” the project), import it as maven project in an eclipse workspace with m2eclipse and scala plugin installed and start to explore it. Of course don’t forget to follow this blog for posts explaining new samples (I think I’ll have 1 or 2 new posts a week).

Posts in this blog will be very important too describing samples and remarking differences, explaining what I have in mind and I would demonstrate abour Scale with various samples. Javadocs of any sample will contain a link to the dedicated blog post. All posts will be laced in a dedicated category java2scala and will also have its own homepage with this description, some useful links and a link to the category index.

Would you contribute in some way? Of course the simplest way is comments on this blog to add some different point of view, or open a discussion about any sample, but also someone who would join with some sample written in another language would be cool: it would permit us to compare Java, Scala, and some other language out there. Any other ideas? Feel free to write me.

Anyway, let’s me start with next post description of the very first samples already committed in git project…it will be posted Tuesday night (CET).

Stay tuned.

another passion: wines and food

Probably not all my readers know that I’ve another big passion
than technology and development: food and wines tasting.
I’m a good chef (well, some friends said I’m a GREAT chef…but you know they are friends…and my guests when they have said that) and a quite expert sommelier.

Don’t worry I’m not transforming this blog in a food blog full of recipes and tastes. Just a single post to inform my readers who share passion for wines with me that I’m compiling a simple sheet with my wine tastes, sharing it with a friend of mine (Marek Goldmann) and readable by any person on the internet

Have fun and contact me for any question about my taste…it’s a pleasure for me to spend some minutes sharing my passion. Of course other passionates who would share their tastes are more than welcome. Just let us know.

Google code issue tracker integration with version control using git commit template

I’m using google code for one of my open source project. And I think it is definitively a good code site.

All open source community code site have its pros and cons (I’ve tried a lot of them sourceforge, github, jboss.org, google code etc), and every one have some beautiful unique feature. But one of my most loved feature is definitively google code’s integration of issue tracker and version control. In a nutshell it permit to create, update and close issues just adding some keyword on your commit.

It’s great in general, but it’s really amazing if you are using a distributed Version Control. And I’m using git for local repository and git-svn to synchronize it with central subversion repository.
Imagine this situation: you are working with your laptop off-line (i.e into an air plane where also GSM connection isn’t available) and you find a bug or you have an idea for an enhancement. What do you do in this case? Of course take care of the bugged code, fix it, write a test for that and then commit it into your git local repository. What is the missing step in this work flow? Of course create an issue into your issue tracker system to keep community up to date, and don’t forget it if you can’t completely finish it now. At this point come very useful the google code feature permitting you to create, update, or fix an issue when you are committing a change.

There is only one problem with this integration: remember the structure the commit message have to fit and all (or at least the most useful) values you can assign to various fields. I’ve solved this problem creating 3 template for the commit messages and I use them with git. Using template during a git commit is very easy: just make a commit like this

git commit -a -t tempalteFile

Git will Use the contents of the given file as the initial version of the commit message. The editor is invoked and you can make subsequent changes. If a message is specified using the -m or -F options, this option has no effect.

Here you have my 3 template file:

NewIssue.tmpl

#commit comment here

#next line doesn't need any number. ssue number will be automaticaly assigned after commit
New issue
#A summary is always needed. Please complete next line
Summary:
#Uncomment the rigt Status
#Status: Accepted
#Status: Fixed
#Status: New
#Uncommet right Labels and/or add labels to the list (comma or space separate)
#Labels: Type-Defect Priority-Medium
#Labels: Type-Defect Priority-High
#Labels: Type-Defect Priority-Critical
#Labels: Type-Enhancement Priority-Medium
#Labels: Type-Enhancement Priority-Low
#Labels: Type-Enhancement Priority-High

#Comment text for the issue tracker goes here

UpdateIssue.tmpl

#commit comment here

#Complete next line with issue number
Update issue
#summary is rarely updated. If you need that uncomment and complete next line
#Summary:
#Uncomment the new Status you would get your issue. No uncomment status will keep the previous status
#Status: Accepted
#Status: Fixed
#Status: New
#Uncommet Labels and/or add labels to the list (comma or space separate) if you want update/add Labels
#Labels: Type-Defect Priority-Medium
#Labels: Type-Defect Priority-High
#Labels: Type-Defect Priority-Critical
#Labels: Type-Enhancement Priority-Medium
#Labels: Type-Enhancement Priority-Low
#Labels: Type-Enhancement Priority-High

#Comment text for the issue tracker goes here

FixIssue.tmpl

#commit comment here

#Complete next line with issue number
Fixes issue

#Comment text for the issue tracker goes here

The same thing should be possible also with svn and other SCM.

Hoping it could help some other people enjoying this google code feature developing open source code during their trips :)

Have fun!

Writing an irc bot for svn commit notification

Recently I’ve started working with a new team. Since we are based in 2 different site a bit far each other we are using extensively an IRC channel to communicate.

We are using subversion as SCM and we need to keep all members of the team up to date about svn commits. The solution I’ve put in place during an insomniac night in an hotel is a post-commit hook invoking an irc bot script written in perl connecting to the server and shotting a message there. Quite simple, and it is taking its goal.

In $SVN_REPOSITORY/hooks edit and make executable the file post-commit

#!/bin/sh

REPOS="$1"
TXN="$2"
SVNLOOK=/usr/bin/svnlook

# get last commit message
COMMIT=`$SVNLOOK log "$REPOS"`
USER=`whoami`

# call bot with arguments reposname, revison and commit message in one string
/usr/bin/perl /usr/local/bin/svn_irc_bot.pl "$USER $REPOS r$TXN: $COMMIT"

# all checks passed, so allow the commit
exit 0

then edit and make executable the file /usr/local/bin/svn_irc_bot.pl

#!/usr/bin/perl -w
#svn_irc_bot.pl

my $server = ""; #put here your address
my $port = 6667;
my $nick = "svn_bot";
my $ident = "svn_bot";
my $realname = "svn_bot";
my $chan = "#YourChannel";#put here your channel name
my $pass = "svn_bot";
my $svn_commit = $ARGV[0];
use IO::Socket;

my $irc=IO::Socket::INET->new(
PeerAddr=>$server,
PeerPort=>$port,
Proto=>'tcp') or die "DEAD!";

#print $irc "USER $ident $ident $ident $ident :$realname\n";
print $irc "NICK $nick\r\n";
#print $irc "PRIVMSG nickserv/@/services.dal.net :identify $pass\n";
print $irc "USER $ident 8 * :P erl IRC Hacks Robot\r\n";

print $irc "join $chan\n";

while(my $in = <$irc>)
{
if($in=~/004/)
{
print $irc "PRIVMSG $chan :$svn_commit \n";
last;
}

if($in=~/^PING(.*)$/i)
{
print $irc "PONG :$1\n";
}
}
close($irc);
#EOF

If user named “fooUser” make a commit on “fooRepository” for release 409 with a comment like “this is a fooComment” on irc channel you will get something like:

<svn_bot> fooUser fooRepository r409: this is a fooComment

I took the base of code here. Then I’ve modified it a little to get result I like.