001: /********************************************************************************
002: * CruiseControl, a Continuous Integration Toolkit
003: * Copyright (c) 2001, 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.sourcecontrols;
037:
038: import java.io.IOException;
039: import java.net.MalformedURLException;
040: import java.net.URL;
041: import java.net.URLConnection;
042: import java.util.ArrayList;
043: import java.util.Date;
044: import java.util.List;
045:
046: import net.sourceforge.cruisecontrol.CruiseControlException;
047: import net.sourceforge.cruisecontrol.Modification;
048: import net.sourceforge.cruisecontrol.util.ValidationHelper;
049:
050: import org.apache.log4j.Logger;
051:
052: /**
053: * Checks a single file on a web server that supports the last-modified header
054: *
055: * @author <a href="mailto:yourgod@users.sourceforge.net">Brad Clarke</a>
056: */
057: public class HttpFile extends FakeUserSourceControl {
058: private static Logger log = Logger.getLogger(HttpFile.class);
059: private String urlString;
060:
061: public void setURL(String urlString) {
062: this .urlString = urlString;
063: }
064:
065: public void validate() throws CruiseControlException {
066: ValidationHelper.assertIsSet(urlString, "url", this .getClass());
067: try {
068: new URL(this .urlString);
069: } catch (MalformedURLException e) {
070: ValidationHelper.fail(
071: "'url' is not a valid connection string", e);
072: }
073: }
074:
075: /**
076: * For this case, we don't care about the quietperiod, only that
077: * one user is modifying the build.
078: *
079: * @param lastBuild date of last build
080: * @param now IGNORED
081: */
082: public List getModifications(Date lastBuild, Date now) {
083: long lastModified;
084: final URL url;
085: try {
086: url = new URL(this .urlString);
087: } catch (MalformedURLException e) {
088: // already checked
089: return new ArrayList();
090: }
091: try {
092: lastModified = getURLLastModified(url);
093: } catch (IOException e) {
094: log.error("Could not connect to 'url'", e);
095: return new ArrayList();
096: }
097: List modifiedList = new ArrayList();
098: if (lastModified > lastBuild.getTime()) {
099: Modification mod = new Modification("http");
100: mod.createModifiedFile(getFilename(url), url.getHost());
101:
102: mod.userName = getUserName();
103: mod.modifiedTime = new Date(lastModified);
104: mod.comment = "";
105: modifiedList.add(mod);
106: }
107:
108: if (!modifiedList.isEmpty()) {
109: getSourceControlProperties().modificationFound();
110: }
111:
112: return modifiedList;
113: }
114:
115: private String getFilename(final URL url) {
116: String fileName = url.getFile();
117: if (fileName.startsWith("/")) {
118: fileName = fileName.substring(1);
119: }
120: return fileName;
121: }
122:
123: protected long getURLLastModified(final URL url) throws IOException {
124: final URLConnection con = url.openConnection();
125: long lastModified = con.getLastModified();
126: try {
127: con.getInputStream().close();
128: } catch (IOException ignored) {
129: }
130: return lastModified;
131: }
132: }
|