package com.ibm.tpc.test.doh;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import javax.ws.rs.FormParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
/**
* This class is for dealing with test log
* @version 1.0
* @author CHENGSHUAI
*
*/
@Path ("/TestLogCreator")
public class TestLogCreator {
@POST
@Produces ("application/json")
public String createHTMLFile(@FormParam("logstatus") String logstatus, @FormParam("logmoudle") String logmoudle
, @FormParam("maxLogCount") int maxLogCount) {
String result = "success";
Date date = new Date();
String dateFormat = new SimpleDateFormat("HHmmss").format(date);
String moudleDir = getMoudleDir(logmoudle);
String projectdir = getProjectDir();
projectdir += moudleDir;
File createdirf = new File(projectdir);
if(!createdirf.exists()){
createdirf.mkdirs();
}
//Get file list by Modified
List <File> filelist = reArrange(createdirf.listFiles());
for(int i = 0;i < filelist.size()-maxLogCount+1;i++){
filelist.get(i).delete();
}
//create report *.html" file in workspace.
projectdir += "dohreport" + dateFormat + ".html";
projectdir = projectdir.replace("/", "\\");
File f = new File(projectdir);
FileOutputStream fos = null;
try {
fos = new FileOutputStream(f);
fos.write(logstatus.getBytes());
} catch (IOException e) {
e.printStackTrace();
result = "failure";
}finally{
if(fos != null){
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//create report "dohreport.html" file in server space.
String resourceDir = getResourceDir();
File fweb = new File(resourceDir + "/dohreport.html");
FileOutputStream fosweb = null;
try {
fosweb = new FileOutputStream(fweb);
fosweb.write(logstatus.getBytes());
} catch (IOException e) {
e.printStackTrace();
result = "failure";
}finally{
if(fosweb != null){
try {
fosweb.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return result;
}
/**
* Get the path of test moudle
* @param moudle
* @return
*/
private String getMoudleDir(String moudle){
String moudleDir = "";
String [] moudleArr = moudle.split("\\.");
for(int i = 2; i < moudleArr.length-1; i++){
moudleDir += moudleArr[i] + "/";
}
return moudleDir;
}
/**
* Get project absolute direction by path of the classes which been put in server.
* So different version web server may change this function.
* @return String
*/
private String getProjectDir(){
String resourceDir = getResourceDir();
String placeName = resourceDir.split(".metadata")[0];
String [] projectTemp = resourceDir.split("/");
String projectName = projectTemp[projectTemp.length-1];
return placeName + projectName + "/WebContent/devDojo/tpc/tests/logs/" ;
}
/**
* The method get class path from the web server;
* @return String
*/
private String getResourceDir(){
String resourceDir = Thread.currentThread().getContextClassLoader().getResource("").getPath().split("/WEB-INF")[0];
if(resourceDir.indexOf("/") == 0){
resourceDir = resourceDir.replaceFirst("/", "");
}
return resourceDir;
}
/**
* Rearrange the File array by modified time, and return the new list.
* @param fileArr
* @return
*/
private List <File> reArrange(File [] fileArr){
List <File> filelist = new ArrayList<File>(fileArr.length);
for ( File f : fileArr){
filelist.add(f);
}
Collections.sort(filelist, new Comparator<File>(){
@Override
public int compare(File f1, File f2) {
return (int) (f1.lastModified()-f2.lastModified());
}
});
return filelist;
}
}
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;
import javax.ws.rs.FormParam;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
/**
* This class is for dealing with test log
* @version 1.0
* @author CHENGSHUAI
*
*/
@Path ("/TestLogCreator")
public class TestLogCreator {
@POST
@Produces ("application/json")
public String createHTMLFile(@FormParam("logstatus") String logstatus, @FormParam("logmoudle") String logmoudle
, @FormParam("maxLogCount") int maxLogCount) {
String result = "success";
Date date = new Date();
String dateFormat = new SimpleDateFormat("HHmmss").format(date);
String moudleDir = getMoudleDir(logmoudle);
String projectdir = getProjectDir();
projectdir += moudleDir;
File createdirf = new File(projectdir);
if(!createdirf.exists()){
createdirf.mkdirs();
}
//Get file list by Modified
List <File> filelist = reArrange(createdirf.listFiles());
for(int i = 0;i < filelist.size()-maxLogCount+1;i++){
filelist.get(i).delete();
}
//create report *.html" file in workspace.
projectdir += "dohreport" + dateFormat + ".html";
projectdir = projectdir.replace("/", "\\");
File f = new File(projectdir);
FileOutputStream fos = null;
try {
fos = new FileOutputStream(f);
fos.write(logstatus.getBytes());
} catch (IOException e) {
e.printStackTrace();
result = "failure";
}finally{
if(fos != null){
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//create report "dohreport.html" file in server space.
String resourceDir = getResourceDir();
File fweb = new File(resourceDir + "/dohreport.html");
FileOutputStream fosweb = null;
try {
fosweb = new FileOutputStream(fweb);
fosweb.write(logstatus.getBytes());
} catch (IOException e) {
e.printStackTrace();
result = "failure";
}finally{
if(fosweb != null){
try {
fosweb.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return result;
}
/**
* Get the path of test moudle
* @param moudle
* @return
*/
private String getMoudleDir(String moudle){
String moudleDir = "";
String [] moudleArr = moudle.split("\\.");
for(int i = 2; i < moudleArr.length-1; i++){
moudleDir += moudleArr[i] + "/";
}
return moudleDir;
}
/**
* Get project absolute direction by path of the classes which been put in server.
* So different version web server may change this function.
* @return String
*/
private String getProjectDir(){
String resourceDir = getResourceDir();
String placeName = resourceDir.split(".metadata")[0];
String [] projectTemp = resourceDir.split("/");
String projectName = projectTemp[projectTemp.length-1];
return placeName + projectName + "/WebContent/devDojo/tpc/tests/logs/" ;
}
/**
* The method get class path from the web server;
* @return String
*/
private String getResourceDir(){
String resourceDir = Thread.currentThread().getContextClassLoader().getResource("").getPath().split("/WEB-INF")[0];
if(resourceDir.indexOf("/") == 0){
resourceDir = resourceDir.replaceFirst("/", "");
}
return resourceDir;
}
/**
* Rearrange the File array by modified time, and return the new list.
* @param fileArr
* @return
*/
private List <File> reArrange(File [] fileArr){
List <File> filelist = new ArrayList<File>(fileArr.length);
for ( File f : fileArr){
filelist.add(f);
}
Collections.sort(filelist, new Comparator<File>(){
@Override
public int compare(File f1, File f2) {
return (int) (f1.lastModified()-f2.lastModified());
}
});
return filelist;
}
}