View Javadoc

1   /*
2    * Copyright 2004-2006 the Seasar Foundation and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.seasar.tuigwaa.system;
17  
18  import java.io.BufferedReader;
19  import java.io.FileInputStream;
20  import java.io.IOException;
21  import java.io.InputStreamReader;
22  import java.util.ArrayList;
23  import java.util.Enumeration;
24  import java.util.List;
25  import java.util.Properties;
26  import java.util.regex.Matcher;
27  import java.util.regex.Pattern;
28  
29  import org.apache.commons.io.IOUtils;
30  import org.apache.commons.logging.Log;
31  import org.apache.commons.logging.LogFactory;
32  import org.apache.commons.logging.impl.LogFactoryImpl;
33  import org.apache.log4j.helpers.OptionConverter;
34  
35  import com.isenshi.util.ResourceUtils;
36  
37  /***
38   * @author someda
39   */
40  public class LogServiceImpl implements LogService {
41  
42  	private List logFiles = new ArrayList();
43  	
44  	private Log log = LogFactory.getLog(getClass());
45  	
46  	private static final Pattern LOG4J_FILEAPPENDER_PATTERN = Pattern.compile("log4j//.appender//..*//.File");
47  	
48  	public LogServiceImpl(){
49  		initLogList();		
50  	}
51  		
52  	private void initLogList(){
53  		
54  		LogFactory factory = LogFactory.getFactory();
55  		String logger = (String) factory.getAttribute(LogFactoryImpl.LOG_PROPERTY);		
56  		if(logger.equals("org.apache.commons.logging.impl.Log4JLogger")){			
57  			
58  			Properties props = new Properties();
59  			FileInputStream fis = null;
60  			String filePath = ResourceUtils.getPath("log4j.properties");
61  			try{
62  				fis = new FileInputStream(filePath);
63  				props.load(fis);				
64  				Enumeration logProp = props.keys();
65  				while(logProp.hasMoreElements()){
66  					String key = (String)logProp.nextElement();				
67  					Matcher m = LOG4J_FILEAPPENDER_PATTERN.matcher(key);				
68  					if(m.matches()){
69  						String value = props.getProperty(key);
70  						logFiles.add(OptionConverter.substVars(value,props));					
71  					}
72  				}				
73  			}catch(IOException ioe){
74  				log.error("reading configuration file");
75  			}finally{
76  				IOUtils.closeQuietly(fis);
77  			}						
78  		}
79  	}	
80  	
81  	public List search(String fileName, LogSearchCondition condition) throws TgwServiceException {
82  
83  		List strList = new ArrayList();
84  		
85  		int lines = condition.getLines();
86  		if(lines > MAX_DISPLAY_LINES){
87  			lines = MAX_DISPLAY_LINES;
88  		}		
89  		int maxLine = condition.getOffset() + lines;
90  				
91  		FileInputStream fis = null;
92  		BufferedReader br = null;
93  		try{
94  			fis = new FileInputStream(fileName);
95  			br = new BufferedReader(new InputStreamReader(fis));
96  			
97  			String line = null;
98  			int lineCount = 0;			
99  			while((line = br.readLine()) != null){				
100 				if(lineCount > maxLine){
101 					break;
102 				}				
103 				if(lineCount < condition.getOffset()){
104 					lineCount++;
105 					continue;
106 				}				
107 				strList.add(line);
108 				lineCount++;
109 			}						
110 		}catch(IOException ioe){
111 			ioe.printStackTrace();
112 		}finally{
113 			IOUtils.closeQuietly(br);
114 			IOUtils.closeQuietly(fis);
115 		}
116 		return strList;
117 	}
118 
119 	public void addLogfile(String fileName) {
120 		logFiles.add(fileName);
121 	}
122 
123 	public List getLogfiles() {				
124 		return logFiles;
125 	}
126 
127 }