KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sourceforge > cruisecontrol > sourcecontrols > HttpFile


1 /********************************************************************************
2  * CruiseControl, a Continuous Integration Toolkit
3  * Copyright (c) 2001, ThoughtWorks, Inc.
4  * 651 W Washington Ave. Suite 600
5  * Chicago, IL 60661 USA
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * + Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * + Redistributions in binary form must reproduce the above
16  * copyright notice, this list of conditions and the following
17  * disclaimer in the documentation and/or other materials provided
18  * with the distribution.
19  *
20  * + Neither the name of ThoughtWorks, Inc., CruiseControl, nor the
21  * names of its contributors may be used to endorse or promote
22  * products derived from this software without specific prior
23  * written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR
29  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  ********************************************************************************/

37 package net.sourceforge.cruisecontrol.sourcecontrols;
38
39 import java.io.IOException JavaDoc;
40 import java.net.MalformedURLException JavaDoc;
41 import java.net.URL JavaDoc;
42 import java.net.URLConnection JavaDoc;
43 import java.util.ArrayList JavaDoc;
44 import java.util.Date JavaDoc;
45 import java.util.Hashtable JavaDoc;
46 import java.util.List JavaDoc;
47 import java.util.Map JavaDoc;
48
49 import net.sourceforge.cruisecontrol.CruiseControlException;
50 import net.sourceforge.cruisecontrol.Modification;
51 import net.sourceforge.cruisecontrol.util.ValidationHelper;
52
53 import org.apache.log4j.Logger;
54
55 /**
56  * Checks a single file on a web server that supports the last-modified header
57  *
58  * @author <a HREF="mailto:yourgod@users.sourceforge.net">Brad Clarke</a>
59  */

60 public class HttpFile extends FakeUserSourceControl {
61     private static Logger log = Logger.getLogger(HttpFile.class);
62     private String JavaDoc urlString;
63
64         public void setURL(String JavaDoc urlString) {
65         this.urlString = urlString;
66     }
67
68     public Map JavaDoc getProperties() {
69         return new Hashtable JavaDoc();
70     }
71
72     public void validate() throws CruiseControlException {
73         ValidationHelper.assertIsSet(urlString, "url", this.getClass());
74         try {
75             new URL JavaDoc(this.urlString);
76         } catch (MalformedURLException JavaDoc e) {
77             ValidationHelper.fail("'url' is not a valid connection string", e);
78         }
79     }
80
81     /**
82      * For this case, we don't care about the quietperiod, only that
83      * one user is modifying the build.
84      *
85      * @param lastBuild date of last build
86      * @param now IGNORED
87      */

88     public List JavaDoc getModifications(Date JavaDoc lastBuild, Date JavaDoc now) {
89         long lastModified;
90         final URL JavaDoc url;
91         try {
92             url = new URL JavaDoc(this.urlString);
93         } catch (MalformedURLException JavaDoc e) {
94             // already checked
95
return new ArrayList JavaDoc();
96         }
97         try {
98             lastModified = getURLLastModified(url);
99         } catch (IOException JavaDoc e) {
100             log.error("Could not connect to 'url'", e);
101             return new ArrayList JavaDoc();
102         }
103         List JavaDoc modifiedList = new ArrayList JavaDoc();
104         if (lastModified > lastBuild.getTime()) {
105             Modification mod = new Modification("http");
106             mod.createModifiedFile(url.getFile().substring(1), url.getHost());
107
108             mod.userName = getUserName();
109             mod.modifiedTime = new Date JavaDoc(lastModified);
110             mod.comment = "";
111             modifiedList.add(mod);
112         }
113         return modifiedList;
114     }
115
116     protected long getURLLastModified(final URL JavaDoc url) throws IOException JavaDoc {
117         final URLConnection JavaDoc con = url.openConnection();
118         long lastModified = con.getLastModified();
119         try {
120           con.getInputStream().close();
121         } catch (IOException JavaDoc ignored) {
122         }
123         return lastModified;
124     }
125 }
126
Popular Tags