Appliweb

All dimensions of your web developments

Affichage des articles dans java

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.

As Google App. Engine uses a datanucleus plugin for database access, I’ve now switched to it (and leaved Hibernate).

Datanucleus uses bytecode instrumentation for persistance mapping, but not in real time like Hibernate does. Your classes must be enhanced just after compilation.

With maven, it’s not a problem for me, as described here:
http://www.datanucleus.org

Netbeans + Linux + Maven + datanucleus-plugin = hell

Using datanucleus-maven-plugin 1.1.4 (don’t use version 1.1.3 it will force download of Datanucleus 2.0), I get an error when running the compilation under Netbeans.

1.
2.
Embedded error: Error while executing process.
java.io.IOException: error=2, No such file or directory.

I then enable the log for the plugin to see what’s happening, but get no logs. No logs at all…

Of course, nothing in the doc about this problem, so I finally searched the plugin source code:

I see that the plugin is calling « java » directly to process the enhancements. And it seems that when Netbeans calls Maven for compilation, it doesn’t provide the linux path, so the plugin couldn’t start java.

I noticed then a hidden or not-so-documented option, fork, that if set to false, will call directly the bytecode enhancer with no calls to « java ».
Here is the maven configuration to use:

1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
<plugin>
  <groupId>org.datanucleus</groupId>
  <artifactId>maven-datanucleus-plugin</artifactId>
  <version>1.1.4</version>
  <configuration>
    <fork>false</fork>
    <mappingIncludes>**/*.class</mappingIncludes>
    <api>JPA</api>
    <log4jConfiguration>
      ${basedir}/src/test/resources/log4j.properties
    </log4jConfiguration>
    <verbose>true</verbose>
    <enhancerName>ASM</enhancerName>
    <props>
       ${project.build.testOutputDirectory}/datanucleus.properties
    </props>
  </configuration>
  <dependencies>
    <dependency>
      <groupId>org.apache.derby</groupId>
      <artifactId>derbyclient</artifactId>
      <version>10.5.3.0_1</version>
    </dependency>
    <dependency>
      <groupId>log4j</groupId>
      <artifactId>log4j</artifactId>
      <version>1.2.12</version>
    </dependency>
  </dependencies>
</plugin>

So I tried, and this worked like a charm.
PS: The other plugin, schema-create doesn’t have this option, so I have to run it from command line.

I’ve just read the last interview from Gosling - See here.

With his usual straight-to-the-point language, he expressed concern about Google and the free Android. Basically, his point is that, as any operator can modify Android, you will have different versions of it and a hard time making your Android application work on all phones.

He’s right.

But personnally I prefer a fragmented platform that is Java based than a non-fragmented one that doesn’t allow Java applications.

Of course, a complete non-fragmented Java platform is much better, but it seems to be too slow-moving and to stiffle most innovation….

GC.

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.

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.

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.

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.

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.

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.

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 !