November 15, 2012

A Better Maven POM for Wicket Archetypes

If you are new or just wants to try Apache Wicket there is a good Wicket Maven Archetype for that. You can find instruction for that at http://wicket.apache.org/start/quickstart.html.

But the generated pom.xml leafs room for improvement. A better pom would be

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

    <modelVersion>4.0.0</modelVersion>
    <groupId>se.msc.examples</groupId>
    <artifactId>example-wicket</artifactId>
    <packaging>war</packaging>
    <version>1.0-SNAPSHOT</version>
    <name>Example Wicket</name>
    <organization>
        <name>MSC</name>
        <url>www.msc.se</url>
    </organization>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.build.outputEncoding>UTF-8</project.build.outputEncoding>
        <wicket.version>6.2.0</wicket.version>
        <jetty.version>7.5.0.v20110901</jetty.version>
    </properties>
    <dependencies>
        <!-- WICKET DEPENDENCIES -->
        <dependency>
            <groupId>org.apache.wicket</groupId>
            <artifactId>wicket-core</artifactId>
            <version>${wicket.version}</version>
        </dependency>
        
        <dependency>
            <groupId>org.apache.wicket</groupId>
            <artifactId>wicket-extensions</artifactId>
            <version>${wicket.version}</version>
        </dependency>
        
        <dependency>
            <groupId>org.apache.wicket</groupId>
            <artifactId>wicket-datetime</artifactId>
            <version>${wicket.version}</version>
        </dependency>

        <!-- LOGGING DEPENDENCIES - LOG4J -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>1.6.4</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.16</version>
        </dependency>

        <!-- JUNIT DEPENDENCY FOR TESTING -->
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.10</version>
            <scope>test</scope>
        </dependency>

        <!-- JETTY DEPENDENCIES FOR TESTING -->
        <dependency>
            <groupId>org.eclipse.jetty.aggregate</groupId>
            <artifactId>jetty-all-server</artifactId>
            <version>${jetty.version}</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>
    <build>
        <resources>
            <resource>
                <filtering>false</filtering>
                <directory>src/main/resources</directory>
            </resource>
            <resource>
                <filtering>false</filtering>
                <directory>src/main/java</directory>
                <includes>
                    <include>**</include>
                </includes>
                <excludes>
                    <exclude>**/*.java</exclude>
                </excludes>
            </resource>
        </resources>
        <testResources>
            <testResource>
                <filtering>false</filtering>
                <directory>src/test/resources</directory>
            </testResource>
            <testResource>
                <filtering>false</filtering>
                <directory>src/test/java</directory>
                <includes>
                    <include>**</include>
                </includes>
                <excludes>
                    <exclude>**/*.java</exclude>
                </excludes>
            </testResource>
        </testResources>
        <plugins>
            <plugin>
                <inherited>true</inherited>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.5.1</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                    <encoding>UTF-8</encoding>
                    <showWarnings>true</showWarnings>
                    <showDeprecation>true</showDeprecation>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.mortbay.jetty</groupId>
                <artifactId>jetty-maven-plugin</artifactId>
                <version>${jetty.version}</version>
                <configuration>
                    <scanIntervalSeconds>1</scanIntervalSeconds>
                    <useTestClasspath>true</useTestClasspath>
                    <connectors>
                        <connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">
                            <port>8080</port>
                            <maxIdleTime>3600000</maxIdleTime>
                        </connector>
                    </connectors>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

The differences I made are the following.

1. Downgrade the Jetty Maven Plugin to 7.5.0.v20110901

<jetty.version>7.5.0.v20110901</jetty.version>

The new jetty plugin are much slower than the old one and you want miss anything important.

2. Add Wicket Datetime Dependency

<dependency>
    <groupid>org.apache.wicket</groupId>
    <artifactid>wicket-datetime</artifactId>
    <version>${wicket.version}</version>
</dependency>

Wicket provides a good datetime picker and I recommend that you use this.

3. Configure Jetty Maven Plugin for Autodeploy

<scanintervalseconds>1</scanIntervalSeconds>

The jetty plugin can automatically be configure to pick up any changes made within Eclipse and automatically redeploy your application. This is a great way to develop and I recommend it.

<usetestclasspath>true</useTestClasspath>

You also might want to mock any possible server facade class, which is located in your test directory. To enable that add your web test classes to jetty classpath.

November 13, 2012

Where is Additional Driver in Ubuntu 12.10?



Yesterday I had problems with my wireless network card and then looked after the Additional Driver that existed in previous version of Ubuntu and it has been moved. To get it open Software Updated from Unity.


Click Settings... and the Additional Drivers tab.


Broadcom Wireless Network Card not working after updating Ubuntu 12.10

Yesterday I updated my HP ProBook 6550 with Broadcom 4313 with my fresh installed Ubuntu 12.10 and suddenly my wireless network card stopped working. I google and found the following article http://www.howopensource.com/2012/10/install-broadcom-b43-legacy-wireless-driver-in-ubuntu-12-10-12-04/. But that did not helped, until I also installed

$ sudo apt-get install broadcom-sta-common

System Development Environment on Ubunut 12.10

With the latest Ubuntu release most of the tool a Java developer needs comes packaged in official release packages.

Open JDK 7

The open JDK 7 is mature and ready for production. This since the latter version of Open JDK 6. The reason for this is that now Oracle really develop the next version of Oracle JDK version is done in the Open JDK version. Previously when Sun owned the Java SDK they merely dumped source code in the Open JDK repository, but Open JDK is really a upstream project.

$ sudo apt-get install openjdk-7-jdk

Maven 3

$ sudo apt-get install maven

Subversion 1.7

Please note that there is a big difference between svn 1.6 and 1.7, where 1.6 was the official package version in Ubuntu 12.04.
$ sudo apt-get install subversion

Subversion Client

If you have previously used RabidSVN, you should consider switching to KDESVN
$ sudo apt-get install kdesvn 

Best Practice Apache Wicket

There are some guidelines written about the best way how to use Apache Wicket, but I think I might have some more to contribute to all the things that has already been written.

1. And maybe the most important is always use Wicket Models.

First Wicket is a component based framework, which in itself implies you should always use Wicket Models.

Secondly wicket is also a framework that out of the box handles the back button dilemma, which is what should happen when the user press the back button in the web browser? Wicket handles this by versioning every page. Which means that if a user have in entered some data in a HTML form, then submitted and thereafter presses back button, Wicket will then out of the box present the values the way it was before pressing submit AND keeping you server Model in synch. You can get a good feeling of the above by playing around with the form example at http://www.wicket-library.com/wicket-examples/forminput/.

To achieve this Wicket uses the HTTP Session to store the previously pages states. To not now blow the RAM space on the server, you should almost always use the LoadableDetachableModel.

Be sure you have completely have understood the antipatterns at https://cwiki.apache.org/WICKET/best-practices-and-gotchas.html#BestPracticesandGotchas-Antipatterns.

The second and maybe finally most important Model is the CompoundPropertyModel. And remember that you can wrap Model. Example of that is:
class PersonList extends ListView<Person> {

    private static final long serialVersionUID = 1L;

    public PersonList(final String id, final IModel<List<Person>> model) {
        super(id, model);
    }

    @Override
    protected void populateItem(final ListItem<Person> item) {
        item.setModel(new CompoundPropertyModel<Person>(item.getModel()));
        item.add(new Label("id"));
        item.add(new Label("name"));
        // ...
    }
}

2. To keep wicket code more readable, avoid using anonymous inner classes.

Wicket as well as Swing code can blow up in your face when it comes to anonymous inner classes. Instead write separate classes in the same WebPage class, like the above example. Which result in in a much more cleaner code.
public class PersonPage extends WebPage {

    private static final long serialVersionUID = 1L;

    public PersonPage(final PageParameters parameters) {
        super(parameters);
        IModel<List<Person>> persons = ...;
        add(new PersonList("personList", persons));
    }
}

3. Try to only use the PageParameters constructor

Example of that you can see in the above PersonPage example. The reason for that is now you page can always be bookmarkable. Which the default constructor also can be, but it is more generic to directly use the PageParameters constructor, in case you later need to use a page parameters.

 

4. Do not try to reinvent the wheel when it comes to graphical components.

Wicket comes with a rich set of ready to use graphical components. Be sure to import them in your pom file.
<dependency>
    <groupid>org.apache.wicket</groupId>
    <artifactid>wicket-core</artifactId>
    <version>${wicket.version}</version>
</dependency>

<dependency>
    <groupid>org.apache.wicket</groupId>
    <artifactid>wicket-extensions</artifactId>
    <version>${wicket.version}</version>
</dependency>

<dependency>
    <groupid>org.apache.wicket</groupId>
    <artifactid>wicket-datetime</artifactId>
    <version>${wicket.version}</version>
</dependency>

If you need more have a look at http://wicket.visural.net/examples/, which have a nice WYSIWYG editor and a spinner.
Also since Wicket 6 is jQuery the backend library for Ajax. Look for example at the jQuery modal window at http://www.wicket-library.com/wicket-examples/ajax/modal-window.

 

5. Use Wicket XHTML namespace

See https://cwiki.apache.org/WICKET/wickets-xhtml-tags.html

 

6. Be sure you have configured Maven Jetty Plugin to restart whenever you have made code changes

<plugin>
    <groupid>org.mortbay.jetty</groupId>
    <artifactid>jetty-maven-plugin</artifactId>
    <version>${jetty.version}</version>
    <configuration>
        <scanintervalseconds>1</scanIntervalSeconds>
        <usetestclasspath>true</useTestClasspath>
        <connectors>
            <connector implementation="org.eclipse.jetty.server.nio.SelectChannelConnector">
                <port>8080</port>
                <maxidletime>3600000</maxIdleTime>
            </connector>
        </connectors>
    </configuration>
</plugin>

7. And remember you can always debug your Wicket code with a JUnit.

Wicket has a great support for module testing support. But these test cases can also be used for debugging. When generating a Wicket project with the archetype you get a single JUnit test case with that you can start Jetty and then simply add breakpoints and you can debug your code.

References: