Friday, September 23, 2011

W3Schools - Helpful for all web programmers





As everybody knows, web is something where people look for information. Well if you want to become a good web programmer, get started by referring to W3Schools. 


This is the best and a simple online web tutorial site, using which you would probably be able to design a good web User Interface. For beginners its a good site to start with. Simple presentation and precise explanation is given here. Not only for beginners, but also useful for any web programmer to design good web User interface.


It mainly deals with web tutorial on HTML 4.01, HTML 4, XML DOM, Javascript, HTML DOM, jQuery, SQL, CSS 1,2,3, PHP, XSLT, XPath, XSL-Fo, Color-Picker, HTML Color, CSS3 Browser Support. 


Just click "W3Schools" for more information.

Sunday, September 4, 2011

Commons-Logging Tutorial

1. Introduction

The Apache Commons Logging (JCL) provides a Log interface that is intended to be both light-weight and independent of numerous logging toolkits. It provides the middleware/tooling developer a simple logging abstraction, that allows the user (application developer) to plug in a specific logging implementation.

The Apache Commons Logging provides a Log interface with thin-wrapper implementations for other logging tools, including Log4J, Avalon LogKit, and JDK 1.4. The interface maps closely to Log4J and LogKit.

2. Configuration:

There are two base abstractions used by Commons-Logging: Log (the basic logger) and LogFactory (which knows how to create Log instances). UsingLogFactory implementations other than the default is a subject for advanced users only, so let's concentrate on configuring the default implementation.
The default LogFactory implementation uses the following discovery process to determine what type of Log implementation it should use (the process terminates when the first positive match - in order - is found):

  1. Look for a configuration attribute of this factory named org.apache.commons.logging.Log (for backwards compatibility to pre-1.0 versions of this API, an attribute org.apache.commons.logging.log is also consulted).
  2. Look for a system property named org.apache.commons.logging.Log (for backwards compatibility to pre-1.0 versions of this API, a system propertyorg.apache.commons.logging.log is also consulted).
  3. If the Log4J logging system is available in the application class path, use the corresponding wrapper class (Log4JLogger).
  4. If the application is executing on a JDK 1.4 system, use the corresponding wrapper class (Jdk14Logger).
  5. Fall back to the default simple logging wrapper (SimpleLog).

3. Developing:

To make use of Commons-Logging,include the following import statements:
import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory;
Note that some components using commons-logging may either extend Log, or provide a component-specific LogFactory implementation. Review the component documentation for guidelines on how commons-logging should be used in such components.
For each class definition, declare and initialize a log attribute as follows:

public class CLASS {    
private static Log log = LogFactory.getLog(CLASS.class);    ...  

Messages are logged to a logger, such as log by invoking a method corresponding to priority. The org.apache.commons.logging.Log interface defines the following methods for use in writing log/trace messages to the log:
log.fatal(Object message);
log.fatal(Object message, Throwable t);
log.error(Object message);
log.error(Object message, Throwable t);
log.warn(Object message);
log.warn(Object message, Throwable t);
log.info(Object message);
log.info(Object message, Throwable t);
log.debug(Object message);
log.debug(Object message, Throwable t);
log.trace(Object message);
log.trace(Object message, Throwable t);

Semantics for these methods are such that it is expected that the severity, from highest to lowest, of messages is ordered as above.
In addition to the logging methods, the following are provided for code guards:
log.isFatalEnabled(); 
log.isErrorEnabled(); 
log.isWarnEnabled(); 
log.isInfoEnabled(); 
log.isDebugEnabled(); 
log.isTraceEnabled();

General - Message Priorities/Levels

It is important to ensure that log message are appropriate in content and severity. The following guidelines are suggested:
  • fatal - Severe errors that cause premature termination. Expect these to be immediately visible on a status console. See also Internationalization.
  • error - Other runtime errors or unexpected conditions. Expect these to be immediately visible on a status console. See also Internationalization.
  • warn - Use of deprecated APIs, poor use of API, 'almost' errors, other runtime situations that are undesirable or unexpected, but not necessarily "wrong". Expect these to be immediately visible on a status console. See also Internationalization.
  • info - Interesting runtime events (startup/shutdown). Expect these to be immediately visible on a console, so be conservative and keep to a minimum. See also Internationalization.
  • debug - detailed information on flow of through the system. Expect these to be written to logs only.
  • trace - more detailed information. Expect these to be written to logs only.

4. Integration:

The minimum requirement to integrate with another logger is to provide an implementation of the org.apache.commons.logging.Log interface. In addition, an implementation of the org.apache.commons.logging.LogFactory interface can be provided to meet specific requirements for connecting to, or instantiating, a logger.

  • org.apache.commons.logging.Log

  • The default LogFactory provided by JCL can be configured to instantiate a specific implementation of the org.apache.commons.logging.Log interface by setting the property of the same name (org.apache.commons.logging.Log). This property can be specified as a system property, or in the commons-logging.properties file, which must exist in the CLASSPATH.
  • Default logger if not plugged

  • The Apache Commons Logging SPI uses the implementation of the org.apache.commons.logging.Log interface specified by the system propertyorg.apache.commons.logging.Log. If the property is not specified or the class is not available then the JCL provides access to a default logging toolkit by searching the CLASSPATH for the following toolkits, in order of preference:
    • Log4J
    • JDK 1.4
    • JCL SimpleLog
  • org.apache.commons.logging.LogFactory

If desired, the default implementation of the org.apache.commons.logging.LogFactory interface can be overridden, allowing the JDK 1.3 Service Provider discovery process to locate and create a LogFactory specific to the needs of the application. Review the Javadoc for the LogFactoryImpl.java for details.

5. Configuring logger implementation:

As Log4J is the default logger, a few details are presented herein to get the developer/integrator going.Configure Log4J using system properties and/or a properties file:

  • log4j.configuration=log4j.properties
  • Use this system property to specify the name of a Log4J configuration file. If not specified, the default configuration file is log4j.properties.  
  • log4j.rootCategory=priority [, appender]*
  • Set the default (root) logger priority.  
  • log4j.logger.logger.name=priority
  • Set the priority for the named logger and all loggers hierarchically lower than, or below, the named logger. logger.name corresponds to the parameter of LogFactory.getLog(logger.name), used to create the logger instance. Priorities are: DEBUG, INFO, WARN, ERROR, or FATAL.Log4J understands hierarchical names, enabling control by package or high-level qualifiers: log4j.logger.org.apache.component=DEBUG will enable debug messages for all classes in both org.apache.component and org.apache.component.sub. Likewise, setting log4j.logger.org.apache.component=DEBUG will enable debug message for all 'component' classes, but not for other Jakarta projects.
  • log4j.appender.appender.Threshold=priority
Log4J appenders correspond to different output devices: console, files, sockets, and others. If appender's threshold is less than or equal to the message priority then the message is written by that appender. This allows different levels of detail to be appear at different log destinations.For example: one can capture DEBUG (and higher) level information in a logfile, while limiting console output to INFO (and higher).

6. Examples:

As it was explained in my previous blog post Log4j Tutorial, we'll use the same sample examples here to commons-logging work.

First download the, commons-logging-1.1.1-bin.zip
by clicking on the above link.

Keep the commons-logging jar in the classpath. I'll be using eclipse builder to try out the examples.

Example 1:

My example code for demonstrating commons logging is Try1.java. It is as shown.


Maintain the directory structure as shown, provided commons-logging and log4j jars are in the classpath.



The contents in the "commons-logging.properties" must be as follows.



The contents in the "log4j.properties" must be as follows.



The output will be as shown.


Example 2:

Now using "log4j.xml"

The content of code is almost the same. But we must specify the log4j configuration file as log4j.xml instead of log4j.properties. The log4j.xml is as shown.


The output will be the same as previous example.

NOTE: By default, the commons-logging jars(if they are in the classpath) look for the default logging being used. Suppose log4j jars are in classpath, then by default it'll set the log4j.configuration as log4j.properties or log4j.xml ( In other words, it;ll look for those two files). So if we have both log4j and commons-logging jars in classpath it is not mandatory to use the "commons-logging.properties" file. This scenario is taken up in the next example.

Example 3:

  1. Create a Project name "SampleLog" in eclipse.
  2. Create two packages. One is named as "com.zinnia.admin" & the other one is named as "com.zinnia.report".
  3. Create two java classes "SampleAdmin.java" & "SampleReport.java", each in one of the packages. The directory structure and the java class placement is as shown.
  4. We can see that log4j and commons-logging libraries are in the classpath along with log4j.xml. Also here there is no commons-logging.properties file. The code for SampleAdmin.java  & SampleReport.java is given below.



The log4j.xml is as follows.




Now when we run the "SampleAdmin.java" class, a directory named "logs" gets created. There will be 2 files inside this directory namely admin.log & report.log .




The contents in "admin.log" and "report.log" is as follows.


It can be concluded that commons-logging is just a wrapper over all different types of loggers like log4j, simplelog or default java logs etc.

For more information on Log4j and different functionalities used in this post like console appenders and file appenders can be referred in Log4j Tutorial .

Saturday, August 13, 2011

Log4j Tutorial


   Log4j is an open source project based on the work of many authors. It allows the developer to control which log statements are output with arbitrary granularity. It is fully configurable at runtime using external configuration files.

   Log4j is an open source project based on the work of many authors. It allows the developer to control which log statements are output with arbitrary granularity. It is fully configurable at runtime using external configuration files.

   The package is distributed under the Apache Software License, a fully-fledged open source license certified by the initiative. The latest log4j version, including full-source code, class files and documentation can be found at http://logging.apache.org/log4j/. By the way, log4j has been ported to the C, C++, C#, Perl, Python, Ruby, and Eiffel languages.


   Inserting log statements into code is a low-tech method for debugging it. It may also be the only way because debuggers are not always available or applicable. This is usually the case for multithreaded applications and distributed applications at large.

Lets get the ball rolling by getting an essence of writing logs in the code. I would explain logging in java codes.

Example 1:

//Try1.java
import org.apache.log4j.Logger;
import org.apache.log4j.BasicConfigurator;

public class Try1 {

        private static final Logger logger = Logger.getLogger(Try1.class);

        public static void main(String args[])
       {
                 BasicConfigurator.configure();
                 logger.info("What a beatiful day.");
                 logger.debug("Hello world.");
        }
}
The output for the above code would be

0 [main] INFO firstTry.Try1 - What a beatiful day.
0 [main] DEBUG firstTry.Try1 - Hello world.


Example 2:

Now logging using the "log4j.properties" file.

The "log4j.properties" file has the following content:

When you configure using the BasicConfigurator.configure() method by default it uses then ConsoleAppender and PatternLayout for all the loggers.

The following configuration creates the same result as the BasicConfigurator.configure()method.


log4j.rootLogger=DEBUG, CA
log4j.appender.CA=org.apache.log4j.ConsoleAppender log4j.appender.CA.layout=org.apache.log4j.PatternLayout
log4j.appender.CA.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n


//Try2.java
import org.apache.log4j.Logger;
import org.apache.log4j.PropertyConfigurator;

public class Try2 {

      static final Logger logger = Logger.getLogger(Try2.class);


      public static void main(String[] args) {

               PropertyConfigurator.configure("log4j.properties");
               logger.debug("Sample debug message");
               logger.info("Sample info message");
               logger.warn("Sample warn message");
               logger.fatal("Sample fatal message");
               logger.error("Sample error message");
      }
}


The output for the above code would be:

0 [main] INFO firstTry.Try2 - Sample info message
0 [main] WARN firstTry.Try2 - Sample warn message
16 [main] FATAL firstTry.Try2 - Sample fatal message
16 [main] ERROR firstTry.Try2 - Sample error message

NOTE: While executing the Try2.java make sure the log4j jars & log4j.properties file are in the classpath.

Instead of using "log4j.properties" file, its better to use "log4j.xml" file which also defines the log properties similar to log4j.properties.

Example 3:

The previous code tells the usage of log4j.properties. Now we will see how to use "log4j.xml"

log4j.xml is as follows,

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'>
   <appender name="CA" class="org.apache.log4j.ConsoleAppender">
      <layout class="org.apache.log4j.PatternLayout">
         <param name="ConversionPattern" value="%-4r [%t] %-5p %c %x - %m%n" />
      </layout>
   </appender>
   <root>
        <level value="DEBUG"/>
        <appender-ref ref="CA"/>
   </root>
</log4j:configuration>


The modified version of Try2.java is,

//Try2.java
import org.apache.log4j.Logger;
import org.apache.log4j.xml.DOMConfigurator;


public class Try2 {

      static final Logger logger = Logger.getLogger(Try2.class);


      public static void main(String[] args) {
               DOMConfigurator.configure("log4j.xml");
               logger.debug("Sample debug message");
               logger.info("Sample info message");
               logger.warn("Sample warn message");
               logger.fatal("Sample fatal message");
               logger.error("Sample error message");
     }
}


The output is the same as Example2

Example 4:

Now let us learn about writing logs into a file. It is called as file appender. A typical requirement in the project is to log different modules in different log files.

Suppose the log4j.xml has following contents:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'>
     <appender name="CA" class="org.apache.log4j.ConsoleAppender">
         <layout class="org.apache.log4j.PatternLayout">
             <param name="ConversionPattern" value="%-4r [%t] %-5p %c %x - %m%n" />
         </layout>
     </appender>
     <appender name="FA" class="org.apache.log4j.FileAppender">
          <param name="File" value="logs/sample.log"/>
          <param name="Threshold" value="WARN"/>
          <layout class="org.apache.log4j.PatternLayout">
               <param name="ConversionPattern" value="%d{dd MMM yyyy HH:mm:ss,SSS} [%t] %-5p %c       %x - %m%n" />
          </layout>
     </appender>
     <root>
          <level value="DEBUG" />
          <appender-ref ref="CA" />
          <appender-ref ref="FA" />
      </root>
</log4j:configuration>


Consider the same Try2.java now. The output on console would be:

0 [main] INFO firstTry.Try2 - Sample info message
0 [main] WARN firstTry.Try2 - Sample warn message
16 [main] FATAL firstTry.Try2 - Sample fatal message
16 [main] ERROR firstTry.Try2 - Sample error message


When the java code executes, it actually creates a directory as specified in "log4j.xml". In this example, directory named "logs" gets created. In that directory "sample.log" gets created, when the log file appender writes the logs according to programmer's requirement.

For the present example, we can see the sample.log file content as:

13 Aug 2011 18:32:27,640 [main] WARN firstTry.Try2 - Sample warn message
13 Aug 2011 18:32:27,656 [main] FATAL firstTry.Try2 - Sample fatal message
13 Aug 2011 18:32:27,656 [main] ERROR firstTry.Try2 - Sample error message


Example 5:

From now on, i would use explain examples using eclipse.

  1. Create a Project name "SampleLog" in eclipse.
  2. Create two packages. One is named as "com.zinnia.admin" & the other one is named as "com.zinnia.report".
  3. Create two java classes "SampleAdmin.java" & "SampleReport.java", each in one of the packages. The directory structure and the java class placement is as shown.
  4. The code for SampleAdmin.java  & SampleReport.java is given below.




5. The contents in log4j.xml is as shown:


Now run the application, we can find that a directory named "logs" gets created in the project.


The contents in the admin.log and report.log is as follows:


 Please try this out giving different combinations of info in log4j.xml. If there are any mistakes or errors in my explanations, just let me know.

Wednesday, July 20, 2011

iTunes Special for Windows x64

Latest release of iTunes 10.4 for Windows x64 machines is now available.
To download visit : Download Link

For download of 10.3.1 visit : Download Link 2

For x86 machines : Download Link 3 (External)

Tuesday, July 19, 2011

Apple Reviews iOS 5.0


Apple's iOS 5.0 has been reviewed by Apple and is said to feature more than 200 new and improved additions to what already is the world's leading and the most enunciated OS, the iOS.



The best of the latest features include:
1. Camera from Lock Screen
2. Compatibility with all latest iPhones and iPads
3. Airplay Mirroring for iPad 2
4. Red Eye Removal; .. and many more!!

More details, visit Apple's official website - Apple Reviews iOS 5