Use file method

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;
}

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值