Monday, February 15, 2010

Copying Recent Updated Files From The Shared Drives To The Local Machine (From Same Network)

In case multiple log files are created on different physical machines connected on the same network, in my case the total physical machines were more than 30 and on each machine multiple log files were written by the application (say the average was 7-8 files). Even the size of the files does matter, as the size of the files written were ~700M and more.
Imagine copying these many files manually would always eat performance testers precious time. To eliminate this workload on the tester we can make use of the automated script to take care of copying the log files after each run.

What needs to be taken care:
1. If any services writes the log on these physical machine (same in my case), When the test is completed, execute the code which will collect the logs from different machines (my case: GAP service/PNL Service/DS/tradeserver logs).
2. The code should know how many logs and which logs it has to collect from which server.
3. The code sort the files based on the latest update (Date & Time) [latest files will be on top]
4. The code picks up the files which starts with text which is mentioned in the below If conditions.
5. The code picks up only the files which has the _error text in the name of the file
e.g. GAP1_BOND_FILT2_error_10142009_101408.log
HS1_BOND_FILT2_error_10142009_103028.log
6. The copied logs will be saved in the location that you provide during the execution of this code.

Code Goes Here:
package copyfiles_env2;
import java.io.*;
import java.io.File;
import javax.swing.*;
import java.util.Arrays;
import java.util.Comparator;

public class Main {
public static void main(String[] args) throws IOException
{
int forloop = 0;
boolean b1;
String string;
String[] servers={"\\\\server1b","\\\\server2b","\\\\server3b","\\\\server4b","\\\\server5b","\\\\server6b","\\\\server7b","\\\\server8b","\\\\server9b","\\\\server10b","\\\\server13b","\\\\server14b","\\\\server17b","\\\\server19b","\\\\server20b","\\\\server21b","\\\\server32b"};
BufferedReader TempReader;
BufferedWriter TempWriter;
File destFileName=null, DestFolderpath=null,tempfile=null;
String folderpathofscript = null, destfolderpath=null, temp1=null, s1=" ",s2=" "; // string to save the folder path selected by user

final JFileChooser fc = new JFileChooser();
fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
int returnVal1 = fc.showSaveDialog(null),fxpssearch2Count=0;
if (returnVal1 == JFileChooser.APPROVE_OPTION){
DestFolderpath = fc.getSelectedFile();
destfolderpath = String.valueOf(DestFolderpath);
JOptionPane.showMessageDialog(null, "Wait.....!!! Copying Files To The Destination Folder");

for (int j=0; j<=servers.length; j++){
folderpathofscript = servers[j]+"\\servers\\RTScripts";
if (servers[j]=="\\\\server1b"){
forloop = 2;
s1 = "GAP1_GAP_";
s2 = "GAP2_GAP_";
}
if (servers[j]=="\\\\server2b"){
forloop = 2;
s1 = "BVS_bondview_";
s2 = "BVS_collateralexceptionview_";
}
if (servers[j]=="\\\\server3b"){
forloop = 2;
s1 = "BVS_credderivdefaview_";
s2 = "BVS_dispexview_";
}
if (servers[j]=="\\\\server4b"){
forloop = 2;
s1 = "BVS_hedgeview_";
s2 = "BVS_incmsgview_";
}
if (servers[j]=="\\\\server5b"){
forloop = 2;
s1 = "BVS_requestsview_";
s2 = "BVS_riskview_";
}
if (servers[j]=="\\\\server6b")
{
forloop = 2;
s1 = "staticchangesserver_";
s2 = "tradeserver_";
}
if (servers[j]=="\\\\server7b"){
forloop = 2;
s1 = "GAP1_GAP_DERIV_";
s2 = "GAP2_GAP_DERIV_";
}
if (servers[j]=="\\\\server8b"){
forloop = 2;
s1 = "PRESTL_ACCT_TR_";
s2 = "PRESTL_BASKETOPT_";
}
if (servers[j]=="\\\\server9b"){
forloop = 2;
s1 = "PRESTL_EXOTIC_";
s2 = "PRESTL_FRA_";
}
if (servers[j]=="\\\\server10b"){
forloop = 2;
s1 = "PRESTL_MM_";
s2 = "PRESTL_MUST_TR_";
}
if (servers[j]=="\\\\server13b"){
forloop = 2;
s1 = "PNL1_PL_CDS_";
s2 = "PNL2_PL_CDS_";
}
if (servers[j]=="\\\\server14b"){
forloop = 2;
s1 = "PNL1_PL_BOND_";
s2 = "PNL2_PL_BOND_";
}
if (servers[j]=="\\\\server17b"){
forloop = 2;
s1 = "BDPS_";
s2 = "FXPS_";
}
if (servers[j]=="\\\\server19b"){
forloop = 2;
s1 = "HS1_BOND_";
s2 = "HS2_BOND_";
}
if (servers[j]=="\\\\server20b"){
forloop = 2;
s1 = "HS1_CDS_";
s2 = "HS2_CDS_";
}
if (servers[j]=="\\\\server21b"){
forloop = 2;
s1 = "HS1_DERIV_";
s2 = "HS2_DERIV_";
}
if (servers[j]=="\\\\server32b"){
forloop = 1;
s1 = "DS_";
}

File dir = new File(folderpathofscript);
File [] files = dir.listFiles();
Arrays.sort(files, new Comparator(){
public int compare(Object o1, Object o2) {
return compare( (File)o1, (File)o2);
}
private int compare( File f1, File f2){
long result = f2.lastModified() - f1.lastModified();
if( result > 0 ){
return 1;
} else if( result < 0 ){
return -1;
} else {
return 0;
}
}
});

for (int i=0;i<=forloop;i++){
if( files[i].getName().lastIndexOf(".log")!=-1 && files[i].getName().indexOf("error_")!=-1){
System.out.println("File Name: "+files[i].getName());
if(b1 = files[i].getName().indexOf(s1)!= -1 || files[i].getName().indexOf(s2)!= -1){
TempWriter = new BufferedWriter(new FileWriter(destfolderpath+"\\"+files[i].getName()));
TempReader = new BufferedReader(new FileReader(files[i].getPath()));
while((temp1=TempReader.readLine())!=null){
TempWriter.write(temp1+"\n");
}
TempWriter.close();
TempReader.close();
}
else if ((files[i].getName().lastIndexOf(".log")!=-1) && (files[i].getName().indexOf("STPAPI_")!=-1) || files[i].getName().lastIndexOf(".log")==-1 || files[i].getName().lastIndexOf("output_")==-1){
forloop = forloop + 1;
}
}
}
s1=" ";s2=" ";forloop=0;
}

JOptionPane.showMessageDialog(null, "Done.....!!! All The Required Files Are Copied Successfully....!!!");
}
}
}