Welcome Message

Hi, welcome to my website. This is a place where you can get all the questions, puzzles, algorithms asked in interviews and their solutions. Feel free to contact me if you have any queries / suggestions and please leave your valuable comments.. Thanks for visiting -Pragya.

May 22, 2010

Spring : Good Points

1. IF you are using DI using constructor .. u have to make the constructor as public ... private / protected / default wont work

May 18, 2010

ClassLoader Documentaion

A class loader is an object that is responsible for loading classes. The
* class ClassLoader is an abstract class. Given the * href="#name">binary name of a class, a class loader should attempt to
* locate or generate data that constitutes a definition for the class. A
* typical strategy is to transform the name into a file name and then read a
* "class file" of that name from a file system.
*
*

Every {@link Class Class} object contains a {@link
* Class#getClassLoader() reference} to the ClassLoader that defined
* it.
*
*

Class objects for array classes are not created by class
* loaders, but are created automatically as required by the Java runtime.
* The class loader for an array class, as returned by {@link
* Class#getClassLoader()} is the same as the class loader for its element
* type; if the element type is a primitive type, then the array class has no
* class loader.
*
*

Applications implement subclasses of ClassLoader in order to
* extend the manner in which the Java virtual machine dynamically loads
* classes.
*
*

Class loaders may typically be used by security managers to indicate
* security domains.

May 13, 2010

Life cycle of a bean with Bean Factory Container and ApplicationContext Container

1 The container finds the bean’s definition and instantiates the bean.
2 Using dependency injection, Spring populates all of the properties as
specified in the bean definition.
3 If the bean implements the BeanNameAware interface, the factory calls
setBeanName() passing the bean’s ID.
4 If the bean implements the BeanFactoryAware interface, the factory calls
setBeanFactory(), passing an instance of itself.
5 If there are any BeanPostProcessors associated with the bean, their post-
ProcessBeforeInitialization() methods will be called.
6 If an init-method is specified for the bean, it will be called.
7 Finally, if there are any BeanPostProcessors associated with the bean,
their postProcessAfterInitialization() methods will be called.

At this point, the bean is ready to be used by an application and will remain in the
bean factory until it is no longer needed. It is removed from the bean factory in
two ways.

1 If the bean implements the DisposableBean interface, the destroy()
method is called.
2 If a custom destroy-method is specified, it will be called.

The life cycle of a bean within a Spring application context differs only slightly
from that of a bean within a bean factory

The only difference here is that if the bean implements the ApplicationContext-
Aware interface, the setApplicationContext() method is called.

Spring : Difference between BeanFactory and ApplicationContext

ApplicationContext interface extends the BeanFactory interface.

A big difference between an application context and a bean factory is how
singleton beans are loaded. A bean factory lazily loads all beans, deferring bean
creation until the getBean() method is called. An application context is a bit
smarter and preloads all singleton beans upon context startup. By preloading
singleton beans, you ensure that they will be ready to use when needed—your
application won’t have to wait for them to be created.

DomParserExample

import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class DomParserExample {

//No generics
List myEmpls;
Document dom;


public DomParserExample(){
//create a list to hold the employee objects
myEmpls = new ArrayList();
}

public void runExample() {

//parse the xml file and get the dom object
parseXmlFile();

//get each employee element and create a Employee object
parseDocument();

//Iterate through the list and print the data
/*printData();
*/
}


private void parseXmlFile(){
//get the factory
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

try {

//Using factory get an instance of document builder
DocumentBuilder db = dbf.newDocumentBuilder();

//parse using builder to get DOM representation of the XML file
//dom = db.parse("BOVarswaps.xml");
dom = db.parse("BOPostTrade.xml");


}catch(ParserConfigurationException pce) {
pce.printStackTrace();
}catch(SAXException se) {
se.printStackTrace();
}catch(IOException ioe) {
ioe.printStackTrace();
}
}


private void parseDocument(){
//get the root elememt
Element docEle = dom.getDocumentElement();

//get a nodelist of elements
NodeList nl = docEle.getElementsByTagName("class");
if(nl != null && nl.getLength() > 0) {
for(int i = 0 ; i < nl.getLength();i++) {

//get the employee element
Element el = (Element)nl.item(i);

//get the Employee object
System.out.println("*"+el.getAttribute("name"));

NodeList propertyList = el.getElementsByTagName("property");
if(propertyList != null && propertyList.getLength() > 0 ){
for(int ii = 0 ; ii < propertyList.getLength();ii++){
Element inner = (Element)propertyList.item(ii);
System.out.println(" * "+inner.getAttribute("name"));
}

}
System.out.println("");
}
}
}

/*private String getClassName(el){

}
private String getPropertyName (){

}*/
/**
* I take an employee element and read the values in, create
* an Employee object and return it
* @param empEl
* @return
*/
/*private Employee getEmployee(Element empEl) {

//for each element get text or int values of
//name ,id, age and name
String name = getTextValue(empEl,"Name");
int id = getIntValue(empEl,"Id");
int age = getIntValue(empEl,"Age");

String type = empEl.getAttribute("type");

//Create a new Employee with the value read from the xml nodes
Employee e = new Employee(name,id,age,type);

return e;
}

*/
/**
* I take a xml element and the tag name, look for the tag and get
* the text content
* i.e for John xml snippet if
* the Element points to employee node and tagName is name I will return John
* @param ele
* @param tagName
* @return
*/
private String getTextValue(Element ele, String tagName) {
String textVal = null;
NodeList nl = ele.getElementsByTagName(tagName);
if(nl != null && nl.getLength() > 0) {
Element el = (Element)nl.item(0);
textVal = el.getFirstChild().getNodeValue();
}

return textVal;
}


/**
* Calls getTextValue and returns a int value
* @param ele
* @param tagName
* @return
*/
private int getIntValue(Element ele, String tagName) {
//in production application you would catch the exception
return Integer.parseInt(getTextValue(ele,tagName));
}

/**
* Iterate through the list and print the
* content to console
*/
private void printData(){

System.out.println("No of Employees '" + myEmpls.size() + "'.");

Iterator it = myEmpls.iterator();
while(it.hasNext()) {
System.out.println(it.next().toString());
}
}


public static void main(String[] args){
//create an instance
DomParserExample dpe = new DomParserExample();

//call run example
dpe.runExample();
}

}

Sample XML :




Seagull
3674
34


Robin
3675
25


Crow
3676
28

Very Simple XML DOMParser

Sample XML :












































Java Code :


import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class MyDOMParser {

Document dom;
public void runExample() {

//parse the xml file and get the dom object
parseXmlFile();

//get each element
parseDocument();


}


private void parseXmlFile(){
//get the factory
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

try {

//Using factory get an instance of document builder
DocumentBuilder db = dbf.newDocumentBuilder();

//parse using builder to get DOM representation of the XML file
//dom = db.parse("BOVarswaps.xml");
dom = db.parse("BOPostTrade.xml");


}catch(ParserConfigurationException pce) {
pce.printStackTrace();
}catch(SAXException se) {
se.printStackTrace();
}catch(IOException ioe) {
ioe.printStackTrace();
}
}


private void parseDocument(){
//get the root elememt
Element docEle = dom.getDocumentElement();

//get a nodelist of elements
NodeList nl = docEle.getElementsByTagName("class");
if(nl != null && nl.getLength() > 0) {
for(int i = 0 ; i < nl.getLength();i++) {

//get the employee element
Element el = (Element)nl.item(i);

//get the Employee object
System.out.println("*"+el.getAttribute("name"));

NodeList propertyList = el.getElementsByTagName("property");
if(propertyList != null && propertyList.getLength() > 0 ){
for(int ii = 0 ; ii < propertyList.getLength();ii++){
Element inner = (Element)propertyList.item(ii);
System.out.println(" * "+inner.getAttribute("name"));
}

}
System.out.println("");
}
}
}


public static void main(String[] args){
//create an instance
MyDOMParser dpe = new MyDOMParser();

//call run example
dpe.runExample();
}

}

http://www.totheriver.com/learn/xml/xmltutorial.html#6

May 6, 2010

What the difference between data hiding and abstraction?

Data hiding means, hiding some essential information or as you say a data from other end user or users and abstraction in simple language means hiding complex features and showing only essential features of any object.

In data hiding we hide the information by using access specifier private, public and protected..so it means we are hiding info from outside the world.But in abstrastion we express only essential feature..
Eg: Three set of customers are going to buy a bike First one wants information about the style. Second one wants about the milage. Third one wants the cost and brand of the bike.So the salesperson explains about the product which customer needs what. So he hiding some information and giving the revelant information.

May 5, 2010

Features of Hibernate

* Hibernate 3.0 provides three full-featured query facilities: Hibernate Query Language, the newly enhanced Hibernate Criteria Query API, and enhanced support for queries expressed in the native SQL dialect of the database.

* Filters for working with temporal (historical), regional or permissioned data.

* Enhanced Criteria query API: with full support for projection/aggregation and subselects.

* Runtime performance monitoring: via JMX or local Java API, including a second-level cache browser.

* Eclipse support, including a suite of Eclipse plug-ins for working with Hibernate 3.0, including mapping editor, interactive query prototyping, schema reverse engineering tool.

* Hibernate is Free under LGPL: Hibernate can be used to develop/package and distribute the applications for free.

* Hibernate is Scalable: Hibernate is very performant and due to its dual-layer architecture can be used in the clustered environments.

* Less Development Time: Hibernate reduces the development timings as it supports inheritance, polymorphism, composition and the Java Collection framework.

* Automatic Key Generation: Hibernate supports the automatic generation of primary key for your.

* JDK 1.5 Enhancements: . While Hibernate3 still runs perfectly with JDK 1.2, Hibernate3 will make use of some new JDK features. JSR 175 annotations, for example, are a perfect fit for Hibernate metadata

* EJB3-style persistence operations: EJB3 defines the create() and merge() operations, which are slightly different to Hibernate's saveOrUpdate() and saveOrUpdateCopy() operations. Hibernate3 will support all four operations as methods of the Session interface.

* Hibernate XML binding enables data to be represented as XML and POJOs interchangeably.

* The EJB3 draft specification support for POJO persistence and annotations.