Pages

Friday, January 22, 2010

JSF PhaseListener Example

I have been reading a book called JSF 1.2 Components from Packt Publishing to do a review. While reading the book, I thought about the numerous code examples that I have written over the years. I decided to publish this example based on JSF 1.2 and Facelets 1.1.14 using NetBeans 6.7.1.

This example demonstrates the use of a PhaseListener which is set in the web.xml file. It also includes a component tree renderer which is also a PhaseListener. Facelets includes a component renderer for debugging purposes. You must include the <ui:debug/> tag in your xhtml file to use it.

Note: The component renderer for debugging components has a Javascript error which was introduced in Facelets 1.1.12 through 1.1.14 which will not render the Component tree. You must use either Facelets 1.1.11 or 1.1.15.B1 to have the component tree render.

Here is the sample code on Bitbucket: JSFPhaseListeners

Monday, January 18, 2010

Apache Commons Configuration Example

The other night at the Greenville Java Users Group meeting I asked the members of the JUG to join an open source project and make a difference. I told them that you do not need to necessarily contribute code, but providing documentation assistance would make a difference.

In the same meeting I asked how many people could tell me about any of the Apache Commons projects. I was surprised at how many people could only list a couple. No one had heard of the Apache Commons Configuration project. I use the project to load properties into some of my projects. I was surprised that so many people "roll their own" around properties files.

I decided to make an Apache Maven project which demonstrates the power and simplicity of the framework.

Here is the sample code: apache-commons-configuration-example.zip

/*
 * Copyright 2010 Blue Lotus Software, LLC.
 * Copyright 2010 John Yeary <jyeary@bluelotussoftware.com>.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
/*
 * $Id: Application.java 168 2010-01-19 03:09:36Z jyeary $
 */
package com.bluelotussoftware.example.apache.commons;

import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;

/**
 * <p>Example application to demonstrate the Apache Commons
 * Configuration framework.</p>
 * @author John Yeary <jyeary@bluelotussoftware.com>
 * @version 1.0-SNAPSHOT
 */
public class Application {

    private static final String PROPERTIES_FILE_NAME = "configuration.properties";

    /**
     * <p>Default no-arg constructor.</p>
     */
    public Application() {
    }

    /**
     * <p>Main application entry point.</p>
     * @param args
     * @throws ConfigurationException
     */
    public static void main(String[] args) throws ConfigurationException {
        Application application = new Application();
        application.createProperties();
        application.setBackground("#c53478");
        String background = application.getBackground();
        System.out.println("background-color:" + background);
    }

    /**
     * <p>Creates a property file on the local system disk.</p>
     * @throws ConfigurationException if an <code>Exception</code> is encountered.
     */
    public void createProperties() throws ConfigurationException {
        // Create a new property and save it.
        PropertiesConfiguration config = new PropertiesConfiguration();
        config.save(PROPERTIES_FILE_NAME);
    }

    /**
     * <p>Sets a property called <code>colors.background</code> to the value provided, or assigns a color
     * value{@literal #000000} if none is provided .</p>
     * @param backgroundColor
     * @throws ConfigurationException if an <code>Exception</code> is encountered. 
     */
    public void setBackground(String backgroundColor) throws ConfigurationException {
        PropertiesConfiguration config = new PropertiesConfiguration();
        config.load(PROPERTIES_FILE_NAME);
        if (backgroundColor == null) {
            config.setProperty("colors.background", "#000000");
        } else {
            config.setProperty("colors.background", backgroundColor);
        }
        config.save(PROPERTIES_FILE_NAME);
    }

    /**
     * <p>Retrieves the background property from the properties file on
     * the system disk.</p>
     * @return the background color assigned in the properties file.
     * @throws ConfigurationException if an <code>Exception</code> is encountered.
     */
    public String getBackground() throws ConfigurationException {
        PropertiesConfiguration config = new PropertiesConfiguration();
        config.load(PROPERTIES_FILE_NAME);
        String background = (String) config.getProperty("colors.background");
        return background;
    }
}

Saturday, January 16, 2010

NetBeans JSF Converter Template

Object conversion is a common requirement in JSF. Surprisingly there is no template for a JSF Converter in the NetBeans Template list. I have created a template to create JSF Converters in NetBeans.


To use the template is very simple:

  1. Download the template.
  2. Start NetBeans.
  3. Go to Tools --> Templates.
  4. Select the JavaServer Faces folder.
  5. Select Add from the menu.
  6. Locate the file you downloaded and select it, and you are done.

Here is the file: Converter.zip


<#assign licenseFirst = "/*">
<#assign licensePrefix = " * ">
<#assign licenseLast = " */">
<#include "../Licenses/license-${project.license}.txt">

<#if package?? && package !="">
package ${package};
</#if>

import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;
/**
 *
 * @author ${user}
 */

public class ${name} implements Converter{

    public Object getAsObject(FacesContext facesContext, UIComponent component, String value) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

public String getAsString(FacesContext facesContext, UIComponent component, Object value) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}

Monday, January 11, 2010

Book Review: Maven By Example

I received a copy of 0.2.1 of the Maven By Example book from Tim O'Brien from Sonatype. I was in the process of learning Maven and moving some of my projects to Maven when Tim contacted me and offered the book to me. He also included a number of copies to give away at my local JUG.

This turned out to be a really good book on learning Maven. It is a Creative Commons licensed book so that it offers the community a chance to update the book and add content. Like any great open source project, giving the community to have a direct chance to update the project produces a better final product.

The most important chapter in the book is chapter 7 which brings together the components of the book into a multi-module enterprise project. The code for the chapter really shows how the components fit together. However, this chapter is lacking a lot of material and does not match the code in the example code in the download.

The chapter needs additional work to explain how to do the project. It really should hand-hold the reader through the chapter, and allow them to use the code in the book to create a project from scratch. I had to download the code to see why examples would not work in the book.

I don't want the previous remarks to seem to critical. They are not meant to be. The book is really well done, and I am glad that I went through it thoroughly. It will serve as a good reference going forward.

You can get a PDF version of the book from here: Maven By Example