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 FileUtilTool { ArrayList<File> subFiles(File file){ File[] temp = file.listFiles(); ArrayList<File> files = ArrayList<File>(); ) { ; i++) { (temp[i].isFile()) { files.add(temp[i]); } } } files; } ArrayList<File> subDirs(File file){ File[] temp = file.listFiles(); ArrayList<File> files = ArrayList<File>(); ) { ; i++) { (temp[i].isDirectory()) { files.add(temp[i]); } } } files; } ArrayList<File> listLogFile(File root){ ArrayList<File> logDirs=subDirs(root); ArrayList<File> logfiles= ArrayList<File>(),temp; (File file: logDirs){ temp=subFiles(file); logfiles.addAll(temp); } logfiles; } copyFile(File from, File toDir) { (!from.exists() || !toDir.exists()) { ; } File to= +from.getName()); (!to.exists()){ { to.createNewFile(); } (Exception e){ ; } } FileInputStream fis; FileOutputStream fos; { fis = FileInputStream(from); fos = FileOutputStream(to); [1024]; readed = -1; ((readed = fis.read(buff)) > 0) fos.write(buff, 0, readed); fis.close(); fos.close(); } (IOException ioe) { } ; } delFolder(String folderPath) { { delAllFile(folderPath); String filePath = folderPath; filePath = filePath.toString(); java.io.File myFilePath = java.io.File(filePath); myFilePath.delete(); } (Exception e) { System. ); e.printStackTrace(); } } delAllFile(String path) { File file = File(path); (!file.exists()) { ; } (!file.isDirectory()) { ; } String[] tempList = file.list(); File temp = ; ; i++) { )) { temp = File(path + tempList[i]); } { temp = + tempList[i]); } (temp.isFile()) { temp.delete(); } (temp.isDirectory()) { delAllFile(path + + tempList[i]); delFolder(path + + tempList[i]); }} IOUtil.java
package import import import import import import import import import import import import import import import import /** * * @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; /** * * 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 '<' */
public static
String inputToHtml(String message){ String s=message.replaceAll(
"<", "<"
);
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 import import import import import import import import import import import import public 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 MultiActionController { ; ; ; setListPage(String listPage){ =listPage; } String getListPage(){ ; } setSuccessPage(String successPage){ =successPage; } String getSuccessPage(){ ; } String getErrorPage(){ ; } setErrorPage(String errorPage){ =errorPage; } ModelAndView list(HttpServletRequest req, HttpServletResponse res) { String projectName = (String) req.getParameter( ); .equals(projectName)) { File dir = + projectName); ArrayList<File> logFiles = FileUtilTool.subFiles(dir); (!logFiles.isEmpty()) { req.getSession().setAttribute( , projectName); ModelAndView mAndv = , , logFiles); mAndv; } } String message = ; ModelAndView mAndv = , message); mAndv; } ModelAndView copy(HttpServletRequest req, HttpServletResponse res){ ArrayList<File> files=(ArrayList<File>)req.getSession().getAttribute( ); i=1; ; String copyto=req.getParameter( ); String message= ; File copytoDir= File(copyto); (!copytoDir.exists()){ message= ; } (!copytoDir.isDirectory()){ message= ; } .equals(message)){ ,message); } (File file: files){ String para=req.getParameter( +i); ){ seleted= ; (!FileUtilTool.copyFile(file, copytoDir)){ message= ; ,message); } } i++; } (!seleted){ message= ; ,message); } String projectName=(String)req.getSession().getAttribute( ); String url= +projectName; req.setAttribute( ,url); message= ; ModelAndView mAndv = ,message); mAndv; } ModelAndView remove(HttpServletRequest req, HttpServletResponse res){ ArrayList<File> files=(ArrayList<File>)req.getSession().getAttribute( ); i=1; ; String fileList= ; (File file: files){ String para=req.getParameter( +i); ){ seleted= ; fileList=fileList+file.getAbsolutePath()+ ; (!file.delete()){ String message= ; ,message); } } i++; } (!seleted){ String message= ; ,message); } //If the project`s log file are all deleted, the project directory should be deleted. String projectName=(String)req.getSession().getAttribute( ); File projectFile= +projectName); (projectFile.exists()&&FileUtilTool.subFiles(projectFile).isEmpty()){ FileUtilTool.delFolder(projectFile.getAbsolutePath()); String url=req.getContextPath()+ ; req.setAttribute( ,url); String message= ; System. +fileList); IndexDAO indexDao=(IndexDAO)BeanFactory.getBean( ); indexDao.buildIndex(); ModelAndView mAndv = ,message); mAndv; } String url= +projectName; req.setAttribute( ,url); String message= ; System. +fileList); IndexDAO indexDao=(IndexDAO)BeanFactory.getBean( ); indexDao.buildIndex(); ModelAndView mAndv = ,message); mAndv;
}}
- Lcr-3
- Lcr-1
- Lcr-2
- Lcr-4
- Lcr-5
- Lcr-6
- 简易LCR测试仪
- hdu 2778 LCR 模拟题
- CodeForces Gym 100646C LCR
- oracle 11g streams 逻辑修改记录(LCR)示例
- 3
- 3
- 3
- 3
- 3
- (3)
- 3
- 3
- 【转】linux中使用命名管道实现客户端/服务器模型的进程间通信
- ASP.NET项目中bin,app_code,app_data 等文件夹的作用
- 解决校园网iNode_linux客户端下45s掉线一次
- 网络通信的工作原理
- Cocos2d开发系列(六)
- Lcr-3
- windows 回车,ctrl z 再回车结束,linux 下ctrl D 结束
- WorkFlow入门Step.1—My Frist WorkFlow Trip!
- PKU2954
- poj 1159
- 什么是WXGA? Android3.1 蜂巢系统分辨率
- 有关自动化测试的误区
- PHP跳转页面的三种方式
- wpf之TXTextControl控件读取、保存文件