2006/03/28

Simplify XML processing with VTD-XML

Summary
As the enabling technology for next-generation Web-based applications, XML is both easy and straightforward to learn and use. Unfortunately, the same thing cannot be said about the current state of XML processing: the Document Object Model and the Simple API for XML are both too slow, inefficient, and difficult to use. Providing a broadly useful and advanced option that goes beyond DOM and SAX, VTD-XML is the next-generation XML processing model that simplifies both XML programming and selection of an XML processing model. By highlighting some of the key technical strengths of VTD-XML using the latest performance benchmark numbers and code examples, this article demonstrates that VTD-XML has the potential to finally take the "monkey" off the back of enterprise architects constantly struggling with the decision of whether to use DOM or SAX.

http://www.javaworld.com/javaworld/jw-03-2006/jw-0327-simplify.html

2006/03/24

Log4j example

**here has four examples about how to utilize log4j.

the core of log4j is log4j.properties, we can do some configuration in this property files, including log message format, output file name, appender

setting and so on.

1. com.ptc.demo.log4j.module1.Module1Test: write log information to console
2. com.ptc.demo.log4j.module2.Module2Test: write log information to log files and rotate the log file by day
3. com.ptc.demo.log4j.module3.Module3Test: write log information to log files and rotate the log file by file size
4. com.ptc.demo.log4j.module4.Module4Test: write log information to log file (in HTML format)


---------------------
output message
---------------------
2006-03-24 13:50:03,015 DEBUG com.ptc.demo.log4j.module1.Module1Test.sortByAsc(Module1Test.java:26) - [ant, cat, dog, dophin, tiger, zebra]
2006-03-24 13:50:03,015 DEBUG com.ptc.demo.log4j.module1.Module1Test.sortByDesc(Module1Test.java:41) - [zebra, tiger, dophin, dog, cat, ant]



log4j.properties
=========================================================================
#module1 -- output to console
log4j.category.com.ptc.demo.log4j.module1=DEBUG,module1
log4j.appender.module1=org.apache.log4j.ConsoleAppender
log4j.appender.module1.layout=org.apache.log4j.PatternLayout
log4j.appender.module1.layout.ConversionPattern=%d %5p %l - %m%n


#module2 -- output to log and rotate by day
log4j.category.com.ptc.demo.log4j.module2=DEBUG,module2
log4j.appender.module2=org.apache.log4j.DailyRollingFileAppender
log4j.appender.module2.threshold=debug
log4j.appender.module2.File=C:\module2.log
log4j.appender.module2.DatePattern='.'yyyyMMdd
log4j.appender.module2.Append=true
log4j.appender.module2.layout=org.apache.log4j.PatternLayout
log4j.appender.module2.layout.ConversionPattern=%d %5p %l - %m%n


#module3 -- output to log and rotate by file size
log4j.category.com.ptc.demo.log4j.module3=DEBUG,module3
log4j.appender.module3=org.apache.log4j.RollingFileAppender
log4j.appender.module3.threshold=debug
log4j.appender.module3.File=C:\module3.log
#rotate size = 500 KB
log4j.appender.module3.MaxFileSize=500KB
#keep three backup files
log4j.appender.module3.MaxBackupIndex=3
log4j.appender.module3.layout=org.apache.log4j.PatternLayout
log4j.appender.module3.layout.ConversionPattern=%d %5p %l - %m%n


#module4 -- output to log and by HTML format
log4j.category.com.ptc.demo.log4j.module4=DEBUG,module4
log4j.appender.module4=org.apache.log4j.FileAppender
log4j.appender.module4.File=C:\module4.html
log4j.appender.module4.Append=true
log4j.appender.module4.layout=org.apache.log4j.HTMLLayout
=========================================================================


class file (each class file has the same content and only has different class name)
=========================================================================
package com.ptc.demo.log4j.module1;

import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.Collections;

import org.apache.log4j.Logger;

/**
*
* @author Albert
*
*/
public class Module1Test {

private static Logger logger = Logger.getLogger(Module1Test.class.getName());

/**
* sorting by ascending
* @param str
*/
public void sortByAsc(String str[]){
List list = Arrays.asList(str);
Collections.sort(list);
logger.debug(list);
}

/**
* sorting by descending
* @param str
*/
public void sortByDesc(String str[]){
List list = Arrays.asList(str);
Collections.sort(list, new Comparator(){
public int compare(Object obj1, Object obj2){
return ((String)obj2).compareTo((String)obj1);
}
}
);
logger.debug(list);
}

/**
* @param args
*/
public static void main(String[] args) {
Module1Test module1Test = new Module1Test();

String str[] = new String[]{"dog","ant","zebra","cat","tiger","dophin"};
module1Test.sortByAsc(str);
module1Test.sortByDesc(str);
}

}
=========================================================================

2006/03/23

Exception Handling

The following are some of the generally accepted principles of exception handling:

1. If you can’t handle an exception, don’t catch it.
2. If you catch an exception, don’t swallow it.
3. Catch an exception as close as possible to its source.
4. Log an exception where you catch it, unless you plan to rethrow it.
5. Structure your methods according to how fine-grained your exception handling must be.
6. Use as many typed exceptions as you need, particularly for application exceptions.


"Best Practices in EJB Exception Handling"
http://www-106.ibm.com/developerworks/java/library/j-ejbexcept.html

"Best Practices for Exception Handling"
http://www.onjava.com/pub/a/onjava/2003/11/19/exceptions.html

"Designing with Exceptions"
http://www.javaworld.com/jw-07-1998/jw-07-techniques.html

"Exceptions in Java"
http://www.javaworld.com/jw-07-1998/jw-07-exceptions.html

"Exceptional Practices, Part 1"
http://www.javaworld.com/javaworld/jw-08-2001/jw-0803-exceptions.html

"Exceptional Practices, Part 2"
http://www.javaworld.com/javaworld/jw-09-2001/jw-0914-exceptions.html

"When catching exceptions, don't cast your net too wide"
http://www.javaworld.com/javaworld/javatips/jw-javatip134.html

"Use nested exceptions"
http://www.javaworld.com/javaworld/javatips/jw-javatip91.html

"Beware the dangers of generic exceptions"
http://www.javaworld.com/javaworld/jw-10-2003/jw-1003-generics.html

[Info] Abator: code generator for iBATIS

Abator is a code generator for iBATIS. Abator will introspect a database table (or many tables) and will generate iBATIS artifacts that can be used to access the table(s). This lessens the initial nuisance of setting up objects and configuration files to interact with database tables. Abator seeks to make a major impact on the large percentage of database operations that are simple CRUD (Create, Retrieve, Update, Delete). You will still need to hand code SQL and objects for custom queries, or stored procedures.


http://ibatis.apache.org/abator.html

2006/03/14

Plug memory leaks in enterprise Java applications

Strategies for detecting and fixing enterprise memory leaks

Summary
Because Java uses automatic garbage collection, developers think Java programs are free from possible memory leaks. Although automatic garbage collection solves the main cause of memory leaks, they can remain in a Java program. Specifically, such memory leaks in complex multitiered applications can be extremely daunting to detect and plug. This article analyzes the main causes of memory leaks in Java Enterprise Edition (Java EE) applications, and suggests strategies for detecting them. (3,200 words; March 13, 2006)

By Ambily Pankajakshan

http://www.javaworld.com/javaworld/jw-03-2006/jw-0313-leak.html