001: /********************************************************************************
002: * CruiseControl, a Continuous Integration Toolkit
003: * Copyright (c) 2007, ThoughtWorks, Inc.
004: * 200 E. Randolph, 25th Floor
005: * Chicago, IL 60601 USA
006: * All rights reserved.
007: *
008: * Redistribution and use in source and binary forms, with or without
009: * modification, are permitted provided that the following conditions
010: * are met:
011: *
012: * + Redistributions of source code must retain the above copyright
013: * notice, this list of conditions and the following disclaimer.
014: *
015: * + Redistributions in binary form must reproduce the above
016: * copyright notice, this list of conditions and the following
017: * disclaimer in the documentation and/or other materials provided
018: * with the distribution.
019: *
020: * + Neither the name of ThoughtWorks, Inc., CruiseControl, nor the
021: * names of its contributors may be used to endorse or promote
022: * products derived from this software without specific prior
023: * written permission.
024: *
025: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
026: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
027: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
028: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
029: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
030: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
031: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
032: * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
033: * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
034: * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
035: * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
036: ********************************************************************************/package net.sourceforge.cruisecontrol.dashboard;
037:
038: import java.io.File;
039: import java.io.FileInputStream;
040: import java.io.IOException;
041: import java.io.InputStream;
042: import java.util.zip.GZIPInputStream;
043: import java.util.regex.Pattern;
044: import java.util.regex.Matcher;
045:
046: /**
047: * Understands parsing a log file.
048: */
049: public class LogFile extends File {
050: private static final String LOG_COMPRESSED_SUFFIX = ".xml.gz";
051:
052: private String dateTime;
053: private String label;
054:
055: public LogFile(String pathname) {
056: super (pathname);
057: initializeInternalState();
058: }
059:
060: public LogFile(File parent, String child) {
061: super (parent, child);
062: initializeInternalState();
063: }
064:
065: private void initializeInternalState() {
066: Matcher matcher = matcher();
067: validateLogFileName(matcher);
068: dateTime = extractDateTime(matcher);
069: label = extractLabel(matcher);
070: }
071:
072: private String extractLabel(Matcher matcher) {
073: return isSuccessful() ? matcher.group(LABEL_GROUP) : "";
074: }
075:
076: private void validateLogFileName(Matcher matcher) {
077: if (!matcher.matches()) {
078: throw new IllegalArgumentException("Invalid logfile name: "
079: + getName());
080: }
081: }
082:
083: public InputStream getInputStream() throws IOException {
084: FileInputStream fileInputStream = new FileInputStream(this );
085: if (isZippedLogFile()) {
086: return new GZIPInputStream(fileInputStream);
087: } else {
088: return fileInputStream;
089: }
090: }
091:
092: public String getDateTime() {
093: return dateTime;
094: }
095:
096: private boolean isZippedLogFile() {
097: return getName().endsWith(LOG_COMPRESSED_SUFFIX);
098: }
099:
100: private static final int YEAR_GROUP = 1;
101: private static final int MONTH_GROUP = 2;
102: private static final int DAY_GROUP = 3;
103: private static final int HOUR_GROUP = 4;
104: private static final int MINUTE_GROUP = 5;
105: private static final int SECOND_GROUP = 6;
106:
107: private static final int LABEL_GROUP = 7;
108:
109: private String extractDateTime(Matcher matcher) {
110: return matcher.group(YEAR_GROUP) + "-"
111: + matcher.group(MONTH_GROUP) + "-"
112: + matcher.group(DAY_GROUP) + " "
113: + matcher.group(HOUR_GROUP) + ":"
114: + matcher.group(MINUTE_GROUP) + "."
115: + matcher.group(SECOND_GROUP);
116: }
117:
118: private static final Pattern SUCCESSFUL_BUILD_PATTERN = Pattern
119: .compile("^log(\\d{4})(\\d{2})(\\d{2})(\\d{2})(\\d{2})(\\d{2})L(.*)\\.xml(\\.gz)?$");
120:
121: private static final Pattern FAILED_BUILD_PATTERN = Pattern
122: .compile("^log(\\d{4})(\\d{2})(\\d{2})(\\d{2})(\\d{2})(\\d{2})\\.xml(\\.gz)?$");
123:
124: private Matcher matcher() {
125: Pattern pattern = isSuccessful() ? SUCCESSFUL_BUILD_PATTERN
126: : FAILED_BUILD_PATTERN;
127: return pattern.matcher(getName());
128: }
129:
130: private boolean isSuccessful() {
131: return getName().indexOf("L") > 0;
132: }
133:
134: public String getLabel() {
135: return label;
136: }
137: }
|