Coding-Content - TmpMail

javajavaee

Coding doesn't only belongs to my day-to-day work but is also one of my favorite hobbies. And like other hobbies it's fun to make a competition out of it. So last week I participated at the Leipziger coding contest. There where 4 categories: PHP, JavaScript and two times Java. I have chosen the second java category where the task was to develop a "fake email" application.

You can find the code here: https://github.com/manuel_mauky/tmpmail

Task: Temporal email addresses

The user of the application should be able to register herself with her private email address. After that the user can create temporal email addresses whose messages are redirected to the private email address of the user.

There are several use cases for this application: First of all you can use this temporal email addresses for registration at online shops, forums and so on where you don't want to use your private email address to prevent spam emails.

Another use case would be to use this application for testing purposes by software developers. They could check whether there own application is sending emails the correct way.

Implementation

My implementation is based on the JavaEE technology stack. I have used JPA2/Eclipselink as persistence framework with an HSQLDB inmemory database.

Dependency-Injection is done by CDI/Weld. In addition I used the Seam Solder library to handle events of the servlet container.

The handling of SMTP is done with the SubethaSMTP library.

The frontend is based on JSF 2.1 and MyFaces as implementation. Besides this PrettyFaces and PrimeFaces where used.

The whole application was developed test driven. For this purpose FEST-Assert and Mockito where used. Additionally Weld-SE was used for integration tests.


p.s. At the end I won the contest and got a Lenovo Thinkpad Edge as an award :-)

more...

Talk about BeanValidation at JavaUserGroup Görlitz

javajavaeejuggrtalk

Once again I gave a talk at the JavaUserGroup Görlitz. This time the talk was about "JSR-303 BeanValidation".

BeanValidation is a part of JavaEE that is used for validation of data. You use java annotations to describe restrictions and constraints for your java classes. The validator can then evaluate these annotations and give you an answer whether your objects are "valid" or "invalid". At the moment there are 3 ways to use this validation.

  1. By hand. To do this you use an instance of the Validator class and use the method "validate". As the result you get a collection of ConstraintViolation objects for every violation. When this collections is empty your object is considered to be valid.

  2. When using JavaServer Faces 2 (JSF) as frontend framework validation can be done automatically. You have nothing to do but to put an BeanValidation implementation (Hibernate Validator or Apache BVal) on the classpath or use an JavaEE compatible application server.

  3. When you are using JavaPersistence API (JPA) to persist your data validation is also done automatically. Everytime an Entity with BeanValidation annotations gets persisted by the EntityManager the validation is done in the first place.

In the talk I was showing some of the build-in annotations and how you can create your own custom constraints and custom validators to fit your needs. I also showed how you could include the Apache Commons Validator into the BeanValidation framework.

The Example code is available on Github: https://github.com/manuel_mauky/juggr_BeanValidation

The slides are available here and on Google-Drive

more...

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/manuel_mauky/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...