David R. Heffelfinger

  Ensode Technology, LLC

 

Maven pom.xml configuration for Servlet 3.0


Good old servlets are getting a facelift in the next major version of Java EE after been somewhat neglected for so long.

One of the major changes of the new Servlet 3.0 API is that a web.xml is no longer required. Instead, servlets can be configured directly in the source code via annotations.

I wanted to try out this new feature, deploying in GlassFish 3 preview, which is the only Java EE 6 compliant application server at the moment.

As usual, I created a Maven project to test out this functionality, unfortunately, nobody has told Maven that a web.xml is no longer required. My build was failing with Maven complaining about the lack of a web.xml.

After some research, I found out how to configure Maven to avoid it complaining about this non issue.

Here is my pom.xml in it's entirety.

<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>net.ensode.glassfishbook</groupId>
<artifactId>simpleapp</artifactId>
<packaging>war</packaging>
<version>1.0</version>
<name>simpleapp</name>
<url>http://maven.apache.org</url>
<build>
<finalName>simpleapp</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1-beta-1</version>
<configuration>
<failOnMissingWebXml>false</failOnMissingWebXml>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.6</source>
<target>1.6</target>
</configuration>
</plugin>
</plugins>
</build>
<repositories>
<repository>
<id>java.net</id>
<url>http://download.java.net/maven/2</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>javax</groupId>
<artifactId>javaee-api</artifactId>
<version>6.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>

Notice that I'm using the Maven war plugin version 2.1 (in beta at the moment), since previous versions of the plugin (like the default 2.0) do not support the <failOnMissingWebXml> tag yet.

 
 
 
 

JasperReports 3.5 For Java Developers Published


I'm proud to announce that the second edition of my JasperReports book, JasperReports 3.5 for Java Developers has been published.

The book has been updated to cover the latest features added to JasperReports since the first edition was published. Here is the table of contents:

Chapter 1: An Overview of JasperReports
Chapter 2: Adding Reporting Capabilities to our Java Applications
Chapter 3: Creating your First Report
Chapter 4: Creating Dynamic Reports from Databases
Chapter 5: Working with Other Datasources
Chapter 6: Report Layout and Design
Chapter 7: Adding Charts and Graphics to Reports
Chapter 8: Other JasperReports Features
Chapter 9: Exporting to Other Formats
Chapter 10: Graphical Report Design with iReport
Chapter 11: Integrating JasperReports with Other Frameworks

The full table of contents can be found here.

I also added JPA integration as part of the chapter on JasperReports integration with other frameworks.

An article based on the book can be found at the Packt Publishing web site, it covers the most common use for JasperReports, which is generating reports from database data.

Also, chapter 10, Graphical Report Design with iReport, is available for free, no registration required.


 
 
 
 

Java DB / Apache Derby: Insecure By Default


Both JDK 1.6 and GlassFish come bundled with Java DB, which is nothing but a rebranded Apache Derby RDBMS. If you have ever done anything with Derby, you know that a database can be created by simply by passing the create attribute with a value of true. For example, to create a database on a server called myserver.com, all that is needed is to use the following JDBC URL:

jdbc:derby://myserver.com:1527/mydb;user=foo;password=bar;create=true

The above JDBC URL will create a database called "mydb", and, in Derby/Java DB, the default schema matches the user name, so the above URL will create a schema called "foo" and set it as the default schema.

This is great in the sense that creating a database is very easy, but it is not so great in the sense that it allows anybody to create a database on the server and do whatever they want with it.

If this wasn't bad enough, by default passwords are ignored, therefore, with the default configuration in place, anybody can connect to the database as user "foo", for example, the following JDBC URL would connect you just fine:

 jdbc:derby://myserver.com:1527/mydb;user=foo;password=aaa

Notice that in this second JDBC URL, the password is different than the one we used when we created the database, it doesn't matter, in the default Derby configuration, the password is ignored, it is as if it didn't exist, therefore this URL would allow us to log in just fine.

Standalone Derby / Java DB only listens for connections from localhost, so in this case the default behavior is not too bad (there is still some security risk in a server with multiple users, however random people cannot connect through the network and create databases/log in to a database willy nilly).

However, GlassFish comes bundled with Java DB/Derby. When starting the database through GlassFish's asadmin utility:

asadmin start-database

The database by default listens accepts network connections. Unfortunately all other (lack of) security defaults stay in place, therefore with GlassFish's JavaDB's default configuration, random Joe's out there can connect to our databases without using a password, and can also create their own databases. This is a major security hole.

Luckily, Java DB/Derby can be easily configured to require valid user credentials, it is unfortunate that it is not configured this way by default. There are various ways to configure authentication in Derby/Java DB, it can be "hooked up" to an LDAP server, additionally, custom code can be written to handle authentication, and finally, a property file can be written set up authentication. The Derby Developer's Guide has all the details, in this entry, I'll just explain the simplest way, in case you are panicking because I just made you realized your database is exposed to the world.

The property file to create to enable authentication has to be called derby.properties it should look something like this:

derby.connection.requireAuthentication=true
derby.authentication.provider=BUILTIN

# Users definition
derby.user.someusername=password1
derby.user.john=doe

derby.connection.requireAuthentication=true tells Derby/Java DB that authentication is required.

derby.authentication.provider=BUILTIN sets up Derby to use its internal, built-in authentication mechanism.

After setting the above two properties, we need to add some users, they are added as shown in the above sample derby.properties file, properties preceeded by "derby.user." are understood to be users, the user name is the string immediately following "derby.user.", and the password is the value to the right of the equal sign. For example, if we wanted to add another user with a username of "joe", and a password of "secret", we would add the following line to derby.properties:

derby.user.joe=secret

Once we have created this file, we need to put it in $DERBY_HOME/bin if we are using the standalone Java DB/Derby or the Java DB version that is bundled with JDK 1.6. Java DB is placed under $JAVA_HOME/db in in Linux and under C:\Program Files\Sun\JavaDB in Windows.

If we are using the Java DB version that is included with GlassFish, then the derby.properties file needs to be placed under $GLASSFISH_HOME/databases, where $GLASSFISH_HOME is the directory where GlassFish is installed.

After copying the file to the appropriate location, the Java DB/Derby needs to be restarted for the changes to take effect.

 
 
 
 

A Slightly Biased Comparison Between Wicket and JSF


Recently I will start working on a project using Wicket. I used Wicket briefly in the past, I wrote an article about it and even worked as a technical reviewer for a book on Wicket, but I have never used it in production systems.

On the other hand, I have written two books that cover JSF, I also teach a class that covers JSF development, and I have worked on some projects that use JSF in production. As the author of two books that cover JSF, I have a vested interest in having JSF remain popular, therefore I am obviously somewhat biased, however I'll try to be as objective as possible when making this comparison.

All of my Wicket experience happened a few years ago, I haven't used it much in about three years, so for the past few days I've been refamiliarizing myself with this framework.

When comparing Wicket with plain vanilla JSF wicket is the clear winner. The fact that the markup in Wicket is plain HTML (with special wicket specific attributes) makes Wicket much more accessible, since no special knowledge of Wicket components is needed to come up with the markup. This allows web designers to do what they do best, design their user interfaces with their preferred tools such as Dreamweaver or other web design tools. The changes to the markup to make it work with Wicket are minimal, the wicket specific attributes need to be added to HTML tags that will be replaced at runtime with Wicket components. However, very few people use "vanilla" JSF. Most JSF developers use a component library such as ICEFaces or RichFaces. Furthermore, Facelets is a very popular view technology that can also use standard XHTML to develop JSF views. When component libraries and Facelets enter the picture, picking a winner is not so easy.

The fact that Wicket uses standard HTML for its markup may be an advantage or a disadvantage depending on your situation. If you are working with a professional web designer, then having straight HTML as markup is a real advantage. However, if your team consists primary of Java developers with little or no web design skills, then using a JSF component library that renders its pages using predefined CSS and Javascript, then in this case JSF may have the advantage.

Wicket reminds me a lot of working with Facelets, when using Facelets, we can develop pages that are plain XHTML, however we are not prohibited from using JSF specific tags. When previewing a Facelets page in the browser, the JSF specific tags are simply ignored.

Wicket also has some Wicket specific tags, however they are used to ignore parts of the HTML when it is rendered in the browser and other functionality, they are not really meant to be rendered. On the other hand, HTML tags in Wicket can have an wicket id corresponding to a component that renders HTML on the page, for example, a <span> tag could map to a custom Wicket component, therefore, a Wicket page may not preview exactly like it will be rendered when the application is deployed, which is the case with Facelets as well.

My impression on which one is better is, like in most cases, it depends. If a professional web designer is available, then Wicket may be the better choice, however if most of the development team are backend Java developers, then using JSF with Facelets plus a component library such as IceFaces or RichFaces may be a better choice.

 
 
 
 

Confessions of a Java Snob


For the past couple of months I've been working on porting a PHP application to Java EE using JSF, JPA and EJB 3 (in case you are wondering, yes, I've been using NetBeans and GlassFish).

I've never had any real exposure to PHP, so this is a new experience for me. I'm not sure if what I'm seeing is typical PHP code, but comparing this legacy system to the typical enterprise Java system shows some striking differences in architecture. When analyzing the PHP code, it became obvious to me that the mindset of PHP developers is very different from your typical Java developer.

In the Java world, we love our design patterns, we can't live without our DAO's and MVC. In PHP, it seems to be no big deal to mix presentation, business logic and data access in a single file.

Having worked with Java for over 13 years and Java EE/J2EE for about 10 years, I have to confess that the architecture (if you can call it that, more like "lack of architecture") seemed appalling to me. In the enterprise Java world, we've been conditioned to think that separation of concerns is a good thing.

Our presentation logic should contain presentation only, that way if in the future we want to switch say, from straight JSPs and servlets to JSF, the rest of the code shouldn't be affected. Additionally, if we want to convert our web application to a desktop application using Swing, it should be fairly straightforward to do so.

Data access logic should be done via Data Access Objects (DAO's), that way if today we are using straight JDBC and tomorrow we want to use an object relational mapping tool such as JPA or Hibernate, all we need to do is change the data access layer, the rest of the code should not be affected.

Communication between layers in our applications should be done via Value Objects, which shouldn't really change if we change our data access layer or presentation layer.

Controllers should manage flow from one page to another, again these should only be rewritten if we change our presentation layer. Most Java web application frameworks provide their own controllers, however they are not self sufficient, for example, in Struts we need to use Actions and in JSF we need to write managed beans, therefore changing the presentation layer would usually involve changing the controller as well.

After analyzing the code for the legacy system I got the impression that PHP is a language for amateurs and Java/J2EE/Java EE is for professional software engineers and architects. Am I right? Or I am just a Java snob? Feel free to set me straight.

 
 
 
 

Solving JasperReports Dependencies with Ivy


Lately, I've been doing some work with JasperReports. On my previous JasperReports projects, I've either used Maven, which automatically takes care of resolving dependencies, or I have simply downloaded the project's dependencies by hand.

Using Maven is nice, since it resolves dependencies, however, JasperReports comes with a series of useful ANT tasks to compile reports, preview them etc. I wanted access to these tasks and I also wanted dependency management.

There were a couple of ways I could achieve both. I know ANT targets can be called from Maven, this could be one approach. Also, there is a dependency manager for ANT called Ivy. I had briefly used Ivy before, I thought I would try this approach.

Ivy works by adding a series of custom ANT tasks, installing Ivy is very simple, all that needs to be done is to copy the Ivy jar file to ${ANT_HOME}/lib. Once Ivy is installed, custom Ivy tasks are available in our ANT build files.

Some additional configuration needs to be done in an Ivy specific XML file, named ivy.xml. In this file is where we actually specify our dependencies.

I set up my project to depend on JasperReports and tried to have Ivy automatically download all of JasperReports dependencies, unfortunately the build failed, complaining about some missing dependencies, specifically mondrian and commons-javaflow.



[ivy:retrieve] 		::::::::::::::::::::::::::::::::::::::::::::::
[ivy:retrieve] :: UNRESOLVED DEPENDENCIES ::
[ivy:retrieve] ::::::::::::::::::::::::::::::::::::::::::::::
[ivy:retrieve] :: commons-javaflow#commons-javaflow;20060411: not found
[ivy:retrieve] :: mondrian#mondrian;2.3.2.8944: not found
[ivy:retrieve] ::::::::::::::::::::::::::::::::::::::::::::::
[ivy:retrieve]
[ivy:retrieve] :: USE VERBOSE OR DEBUG MESSAGE LEVEL FOR MORE DETAILS


After some googling, some head scratching (and some hair pulling and banging on the table for good measure), I found out what was going on.

JasperReports has some optional dependencies declared in its pom.xml, these dependencies are not downloaded by default when using Maven, however Ivy attempts to download them. For some reason these dependencies do not exist in the repository, because of this the ANT build fails.

After some research, I found out the necessary modifications to ivy.xml to make the build succeed:



<ivy-module version="2.0">
    <info organisation="ensode" module="mymodule"/>  
    <dependencies>
        <dependency org="jasperreports" name="jasperreports" rev="3.1.2" conf="*->default"/>
    </dependencies>
</ivy-module>

What I had to do was to  use the conf attribute of the <dependency> tag to specify that I wanted that configuration for this particular dependency. The *-> default means that all your module configurations depend on the 'default' configuration of JasperReports, as explained by Xavier Hanin in this message of the Ivy users mailing list.

After making this change, Ivy was able to successfully download all JasperReports dependencies.

[ivy:retrieve]     commons-beanutils#commons-beanutils;1.8.0 by [commons-beanutils#commons-beanutils;1.7.0] in [runtime]
    ---------------------------------------------------------------------
    |                  |            modules            ||   artifacts   |
    |       conf       | number| search|dwnlded|evicted|| number|dwnlded|
    ---------------------------------------------------------------------
    |      runtime     |   14  |   12  |   12  |   2   ||   11  |   11  |
    ---------------------------------------------------------------------

I figured I would document this procedure in case others are having similar issues.

 
 
 
 

Common JPA Questions



  1. How can I have a composite primary key in JPA?

    There are several ways to do it, this page explains them.

  2. How can I prevent users from overwriting each other's changes to the database?

    Use the @Version annotation.

  3. Is there a way to do bulk updates and/or deletes with the Java Persistence Query Language (JPQL)?

    Yes.

Upgraded to Roller 4.0.1


Since I just upgraded JavaDB, I figured this was a good time to upgrade to the latest version of roller as well. I was using roller 4.0, which is fairly up to date, but I figured it wouldn't hurt to upgrade to the latest, 4.0.1.

I simply downloaded roller 4.0.1, created a war file from the roller directory and deployed it to GlassFish, but for some reason the upgrade didn't go smoothly, I kept getting a "Roller Weblogger has not been bootstrapped yet" error.

I tried various ways of deploying, using asadmin, copying the war file to the autodeploy folder, etc, but I kept getting the same error. I restarted GlassFish several times, I restarted the database (JavaDB/Derby) several times, nothing seemed to solve the problem.

I then decided to reinstall roller 4.0 (thank goodness I made a backup), and it came back up successfully. After doing this, I then redeployed roller 4.0.1 using a different temporary context root, and this time, it installed successfully, asking me to upgrade the database. I did upgrade the database (the only thing that needs to be done when going from roller 4.0 to roller 4.0.1 is change the version number), and I had both roller 4.0 and 4.01 running in parallel with different context roots.

I then undeployed roller 4.0, and changed the context root of 4.0.1 to match the one I had in 4.0 (/roller), I am now in business.

Why wouldn't roller 4.0.1 just install over 4.0 is anyone's guess, however I was glad I was able to work around the issue.

 
 
 
 

Java EE 5 Reference Material




My Books

Two of my books cover Java EE 5, one focuses on GlassFish deployment, the other focuses on developing using NetBeans.

  • Java EE 5 Development using GlassFish Application Server - free chapter on JSF
  • Java EE 5 Development with NetBeans 6 - free chapter on JSF
  • Java EE 

    The Java EE 5 Tutorial covers most aspects of Java EE 5. Examples can be downloaded as NetBeans projects.

  • Java EE 5 Tutorial
  • Java EE 6 (JSF 2.0, EJB 3.1)
  • JSF

    JavaServer Faces is the standard component framework for Java EE 5.

  • JSF HTML Tag reference
  • JSF Custom Conversion and Validation
  • JSF Phase Listeners
  • Facelets
  • JSF Component Library Matrix
  • JSF Localization
  • JPA

    The Java Persistence API is the standard Object Relational Mapping (ORM) tool for Java EE 5. It takes the best ideas from Hibernate and other ORM tools and incorporates them into the standard.

  • JPA Composite Primary Keys
  • JPA Optimistic Locking using @Version annotation
  • JPQL Reference

  •  
     
     
     

    OpenPortal First Impressions


    I recently got involved in a project that may need some portlet development. The project is a completely new application to be built from the ground up.

    The customer has been using GlassFish as their application server and they are very pleased with it, therefore it just makes sense to continue using it.

    The OpenPortal Portlet Container can be installed in GlassFish from its update tool, therefore it made sense to try it out.

    GlassFish Update Center

    Once installed, I also installed the NetBeans Portal Pack plugin from NetBeans 6.5 RC2, that can be installed directly from the NetBeans Plugin Manager.

    NetBeans Plugin Manager

    The plugin makes deploying portlets to OpenPortal a breeze, as can be seen in this flash demo.

    My main complaint? The default look and feel of the portlet is, let's just say, not the best looking.

    OpenPortal default look

    As far as I can tell the only way to change the default look is to hack the CSS and JSPs by hand. I noticed the icons to minimize a portlet, maximize it, edit it, etc had transparent backgrounds, therefore it gave me some hope that they should be able to be used when simply changing the background color of the portlet bar. I made a simple modification to the CSS to test this, unfortulately it seems like some of the pink background creeped into the icons.

    OpenPortal modified look

    Also, as far as I can tell, modifying the available tabs to match our application requirements will involve some customizations of the JSPs included with OpenPortal, there is no tool to create or modify tabs as necessary.

    Even with these disadvantages, I'm leaning towards using OpenPortal if we in fact turn out to need portlets for this project. The tight integration with GlassFish and NetBeans is a great advantage and I can live with having to do some CSS and JSP hacking to make it look the way we need it to look.

     
     
     
     

    JRE 6u12 to include Java Plugin for 64 bit Linux


    For the longest time, 64 bit versions of the Java Runtime Environment (JRE) and Java Development Kit (JDK) have not included a Java Plugin.

    This fact has let us 64 bit Linux users out in the cold when it comes to running Java applets in our operating system of choice. There are workarounds, but they involved downloading a 32 bit browser and a 32 bit JRE, a less than ideal solution.

    There has been a bug in Java's bug parade reporting this issue (submitted in 2003!), finally, today I got notice that this issue will finally be addressed.

    According to the comments in the bug report, JRE 6u12 will include a Java plugin for 64 bit Linux, freeing us from jumping through hoops to be able to execute Java applets under 64 bit Linux.

    JRE 6u12 is scheduled to be released early 2009.

    Installing the Java Plugin on Ubuntu 64 bit


    Recently I acquired a new client that has a lot of client side Java technology as part of their infrastructure. A lot of this infrastructure is in the form of Java applets.

    My primary operating system is Ubuntu Hardy 64 bit. Unfortunately there is still no Java plugin for the 64 bit Linux version of Firefox, therefore, I was finding myself having to boot to Windows to be able to use some of these utilities.

    Since I am much more comfortable in a Unix environment, I wanted to find a solution to run this software on my Linux laptop. I knew from past experience that a 32 bit version of Firefox can be installed under 64 bit Linux, and a 32 bit JRE can be installed as well, and the 32 bit firefox can be made to work with the 32 bit JVM.

    I searched around and found this thread in the Ubuntu forums, which provides a link to a script that will install the 32 version of Firefox, along with the 32 bit version of the Java plugin and other plugins such as Flash. I downloaded the script and ran it, but unfortunately Java applets were still not working, at least I had 32 bit Firefox in place.

    In order to get the Java plugin under Firefox 32, I had to download the 32 bit JRE from java.com, since Ubuntu is a deb based distribution (as opposed to RPM based), the right version to download is the self extracting file.

    After downloading the JRE, it needs to be installed, which can be done by simply executing the downloaded shell script as root. I installed mine under /opt.

    Once the JRE is installed, I had to cd to my home directory, then cd to .mozilla. At this point, I had to create a plugins subdirectory, and cd to it.

    Then I needed to create a symlink for the Java plugin in the downloaded JRE to the ~/.mozilla/plugins directory: ln -s /opt/jre32/jre1.6.0_10/plugin/i386/ns7/libjavaplugin_oji.so ~/.mozilla/plugins.

    At this point I started the 32 bit version of Firefox, navigated to a test page and verified that my Java plugin was working properly.

     
     
     
     

    Facelets To Be Default View Technology in JSF 2.0


    By default, JSF 1.2 uses JSP as its view technology, however JSF was designed to be very modular, allowing defaults to be swapped out for alternative technologies.

    Facelets, by Jacob Hookom, is an alternate view technology for JSF. The main advantage of Facelets is that pages are written using standard XHTML, which allows web designers to freely design the pages, using design tools such as Dreamweaver. Using Facelets allows a true separation of concerns, with web designers designing the user interface of the system, and Java developers implementing the functionality.

    Another advantage of Facelets is that it provides templating for JSF. Facelets templating is similar in functionality to tools such as Sitemesh and Apache Tiles. Using Facelets templates, common areas of all pages in the application, such as headers, footers and navigation menus, can be placed in in separate files, and they are "included" by all pages in the application. Templating allows us to make changes to these common areas in a single file, and they are "inherited" by all pages in the application, greatly simplifying maintenance.

    Until now, in order to take advantage of Facelets additional dependencies had to be added to our project, since Facelets was non standard. Well, according to Arun Gupta and Ed Burns, Facelets will become the default JSF technology as of JSF 2.0. This will be a great step in simplifying JSF application development.

    Arun Gupta has a very nice tutorial explaining how to get started developing JSF 2.0 applications under GlassFish today.

     
     
     
     

    Excel Unlock Utility


    After I implemented and deployed the PDF Unlocking Tool, I was fortunate to find out it became very popular. Apparently I had written and deployed a useful tool.

    It's popularity got me thinking, what else I could write that would be helpful to others? Then it hit me, just like PDF files are sometimes restricted, preventing users from copying and pasting their content, sometimes Excel spreadsheets contain locked, uneditable cells.

    I started looking around for Java libraries to manipulate Excel spreadsheets, and found JExcelAPI. I started researching this library and sure enough, I found a way to unlock cells in an Excel spreadsheet.

    Using this API, I developed an online utility to unlock cells in a spreadsheet, which can be found at http://ensode.net/xls-crack.jsf. It soon became a very popular destination on my site.

     
     
     
     
     

    « April 2024
    SunMonTueWedThuFriSat
     
    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
        
           
    Today

     
    © David R. Heffelfinger