RSS

Monthly Archives: April 2010

Introducing ANT

ANT is an acronym for Another Neat Tool

It is a build tool. It is used to automate complicated repetitive tasks like setting classpath, compiling the code or packing the compiled files and many more things which you can ever imagine. ANT is developed in JAVA so it is platform independent. Ant accepts instructions in the form of XML documents thus is extensible and easy to maintain. You don’t need any special editor for writing ANT files a normal text editor is enough.

ANT Installation:

If you are using some IDE like Eclipse then there is no need to download anything as most of the IDE come with ANT libraries.

If you are not using any IDE and doing all in notepad and you need to do these things:
Steps to set path cmd if you are not using any IDE:

  1. Download the files from http://jakarta.apache.org/ant/index.html and unzip them to some directory.
    In our case we are naming the folder as ant.
  2. Append /path/to/ant/bin to the PATH environment variable.
  3. Append the .jar files in /path/to/ant/lib/ to the CLASSPATH environment variable. Set JAVA_HOME to point to the location of the JDK installation on the machine that the software is being installed on. Append /path/to/jdk/lib/* to the CLASSPATH environment variable.

ANT file(build.xml) format:

ANT file is an XML file. As ANT is mostly used for building the project we name it as build.xml.
Here you can see a snapshot of the build file. It is written to complie the code present in src folder.

This is the declaration of the XML file with its version. All files must start by this.

This project element has three attributes.
1. name: Here you will write the name of your project for which you are writing this file.
2. default: This is the default target to be executed by ant file.
3. basedir: This is the base directory(root folder) of your project. Here . means current directory. If this is omitted the parent directory of the build file will be used.

This explanation is for you. DO IT YOURSELF.

This element allows you to write your own variable with their values. You can define as many properties as you need. This is a name-value pair where you can access the value of a property by writing it in $ {&ltproprtyNmae>}.
NOTE: THERE IS NO SPACE BETWEEN $ AND { }.

This point has two elements.
1. Target: This element can be treated as a function which has a set of instructions to do. It has an attribute name which is used to call the target of the specified name.
2. mkdir: This element is used to make directries. It has an attribute dir which takes the name of the directory to be made.

This target compiles the code. Here the depends attribute specifies that this target depends on init to be performed.javac uses the system java compliler if you have not given the classpath explicitly. So to run this you should have all the JAVA_HOME and PATH variables set.
srcdir is the source directory in which all the source file(.java) are present and destdir is the folder into which all the complied files(.class) will be created.

Writing Build Files for compiling the project:

Here we will write an ant file which will perform a clean build of the project. Below you can see snapshot of the build.xml file. In this project we have all the source files in src folder and we have only one file HelloWorld.java to be complied.

<?xml version="1.0" encoding="UTF-8"?>

<!--  Project name Declaration -->
<project name="HelloWorld" basedir="." default="compile">

	<!-- Setting the property here -->
	<property name="bin" value="bin" />

	<!-- This target makes a fresh directory if not present -->
	<target name="init" depends="clean">
		<mkdir dir="${bin}" />
	</target>

	<!-- Delete the old directory with all its files -->
	<target name="clean">
		<delete dir="${bin}" />
	</target>

	<!-- Compiles the source files -->
	<target name="compile" depends="init">
		<javac srcdir="src" destdir="${bin}" />
	</target>
</project>

As the code is self Explanatory we will leave all the dicussed topics and will bring new important thing called dependency.
depends:
When a target declares depends attribute then that target is only executed after the execution of the target on which it depends.

    In this case the dependency goes like this

  1. Compile is the default target which the file calls.
  2. Compile target depends on init so init target starts executing.
  3. Now init also depends on clean so the target names clean will be executed before the execution of init.
  4. So first of all target clean is executed, then init and then compile is executed.

Note: You can write more than one names of target in depends attribute like depends=”clean,init” and the execution is from left to right. So first clean will be executed and then init will get executed.Be careful while declaring dependency.

Running an ANT file

If you are using IDE like eclipse you can simply right click and select Run As –> ANT Build

If your classpath and code is all correct then you will see the following text in your console

If Environment variables like PATH and JAVA_HOME is not set in your system than you will get an error as shown below.

So if you get this error message forst set all the variables and then restart the eclipse and run the ant file again.


Note: In ant the error messages are well explained so error can be identified easily. If you set the verbose mode by giving the argument -v then you will get detailed log in the console.

Advertisements
 
Leave a comment

Posted by on April 22, 2010 in ANT, Java

 

Tags: ,

What is SMTP?

In simplest form an SMTP server is a computer that receives outgoing mail messages from users and routes them to their intended recipients. All SMTP servers implement some version of the Simple Mail Transport Protocol.


Electronic mail server software uses SMTP to send and receive mail messages, user-level client mail applications typically only use SMTP for sending messages to a mail server for relaying. SMTP is a “push” protocol that cannot “pull” messages from a remote server on demand. To retrieve messages only on demand, which is the most common requirement on a single-user computer, a mail client must use POP3 or IMAP. Here we have described a series of actions which happens when you hit Send button

  • The request goes to mail server where it connects to its port 25. Then it sends some command for its communication and if evrything works fine then the message is posted in to the network.
  • Server maintains a mail queue: it will save the mail locally and send it together with all the other queued mail in intervals (for example, every 30 minutes). This behavior depends on the configuration and the server may also deliver the mail instantly.
  • To get the mail POP3 or some other protocol is used.

 
Leave a comment

Posted by on April 21, 2010 in Protocols

 

Tags: , ,

Google Maps for JSF

Gone are the days when you have to open an atlas for looking some location. Thanks to GOOGLE MAPS which shows you whatever you want and that too within minutes. You can’t only see the location but also get its real time traffic details with lots of other information. Is not it cool to show your location in google map on your website or blog so that everyone can know where you are living or working?

GMaps4JSF is the extensive library provided by the google to integrate Google maps in JSF enabled pages. GMaps4JSF aims at integrating Google maps with JSF. JSF users will be also able to construct complex Streetview Panoramas and Maps with just few lines of code (JSF tags). It allows you to mark polygons, lines, HTML informatin window to your Map. GMaps4JSF is one of the JSF Mashups libraries that enables JSF users to build web 2.0 mashup applications in JSF easily.

Configuring GMap4JSF

You really don’t have to do much to use Google Maps with your JSF framework. You only need to follow these given steps:

  1. Download the gmaps4jsf-core-1.1.jar from http://code.google.com/p/gmaps4jsf/downloads/list and put it in your project’s lib folder.
  2. Include the tag library in your JSF page as follows:
    
    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
    
    
  3. Add the following script tags in your jsp HEAD tags.
    < script src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=ABQIAAAAxrVS1QxlpJHXxQ2Vxg2bJBQdkFK-tWRbPPQS4ACM1pq_e-PltxQXeyH20wQuqDaQ_6EM5UeGGVpnIw"  type="text/javascript"> < /script >
    

    The key which is used in JavaScript can be generated by signing up at http://code.google.com/apis/maps/signup.html

Embedding Google Map in JSF page.

After doing the above mentioned tasks you are all ready to insert map in your jsf page. This is the basic structure of a jsf page which is ready to incorporate Google map in it.

sampleJspPage.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
	pageEncoding="ISO-8859-1"%>
<%@taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<%@taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<%@ taglib uri="http://code.google.com/p/gmaps4jsf/" prefix="m"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<script
	src="http://maps.google.com/maps?file=api&amp;v=2&amp;key=ABQIAAAAxrVS1QxlpJHXxQ2Vxg2bJBQdkFK- tWRbPPQS4ACM1pq_e-PltxQXeyH20wQuqDaQ_6EM5UeGGVpnIw"
	type="text/javascript"></script>
<title>Insert title here</title>
</head>
<body>
<f:view>
	<h:form id="form">
		<m:map zoom="16" width="500px" height="500px" address="White house">
			<m:mapControl name="GLargeMapControl" position="G_ANCHOR_BOTTOM_LEFT" />
			<m:mapControl name="GMapTypeControl" />
			<m:htmlInformationWindow htmlText="White house"></m:htmlInformationWindow>
		</m:map>
	</h:form>
</f:view>
</body>
</html>

Explanation:

<%@ taglib uri="http://code.google.com/p/gmaps4jsf/" prefix="m" %>

As described before the include directive tag imports the usable taglib and uses m as its prefix

<m:map zoom="16" width="500px" height="500px" address="White house" >
                <m:mapControl name="GLargeMapControl" position="G_ANCHOR_BOTTOM_LEFT"/>
                        <m:mapControl name="GMapTypeControl"/>
                <m:htmlInformationWindow htmlText="White house"></m:htmlInformationWindow>
</m:map>
  • <m:map> is for map component. It allows us to create a map. It has various attributes which can be used to customize it. None of them are compulsory. So you are free to use them or not according to ypur needs.
    • zoom: Its default value is 11. You can change its value as per your requirements.
    • width and height: They represent the height and width of the map. The default values are 500 for both.
    • address: The address you wish the map to point to. If you use this attribute then don't use longitude or latitude attributes.
  • <m:mapControl> This component allows us to add a map control to the map component. Like if you want to add a control so that the user can zoom in or zoom out the map, or a control to switch between the types of the map.
  • htmlInformationWindowComponent: This component allows us to add an information window to the map component. Following we have provided the description about their attributes.
  •  
    7 Comments

    Posted by on April 21, 2010 in Java, JSF

     

    Tags: ,

    Program to an interface, not an implementation.

    In Software Development process more time is spent on maintaining the code rather than its development and during this period there may be requirement of new classes. By Programing to Interfaces your application/code is always open for modification without much code change.

    Like if you coded to concrete classes then your code will look something like this

       
     
    public ProcessedTea processTea(Tea tea){  
    ProcessedTea processedTea = tea.getProcessedTea();  
    return processedTea;  
     }  
    
    

    But if you program to Interfaces then it can be made generic

          
    
    public ProcessedFood processFood(FoodItem foodItem){  
    ProcessedFood processedFood = foodItem.getProcessedFood();  
    return processedFood;  
    }  
    

    UML Diagram:

    Here FoodItem is an Interface and rest of the classes like Tea, Coffee etc implements it.

    Now if there is any new addition of any foodItem/Beverages then its a lot easier for you to process it as the method processFood(FoodItem foodItem) takes an implementation of FoodItem. On the other hand if you have programmed to concrete classes like the code given at first then you have to write new method for every food type.

    Hope it makes clear that why programming to interfaces is more useful as compared to program to concrete classes.

     
    1 Comment

    Posted by on April 7, 2010 in Design Patterns, Java

     

    Tags: ,

    Strategy Pattern

    Strategy pattern defines a family of algorithm which can be used to give desired behaviors to the Objects dynamically.
    Objects which can have different behaviors in their life time are harder to manage and it is difficult to use only inheritance to solve the purpose of code-reuse.  So here composition seems to be a better candidate which can ease this situation.

    Implementing Strategy Pattern and following the practice of Programming to Interfaces make things flexible enough. Behaviors can be added or changed at runtime. Algorithms/Behaviors can be added to its Family at any time and so does adding behavior to the Object is as easy as calling a setter and passing the new Behaviors Object reference.

    To demonstrate this lets consider the following figure.

    Here in this figure Vehicle class can have the functionality of start by any means like StartByKick or StartByBattery. These behaviors can be added to any class/subclass of Vehicle. Programming to Interfaces gives the flexibility of even extending and using the family of StartBehaviors.

    The most apparent drawback of this Design Pattern is that the Behavior Classes which can be termed as Family Of Algorithms are not in adherence of OOPS. They do not represent a fully qualified Object with its properties and methods. Here the Object/Class is only to have  some specific implementation of Algorithm.

     
    Leave a comment

    Posted by on April 1, 2010 in Design Patterns

     

    Tags: