Archive pour le mot-clef ‘java’

Writing the Ultimate Form – Part 2

Mardi 9 mars 2010

Basic error handling

Seam, JSF and Java all team up together to help developers in displaying error messages to the user.

JSF provides the basis, with the FacesMessage mechanism. Anywhere in your code, you can create FacesMessages, and add them to a FacesMessages object. Then, when the next page displays, the error messages are displayed to the user.

You can use special tags for that in your .xhtml page:

1
2
<ice:messages id="messages" globalOnly="true" styleClass="message"
    errorClass="errormsg" infoClass="infomsg" warnClass="warnmsg"/>

Notice icefaces allows us to set various css style depending on the type of error.

The result is here:

Now, when a validation is bad, we want to highlight the input field, then display the error message next to it. Fortunately, JSF allows a developer to assign an error message to a field, and by default the validators and converters do just that.

And Seam, allows us to define specific html code when a field is in error. This is done using the s:decorate tag, and the best option is to refer to a template file that will be reused for all fields.

Now the input field is declared like that:

1
2
3
4
5
<s:decorate template="/layout/decorate.xhtml">
  <ice:inputText id="nbElements" value="#{searchPage.nbElements}" />
  <ice:message for="nbElements" styleClass="message"
    errorClass="errormsg" infoClass="infomsg" warnClass="warnmsg"/&gt;
</s:decorate>

and for the field that must not be null or empty, we just add an required= »true » attribute:

1
2
3
4
5
<s:decorate template="/layout/decorate.xhtml">
  <ice:inputText id="notNull" value="#{searchPage.notNull}" required="true" />
  <ice:message for="notNull" styleClass="message"
      errorClass="errormsg" infoClass="infomsg" warnClass="warnmsg"/>
</s:decorate>

and the content of the error template file is this:

1
2
3
4
5
6
7
<div class="errorDiv">
  <span class="#{invalid?'errors':''}>
    <s:validateAll>
      <ui:insert/>
    </s:validateAll>
  </span>
</div>

The errors css style just sets the component border to Red.

Now, if we try some basic errrors, like by inputing letters in a field mapped to an integer value, here is the result:

Notice the error message is more precise than the previous screenshot. Any error message can be changed in JSF by a simple properties file.

That’s all for basic error handling. Next time, we’ll try to see the behavior of the current page for all errors defined in the first part.

Writing the ultimate form

Vendredi 26 février 2010

Using Seam, JSF and IceFaces, I’m trying to write THE form.

I mean, even the basic stuff like a good form is not easy to do, and we’re experiencing some troubles in our project.

So I decided to try to write the « perfect » and « working in all cases » form.

What features are needed ?

  1. Input must be validated. A integer field should not accept letters for example.
  2. Fields can be mandatory or not
  3. Erroneous fields should be highlighted, with an error message.
  4. I want to select easily a date. Moreover, I want to select it, even if a mandatory field has not been set.
  5. A Field can update values and rendering of other fields. Even if some fields contains errors

Well, that’s all for now. I can’t think of others features, but I’m quite sure I forgot some. We’ll add it later.

The basic:

For beginning, I’m just going to write an input field accepting integers, a date selector, and another field that is mandatory.

To do this, a few xhtml lines of code are needed:

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
<ice:form id="searchForm" >
  <ice:panelGrid id="homePanelGrid" columns="2" columnClasses="rightMenu,leftMenu">
    <ice:outputLabel value="Nb Elements:" />
    <ice:inputText id="nbElements" value="#{searchPage.nbElements}" />
    <ice:outputLabel value="Not null:" />
    <ice:inputText id="notNull" value="#{searchPage.notNull}" />
    <ice:outputLabel value="Start Date:" />
    <ice:selectInputDate id="startDate" value="#{searchPage.startDate}" renderAsPopup="true" />
  </ice:panelGrid>
  <ice:commandButton id="searchAction" value="Search" action="#{searchPage.search}" />
</ice:form>

Quite easy, and the result is here:

Now, if I try to enter an invalid value in the integer field and press the button:

No error message is displayed…

We’ll see in our next chapter how to handle error messages.

Share an xsd with multiple webservices using maven, netbeans, jax-ws and jaxb2

Samedi 27 juin 2009

My work really consist in making different tools and java framework work together. I need to find solutions so that any developper can understand and use them afterwards.

This time the project involves developing lots of web services, and some batch treatments accessing to a single and complete business model. The business model is defined by an XSD file. This file will often change during the lifetime of the projet, so I need some automatic solutions. I decided to have a library containing the Java classes of the Business Model, and this library will be used by the web services and the batches. I’m using Maven, jaxb2 and jaxws, all under Netbeans, and I will explain how I managed to do that.

Generating jaxb2 beans using Maven:

First, I use the maven jaxb2 plugin to generate the corresponding Java Classes. Netbeans doesn’t support Jaxb2 mapping with Maven (only with ant), so I had to find and modify the pom.xml file manually. Once properly configured (with generated classes under target/generated-sources/xjc), the generated source code is automatically recognized by Netbeans (and greyed so that you know it’s generated, like in the screenshot below).

Creating the WebService:

Next, I create the .wsdl files, I’m not really expert in web services, so I create a web service using Netbeans (with the option to generate a wsdl from java classes), this was an easy way to get the .wsdl. Then I discard the other generated files from Netbeans, and told it to generate again the web service, this time using the .wsdl tweaked to import my Business Model .xsd.

The nice thing with Netbeans 6.7 and Maven, is that Netbeans modifies your maven project when you create a web service. The web service generation is really done by the maven project (using jax:ws plugin) and so is not dependent at all of Netbeans. Using other IDEs, you’ll have to create the web service using the wizards, and then manually modify the Maven project. Below is the modification made by Netbeans to the maven project file:

The problem:

When generating the Java source from the wsdl, the imported xsd is mapped again to Java classes. With the same files than the ones generated by Jaxb2. So I tried to tell the jaxws plugin that the .xsd file was already mapped. Documentation says you can use « episode » files, but this only works if the namespace of the webservice is different from the namespace of the XSD. That was not the case.

One solution is be to re-generate the source code, and delete the duplicated source so that only the classes in the business model project are used. That should work, but I need to keep some generated java classes: The ones from the Web Service itself. So I want to generate the .xsd classes in another package than the classes from the .wsdl (all with the same namespace !!)

The solution:

After lots of trials and errors, I eventually managed to do this by carefully crafting two files: one for jaxb and the other for jaxws, like this:

The content of  bindings-jaxb.xml:

And the content of bindings-jaxw.xml:

I get the web service interface and factory generated  in com.xxx.xxx.ws.mediation.service, and the source code from the business model in com.xxx.xxx.sas.data. It was then really easy to tell maven project to delete duplicates generated source code before compilation.

And as Netbeans compiles using maven, I get this behavior completely integrated into my IDE.

GC.

Netbeans compared to IDEA #2. Maven again

Mercredi 8 avril 2009

Following the first post about « using Netbeans for an IDEA user », I would like to add some more informations about maven support.

Skipping unit tests:

Each time I compile my maven project in Netbeans, it runs the unit tests, even for small changes. Fortunately, you can easily tell Netbeans to avoid unit test during compilation. Simply right-click on the project, then select Properties. You can check the « Skip tests » control like in the dialog above:

Importing a maven Web project:

I have successfully opened a Maven project that generates a .war file into Netbeans. It is recognized as a web project, you simply by sets the target application server, and then you can run and debug your web application.

It generates the .war file using Maven, and deploys it automatically to the application server.

The neat feature with Web projects:

It’s easy to use, and great for day-to-day web development: It’s tedious to wait for Maven to generate the war and deploys it each time you modify a .jsp or a .xhtml page. So Netbeans automatically deploy the webapp resources files when you save them. Great ! And IDEA does it too.

Android Game Development #3: Games specific

Dimanche 5 avril 2009

In this third installment of the Android Game Development series, I’ll try to explain how to construct the project. I know the LunaLander sample directly uses thread and SurfaceHolder.Callback, but my first goal is simply to display an image, I will complexify later.

Games don’t use Views

In Android, most of the games don’t really use the View hierarchy of objects, because it’s too slow and cumbersome. They instead write directly to a Canvas.

In order to get the Canvas displayed to the screen, you create a SurfaceView object.

Thus, the page is a  FrameLayout, and inside we put our SurfaceView class, as shown in the main resource file:

How to insert an Image into Android project ?

Using Gimp, I have drawn a small red ball:  Nice isn’t it ?

How can I use it in my project. Well it’s quite simple, you copy the file in your project res/drawable subdirectory, you update your Eclipse Project (F5 key). Automagically, you see your new image in the project, and the Eclipse Plugin regenerates the R class so that you can easily access the resource in your code or Layout file. Just take a look at this sample (called by the constructor of our SurfaceView):

We can now easily override the onDraw method on our SurfaceView derived class, and display the image:

Notice we set the position and dimension to display in the image itself, and tells it to display into the canvas. I would have expected the other way round (calling a Canvas method to draw the image).

I run the  project and here is the result :

Well something got wrong, but what ? Maybe I should check in Android documentation how to see some sort of logs or whatever, but for now, each time I get this, I’m clueless about what went wrong.
This time it was easy, I put the wrong class name in the layout.xml file, and it couldn’t find my SurfaceView class. Too bad Eclipse didn’t warn me about this. I fixed the error, started again, and then:

A Black screen. No Error but no red ball neither….

Running it under debugger shows me that the onDraw method on my SurfaceView class is not called !

After some more experiments, (checked the LunarLander sample, tried to draw a big green square on Canvas, etc….) I finally managed to make it work.
To have the SurfaceView displayed, you have to set a background color. With my init method like that:

I get the « good » result as follow: Yeepeee !!!


GC.

Android Game Development #2: Discovering

Mardi 31 mars 2009

Welcome into part 2 of the Android blog series !

Today, I will try to explain the concepts I discovered when creating the project.

Finding a Sample:

When trying new libraries or new functionalities, I always try to find sample code I can analyse and copy to my own project. Fortunately, Android project pages contains such samples.

One of them seems to be a perfect starting point for my game: The Lunar Lander Sample. It’s a classic game where you want to land a lunar module by switching off and on reactors. The difficulty is that the module follows gravity rules, and you really need to predict it’s behavior.

Remember this game now ?

The documentation explains to a great extend the source code of the sample, so I won’t repeat it here. Well, just in two words then.

General Guidelines:

The Main class is called an Activity. An Activity is like a controler (in MVC pattern), there can be many activities in a single application. Think of it has a screen in your application. It creates a View class, either programmatically or by reading an XML description file. The View class represents, errr…. the View part of the MVC pattern.

All images, view xml files and string resources are parsed by a sdk tool that generates a class, the R class (what’s this name ???), that contains the Ids of these resources. You can write your application like that:

- You design you application U.I. using the XML file (and the plugin shows you instantly the result).

- You override the  onCreate function in your activity class to load and set the view to display.

- From the View class you just created, you get the Java classes of your controls  (buttons, input fields) by using their Ids in the generated R.class

-  You then can get / set  the values of these controls, listen to a click, etc….

- Of course, for a game, you don’t use the View classes hierarchy, but directly draw into a canvas. More on that later.

NB: The eclipse plugin handles SDK tools for you.  It re-generates automatically the R.class (Did I told you the name was awful ?) whenever you create a new resource, it immediatly shows a bad reference in your layout file, it can display the result of your layout (screen below), and it packs the project and installs it into the emulator when you click on the Eclipse Run button.

GC.

Android Game Development #1: The project

Mardi 31 mars 2009

As a side project, I’m trying to develop a small game in Android.

As you know, Android is the new open source platform, designed by Google, for smartphones.

It is based on Linux, and the language used to develop applications on it is Java. So I decided to try to develop a small application for it: A game.

I will try to  explain here the things are discovered in this journey.

The project:

A very simple game that uses the touch screen capabilities of the phone: You throw a ball using you fingers and it must either go into holes, avoid pitfalls, bump into, errr…. bumpers and so on. You will have many different levels with many kinds of obstacles to overcome. This game will involve precision in your fingertips !

Starting:

First I downloaded the Android SDK, along with Eclipse and it’s plugin. All is clearly explained here and installs without an itch. Along with the SDK comes a smartphone emulator, that shows this:

Nice no ?

The Eclipse plugin integrates Android SDK seamlessly with eclipse: You can create a new android project, run it in the emulator, design the screen (with preview), etc….

Next part will talk about creating the game project using a provided sample  project.

Netbeans compared to Idea #1. Maven

Mardi 24 mars 2009

s you may know, I was using the trial version of IntelliJ IDEA to develop Appliweb.

Alas, the version expired, so I decided to switch to Netbeans 6.5.1. No I’m not using Eclipse, because I don’t like how it is organized.

I will try to highlight the differences I see between the 2 IDEs.I’ll try to stay neutral, so that you can choose which you prefer.

Today we talk about Maven support:

  • IDEA imports maven pom.xml files to recreate it’s own project, whereas netbeans uses directly the maven files. Netbeans compiles using maven, not IDEA. It imports correctly sub-projects, and sub-sub-projects.
  • Completion in pom.xml file seems to be the same. Maybe Idea completes a little more data.
  • Changes you made in pom.xml files are faster to import in Netbeans. Almost immediate.
  • Netbeans uses Maven when building or Running a Unit test. That’s a good and a bad thing.
  • Good thing: More often than not, you get the unit tests running in your IDE and not in Maven. With Netbeans, you’re sure that it works in maven, hence in the Integration servers.
  • Bad thing: In netbeans, each build runs all the unit tests by default (sloooow), and running a unit test class takes a long time because it is run with maven.

I’m really pleased for now by the overall behavior of Netbeans, kudos for the maven support.

GC.

Multithreading Spring-Batch using Concurrent API

Mercredi 11 mars 2009

One of the recurrent task in my job is to create batch programs in Java.

Some are extraction batches (we get data from a DB and write it to an XML file), some are import (from XML to DB), and some are DB treatments only. All these batches are using Spring-Batch Library to do their tasks.

Spring Batch is a well thought-out library, that, among other things, allows you to:

  • Handle large data by splitting it in small chunks
  • Restart a batch exactly where it has stopped
  • Define how many handled chunks per commit steps
  • Automatic Rollback / Retry in case of error
  • Etc….

The diagram below shows a typical execution of a batch:


To simplify things, let’s say a batch is composed of steps, and a Step is a Reader and a Writer communicating together.

  1. At the beginning of the step, the Reader and  Writer are opened
  2. The Reader opens a cursor on the DB, using a query.
  3. The Writer creates the XML file to write
  4. Then for each element read by the reader (For each row of the result set, we convert it into a Java Object)
  5. The element read is sent to the Writer
  6. The Writer convert it to XML then write it to the file.
  7. When the Reader returns null, that means no more data to process
  8. The Step tells the Writer to flush all it’s data
  9. The Step closes the Writer and Reader.

Recently, I was asked to optimize a slow extraction batch.

Profiling the code,  I see that the Reader must execute 15 SQL requests to get all the data to send to the Writer. As all elements are handled sequentially, that meant 150 000 requests for 10 000 elements ! A quick look at the database status with a batch running showed me that the database was not fully utilized.

I decided to Multi-thread the queries done in the Reader. By default, Spring-Batch does not easily support Multi-threading, and I didn’t want to do much change in the existing code,  so I used the famous Java Concurrent API features of Java.

In my multithreaded batch, the Reader don’t execute the queries to get data for the Writer, but instead creates a FutureTask, and adds it to a Multithreaded ExecutorService. This FutureTask is then send to the Writer  which  ony stores it.

When writer.flush is called, we get the data one by one from each of the FutureTasks. All the threads of the ExecutorService are busy querying the DB to fill the data for the FutureTask. Thus all the reading will be multi-threaded.

Next, let’s deep a little bit into the code:

We create  a Callable class that will execute the queries, we suppose we want to read contracts from the DB:

1
2
3
4
5
6
7
8
9
10
11
12
protected  class ContractCreator implements Callable  {
protected long elementId;

public ContractCreator (long elementId)
{
this.elementId=elementId;
}
public Contract call() throws Exception {
// Called by the threads
// Execute all the queries here
// And return the real data object
} }

The reader creates the ExecutorService with a pool of 10 threads:

1
protected ExecutorService executor=Executors.newFixedThreadPool(10);

Then the reader creates the Callable class for each element to read and submits it to the threads of the ExecutorService.

1
2
3
4
5
6
public Object read (ResultSet rs, int rowNum) throws SQLException {
long elementId=rs.getLong("ELEMENT_ID");
// Multithreaded execution
Future result=executor.submit(new ContractCreator (elementId));
return result;
}

The Future created is sent to the Writer which only stores it.

1
2
3
4
5
6
7
8
/**
* We store all the contracts running in parallel here
*/

protected List&gt; contracts=new ArrayList&gt; (10);

public void write(Object output) {
contracts.add((Future) output);
}

It’s only when flush is called that the Writer tries to get each contract and write them to the XML file:

1
2
3
4
5
6
7
8
9
10
11
12
@Override
public void flush() throws FlushFailedException {
for (FuturecontractCreator:contracts)
{
// Get the data read by the threads
// Will block until the data is avaiable
Contract contract = (Contract) contractCreator.get();
writeContractToFile (contract):
}
contracts.clear();
super.flush();
}

We then have now 10 threads querying the DB in parallel with little efforts ! All the basic thread / synchronization handling stuff is done by the Concurrent API, and the resulting file is exactly the same as with the single threaded example.

I hope this Spring-Batch example allows you to understand better the power of the Concurrent API of Java !

Swing 2.0 blog

Lundi 26 janvier 2009

I totally agree with what the blogger said about swing 2.0: Here

The Java language has much evolved since Swing and Swing API should use the « new » language features.

And while we’re at it, Java beans API should be updated too. Support for Collection should be added for example.