Wednesday, March 25, 2015

JAVA Class Loader

Applications written programming languages, such as C and C++, are compiled into native, machine-specific instructions and saved as an executable file. But in case of java specific application this is different. JAVA is dynamically compiled programming languages. In Java, compiler generate the .class files and it remain as-is until loaded into the Java Virtual Machine (JVM) . Classes are loaded into the JVM on an 'as needed' basis. And when a loaded class depends on another class, then that class is loaded as well.

Lets try to understand using our famous HelloWorld

public class HelloWorld {
   public static void main(String argv[]) {
      System.out.println("Hello World");
   }
}
If you run this class specifying the -verbose:class command-line option, so that it prints what classes are being loaded

java -verbose:class HelloWorld



[Opened C:\Program Files\Java\jre1.5.0\lib\rt.jar]
[Opened C:\Program Files\Java\jre1.5.0\lib\jsse.jar]
[Opened C:\Program Files\Java\jre1.5.0\lib\jce.jar]
[Opened C:\Program Files\Java\jre1.5.0\lib\charsets.jar]
[Loaded java.lang.Object from shared objects file]
[Loaded java.io.Serializable from shared objects file]
[Loaded java.lang.Comparable from shared objects file]
[Loaded java.lang.CharSequence from shared objects file]
[Loaded java.lang.String from shared objects file]
[Loaded java.lang.reflect.GenericDeclaration from shared objects file]
[Loaded java.lang.reflect.Type from shared objects file]
[Loaded java.lang.reflect.AnnotatedElement from shared objects file]
[Loaded java.lang.Class from shared objects file]
[Loaded java.lang.Cloneable from shared objects file]
[Loaded java.lang.ClassLoader from shared objects file]
[Loaded java.lang.System from shared objects file]
[Loaded java.lang.Throwable from shared objects file]
---
---
[Loaded java.security.BasicPermissionCollection from shared objects file]
[Loaded java.security.Principal from shared objects file]
[Loaded java.security.cert.Certificate from shared objects file]
[Loaded HelloWorld from
Hello World
[Loaded java.lang.Shutdown from shared objects file]
[Loaded java.lang.Shutdown$Lock from shared objects file]
As you can see, the Java runtime classes required by the application class (HelloWorld) are loaded first.

How to add external JS in GWT Project

Let say your GWT project name is barchart


Add following line in BarChart.html

Create a public folder and keep all your *.js file in it. Make sure to have same flow structure of packages.




Folder wise screen shot





How to add Fusion chart js to gwt project

Add following line to your Project.html





Add following line to your Project.html



Tuesday, February 12, 2013

Different ways to create objects in Java


There are four different waysto create objects in java:

1. Using new keyword
This is the most common way to create an object in java.

MyObject object = new MyObject();

2. Using Class.forName()


MyObject object = (MyObject) Class.forName("com.MyTest").newInstance();

 3. Using clone()

The clone() can be used to create a copy of an existing object.

MyObject anotherObject = new MyObject();
MyObject object = anotherObject.clone();

4. Using object deserialization

Object deserialization is nothing but creating an object from its serialized form.

ObjectInputStream inStream = new ObjectInputStream(anInputStream );
MyObject object = (MyObject) inStream.readObject();
Now you know how to create an object. But its advised to create objects only when it is necessary to do so.

5.Using Reflection

this.getClass().getClassLoader().loadClass("com.MyTest").newInstance();

Annotation concept in JAVA

Annotation is a concept introduced by J2SE 5 and above it that allows programmers to add additional information called metadata into a Java source file. Annotations do not change the execution of a program but the information embedded using annotations can be used by various tools during development and deployment.


Annotation is similar to creating an interface. Having declaration preceded by an @ symbol. The @Retention annotation is used to specify the retention policy, i.e. SOURCE, CLASS, or RUNTIME.

RetentionPolicy.SOURCE retains an annotation only in the source file and discards it during compilation.
RetentionPolicy.CLASS stores the annotation in the .class file but does not make it available during runtime.
RetentionPolicy.RUNTIME stores the annotation in the .class file and also makes it available during runtime.


There are two part for Annotation

1) Annotation Type
2) Annotation itself


1) Annotation Type

package hello;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation {
String MyAuthor();    // Annotation member
    String MyDate();      // Annotation member
}

2) Annotation itself

package hello;

import java.lang.reflect.Method;

@MyAnnotation(MyAuthor="Siddhu",MyDate="12/2/2013,12/02/2013")
public class TestMyAnnotation {
@MyAnnotation(MyAuthor="Siddhu",MyDate="12/02/2013")
   public static void myMethod()
   {
       System.out.println("Hi .......... inside the method");      
   }
public static void showMyAnnotations()
{
TestMyAnnotation test=new TestMyAnnotation();
   try
   {
    Class c=test.getClass();
       Method m=c.getMethod("myMethod");
       MyAnnotation annotation1=(MyAnnotation)c.getAnnotation(MyAnnotation.class);
       MyAnnotation annotation2=m.getAnnotation(MyAnnotation.class);
       System.out.println("Author for class: "+annotation1.MyAuthor());
       System.out.println("Date for  class: "+annotation1.MyDate());
       System.out.println("Author for method: "+annotation2.MyAuthor());
       System.out.println("Date for method: "+annotation2.MyDate());
   }
   catch(NoSuchMethodException ex)
   {
       System.out.println("Invalid Method..."+ex.getMessage());
   }
}
   public static void main(String args[])
   {
       myMethod();
       showMyAnnotations();
   }
}


OutPut:

Hi .......... inside the method
Author of the class: Siddhu
Date of Writing the class: 12/2/2013,12/02/2013
Author of the method: Siddhu
Date of Writing the method: 12/02/2013

Wednesday, August 01, 2012

Concept of Connection Pool using JAVA Code


class MypoolCountMgr
{

 String databaseUrl = "jdbc:oracle://IpAddresss:XXXX/myDatabase";
 String userName = "UN";
 String password = "UP";

 Vector poolCount = new Vector();

 public MypoolCountMgr()
 {
  initialize();
 }

 public MypoolCountMgr(
  String databaseUrl,
  String userName,
  String password
  )
 {
  this.databaseUrl = databaseUrl;
  this.userName = userName;
  this.password = password;
  initialize();
 }

 private void initialize()
 {
  //initialize
  initializepoolCount();
 }

 private void initializepoolCount()
 {
  while(!checkIfpoolCountIsFull())
  {
   System.out.println("Connection is available. We are proceeding to add new connections");
   //Adding new connection
   poolCount.addElement(createNewConnectionForPool());
  }
  System.out.println("Connection Pool is full.");
 }

 private synchronized boolean checkIfpoolCountIsFull()
 {
  final int MAX_POOL_SIZE = 5;

  //Check if the pool size
  if(poolCount.size() < 5)
  {
   return false;
  }

  return true;
 }

 //Normal Code to Creating a new connection
 private Connection createNewConnectionForPool()
 {
  Connection connection = null;

  try
  {
   Class.forName(DRIVER_TOLOAD);
   connection = DriverManager.getConnection(databaseUrl, userName, password);
   System.out.println("Connection: "+connection);
  }
  catch(SQLException sqle)
  {
   System.err.println("SQLException: "+sqle);
   return null;
  }
  catch(ClassNotFoundException cnfe)
  {
   System.err.println("ClassNotFoundException: "+cnfe);
   return null;
  }

  return connection;
 }

 public synchronized Connection getConnectionFromPool()
 {
  Connection connection = null;

  //Check if there is a connection available
  if(poolCount.size() > 0)
  {
   connection = (Connection) poolCount.firstElement();
   poolCount.removeElementAt(0);
  }
    return connection;
 }

 public synchronized void returnConnectionToPool(Connection connection)
 {
  //Adding the connection from the client back to the connection pool
  poolCount.addElement(connection);
 }

 public static void main(String args[])
 {
  MypoolCountMgr MypoolCountMgr = new MypoolCountMgr();
 }

}

Wednesday, July 04, 2012

Steps Involved to install Apache 2.4.2 server on Red Hat Linux


 -- Steps Involved to install Apache 2.4.2 server.

1 - Download Apache 4.2.2 server.
Please find the below link to download the Apache 4.2.2. server http://httpd.apache.org/docs/2.4/install.html#download

For Liux O/S: http://httpd.apache.org/download.cgi#apache24

Download httpd-2.4.2.tar.gz

Untar above zip in current “/” directory of Linux using following commands:

1) gzip -d httpd-2.4.2.tar.gz
2) tar xvf httpd-2.4.2.tar

2 - Download apr, apr-util and pcre required to run Apache Server. 
Please find the below link to download the apr, apr-util and pcre ftp://ftp.csx.cam.ac.uk/pub/software/programming/pcre/

 

Keep the above files inside /httpd-2.4.2/srclib  and perform following steps

1) cd /httpd-2.4.2/srclib
2) gzip -d apr-1.4.6.tar.gz
3) tar xvf apr-1.4.6.tar
4) gzip -d apr-util-1.4.1.tar.gz
5) tar xvf apr-util-1.4.1.tar
6) gzip -d pcre-8.20.tar.gz
7) tar xvf pcre-8.20.tar

Rename all the above folder inside /httpd-2.4.2/srclib from
-apr-1.4.6 to apr
-apr-util-1.4.1 to apr-util
-pcre-8.20.tar to pcre

3 - Configure pcre package.

Execute the following steps to configure pcre package on Linux Box.

1) cd /httpd-2.4.2/srclib/pcre
2) ./configure
3) make
4) make install

4 - Installing Apache Server.

Please follow following steps to installed Apache Server.

1) cd /httpd-2.4.2
2) ./configure --prefix=/httpd-2.4.2 \
--with-included-apr
3) make
4) make install

Make following changes into httpd.conf

1) Vi /httpd-2.4.2/conf/httpd.conf
a. Listen IPADDRESSOFMACHINE:9090
b. ServerName IPADDRESSOFMACHINE:9090

5 - Starting Apache Server.

Set the Path variable with following command:

export PATH=$PATH$:/httpd-2.4.2/bin:

Stop the existing httpd service

service httpd stop

Run the Apache Server with following command

/httpd-2.4.2/bin/apachectl -k start

Note : Please cross verify that your 9090 port is available and not in used by other application.