001 /**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.geronimo.jetty6.requestlog;
018
019 import java.io.File;
020 import java.io.FilenameFilter;
021 import java.io.RandomAccessFile;
022 import java.nio.CharBuffer;
023 import java.nio.MappedByteBuffer;
024 import java.nio.channels.FileChannel;
025 import java.nio.charset.Charset;
026 import java.text.ParseException;
027 import java.text.SimpleDateFormat;
028 import java.util.ArrayList;
029 import java.util.Collection;
030 import java.util.Date;
031 import java.util.Iterator;
032 import java.util.LinkedList;
033 import java.util.List;
034 import java.util.regex.Matcher;
035 import java.util.regex.Pattern;
036
037 import org.apache.commons.logging.Log;
038 import org.apache.commons.logging.LogFactory;
039 import org.apache.geronimo.gbean.GBeanInfo;
040 import org.apache.geronimo.gbean.GBeanInfoBuilder;
041 import org.apache.geronimo.system.serverinfo.ServerInfo;
042
043 /**
044 * Jetty implementation of the WebAccessLog management interface.
045 *
046 * @version $Rev: 482336 $ $Date: 2006-12-04 15:12:19 -0500 (Mon, 04 Dec 2006) $
047 */
048 public class JettyLogManagerImpl implements JettyLogManager {
049 private final static Log log = LogFactory.getLog(JettyLogManagerImpl.class);
050
051 // Pattern that matches the date in the logfile name
052 private final static Pattern FILENAME_DATE_PATTERN = Pattern.compile("[-_ /.](((19|20)\\d\\d)[-_ /.](0[1-9]|1[012])[-_ /.](0[1-9]|[12][0-9]|3[01]))");
053 private final static int GROUP_FILENAME_FULL_DATE = 1;
054 private final static int GROUP_FILENAME_YEAR = 2;
055 private final static int GROUP_FILENAME_MONTH = 4;
056 private final static int GROUP_FILENAME_DAY = 5;
057 // NOTE: The file separators are specified here rather than using something like File.separator because
058 // they are hard coded in config plans and sometimes in java code itself rather than being dependent
059 // upon the OS. This should be fixed someday, but for now we will manually check for either format.
060 private final static String FILE_SEPARATOR_UNIX_STYLE = "/";
061 private final static String FILE_SEPARATOR_WIN_STYLE = "\\";
062
063 // Pattern that matches a single line (used to calculate line numbers)
064 private final static Pattern FULL_LINE_PATTERN = Pattern.compile("^.*", Pattern.MULTILINE);
065 private final static Pattern ACCESS_LOG_PATTERN = Pattern.compile("(\\S*) (\\S*) (\\S*) \\[(.*)\\] \\\"(\\S*) (\\S*).*?\\\" (\\S*) (\\S*).*");
066 private final static int GROUP_HOST = 1;
067 private final static int GROUP_USER = 3;
068 private final static int GROUP_DATE = 4;
069 private final static int GROUP_METHOD = 5;
070 private final static int GROUP_URI = 6;
071 private final static int GROUP_RESPONSE_CODE = 7;
072 private final static int GROUP_RESPONSE_LENGTH = 8;
073 private final static String ACCESS_LOG_DATE_FORMAT = "dd/MMM/yyyy:HH:mm:ss ZZZZ";
074 private final static String LOG_FILE_NAME_FORMAT = "yyyy_MM_dd";
075 private final Collection logGbeans;
076 private final ServerInfo serverInfo;
077
078 public JettyLogManagerImpl(ServerInfo serverInfo, Collection logGbeans) {
079 this.serverInfo = serverInfo;
080 this.logGbeans = logGbeans;
081 }
082
083 /**
084 * Gets the name of all logs used by this system. Typically there
085 * is only one, but specialized cases may use more.
086 *
087 * @return An array of all log names
088 *
089 */
090 public String[] getLogNames() {
091 List logNames = new ArrayList();
092 for (Iterator it = logGbeans.iterator(); it.hasNext();) {
093 JettyRequestLog jettyLog = (JettyRequestLog) it.next();
094 if(jettyLog.getFilename() != null) {
095 logNames.add(jettyLog.getFilename());
096 }
097 }
098 return (String[]) logNames.toArray(new String[logNames.size()]);
099 }
100
101 /**
102 * Gets the names of all log files for this log name.
103 *
104 * @param logName The name of the log for which to return the specific file names.
105 *
106 * @return An array of log file names
107 *
108 */
109 public String[] getLogFileNames(String logName) {
110 List names = new ArrayList();
111
112 // Find all the files for this logName
113 File[] logFiles = getLogFiles(logName);
114
115 if (logFiles !=null) {
116 for (int i = 0; i < logFiles.length; i++) {
117 names.add(logFiles[i].getName());
118 }
119 }
120 return (String[]) names.toArray(new String[names.size()]);
121 }
122
123 /**
124 * Gets the name of all log files used by this log. Typically there
125 * is only one, but specialized cases may use more.
126 *
127 * @param logName The name of the log for which to return the specific files.
128 *
129 * @return An array of all log file names
130 *
131 */
132 private File[] getLogFiles(String logName) {
133 File[] logFiles = null;
134
135 try {
136 String fileNamePattern = logName;
137 if (fileNamePattern.indexOf(FILE_SEPARATOR_UNIX_STYLE) > -1) {
138 fileNamePattern = fileNamePattern.substring(fileNamePattern.lastIndexOf(FILE_SEPARATOR_UNIX_STYLE) + 1);
139 } else if (fileNamePattern.indexOf(FILE_SEPARATOR_WIN_STYLE) > -1) {
140 fileNamePattern = fileNamePattern.substring(fileNamePattern.lastIndexOf(FILE_SEPARATOR_WIN_STYLE) + 1);
141 }
142
143 String logFile = serverInfo.resolvePath(logName);
144
145 File parent = new File(logFile).getParentFile();
146
147 if (parent != null) {
148 logFiles = parent.listFiles(new PatternFilenameFilter(fileNamePattern));
149 }
150 } catch (Exception e) {
151 log.error("Exception attempting to locate Jetty log files", e);
152 logFiles = new File[0];
153 }
154 return logFiles;
155 }
156
157 /**
158 * Searches the log for records matching the specified parameters. The
159 * maximum results returned will be the lesser of 1000 and the
160 * provided maxResults argument.
161 *
162 * @see #MAX_SEARCH_RESULTS
163 */
164 public SearchResults getMatchingItems(String logName, String host, String user, String method, String uri, Date startDate,
165 Date endDate, Integer skipResults, Integer maxResults) {
166
167 // Clean up the arguments so we know what we've really got
168 if(host != null && host.equals("")) host = null;
169 if(user != null && user.equals("")) user = null;
170 if(method != null && method.equals("")) method = null;
171 if(uri != null && uri.equals("")) uri = null;
172
173 long start = startDate == null ? 0 : startDate.getTime();
174 long end = endDate == null ? 0 : endDate.getTime();
175
176 List list = new LinkedList();
177 boolean capped = false;
178 int lineCount = 0, fileCount = 0;
179
180 // Find all the files for this logName
181 File logFiles[] = getLogFiles(logName);
182
183 if (logFiles !=null) {
184 for (int i = 0; i < logFiles.length; i++) {
185 fileCount = 0;
186 try {
187 // Obtain the date for the current log file
188 String fileName = logFiles[i].getName();
189 Matcher fileDate = FILENAME_DATE_PATTERN.matcher(fileName);
190 fileDate.find();
191 SimpleDateFormat simpleFileDate = new SimpleDateFormat(LOG_FILE_NAME_FORMAT);
192 long logFileTime = simpleFileDate.parse(fileDate.group(GROUP_FILENAME_FULL_DATE)).getTime();
193
194 // Check if the dates are null (ignore) or fall within the search range
195 if ( (start==0 && end==0)
196 || (start>0 && start<=logFileTime && end>0 && end>=logFileTime)) {
197
198 // It's in the range, so process the file
199 RandomAccessFile raf = new RandomAccessFile(logFiles[i], "r");
200 FileChannel fc = raf.getChannel();
201 MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
202 CharBuffer cb = Charset.forName("US-ASCII").decode(bb); //todo: does Jetty use a different charset on a foreign PC?
203 Matcher lines = FULL_LINE_PATTERN.matcher(cb);
204 Matcher target = ACCESS_LOG_PATTERN.matcher("");
205 SimpleDateFormat format = (start == 0 && end == 0) ? null : new SimpleDateFormat(ACCESS_LOG_DATE_FORMAT);
206 int max = maxResults == null ? MAX_SEARCH_RESULTS : Math.min(maxResults.intValue(), MAX_SEARCH_RESULTS);
207
208 while(lines.find()) {
209 ++lineCount;
210 ++fileCount;
211 if(capped) {
212 continue;
213 }
214 CharSequence line = cb.subSequence(lines.start(), lines.end());
215 target.reset(line);
216 if(target.find()) {
217 if(host != null && !host.equals(target.group(GROUP_HOST))) {
218 continue;
219 }
220 if(user != null && !user.equals(target.group(GROUP_USER))) {
221 continue;
222 }
223 if(method != null && !method.equals(target.group(GROUP_METHOD))) {
224 continue;
225 }
226 if(uri != null && !target.group(GROUP_URI).startsWith(uri)) {
227 continue;
228 }
229 if(format != null) {
230 try {
231 long entry = format.parse(target.group(GROUP_DATE)).getTime();
232 if(start > entry) {
233 continue;
234 }
235 if(end > 0 && end < entry) {
236 continue;
237 }
238 } catch (ParseException e) {
239 // can't read the date, guess this record counts.
240 }
241 }
242 if(skipResults != null && skipResults.intValue() > lineCount) {
243 continue;
244 }
245 if(list.size() > max) {
246 capped = true;
247 continue;
248 }
249 list.add(new LogMessage(fileCount,line.toString()));
250 }
251 }
252 fc.close();
253 raf.close();
254 }
255 } catch (Exception e) {
256 log.error("Unexpected error processing logs", e);
257 }
258 }
259 }
260 return new SearchResults(lineCount, (LogMessage[]) list.toArray(new LogMessage[list.size()]), capped);
261 }
262
263
264 public static final GBeanInfo GBEAN_INFO;
265
266 static {
267 GBeanInfoBuilder infoFactory = GBeanInfoBuilder.createStatic("Jetty Log Manager", JettyLogManagerImpl.class);
268 infoFactory.addReference("LogGBeans", JettyRequestLog.class);
269 infoFactory.addReference("ServerInfo", ServerInfo.class, "GBean");
270 infoFactory.addInterface(JettyLogManager.class);
271
272 infoFactory.setConstructor(new String[]{"ServerInfo","LogGBeans"});
273 GBEAN_INFO = infoFactory.getBeanInfo();
274 }
275
276 public static GBeanInfo getGBeanInfo() {
277 return GBEAN_INFO;
278 }
279
280 /*
281 * Static inner class implementation of java.io.Filename. This will help us
282 * filter for only the files that we are interested in.
283 */
284 static class PatternFilenameFilter implements FilenameFilter {
285 Pattern pattern;
286 //todo: put this pattern in a GBean parameter?
287 PatternFilenameFilter(String fileNamePattern) {
288 fileNamePattern = fileNamePattern.replaceAll("yyyy", "\\\\d{4}");
289 fileNamePattern = fileNamePattern.replaceAll("yy", "\\\\d{2}");
290 fileNamePattern = fileNamePattern.replaceAll("mm", "\\\\d{2}");
291 fileNamePattern = fileNamePattern.replaceAll("dd", "\\\\d{2}");
292 this.pattern = Pattern.compile(fileNamePattern);
293 }
294
295 public boolean accept(File file, String fileName) {
296 return pattern.matcher(fileName).matches();
297 }
298 }
299 }