Maven Archetype for CDI and JSF

javajavaeejsfcdi

Today I created a maven archetype for small JSF/CDI based applications. It creates a small web application that is meaned to be run on a Servlet Container and not on a JavaEE Application Server.

As you know on a servlet container there are no implementations provided for JavaEE-API's like JavaServer Faces or JavaPersistence API. Therefore I added the needed dependencies in the archetype.

In addition there are some Testing libraries like JUnit, Mockito and FEST-Assert added too.

The configuration of the archetype is highly opinionated and contains the libraries I'm using regularly. It is intended for my own usage but as it doesn't cost me anything to publish it to github you might find it usefull too.

Github: https://github.com/lestard/jsf-cdi-archetype

more...

Java Game ColorPuzzle

javagames

Today I like to show you a little java game I have written roughly a year ago. Back in that time I was looking at the Java-2D-API and Swing/Canvas and thought that the best way to learn this stuff is to write a little game. Thinking of an good example game idea was not that easy so I decided to recreate an existing JavaScript game that I had played some time ago. The problem is that I can't remember what the original name was and where I found this game. :-(

screenshot

The gameplay is easy: You have a grid of squares like a chessboard but not in black and white. Instead the squares are randomly filled with one of 7 colors. At the beginning the player has control over the square in the top left corner. With the buttons on the right (one for every color) you can change the color of the square(s) you control. When there is a square with the same color in the direct neighbourhood of one of your controlled squares, this square is now also controlled by you. When you now click on one of the color-buttons, all squares controlled by you are changing the color again. The aim of the game is to control all squares with as little clicks as possible.

Like I said, the main reason to create the game was not to build a great new super game but to learn and exercise the programming language and framework. The game was working more or less but there was an exception with the painting of the component in rare cases. I wasn't able to find the real reason for this exception and so I haven't released the game.

Since then I haven't worked on the game but some weeks ago I remembered the game and wanted to finish it. While looking at the code I found some code smells and bad design. When I would build a game like this from scratch today I would do many things differently.

As the first task I was looking at the unit-tests: I replaced EasyMock with Mockito and changed all asserts to used FEST-Assert. After this I refactored many classes and splitted the code into smaller pieces. While I didn't know the concept of Dependency Injection back in the time I created the game, nowadays DI for me is one of the most important design patterns and so I modified the application to use CDI/WELD-SE. I changed the whole event-mechanism to also use the features of CDI-Events. With this modifications I was able to reduce the coupling between the components a lot.

The last modification was to change the way the canvas is repainted. The old version was using a game-loop with a repaint-mechanism that was more suitable for games that need a high framerate. As this is not the case for this game I changed it to used the default repaint mechanism of the swing components and removed the game loop. The exception that irrirtated me at first was now gone and so I thought it would be the right time to "release" the game now.

Another reason to create the game was the idea to create something like an "artificial intelligence" (of cause it's not realy intelligent or clever) that can solve the game. I liked to test many strategies to build AIs and let them play against each other. I created a "Bogo" algorithm that chooses a color at random (referring to BogoSort) and a "Bruteforce" algorithm that tries every available color. At the moment this artificial intelligence feature is disabled but probably in the future I will create a UI for this.

Have fun with the game. Before I forget, of course the Code is free software under the GPL and can be seen on github. Feel free to look into it and tell me what you think.

more...

Another talk at the JUG Görlitz about JSF and CDI

javajavaeejuggrjsfcditalk

Last year I already gave a talk about JavaServer Faces and Contexts and Dependency Injection at the Java User Group Görlitz. At the january meeting of the JUG Görlitz on january the 25th I gave another talk building upon the last one. This time I explained some other features of JSF and CDI, namely:

  • navigation in JSF
  • templates and reusing of facelets
  • Ajax with JSF
  • little example with PrimeFaces
  • CDI Producer
  • Observer Pattern with CDI
more...

Problem with el-api in Eclipse/Maven/Tomcat

java

Recently I have worked a lot on Java Web projects and today I got strange exceptions like this:

java.lang.LinkageError: loader constraint violation:
when resolving interface method "javax.servlet.jsp.JspApplicationContext.addELResolver(Ljavax/el/ELResolver;)V"
the class loader
(instance of  org/apache/catalina/loader/WebappClassLoader)
of the current class, com/sun/faces/config/ConfigureListener,
and the class loader (instance of org/apache/catalina/loader/StandardClassLoader) for resolved class,
javax/servlet/jsp/JspApplicationContext, have different Class objects
for the type javax/el/ELResolver used in the signature

The origin was a little helper method that should lookup a String value in the expression language context:

private String getStringELValue(final String el) {
   String result = "";

   FacesContext fc = FacesContext.getCurrentInstance();
   if (fc != null) {
      String value = fc.getApplication().evaluateExpressionGet(fc, el, String.class);
      if (value != null && !value.isEmpty()) {
         result = value;
      }
   }

   return result;
}

This methods depends on classes from the el-api.jar. This jar is provided by tomcat per default so you don't have to provide it with your application but to be able to compile (and test) the code with maven you have to specify the dependency in your project's pom.xml:

<dependency>
    <groupId>javax.el</groupId>
    <artifactId>el-api</artifactId>
    <version>2.2</version>
    <scope>provided</scope>
</dependency>

Line 5 (<scope>provided</scope>) is important in this dependency definition because it tells maven to use the el-api.jar to compile and test the application but not to put it in the WEB-INF/libs folder of the resulting web-application because this jar is already provided by the server.

After a lot of investigation I found out that the maven-plugin of eclipse doesn't care about this scope declaration. The result is that there are 2 copies of some classes on the classpath which causes the exception mentioned above.

The solution was to install the m2e-wtp plugin. As the name suggests this plugin does some kind of integration between maven and the web tools platform (wtp). After installing this plugin you have to choose "Update Project Configuration" in the maven sub-menu of your project. After this, everything was working perfectly. Maven was able to build the application and run all tests and tomcat was able to deploy and run the application without any exceptions.

After installing the plugin I got some red crosses on my projects telling me that there are still some problems. The reason for this was a wrong compiler setting. It seems that the new maven plugin tells the projects that they are of java version 1.5 and not 1.6 as it should be. The solution was to explicitly configure the compiler setting in the pom.xml:

<build>
    <plugins>
        <plugin>
            <artifactId>maven-compiler-plugin</artifactId>
            <version>2.3.2</version>
            <configuration>
                <source>1.6</source>
                <target>1.6</target>
            </configuration>
        </plugin>
    </plugins>
</build>
more...

Talk at the JavaUserGroup Görlitz about JavaServer Faces 2

javajavaeejuggrjsftalk

On November the 30th I gave a talk about "JavaServer Faces 2" and "Context and Dependency Injection (CDI)" at the Java User Group Görlitz. I talked about some of the main features and concepts of JSF2 like ManagedBeans, Facelets and the Expression Language. After that I gave a little introduction to CDI because in my opinion it harmonizes very good with JSF2.

After the talk I developed a little example application that uses JSF2 and CDI on the projector. In this application the user can add and store notes. The project uses maven for the dependency management.

You can download the slides and the project here:

more...

Interesting Blogs of two fellows of mine

Today I want to recommend the Blogs of two fellows of mine. First the blog of Max Wielsch in which he writes about his experiences related to computer science and software development. He is working at the same company as me and is proceeding a project I started implementing at the beginning of the year which is called the "Human Task Service". This is a Software which integrates human interactions in Service Oriented Architectures (SOA). This is necessary because BPEL, the standard to orchestrate webservices to SOA's doesn't come with build in support for human interactions. Amoung other things, in his blog Max describes one component of this Human Task Infrastructure in detail which is used to monitor the hole application.

The second blog I like to recommend is the blog of Raik Bieniek. He is also one of my fellows and in his blog he writes about Acceptance Tests with internal DSLs. "DSL" stands for "Domain Specific Language" and means that there is a language (similar to programming languages) with which a customer can write acceptance tests in his own words. He doesn't need to know technical things and terms but can use terms from his own subject area. Raik developes a framework with which you can create such DSLs for your desired subject. "Internal DSL" means that you use an existing programming language to build your DSL on top of it. The benefit is that you can use features of this existing programming language. He uses for his framework the programming language Groovy which runs on the Java Virtual Machine.

more...

Hiding Toolbars for App-Tabs in Firefox

javascriptxul

Some weeks ago I have written a little Addon for Firefox which lets you hide the toolbars for domains. This was mainly aimed at App-Tabs introduced in Firefox 4. The Addon was working for me and some friends but sadly the people on Firefox's Addonsite obviously couldn't get it to work and so it wasn't hostet on there.

Today I have read a wikipage which stated that the Firefox developers are working on a similar functionality for a future release of Firefox. I think that's great because this feature is really usefull.

more...

YouPay: Android/iPhone app for roommates and everyone else

java

If you have ever lived in a flat-sharing community you probably know this situation: Everyone has a turn to do the shopping from time to time. Today you go shopping for 12 € and the next time your roommate goes for only 8 € and tells you that you are the next in line. That's not fair, isn't it?

Some friends of mine and me have thought of a solution for this problem: YouPay - An app where you and your friends can enter your expenses and the app tells you who is the next in line. There is already an iPhone app available, last week the Android version was released and I'm working on an web version of this app. At the moment I can't say when the webapp will be ready because I have also some other projects I'm working on and there is some additional planing to do because the web version will have some extra functionality compared to both smartphone apps (of cource I don't tell you more about this at the moment).

Please check out the apps if you are an iPhone or Android user and give feedback. Both Apps are free (this time only free as in free beer).

logo screenshot

more...

OSGi and SpringDM workshop for the JavaUserGroup Görlitz

javajuggrtalk

Last wednesday I have done a workshop for the Java User Group Görlitz with the topic "OSGi Serviceplatform und Spring DM".

OSGi is a specification for a service-based application framework for Java. While in pure Java you use packages to encapsulate code, with OSGi you have another abstraction layer called "bundles". These bundles can contain many packages and you can define that the bundle "exports" some of these packages. Other bundles can than "import" these exported packages to use that code. You can encapsulate different functionalities into different bundles.

One of the main features of OSGi is the ability to load a bundle at runtime. While the whole application is running you can install, update, start, stop or uninstall a bundle. So when you change some code inside a bundle or you fix a bug, the application don't have to be redeployed to get the new code running.

With Spring DM you can combine the features of OSGi with the Spring framework. This is a great combination to build modular business applications in Java.

more...

Fixed wrong behaviour in log4xul

javascriptxul

Last week I was coding a little bit on my music player and as everytime I used my little logging-library log4xul. Most of the time I have the Level of the logger set to "debug" so that it prints every message. Because of this I haven't noticed that I have made a really embarrassing mistake with the logging levels in Log4xul. I have mixed the hierarchy of the levels so that with the logging level set to "DEBUG" the logger only prints this debug-messages while when set to "ERROR" it prints everything.

Of cause this is wrong because you want the exactly reverse thing: When the level is set to "DEBUG" you want to get every message and with level set to "ERROR" you only want to get the error messages.

Now I have fixed this so the Log4xul should now work as expected.

Log4Moz

I have also found out that I have not searched enought for other logging frameworks at the time when I started to write Log4xul. In the meantime I have found the Log4Moz framework which is made for logging within firefox extensions. It is a lot more similar to the "original" Log4J and gives you the possibility to create hierarchically structured loggers for your files.

When I first used Log4Moz I was impressed and intended to use it in the future. But then I switched back to my own logger because Log4Moz has no ability to print the messages in a textbox but only to the JSConsole and to a logfile.

I have tried to write my own TextboxAppender for Log4Moz but it only worked till I hit the reload-button. After this it crashed. I don't know if it even would be possibile to write this kind of appender with the achitecture of Log4Moz.

more...