Logging with SLF4J

I. Introduction

SLF4J is an abstract layer for logging APIs. The principle is roughly the same as Jakarta Commons Logging. The advantages of the use of such a layer enable to be completely independant of the logging implementation. So it's possible to easily change the logging implementation without modifying the existing code. You only have to change the configuration of the implementation. And finally in the case of the conception of a library, this leaves the user to choose the logging system.

You gonna tell me : if Commons Logging makes already that, why an other framework of logging abstraction ? Simply, because Commons Logging as his defaults. The first one concern the loading of the logging implementation. Indeed, the search is made dynamically at the execution through a classloader system. And this method can be problematic in several situations by example when the application use custom classloaders or when using OSGi. And finally the implementation of the Commons Logging can cause some memory leaks.

Furthermore, Commons Logging constrain the user to test if a loging level is enabled or not before making logging containing heavy concatenation.

We will see than SLF4J solve this problems in an efficient way.

You can download the official distribution on the SLFJ4J website.

Read more…

Introduction to JR programming language

1. Language overview

JR is a programming language especially created to solve concurrent programming problems. This language is an overview of Java who add to this last the main paradigms of concurrent programming. Moreover JR make easier the concepts still implemented in Java like process or semaphores. There is also several extensions for JR to add more functionalities like monitors and Conditional Critical Region (CCR). JR is the implementation of the SR language for Java.

JR makes nothing else than add a layer over Java. Once we use the JR compiler, the JR source files are transformed in Java files and are executed by the virtual machine like any other Java class.

JR is often used as a school support to learn concurrent programming.

In this article, we will see the bases of the programmation with JR.

The presented version is the one of June 2009, the version 2.00602 who are based on Java 6.0.

This article need that you've installed the JR environment on your system. An article is available here for the installation under Windows.

In this article, we will especially focus on the apports of the JR language for the concurrent programming. We will not see the the integrality of the language. JR has other benefits than make easier concurrent programming, but we will not see that in this article. Moreover, all the aspects of concurrent programming in JR will not be treated here.

Read more…

Install the JR environment on Windows

1. JR Programming language

This article will present you the installation of JR on Windows. It's not an article to learn the JR programming language, this article focus on the installation of JR on Windows.

JR is a programming language especially created to resolve concurrent programming problems. This language is an overlay of Java who add to this last the main paradigms of the concurrent programming. Moreover, JR make easier some concepts also implemented in Java like the process or the semaphores. There is also extensions to JR to implement other functionalities like monitors and Conditional Critical Region (CCR). JR is the implemenation of the SR language for Java.

JR is mainly used as a school support to learn concurrent programming.

In this article, we'll see how to install the JR environment under Windows.

The used version is the one of June 2009, the 2.00602 who is based on Java 6.0.

2. Prerequisites

The prerequisites of JR are not many, you need two things :

  • Java : in the case of the last version of JR, you need Java 6.0. An older version (1.00601) is still available for Java 1.4. To install it if it's not yet the cas, go to the website of Sun and follow the described procedure.
  • Perl : go on the official site and download the last version of ActivePerl. Then, you just have to follow the installation procedure.

Once this two programs installed, we can go to the installation of JR.

Read more…

Tip : Don’t use shorts for loop indexes !

After a post I read on a french forum, i asked myself of the performances using shorts as loop indexes for loop with few iterations (less than 32768).

At first view, it can be tempting because we save 2 octets, so why use an int instead a short ?

But, when we think of that, we see that the int is more adapted. Indeed, it's more performant.

Why ?

In Java language, all the operations on integers are made in int. Thus, if we use a short as loop index, at each iterations, a typecasting will be made, that's really heavier than a simple affectation to int.

Here were my first results :

Time for int : 1441 ms
Time for short : 3015 ms

The short version is two times slower !

But like Jean have said, my first test was not correct at all because it doesn't consider some issues that could occurs with micro-benchmarks.

So I write a new test using a Java Benchmarking framework from Brent Boyer. Here is a little test with that framework :

package com.wicht;

import bb.util.Benchmark;
import java.util.concurrent.Callable;

public class ShortIndexesLoop {
    public static void main(String[] args) {
        Callable callableInt = new Callable(){
            public Long call() throws Exception {
                long result = 0;

                for (int f = 0; f < 32760; f++){
                      result += f;
                  }

                return result;
            }
        };

        Callable callableShort = new Callable(){
            public Long call() throws Exception {
                long result = 0;

                for (short f = 0; f < 32760; f++){
                      result += f;
                  }

                return result;
            }
        };

        try {
            System.out.println("Result with int " + new Benchmark(callableInt));
            System.out.println("Result with short " + new Benchmark(callableShort));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

And here are the results :

Result with int first = 695.181 us, mean = 43.233 us (CI deltas: -49.358 ns, +74.073 ns), sd = 42.938 us (CI deltas: -11.634 us, +16.426 us)
Result with short first = 733.224 us, mean = 45.679 us (CI deltas: -62.975 ns, +63.932 ns), sd = 45.567 us (CI deltas: -5.877 us, +8.020 us)

So the results are lower. This time, the short version is 5.657% slower than the int version. Note that this can vary a lot depending on your configuration.

I talk here of for loops, but the case is the same when you use while loops with indexes.

Here are also the results with long, double and float versions :

Result with long first = 816.555 us, mean = 104.771 us (CI deltas: -236.563 ns, +344.219 ns), sd = 143.295 us (CI deltas: -39.149 us, +63.700 us)
Result with float first = 1.018 ms, mean = 58.055 us (CI deltas: -87.036 ns, +113.537 ns), sd = 70.757 us (CI deltas: -14.269 us, +19.962 us)
Result with double first = 912.115 us, mean = 57.918 us (CI deltas: -66.644 ns, +91.312 ns), sd = 55.185 us (CI deltas: -12.617 us, +25.160 us)

We can see that the long version is the slowest one and that float and double are equivalent but slower than int and short.

To conclude, always use int as loop indexes add a very little improvements of performances, but not a great thing and do not use long for loops indexes.

Creation of Swing User Interface : Tables (JTable)

1. Introduction

This article will introduce you to create tables in Swing. This composent often gives problems when we start with Swing. I will try to explain the different concepts linked to the use of tables in Swing.

In this document we will see the basic concepts of JTable, the definition of table's models, the dynamic modification of table, the way to edit the rendering of cells, the edition of the content of the table and finally the sort and the filter of the table.

Read more…

JTheque Movies 1.0

I finished the development of JTheque Movies 1.0 :)

This application enable the user to manage a collection of movies and to read them directly in the application. For this last functionality, on Windows, you need Windows Media Player and on Linux you need VLC Media Player as web plugin. The user interface has been completely remade to be more esthetic.

Read more…

Improve performance of your web site with Page Speed

Page Speed is a Firefox addon who add a new functionality to Firebug enabling the user to evaluate and improve the performances of a web site.

Although as improving perofrmances of a web site is no longer as critical as in the past with the evolution of new technologies, this has some advantages :

  • Make your site faster and logically more cumfortable for users. Moreover, all the users doesn't have broadband and improve the rendering speed can save users.
  • Reduce the used bandwith and the hosting costs
  • Improve the web itself respecting some simple rules.

Discover how to achieve this objectives with Page Speed.

Read more…

Review of Jetbrains Intellij Idea 9 Ultimate Edition

Here is my review of the IDE Jetbrains Intellij Idea 9 Ultimate Edition.

1. Introduction

JetBrains just released the 9 version of Intellij Idea. This version is available in two editions :

  • Community Edition : Opensource version and free. Contains only the basic functionalities of the IDE.
  • Ultimate Edition : Complete commercial version of the IDE. I am going to introduce here this version.

This IDE is principally made for Java development, but now supports also a lot of others languages : JavaScript/Flex, HTML/XHTML/CSS, XML/XSL, Ruby/JRuby, Groovy, SQL, FreeMarker/Velocity and PHP.

For Java Development, it supports numerous frameworks and technologies : JSP, JSF, EJB, AJAX, GWT, Struts, Struts 2, JBoss Seam, Spring, JPA/Hibernate, Web Services, Rails, Grails, Java ME MIDP/CLDC, Tapestry, Google App Engine, Android or OSGi.

I am going to introduce some of this news functionalités and give you my opinion about the different themes.

IntelliJ Idea 9 loading view

2. Installation

The installation is very easy. It's a common installer like all others instlallers you can found, with the basic operations : License, folder, shortcuts, start menu and finish. Nothing special about that.

Once installation is complete, we must enter the license and choose the modules to activate like it was already the case in the previous versions. précédentes versions.

3. General improvements

The first thing we note is a real improvement of performance. More tasks are made in background. This is the case for indexing who launches at start but doesn't block the user interface. Thus, we can start work during indexation but some actions are not available, like refactorings.

Background indexing

We can also see that there is much less freeze of graphical interface who is also more reactive. The feedback of the different actions has been improved. It seems that we can see that an action has been launched or done and we're not to ask us if something happens or not, like that can happens with Idea 8.

4. User Interface

As previously stated, the reactivity of the user interface has been greatly improved.

The views have not major changes, but we can still note that there is some interesting new functionalities.

First of all, we can now debug and launch directly from keyboard shortcuts :

  • Alt+Shift+F9 : Open debbuging view
  • Alt+Shift+F10 : Open launch view

Here are these new views :

Quick Debug ViewQuick Run View

Then, we've also a new bookmarks management functionality. We can now put bookmarks in every portion of code, either class or lines of code. We've now there shortcuts :

  • F11 : Create a new bookmark at the selected item (file in project view or line of code in editor)
  • Shift + F11 : Show the bookmarks and enable the user to go to one bookmark.

An example of bookmark on a line of code :

Bookmark on a line of code

And the view of bookmarks :

Bookmarks

And last but not least, we can now copy files/directory from operating system and paste them in Idea and vice-versa. That is very practice but was not available in the previous versions.

5. Maven support

The "Maven Projects" view has had little modifications. In the "Lifecycle" folder, only the main goals are displayed (clean, validate, compile, test, package, install, site et deploy) . I think it's a good thing, because we use especially these goals. Of course, we can display all the goals with an option in the tool bar.

Always in this view, we can now see the profiles (Maven 2 profiles) who are configured in settings.xml and go quickly to this settings.xml file. More than the profiles, we can now directly display the the dependencies of a project : Maven 2 :

Maven 2 project dependencies

We also have access to a dependency graph for a Maven module :

Maven 2 dependency graph

It is now possible to automatically reimport the Maven projects after a change in the pom.xml files.

For the reimport, we can now reimport only projects who have changes in their pom.xml or concerned by the others changes. More than this improvement, the speed of this operation has been improved by a factor of 3 or 4.

6. OSGI support

From this version, Idea natively integrate an OSGi plugin, Osmorc. This plugin try to make easier the development of OSGi applications in Idea. I'll try to show you the main functionalities who are now integrated in Idea.

The first thing to do is to configure the project for OSGi. To do that, we just need to activate the OSGi facet for the necessary modules. Then (or before, as you want), we must configure the installed OSGi instances.

Configuration of installed OSGi instances

Then, we must configure the instance to use for the project :

Configure the OSGi instance for the project

We can then go to the creation of a simple OSGi project.

The first functionality to note is of course the code completion of the MANIFEST.MF entries :

OSGi Headers code completion

When you add the Import-Package header, we can use the classes of this package from the project without doing anything else.

But we can also define a Manifest file from Idea :

Manifest configuration

Et we continue with an activator :

package com.pragprog.hello;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;

public class HelloWorld implements BundleActivator{
    @Override
    public void start(BundleContext bundleContext) throws Exception {
        System.out.println("I'm in OSGi :)");
    }

    @Override
    public void stop(BundleContext bundleContext) throws Exception {
        System.out.println("I'm leaving OSGi :(");
    }
}

To finish, we create a new run configuration "OSGi bundles". For That, we just need to add the bundle to the list and in the "Parameters" tab to choose the good OSGi instance to run :

OSGi run configuration

And we launch the configuration, that give us :

OSGi application

But we cannot interact with the OSGi instance from Idea, that's not very practical. We cannot stop modules or either manage them from the console. But the OSGi development is still facilitated by Idea. But it would have been better to have something a bit more advanced.

7. PHP support

Here is a functionality who's perhaps not interesting for everybody but that will be interesting for the Java developers who manages a PHP site or simply for the PHP developers. For me, who manages also a PHP website, it's a great news. Before that, i used other editors and IDE to edit my website, but now i can do everything from my favourite IDE.

First and of course, we have syntax highlighting of PHP files :

Syntax highlighting of PHP file

We also have automatic code completion for PHP functions :

Automatic Code Completion

But the IDE make more than simple code completion. In fact, it discovers automatically the fields and methods from a class and fields and methods from the standard library. You can see this functionalities in the next images :

Contextual Code completion

Advanced Code completion

Moreover, Idea also supports PHPDcod documentation and make code completion based on PHPDoc :

Code Completion PHPDoc based

We can also note that it's possible to do PHP Debugging and unit tests with PHPUnit. But this is out of the scope of this article.

8. Task management

This functionnality isn't for me a major change, but it remains interesting. It enables the user to save state of view in a specified task.

A task save the current changelist, the open editions, the state of the project view, the run configurations and the breakpoints. If you work in two parts of a project, you can create two tasks and you have also the same properties when you switch from one to the other. And you can quickly commit only the changes of a task because of the decoupled changelists.

Task Creation

Then, it is possible to go to a specific task, who has for effect to save the state of the current task and restore the state of the task to open.

Open task

It's also possible to synchronize the different tasks with YouTrack or JIRA.

9. Miscellaneous

More that these new big news, there is many other changes that i will not detail including :

  • It is now possible to create a module from a Grails/Griffon project in the same way we can import project from Eclipse. We can also create Griffon/Grails modules or applications.
  • There is now a Spell Checking inspection for the code and resources who is enabled by default in this new version.
  • The public classes and methods who aren't used are not directly shaded like it was already the case for unused private fields and methods.
  • Grails support improvement with Griffon and Gradle support
  • Android support
  • Flex support improvements

10. Conclusion

To conclude, JetBrains has one more times demonstrated its expertise in Java IDE presenting an excellent new version full of new functionalities and improvements that made the Java development even better.

The only thing i had to complain in this version, is the lack of documentation of OSGi plugin and the limitations of the latter.

You can download IntelliJ Idea 9 on the official site.

Google Analytics goes asynchronous !

From this month, Google provide a new Google Analytics script that works in an asynchronous way.

This has several advantages :

  • The time to load the script has less impact on the page loading time.
  • Best data recovery and so more saved visits. The visits terminated before the script loading doesn't happens anymore. Because, due to the asynchronous way of loading, we can add the analytics code in the head of the page.
  • Less tracking errors.

Voici le nouveau code à insérer dans vos pages :

  var _gaq = _gaq || [];
  _gaq.push(['_setAccount', 'UA-XXXXX-X']);
  _gaq.push(['_trackPageview']);

  (function() {
    var ga = document.createElement('script');
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 
        'http://www') + '.google-analytics.com/ga.js';
    ga.setAttribute('async', 'true');
    document.documentElement.firstChild.appendChild(ga);
  })();

We have to note that this code use the async parameter of HTML 5 and that functionality is not supported by all the browsers. If you use that script in a non HTML-5 Browser, the script will be executed exactly as the old Analytics script in an synchronous way.

That's a good news for the fans of performances.

Source : Google Analytics Launches Asynchronous Tracking