Monday, 28 October 2013

STRUTS 1.3

Struts 1 is a MVC frame work to develop web applications it is one of the most successful frame works for web applications though industry is no more using this frame work still there are many large scale applications which were developed using this frame work and organisations are maintaining those applications.
                                                           To understand this frame work first you have to understand on what api and on which technology its designed on. This is a very simple frame work which utilizes jsp and servlet api .Here in the applications developed using this frame work every user request passes through a common place which will be nothing but a central servlet (ActionServet) and accordingly the control will be passed to appropriate user defined actions and request will be processed and results will be generated to end users.
This simple frame work consists of forms,actions,messages and configurations files.

Configuring eclipse for struts 1 applications development:
                                  1)Download struts 1 bundle from the apache struts website or you can also download it from here http://struts.apache.org/download.cgi#struts1310
                                  2)Unzip the bundle in your local system and look into the lib folder you will find all the required jars for the frame work.
                                  3)Open you eclipse IDE and start a new dynamic web project, righ click on the project select properties, select java build path and from right hand side select add library select user library and then select user libraries from the pop up window select new and name the library and add external jars
and select all the required jars of struts from the lib folder of the unzipped struts bundle folder.
                                  4)Select Deployment Assembly from the properties and select add and click java build path entries and pick the struts 1 user library from the list of libraries.
Now your web application is struts 1 enabled and lets start developing a simple struts1 based web application.

Thursday, 31 January 2013

Log4j with Ant:
                                         Now lets use log4j with ant to log messages from source code to log files, before that i suggest you to download the log4j binary from logging.apache.org/log4j/ it usually comes in .tar and .zip formats choose .zip format unzip, extract it's contents to some drive. Open your eclipse IDE create new project, select java project, now create a file and specify it's extension as .log for example(logging.log) and save it in src folder. create log4.properties file save it in src folder along with logging.log(empty file) file.import all the jars from the lib directory of ant extracted path and log4j.jar from ant extracted path .
Lets code now

logging.log
its an empty file

log4j.properties
log=src
log4j.rootLogger=DEBUG,INFO,rollingFile
log4j.appender.rollingFile.=org.apache.log4j.RollingFileAppender
log4j.appender.rollingFile.File=${log}/logging.log
log4j.appender.rollingFile.MaxFileSize=2MB
log4j.appender.rolingFile.MaxBackupIndex=2
log4j.appender.rollingFile.layout=org.apache.log4j.PatternLayout
log4j.appender.rollingFile.layout.conversionPattern=%d%m%n

package com.ant.log4j.tests;
public class Ant_Log_Test_Class
{
private static Logger logger=Logger.getLogger(Ant_Log_Test_Class.class);
public static void main(String...s)
{
BasicConfigurator.configure();
logger.debug("test debug message !");
logger.info("test info message !");
}
};

build.xml
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project name="ant_log_project" basedir="." default="main">
<property name="src.dir" value="src"/>
<property name="bin" value="bin"/>
<property name="build.dir" value="${bin.dir}/build"/>
<property name="classes.dir" value="${build.dir}/classes"/>
<property name="jar.dir" value="${build.dir}/jars"/>
<property name="main-class" value="com.ant.log.tests.Ant_Log_Test_Class"/>
<path id="supportjarsid">
<pathelement location="bin" />
<pathelement location="fully qualified name of the log4j.jar  with .jar extension"/>
</path>
<target name="clean">
<delete dir="${bin.dir}/>
</target>
<target name="compile">
<mkdir dir="${classes.dir}" />
<javac srcdir="${src.dir}" destdir="${classes.dir}" classpathref="supportjarsid" includeantruntime="false" />
</target>
<target name="jar" depends="compile">
<mkdir dir="${jar.dir}" />
<jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${classes.dir}">
<manifest>
<attribute name="Main-Class"  value="${main-class}""/>
</manifest>
</jar>
</target>
<target name="run" depends="jar">
<java classname="${main-class}" fork="true">
<classpath >
<path refid="supportjarsid"/>
<path location="${jar.dir}/${ant.project.name}.jar"/>
</classpath>
</java>
</target>
<target name="clean-build" depends="clean,jar"/>
<target name="main" depends="clean,run"/>
</project>

........right click on this build.xml file select  run-as from run as select 2.ant-build an external tool will pop out select the choice of target and run, check the logging.log file you will find the messages !bingo
ANT (build automation tool):
                                                                          Before we put our fingers on key board lets discuss about what the heck is this thing ANT ennnnn !, Being a java developer managing vast application life cycle is a devastating job ie.,(including support jars,class path entries,compiling,testing,debugging,packaging,executing), to wipe out the overhead one can use build automation tools and ant is one such tool.
so lets do it. things that are to be done before using this tool are downloading it from ant.apache.org/ it comes in various packaged formats choose the .zip binary, extract it to your disk, set environment variables
so that it's visible to the system.
i.e., if you are using windows boxes perform this. Go to command prompt and type
                            :/>set ANT_HOME=full path of the .zip extracted location
                            :/>set JAVA_HOME=full path of the jdk location
                            :/>set PATH=%ANT_HOME%\bin
Once you are done with this open your text editor and put your fingers nailed to the keyboard.

To perform the automation tasks with the developed application will have to create build.xml file or generate it unless you are using some kind of IDE. Basically the thing what happens is every thing lies in this xml file.
This file consists of some set of xml tags i.e.,(project,target,property,path,classpath,copy,....on and on and on).
The basic skeleton of build.xml file will be as follows
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project name="some project name" basedir="." default="some target name">
   <target name="some target name">
   .
   .
   .
   some more xml tags
   .
   .

</project>

save the file in the root path of the application and from the cmd prompt invoke appropriate targets.

Simple ANT application:

Lets type our first ant application the hierarchy of the application would be some thing like shown below

Ant_Test_Application
 |
 src
    |
    test
          |
          geeks
                  |
                  code
                         |
                         First_Ant_Class.java
 bin
 |
 build
       |
       classes
                 |
                 First_Ant_Class.class
jars
     |
     Ant_Test_Application.jar
build.xml

package test.geeks.code;
public class First_Ant_Class
{
public static void main(String...s)
{
System.out.println("HELLO ANT APPLICATION BEGINNERS  !");
}
};
build.xml
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<project name="Ant_Test_Application" basedir="." default="main">
<property name="src.dir" value="src"/>
<property name="bin.dir" value="bin"/>
<property name="build.dir" value="${bin.dir}/build"/>
<property name="classes.dir" value="${build.dir}/classes"/>
<property name="jar.dir" value="${build.dir}/jars"/>
<property name="main-class" value="test.geeks.code.First_Ant_Class"/>
<target name="clean">
<delete dir="${bin.dir}"/>
</target>
<target name="compile">
<mkdir dir="${classes.dir}"/>
<javac srcdir="${src.dri}" destdir="${classes.dir}"/>
</target>
<target name="jar" depends="compile">
<mkdir dir="${jar.dir}" />
<jar destfile="${jar.dir}/${ant.project.name}.jar" basedir="${classes.dir}">
<manifest>
<attribute name="Main-Class"  value="${main-class}"/>
</manifest>
</jar>
</target>
<target name="run" depends="jar"/>
<java jar="${jar.dir}/${ant.project.name}.jar" fork="false"/>
</target>
<target name="clean-build" depends="clean,jar"/>
<target name="main" depends="clean,jar,run"/>
</project>

save First_Ant_Class.java,build.xml into src and root directories respectively.,
now open the cmd prompt and go to the root directory of  the application where build.xml is visible and type appropriate suffix target names to ant command
i.e.,
L:\anttests\somefolders\Ant_Test_Application\:>ant compile
it gives you a meaningful message for appropriate targets and finally BUILD SUCCESS if every thing is fine if not BUILD FAILED.
instead of go for step by step targets you can just type
L:\anttests\somefolders\Ant_Test_Application\:>ant
it's equivalent to ant compile jar run since we have mentioned the default attribute to main target which indeed is dependent upon jar,jar and run targets.

so thanks for now for going through the article ! soon i'll be publishing the subsequent tuts .