Thursday, 6 November 2008

Programming Jakarta Struts By Chuck Cavaness - Final Part

Couple Of Days i started reading Programming Jakarta Struts By Chuck Cavaness.

Nice book written by Chuck Cavaness.

I wanted to share few quotations found from the remaining chapters from this book.

1) Implementing a business delegate clearly isolates and minimizes the dependencies between the web and application tiers.

2) Performing a JNDI lookup to obtain a home interface reference is an expensive (slow) operation. We couldn't do much about this overhead if we actually needed a new home reference for each request, but that's not the case. An EJB home is a factory object that is valid throughout the lifetime of the client application. There is no state in this object that prevents it from being used across requests or client threads. Our delegate would be significantly improved if the home reference it needed were cached within the web tier after being requested the first time.

Example:


import java.io.InputStream;
import java.io.IOException;
import java.util.*;
import javax.ejb.*;
import javax.naming.*;
import javax.rmi.PortableRemoteObject;

/**
* This class implements the EJBHomeFactory pattern. It performs JNDI
* lookups to locate EJB homes and caches the results for subsequent calls.
*/

public class EJBHomeFactory {
private Map homes;
private static EJBHomeFactory singleton;
private Context ctx;

private EJBHomeFactory( ) throws NamingException {
homes = Collections.synchronizedMap(new HashMap( ));
try {
// Load the properties file from the classpath root
InputStream inputStream = getClass( ).getResourceAsStream(
"/jndi.properties" );
if ( inputStream != null) {
Properties jndiParams = new Properties( );
jndiParams.load( inputStream );

Hashtable props = new Hashtable( );
props.put(Context.INITIAL_CONTEXT_FACTORY,
jndiParams.get(Context.INITIAL_CONTEXT_FACTORY));
props.put(Context.PROVIDER_URL, jndiParams.get(Context.PROVIDER_URL));
ctx = new InitialContext(props);
}
else {
// Use default provider
ctx = new InitialContext( );
}
} catch( IOException ex ){
// Use default provider
ctx = new InitialContext( );
}
}

/**
* Get the Singleton instance of the class.
*/

public static EJBHomeFactory getInstance( ) throws NamingException {
if (singleton == null) {
singleton = new EJBHomeFactory( );
}
return singleton;
}

/**
* Specify the JNDI name and class for the desired home interface.
*/

public EJBHome lookupHome(String jndiName, Class homeClass)
throws NamingException {
EJBHome home = (EJBHome)homes.get(homeClass);
if (home == null) {
home = (EJBHome)PortableRemoteObject.narrow(ctx.lookup(
jndiName), homeClass);
// Cache the home for repeated use
homes.put(homeClass, home);
}
return home;
}
}



3) The Tiles framework provides a templating mechanism that allows you to separate the responsibilities of layout from those of content. As with the templates described earlier in this chapter, you have the ability to establish a layout and dynamically insert the contents of your pages into that layout at runtime. This is a powerful mechanism if you need to customize your site based on such things as internationalization, user preferences, or just the look-and-feel changes that occur in every web application sooner or later. The Tiles framework provides the following features:


· Template capabilities


· Dynamic page construction and loading

· Screen definitions


· Support for tile and layout reuse

· Support for internationalization


· Support for multiple channels

4) A tile is an area or region within a web page. A page can consist of just one region or be broken up into several regions.

5) Although the Struts framework provides certain I18N capabilities that can be used with Tiles, Tiles also provides the ability to select a particular tile based on the user's locale. To support this feature in your application, you need to create a different Tiles definition file for each locale that you need to support. For example, if you need to support a set of definitions for the U.S. locale and a separate set for the German locale, you must create two separate definition files:

· tiles-tutorial-defs_en.xml

· tiles-tutorial-defs_de.xml

6) The Commons Logging package is an open source Logging library that allows developers to use a common logging API, while maintaining the freedom to use many different third-party logging implementations. The Commons Logging API insulates the application and protects it from becoming coupled to a specific logging implementation. The API provides a small set of Java classes and interfaces that an application imports and relies upon but that has no implicit dependencies on any one logging product.

The Logging library allows developers to declaratively configure the logging implementation; the library will dynamically discover which implementation is being used. An application that uses the Commons Logging API does not have to be modified when the logging implementation is changed. This is the greatest benefit of such a package.

7) The org.apache.log4j.Logger is the central class in the log4j toolkit. Other than configuration, most of the functionality is performed through this class. In earlier versions of the log4j project, the org.apache.log4j.Category class implemented this functionality. To promote backward compatibility, the Logger class extends the Category class. Although the methods in the Category class have not yet been deprecated, you should always go through the Logger class itself. Eventually, the Category class will be removed from the library.

8) Understanding the log4j Log Levels

A log message in log4j can be assigned one of five different levels or priorities. The levels allow you to set a threshold for a particular logger and filter out any log messages that don't reach the threshold for which the Logger is configured. The five logging levels are:

· DEBUG

· INFO

· WARN

· ERROR

· FATAL

9) Ant is a platform-independent build tool that can be configured to compile your Java source code files, build your deployment JAR and WAR files, unit-test your code, and create your project's JavaDoc documentation. It also has many other uses and can be expanded to perform new tasks of your own creation.

About the Author

Chuck Cavaness is a graduate from Georgia Tech with degrees in computer engineering and computer science, has built Java-based enterprise systems in the healthcare, banking, and B2B sectors. Working at an Internet company to design and develop software architecture, Chuck has spent many frustrating hours figuring out the dos and the don'ts of web applications. With each enterprise system he's developed, Chuck has learned several valuable lessons about building "real-world" web applications, information that he's made available to developers who haven't had the opportunity to work on large systems.

Chuck is the co-author of Special Edition Using Java 1.3 and Special Edition Using EJB 2.0, both available from QUE.

No comments: