Lcr-3

来源:互联网 发布:哥特式字体软件 编辑:程序博客网 时间:2024/06/07 02:16

LogDAO.java

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

package

 

 

information.sh.lp.model;

 

import

 

 

information.sh.lp.infoBean.ContextInfo;

import

 

 

information.sh.lp.infoBean.Log;

import

 

 

information.sh.lp.infoBean.LogFilter;

import

 

 

information.sh.lp.infoBean.Position;

import

 

 

information.sh.lp.util.IOUtil;

import

 

 

information.sh.lp.util.ParseString;

 

import

 

 

java.io.BufferedReader;

import

 

 

java.io.File;

import

 

 

java.io.FileInputStream;

import

 

 

java.io.InputStreamReader;

import

 

 

java.util.ArrayList;

import

 

 

java.util.Collections;

import

 

 

java.util.Comparator;

import

 

 

java.util.Iterator;

import

 

 

java.util.regex.Matcher;

import

 

 

java.util.regex.Pattern;

 

/**

*

*

 

 

@author

Li,

Chengri

*

*/

public

 

 

class

LogDAO {

 

 

/**

*

*

 

 

@param

fitler

*

 

 

@return

* Describe: According to the 'filter' get read logs form log files

*/

 

 

public

ArrayList<Log> queryLog(LogFilter fitler) {

ArrayList<Position> positions=IOUtil.readRoughPosition(fitler.getStartTime(),fitler.getEndTime(),fitler.getPjName());

ArrayList<Log> logs =

 

new

ArrayList<Log>();

ArrayList<Log> fileLogs;

Position position;

 

 

if (positions != null

) {

Iterator itPo = positions.iterator();

 

 

while

(itPo.hasNext()) {

position = (Position) itPo.next();

fileLogs =

 

this

.readLog(position, fitler);

 

 

if (fileLogs != null

&& !fileLogs.isEmpty()) {

logs.addAll(fileLogs);

}

}

 

 

if

(logs.isEmpty()){

 

 

return null

;

}

}

 

else

{

 

 

return null

;

}

 

 

if(fitler.getOrderBy().equals("time"

)){

sortLogByTime(logs);

}

 

 

else if(fitler.getOrderBy().equals("level"

)){

sortLogByLevel(logs);

}

 

 

return

logs;

}

 

 

/**

*

*

 

 

@param

Position Marks the logs` file name and line number

*

 

 

@param

Filter Save the information which user requires

*

 

 

@return

The logs which meet the requirement.

*/

 

 

public

ArrayList<Log> readLog(Position position,LogFilter filter) {

File file =

 

new

File(position.getFile());

ArrayList<Log> logs =

 

new

ArrayList<Log>();

 

 

int

start_line = position.getStart_line();

 

 

int

currLine=1;

StringBuffer block =

 

new

StringBuffer();

String line=

 

""

;

Pattern p = Pattern.compile(

 

"//d{4}-//d{2}-//d{2}//s//d{2}://d{2}://d{2}"

);

Matcher m;

 

 

boolean matched = false

;

 

 

try

{

BufferedReader br =

 

new BufferedReader(new

InputStreamReader(

 

 

new

FileInputStream(file)));

 

 

while ((line=br.readLine())!=null

&&currLine<= position.getEnd_line()) {

m = p.matcher(line);

matched = m.find();

 

 

if

(currLine<start_line){

}

 

 

else if

(currLine == position.getStart_line()) {

block.append(line);

start_line = currLine;

}

 

else if

(matched) {

Log log = ParseString.stringToLog(block.toString(),ContextInfo.

 

LOG_PROPER_SPLITER

);

log.setFile(position.getFile());

log.setStart_line(start_line);

log.setEnd_line(currLine - 1);

 

 

if

(filter.filt(log)) {

logs.add(log);

}

block =

 

new

StringBuffer(line);

start_line = currLine;

}

 

else

{

block.append(

 

"/n"

+ line);

}

currLine++;

}

Log log = ParseString.stringToLog(block.toString(),ContextInfo.

 

LOG_PROPER_SPLITER

);

log.setFile(position.getFile());

log.setStart_line(start_line);

log.setEnd_line(currLine - 1);

 

 

if

(filter.filt(log)) {

 

 

if

((currLine - 1)!=position.getStart_line())

logs.add(log);

}

}

 

catch

(Exception e) {

 

 

return null

;

}

 

 

return

logs;

}

 

 

/**

*

*

 

 

@param

logs The logs which need to be ordered by level

*

 

 

@return

*/

 

 

public

ArrayList<Log> sortLogByLevel(ArrayList<Log> logs){

ArrayList<Log> newLogs=

 

new

ArrayList<Log>();

ArrayList<Log> errorLevel=

 

new

ArrayList<Log>();

ArrayList<Log> warnLevel=

 

new

ArrayList<Log>();

ArrayList<Log> debugLevel=

 

new

ArrayList<Log>();

ArrayList<Log> infoLevel=

 

new

ArrayList<Log>();

Iterator iterator=logs.iterator();

 

 

while

(iterator.hasNext()){

Log log=(Log)iterator.next();

 

 

if(log.getLevel().equals("ERROR"

)){

errorLevel.add(log);

}

 

else if(log.getLevel().equals("WARN"

)){

warnLevel.add(log);

}

 

else if(log.getLevel().equals("DEBUG"

)){

debugLevel.add(log);

}

 

else if(log.getLevel().equals("INFO"

)){

infoLevel.add(log);

}

}

newLogs.addAll(infoLevel);

newLogs.addAll(errorLevel);

newLogs.addAll(warnLevel);

newLogs.addAll(debugLevel);

logs.clear();

logs.addAll(newLogs);

 

 

return

newLogs;

}

 

 

public

ArrayList<Log> sortLogByTime(ArrayList<Log> logs){

Collections.sort(logs,

new

 

Comparator(){

 

 

public int compare(final Object o1,final

Object o2){

 

 

final

Log log1=(Log)o1;

 

 

final

Log log2=(Log)o2;

 

 

return

 

return

logs;}

}

 

FileUtilTool.java

 

ParseString.compareTime(log2.getTime(),log1.getTime());}

})

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

package

 

 

information.sh.lp.util;

 

import

 

 

java.io.File;

import

 

 

java.io.FileInputStream;

import

 

 

java.io.FileOutputStream;

import

 

 

java.io.IOException;

import

 

 

java.util.ArrayList;

 

public

 

 

class

FileUtilTool {

 

 

public static

ArrayList<File> subFiles(File file){

File[] temp = file.listFiles();

ArrayList<File> files =

 

new

ArrayList<File>();

 

 

if (temp != null

) {

 

 

for (int i = 0; i < temp.length

; i++) {

 

 

if

(temp[i].isFile()) {

files.add(temp[i]);

}

}

 

}

 

 

return

files;

}

 

 

public static

ArrayList<File> subDirs(File file){

File[] temp = file.listFiles();

ArrayList<File> files =

 

new

ArrayList<File>();

 

 

if (temp != null

) {

 

 

for (int i = 0; i < temp.length

; i++) {

 

 

if

(temp[i].isDirectory()) {

files.add(temp[i]);

}

}

}

 

 

return

files;

}

 

 

public static

ArrayList<File> listLogFile(File root){

ArrayList<File> logDirs=subDirs(root);

ArrayList<File> logfiles=

 

new

ArrayList<File>(),temp;

 

 

for

(File file: logDirs){

temp=subFiles(file);

logfiles.addAll(temp);

}

 

 

return

logfiles;

}

 

 

 

public static boolean

copyFile(File from, File toDir) {

 

 

if

(!from.exists() || !toDir.exists()) {

 

 

return false

;

}

 

File to=

 

new File(toDir.getAbsolutePath()+"/"

+from.getName());

 

 

if

(!to.exists()){

 

 

try

{

to.createNewFile();

}

 

catch

(Exception e){

 

 

return false

;

}

}

FileInputStream fis;

FileOutputStream fos;

 

 

try

{

fis =

 

new

FileInputStream(from);

fos =

 

new

FileOutputStream(to);

 

 

byte[] buff = new byte

[1024];

 

 

int

readed = -1;

 

 

while

((readed = fis.read(buff)) > 0)

fos.write(buff, 0, readed);

fis.close();

fos.close();

}

 

catch

(IOException ioe) {

 

}

 

 

return true

;

}

 

 

public static void

delFolder(String folderPath) {

 

 

try

{

delAllFile(folderPath);

String filePath = folderPath;

filePath = filePath.toString();

java.io.File myFilePath =

 

new

java.io.File(filePath);

myFilePath.delete();

 

}

 

catch

(Exception e) {

System.

 

out.println("delete error "

);

e.printStackTrace();

 

}

 

}

 

 

 

public static void

delAllFile(String path) {

File file =

 

new

File(path);

 

 

if

(!file.exists()) {

 

 

return

;

}

 

 

if

(!file.isDirectory()) {

 

 

return

;

}

String[] tempList = file.list();

File temp =

 

null

;

 

 

for (int i = 0; i < tempList.length

; i++) {

 

 

if (path.endsWith(File.separator

)) {

temp =

 

new

File(path + tempList[i]);

}

 

else

{

temp =

 

new File(path + File.separator

+ tempList[i]);

}

 

 

if

(temp.isFile()) {

temp.delete();

}

 

 

if

(temp.isDirectory()) {

delAllFile(path +

 

"/"

+ tempList[i]);

delFolder(path +

 

"/"

+ tempList[i]); }}

 

 

IOUtil.java

 

package

 

 

information.sh.lp.util;

 

import

 

 

information.sh.lp.infoBean.ContextInfo;

import

 

 

information.sh.lp.infoBean.Index;

import

 

 

information.sh.lp.infoBean.Position;

 

import

 

 

java.io.BufferedReader;

import

 

 

java.io.BufferedWriter;

import

 

 

java.io.File;

import

 

 

java.io.FileInputStream;

import

 

 

java.io.FileNotFoundException;

import

 

 

java.io.FileOutputStream;

import

 

 

java.io.IOException;

import

 

 

java.io.InputStreamReader;

import

 

 

java.io.OutputStreamWriter;

import

 

 

java.util.ArrayList;

import

 

 

java.util.Iterator;

import

 

 

java.util.regex.Matcher;

import

 

 

java.util.regex.Pattern;

 

/**

*

*

 

@author Li,

Chengri

* Describe: This class is a IO toolkit

*/

public

 

 

class IOUtil {

 

/**

*

*

 

@param file The file need to be read

*

 

@param lineNumber The line number

*

 

@return The string at 'lineNumber' line in the 'file' file.

*/

 

public static String readLine(File file, int lineNumber) {

String line;

 

int i = 0;

 

if (lineNumber <= 0) {

 

return null;

}

 

try {

BufferedReader br =

new BufferedReader(new InputStreamReader(

 

new FileInputStream(file)));

line = br.readLine();

 

while (i < lineNumber - 1 && line != null) {

line = br.readLine();

i++;

}

br.close();

}

catch (FileNotFoundException e) {

line =

null;

}

catch (IOException e) {

line =

null;

}

 

if (i != lineNumber - 1) {

line =

null;

}

 

return line;

}

 

/**

*

*

 

@param logfile The log file

*

 

@param interval The interval of two indexes.

*

 

@return indexes

* Describe: Get the indexes by reading a log file, and the indexes` interval is 'interval'

*/

 

public static ArrayList<Index> parseIndexs(File logfile, int interval) {

ArrayList<Index> indexs =

new ArrayList<Index>();

Index index=

new Index();

String startWith =

"^//d{4}-//d{2}-//d{2}//s//d{2}://d{2}://d{2}";

Pattern p = Pattern.compile(startWith);

Matcher m;

 

boolean matched;

String line,start_time_s=

"";

 

int lineNumber = 1,min, start_time=0;

 

try {

BufferedReader br =

new BufferedReader(new InputStreamReader(new FileInputStream(logfile)));

 

while ((line= br.readLine())!= null) {

m = p.matcher(line);

matched = m.find();

 

if (!matched) {

lineNumber++;

 

continue;

}

min = Integer.parseInt(line.substring(14, 16));

 

if (lineNumber==1){

index =

new Index();

start_time = (min / interval) * interval;

start_time_s = line.substring(0, 14) + addZero(start_time)+

":00";

index.setStart_time(start_time_s);

Position position =

new Position();

position.setFile(logfile.getAbsolutePath());

position.setStart_line(lineNumber);

index.addPosition(position);

index.setInterval(interval);

}

else if(min>= (start_time + interval)||!start_time_s.substring(0,14).equals(line.substring(0,14))){

index.getPosition(0).setEnd_line(lineNumber-1);

indexs.add(index);

index =

new Index();

start_time = (min / interval) * interval;

start_time_s = line.substring(0, 14) + addZero(start_time)+

":00";

index.setStart_time(start_time_s);

Position position =

new Position();

position.setFile(logfile.getAbsolutePath());

position.setStart_line(lineNumber);

index.addPosition(position);

index.setInterval(interval);

}

lineNumber++;

}

index.getPosition(0).setEnd_line(lineNumber-1);

indexs.add(index);

br.close();

}

catch (FileNotFoundException e) {

 

return null;

}

catch (IOException e) {

 

return null;

}

 

return indexs;

}

 

/**

*

*

 

@param indexes

*

 

@param file The file in which write the indexes

* Describe: Write the indexes into the file.

*/

 

public static void writeIndexes(ArrayList<Index> indexes,File file){

 

try{

BufferedWriter bw=

new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file)));

Iterator iterator=indexes.iterator();

Index index;

 

int flush=0;

 

while(iterator.hasNext()){

index=(Index)iterator.next();

bw.write(index+

"/n");

 

if(flush%1000==0){

bw.flush();

}

flush++;

}

bw.flush();

bw.close();

}

catch(IOException e){

e.printStackTrace();

}

}

 

public static String addZero(int s) {

 

if (s < 10) {

 

return "0" + s;

}

else

 

return String.valueOf(s);

}

 

/**

*

*

 

@param start The time where query from.

*

 

@param end The time where query end.

*

 

@return The log positions whose time between 'start' and 'end'

* Describe: Read the rough position from the index file according to the 'start' and 'end'.

*/

 

public static ArrayList<Position> readRoughPosition(String start,String end,String pjName){

File indexFile=

new File(ContextInfo.DATA_DIR+"//"+pjName+"//indexFile//index.txt");

ArrayList<Position> positions=

new ArrayList<Position>();

 

int lineNumber=1;

String line;

String time;

 

int compareSe,compareEe,compareEs;

 

while((line=IOUtil.readLine(indexFile,lineNumber))!=null){

time=line.substring(0,19);

compareSe=ParseString.compareTime(start,time,ContextInfo.

INDEX_INTERVAL);

compareEs=ParseString.compareTime(time, end);

compareEe=ParseString.compareTime(end,time,ContextInfo.

INDEX_INTERVAL);

 

if(compareEs==-1){

 

break;

}

else if(compareEe==1){

 

int reCount=Integer.parseInt(line.substring(line.indexOf('?')+8));

 

for(int i=0;i<reCount;i++){

String s=IOUtil.readLine(indexFile,++lineNumber);

Position position=ParseString.parsePosition(s);

positions.add(position);

}

 

break;

}

else if(compareSe==1){

 

int reCount=Integer.parseInt(line.substring(line.indexOf('?')+8));

 

for(int i=0;i<reCount;i++){

String s=IOUtil.readLine(indexFile,++lineNumber);

Position position=ParseString.parsePosition(s);

positions.add(position);

}

lineNumber++;

}

else{

 

int reCount=Integer.parseInt(line.substring(line.indexOf('?')+8));

lineNumber=lineNumber+reCount+1;

}

}

 

if(positions.isEmpty()){

positions=

null;

}

 

return positions;

}

 

 

 

}

 

 

ParseString.java

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

package

 

 

information.sh.lp.util;

 

import

 

 

information.sh.lp.infoBean.Log;

import

 

 

information.sh.lp.infoBean.Position;

 

import

 

 

java.util.Calendar;

import

 

 

java.util.regex.Matcher;

import

 

 

java.util.regex.Pattern;

/**

*

*

 

 

@author

Li,

Chengri

* Describe: A assistant class.

*/

public

 

 

class

ParseString {

 

 

 

/**

*

*

 

 

@param

block The string type log

*

 

 

@return

A log instance

* Describe: Parse the string to a log instance

*/

 

 

public static

Log stringToLog(String block,String spliter){

// String timePattern="//d{4}-//d{2}-//d{2}//s//d{2}://d{2}://d{2}";

// String levelPattern="INFO|WARN|DEBUG|ERROR";

// String locationPattern="(//s-//s.*:[?])|(//s-//s.*:(//d){1,8}//s*-)";

Pattern pattern;

Matcher matcher;

Log log=

 

new

Log();

 

 

int

endIndex;

 

endIndex=block.indexOf(spliter);

log.setTime(block.substring(0,endIndex));

 

block=block.substring(endIndex+spliter.length());

endIndex=block.indexOf(spliter);

log.setThread(block.substring(0,endIndex));

 

block=block.substring(endIndex+spliter.length());

endIndex=block.indexOf(spliter);

log.setLevel(block.substring(0,endIndex));

 

block=block.substring(endIndex+spliter.length());

endIndex=block.indexOf(spliter);

log.setPosition(block.substring(0,endIndex));

 

block=block.substring(endIndex+spliter.length());

log.setMessage(block);

 

 

 

return

log;

}

 

 

/**

*

*

 

 

@param

t1 String type time

*

 

 

@param

t2 String type time

*

 

 

@param

t2Add The amount minutes which t2 adds

*

 

 

@return

* Describe: Compare t1 to (t2+t2Add)

*/

 

 

public static int compareTime(String t1, String t2, int

t2Add) {

Calendar c1 = parseTime(t1);

Calendar c2 = parseTime(t2);

c2.add(Calendar.

 

MINUTE

, t2Add);

 

 

if

(c1.before(c2)) {

 

 

return

1;

}

 

else if

(c2.before(c1)) {

 

 

return

-1;

}

 

else

{

 

 

return

0;

}

}

 

 

/**

*

*

 

 

@param

time String type time

*

 

 

@return

A Calendar instance

*/

 

 

public static

Calendar parseTime(String time) {

Calendar c = Calendar.getInstance();

 

 

int

paras[] = { Integer.parseInt(time.substring(0, 4)),

Integer.parseInt(time.substring(5, 7)),

Integer.parseInt(time.substring(8, 10)),

Integer.parseInt(time.substring(11, 13)),

Integer.parseInt(time.substring(14, 16)),

Integer.parseInt(time.substring(17, 19)), };

c.set(paras[0], paras[1] - 1, paras[2], paras[3], paras[4], paras[5]);

 

 

return

c;

}

 

 

/**

*

*

 

 

@param

s String type log

*

 

 

@return

The log`s position

*/

 

 

public static

Position parsePosition(String s){

Position position=

 

new

Position();

 

 

int quChar=s.indexOf('?'

);

 

 

int waChar=s.indexOf('~'

);

 

 

int

startLine=Integer.parseInt(s.substring((quChar+1),waChar));

 

 

int

endLine=Integer.parseInt(s.substring((waChar+1)));

position.setFile(s.substring(0,quChar-1));

position.setStart_line(startLine);

position.setEnd_line(endLine);

 

 

return

position;

 

}

 

 

/**

*

*

 

 

@param

t1 String type time

*

 

 

@param

t2 String type time

*

 

 

@return

The result of comparison of t1 and t2

*/

 

 

public static int

compareTime(String t1,String t2){

 

 

if

(t1.equals(t2)){

 

 

return

0;

}

Calendar c1=parseTime(t1);

Calendar c2=parseTime(t2);

 

 

if

(c1.before(c2)){

 

 

return

1;

}

 

 

return

-1;

}

 

 

/**

*

*

 

 

@param

message A string

*

 

 

@return

A string

* Describe:To avoid input the message which includes html tag, must change the '<' to '&lt'

*/

 

 

public static

String inputToHtml(String message){

String s=message.replaceAll(

 

"<", "&lt"

);

 

 

return

s;

}

 

 

 

public static void

main(String args[]){

String block=

 

"2010-12-01 16:48:59 |/*/*| Main Thread |/*/*| INFO |/*/*| citi.sh.lp.jms.FileAppTest.main(FileAppTest.java:26) |/*/*| The info 213"

;

String spliter=

 

" |/*/*| "

;

Log log=ParseString.stringToLog(block, spliter);

System.

 

out

.println(log);}}

 

 

LoginController.java

 

package

 

 

information.sh.lp.web.controller;

 

import

 

 

information.sh.lp.infoBean.ContextInfo;

import

 

 

information.sh.lp.util.FileUtilTool;

import

 

 

information.sh.lp.web.service.BeanFactory;

import

 

 

information.sh.lp.web.service.LogReceiver;

 

import

 

 

java.io.File;

import

 

 

java.io.IOException;

import

 

 

java.util.ArrayList;

 

import

 

 

javax.servlet.http.HttpServletRequest;

import

 

 

javax.servlet.http.HttpServletResponse;

import

 

 

javax.servlet.http.HttpSession;

 

import

 

 

org.springframework.web.servlet.ModelAndView;

import

 

 

org.springframework.web.servlet.mvc.Controller;

 

public

 

 

class LoginController implements Controller {

 

private String loginUser;

 

private String loginAdmin;

 

private String loginDeny;

 

 

public ModelAndView handleRequest(HttpServletRequest req,

HttpServletResponse res) {

HttpSession session=req.getSession();

String role=req.getParameter(

"role");

String userName = req.getParameter(

"userName");

String passWord = req.getParameter(

"passWord");

 

 

if ("user".equals(role)){

 

if(ContextInfo.userName.equals(userName)&&ContextInfo.userPassword.equals(passWord)){

session.setAttribute(

"userName",userName);

 

return new ModelAndView(loginUser);

}

 

return new ModelAndView(loginDeny);

}

 

else{

 

if(ContextInfo.adminName.equals(userName)&&ContextInfo.adminPassword.equals(passWord)){

session.setAttribute(

"userName",userName);

 

//get the projects` directory (this part should be in ManagementController class)

File root=

new File(ContextInfo.DATA_DIR);

ArrayList<File> projects=FileUtilTool.subDirs(root);

 

// Flush the messages in the cache(this part should be in ManagementController class)

LogReceiver logReceiver = (LogReceiver) BeanFactory.getBean(

"logReceiver");

 

if (logReceiver.getEvents() != null

&& !logReceiver.getEvents().isEmpty()) {

 

try {

logReceiver.writeLogs(logReceiver.getEvents());

logReceiver.getEvents().clear();

}

catch (IOException e) {

e.printStackTrace();

}

}

req.getSession().setAttribute(

"projects",projects);

 

return new ModelAndView(loginAdmin);

}

 

else{

 

return new ModelAndView(loginDeny);

}

}

}

 

 

public void setLoginUser(String LoginUser) {

 

this.loginUser = LoginUser;

}

 

public void setLoginAdmin(String loginAdmin){

 

this.loginAdmin=loginAdmin;

}

 

public void setLoginDeny(String loginDeny){

 

this.loginDeny=loginDeny;

}

}

 

 

ManageController.java

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

package

 

 

information.sh.lp.web.controller;

 

import

 

 

information.sh.lp.infoBean.ContextInfo;

import

 

 

information.sh.lp.model.IndexDAO;

import

 

 

information.sh.lp.util.FileUtilTool;

import

 

 

information.sh.lp.web.service.BeanFactory;

 

import

 

 

java.io.File;

import

 

 

java.util.ArrayList;

 

import

 

 

javax.servlet.http.HttpServletRequest;

import

 

 

javax.servlet.http.HttpServletResponse;

 

import

 

 

org.springframework.web.servlet.ModelAndView;

import

 

 

org.springframework.web.servlet.mvc.multiaction.MultiActionController;

 

public

 

 

class ManageController extends

MultiActionController {

 

 

private String listPage

;

 

 

private String successPage

;

 

 

private String errorPage

;

 

 

public void

setListPage(String listPage){

 

 

this.listPage

=listPage;

}

 

 

public

String getListPage(){

 

 

return this.listPage

;

}

 

 

public void

setSuccessPage(String successPage){

 

 

this.successPage

=successPage;

}

 

 

public

String getSuccessPage(){

 

 

return this.successPage

;

}

 

 

public

String getErrorPage(){

 

 

return this.errorPage

;

}

 

 

public void

setErrorPage(String errorPage){

 

 

this.errorPage

=errorPage;

}

 

 

 

public

ModelAndView list(HttpServletRequest req, HttpServletResponse res) {

String projectName = (String) req.getParameter(

 

"project"

);

 

 

if (projectName != null && !""

.equals(projectName)) {

File dir =

 

new File(ContextInfo.DATA_DIR + "/"

+ projectName);

ArrayList<File> logFiles = FileUtilTool.subFiles(dir);

 

 

if

(!logFiles.isEmpty()) {

req.getSession().setAttribute(

 

"project"

, projectName);

ModelAndView mAndv =

 

new ModelAndView(this.listPage

,

 

 

"logFiles"

, logFiles);

 

 

return

mAndv;

}

}

String message =

 

"The project is empty!"

;

ModelAndView mAndv =

 

new ModelAndView(this.errorPage, "message"

,

message);

 

 

return

mAndv;

}

 

 

public

ModelAndView copy(HttpServletRequest req, HttpServletResponse res){

ArrayList<File> files=(ArrayList<File>)req.getSession().getAttribute(

 

"logFiles"

);

 

 

int

i=1;

 

 

boolean seleted=false

;

String copyto=req.getParameter(

 

"copyto"

);

String message=

 

""

;

File copytoDir=

 

new

File(copyto);

 

 

if

(!copytoDir.exists()){

message=

 

"Please input a corret directory path"

;

}

 

else if

(!copytoDir.isDirectory()){

message=

 

"The destination file is not directory. Please input a directory."

;

}

 

 

if(!""

.equals(message)){

 

 

return new ModelAndView(this.errorPage,"message"

,message);

}

 

 

for

(File file: files){

String para=req.getParameter(

 

""

+i);

 

 

if(para!=null

){

seleted=

 

true

;

 

 

if

(!FileUtilTool.copyFile(file, copytoDir)){

message=

 

"Occur an error then copy files!"

;

 

 

return new ModelAndView(this.errorPage,"message"

,message);

}

}

i++;

}

 

 

if

(!seleted){

message=

 

"Please chooce at least one file to copy!"

;

 

 

return new ModelAndView(this.errorPage,"message"

,message);

}

String projectName=(String)req.getSession().getAttribute(

 

"project"

);

String url=

 

"manage.do?action=list&&project="

+projectName;

req.setAttribute(

 

"url"

,url);

message=

 

"Success copy file !"

;

ModelAndView mAndv =

 

new ModelAndView(this.successPage,"message"

,message);

 

 

return

mAndv;

}

 

 

public

ModelAndView remove(HttpServletRequest req, HttpServletResponse res){

ArrayList<File> files=(ArrayList<File>)req.getSession().getAttribute(

 

"logFiles"

);

 

 

int

i=1;

 

 

boolean seleted=false

;

String fileList=

 

""

;

 

 

for

(File file: files){

String para=req.getParameter(

 

""

+i);

 

 

if(para!=null

){

seleted=

 

true

;

fileList=fileList+file.getAbsolutePath()+

 

"/n"

;

 

 

if

(!file.delete()){

String message=

 

"Delete file:"+file.getAbsoluteFile()+" failed(Maybe the file is still using)!"

;

 

 

return new ModelAndView(this.errorPage,"message"

,message);

}

}

i++;

}

 

 

if

(!seleted){

String message=

 

"Please chooce at least one file to remove!"

;

 

 

return new ModelAndView(this.errorPage,"message"

,message);

}

 

 

 

//If the project`s log file are all deleted, the project directory should be deleted.

String projectName=(String)req.getSession().getAttribute(

 

"project"

);

File projectFile=

 

new File(ContextInfo.DATA_DIR+"/"

+projectName);

 

 

if

(projectFile.exists()&&FileUtilTool.subFiles(projectFile).isEmpty()){

FileUtilTool.delFolder(projectFile.getAbsolutePath());

 

String url=req.getContextPath()+

 

"/jsp/manageData.jsp"

;

req.setAttribute(

 

"url"

,url);

String message=

 

"Success delete file!"

;

System.

 

out.println("Success delete files:/n"

+fileList);

 

IndexDAO indexDao=(IndexDAO)BeanFactory.getBean(

 

"indexDAO"

);

indexDao.buildIndex();

ModelAndView mAndv =

 

new ModelAndView(this.successPage,"message"

,message);

 

 

return

mAndv;

 

}

String url=

 

"manage.do?action=list&&project="

+projectName;

req.setAttribute(

 

"url"

,url);

String message=

 

"Success delete file!"

;

System.

 

out.println("Success delete files:/n"

+fileList);

 

IndexDAO indexDao=(IndexDAO)BeanFactory.getBean(

 

"indexDAO"

);

indexDao.buildIndex();

ModelAndView mAndv =

 

new ModelAndView(this.successPage,"message"

,message);

 

 

return

mAndv;

 

}}

原创粉丝点击