Logging using Log4j

Logging is an important of any application. There are several APIs available majorly used are:
1. Apache Log4J
2. Java Logging (java.util.logging)

Logging is required to monitor the state of the running application, debug the application, ignorant developers heavily relies on System.out.println(), which is quite expensive in terms of resources and you dont have any way to manage these messages.

To control this we have different API's like mentioned above.

Using Log4J from Apache:
Log4j consists of 3 aspects: logger, appender and, layout relations between three can be expressed as "logger logs to an appender using a layout"

Each class in an application can have seperate logger or a common logger, now this logger should know where to send request for logging. This where are known as appenders which can be:
- FileAppender (flat file)
- JDBCAppender (database )
- Console Appender (Console)
- SMTPAppender (Email)
- JMSAppender (to remote JMS servers)
- SocketAppender (to remote server).
there are few more.

Now we need to decide what and how we want the out out of that log, this is decided by layout

Each class can have a logger as mentioned above similarly each logger should have log level. There are five different log levels (ordered):
- DEBUG
- INFO
- WARN
- ERROR
- FATAL

To configure a class with a logger, appender, layout and log level we need to use an external configuration file (log4j.properties is the default)

Sample configuratiion file is as:
--------------------------------
# Set root logger level to DEBUG and its only appender to CONSOLE.
log4j.rootLogger=INFO, CONSOLE

# A1 is set to be a ConsoleAppender.
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender

# A1 uses PatternLayout.
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n

# Change the level of messages for various packages.
log4j.logger.com.tj.cand.controller=WARN
log4j.logger.com.tj.cand.domain=WARN
log4j.logger.com.tj.cand.dao=DEBUG
--------------------------------

Usage:

Logger log = Logger.getLogger(MyController.class);
log.info("This is a logging message);




Why Spring Framework

Last year I had to shift my web application to some latest framework.
I decided to go for Spring Framework, EJB3 was also an option.

The reason why Spring was:

1. Spring was a well established Open Source
2. Spring provides all the configurations into XML where as EJB3.0 relies in annotations, in my scenario I am using WebSphere 6.1 which runs on JDK1.4 which doesn't support annotations.

3. Spring is a loosely coupled Framework You can decide your own stack. Whereas EJB is whole integrated package.

4. One major reason was You are not forced to use any Spring imports or rely on Spring classes. Dependency Injection combined with external meta-data helps to achieve this independence. So you can easily migrate. While in case of EJB 3.0 this is different.

5. One disadvantage of using annotations based EJB 3.0 is you need to change java source to incorporate any configurations whereas in case of spring you need to change XML file.

XML binding using JiBX

JiBX is a XML binding framework which binds XML to your java class.
XML binding is a 2 phase process:
1. JiBX uses binding definition to define the rule how conversion wil happen and modifies byte code of the class.
2. In phase 2 JiBX runtime does marshalling and un-marshalling of java objects to populate XML to java and java 2 XML.

Algorithm
  • Add following jar files to your lib directory from JiBX distribution
    1. bcel.jar (Byte Code Engineering Library used to change bytecode for the class)
    2. jibx-bind.jar (1,2 are used in phase I)
    3. jibx-run.jar
    4. jibx-extras.jar
    5. xpp3.jar (3,4,5 are used in phase II)
  • Modify java class using binding compiler (java -jar ../lib/jibx-bind.jar binding.xml)
  • Write code for marshalling and unmarshalling of java object

Example:



public void XML2Java2XML() {
try {
IBindingFactory bfact = BindingDirectory.getFactory(Customer.class);
IUnmarshallingContext uctx = bfact.createUnmarshallingContext();

Object obj = uctx.unmarshalDocument
(new FileInputStream("customer.xml"), null);
Customer customer = (Customer)obj;
System.out.print(customer.toString());

IMarshallingContext mctx = bfact.createMarshallingContext();
mctx.setIndent(4);
mctx.marshalDocument(obj, "UTF-8", null,
new FileOutputStream("customer2.xml"));

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (JiBXException e) {
e.printStackTrace();
}
}

Password Strength Meter

Almost every web site provides password strength meter.
To make a strong password it should contain:
  • Sufficient length
  • Mixed case
  • Combination of numbers and Special characters

My password strength meter works on same line:

If password is of minimum length and same case it is a weak passwd

If password is min length mixed case and number normal passwd

if password is min length mixed case number and special char it is medium password

if password is above min length and combination of number and special char, it is strong passwd