Monday, March 5, 2012

Java Method to copy a folder directory contents from source location to destination location


public void copyDirectory(java.io.File sourceLocation, java.io.File targetLocation) {
 System.out.println("Starting Copying");
 try {
  if (sourceLocation.isDirectory()) {
   if (!targetLocation.exists()) {
    targetLocation.mkdir();
   }

   String[] children = sourceLocation.list();
   for (int i = 0; i < children.length; i++) {
    copyDirectory(new java.io.File(sourceLocation, children[i]),
      new java.io.File(targetLocation, children[i]));
   }
  } 
  else {
   java.io.InputStream in = new java.io.FileInputStream(sourceLocation);
   java.io.OutputStream out = new java.io.FileOutputStream(targetLocation);
   byte[] buf = new byte[1024];
   int len;
   while ((len = in.read(buf)) > 0) {
    out.write(buf, 0, len);
   }
   in.close();
   out.close(); 
  }
 } catch (java.io.IOException e) {
  e.printStackTrace();
  throw new RuntimeException(e);
 }
 System.out.println("Completed Copying");
}

To test this method just create a directory at any location say: (c:/sourceLocation/) with some files in it. And you want to copy all the contents of this directory to a new location say : (d:/targetLocation) which may or may not exist. Then this function will create the target location and copy all the contents from the given source location to the target location. 

 The sample test class to test this method:
public class TestCopyDrectory {

 public static void main(String[] args) {
  copyDirectory(new java.io.File("C:/sourceLocation/"), new java.io.File("D:/targetLocation"));
 }

 public static void copyDirectory(java.io.File sourceLocation, java.io.File targetLocation) {
  System.out.println("Starting Copying");
  try {
   if (sourceLocation.isDirectory()) {
    if (!targetLocation.exists()) {
     targetLocation.mkdir();
    }

    String[] children = sourceLocation.list();
    for (int i = 0; i < children.length; i++) {
     copyDirectory(new java.io.File(sourceLocation, children[i]),
       new java.io.File(targetLocation, children[i]));
    }
   } 
   else {
    java.io.InputStream in = new java.io.FileInputStream(sourceLocation);
    java.io.OutputStream out = new java.io.FileOutputStream(targetLocation);
    byte[] buf = new byte[1024];
    int len;
    while ((len = in.read(buf)) > 0) {
     out.write(buf, 0, len);
    }
    in.close();
    out.close(); 
   }
  } catch (java.io.IOException e) {
   e.printStackTrace();
   throw new RuntimeException(e);
  }
  System.out.println("Completed Copying");
 }

}

Wednesday, February 22, 2012

Steps to create a dataSource bean in applicationContext.xml in Spring Framework with its properties for jdbc connection string obtained from a properties file

In Spring Framework while creating a dataSource bean in applicationContext.xml, it is required to establish the jdbc connection with the connection properties like driverClassName, url, username, password being defined.

The same can be defined directly with all the property values there itself hard coded like :


But there is a better way, separate the jdbc connection dataSource bean in a seperate xml say dataSource.xml which you can import in the main applicationContext.xml

And it will be even further better if the properties  like driverClassName, url, username, password of jdbc connection string are fetched from a properties file which can be easily changed depending upon the environment without even touching the applicationContext.xml file thus the dependency is removed.

To separate the dataSource bean two files need to be created in src folder (in the same directory as applicationContext.xml):


1) dataSource.xml
2) jdbc.properties


Steps :

1) Inside src folder create a xml file dataSource.xml Paste the following in it :


Here i have used "org.springframework.jdbc.datasource.DriverManagerDataSource" as dataSource class which can be any other datasource class.


2) Inside src folder create a properties file jdbc.properties. Paste the following in it :


The Database connection string is created for mysql for database "TEST"

3) Import the created dataSource.xml in your applicationContext.xml:




That was all, you application is good to go. Got any doubts, feel free to ask/comment.

Thursday, December 15, 2011

ABC of WCF


ABC of WCF

WCF Service is a program that exposes a collection of Endpoints. Each Endpoint is a portal for communicating with the world.
All the WCF communications are take place through end point. End point consists of three components.

Address

Basically URL, specifies where this WCF service is hosted .Client will use this url to connect to the service. e.g
http://localhost:8090/MyService/SimpleCalculator.svc

Binding

Binding will describes how client will communicate with service. There are different protocols available for the WCF to communicate to the Client. You can mention the protocol type based on your requirements.
A binding has several characteristics, including the following:
  • Transport -Defines the base protocol to be used like HTTP, Named Pipes, TCP, and MSMQ are some type of protocols.
  • Encoding (Optional) - Three types of encoding are available-Text, Binary, or Message Transmission Optimization Mechanism (MTOM). MTOM is an interoperable message format that allows the effective transmission of attachments or large messages (greater than 64K).
  • Protocol(Optional) - Defines information to be used in the binding such as Security, transaction or reliable messaging capability
The following table gives some list of protocols supported by WCF binding.
Binding
Description
BasicHttpBinding
Basic Web service communication. No security by default
WSHttpBinding
Web services with WS-* support. Supports transactions
WSDualHttpBinding
Web services with duplex contract and transaction support
WSFederationHttpBinding
Web services with federated security. Supports transactions
MsmqIntegrationBinding
Communication directly with MSMQ applications. Supports transactions
NetMsmqBinding
Communication between WCF applications by using queuing. Supports transactions
NetNamedPipeBinding
Communication between WCF applications on same computer. Supports duplex contracts and transactions
NetPeerTcpBinding
Communication between computers across peer-to-peer services. Supports duplex contracts
NetTcpBinding
Communication between WCF applications across computers. Supports duplex contracts and transactions

Contract

Collection of operation that specifies what the endpoint will communicate with outside world. Usually name of the Interface