1 37 package net.sourceforge.cruisecontrol.sourcecontrols; 38 39 import java.io.IOException ; 40 import java.net.MalformedURLException ; 41 import java.net.URL ; 42 import java.net.URLConnection ; 43 import java.util.ArrayList ; 44 import java.util.Date ; 45 import java.util.Hashtable ; 46 import java.util.List ; 47 import java.util.Map ; 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 60 public class HttpFile extends FakeUserSourceControl { 61 private static Logger log = Logger.getLogger(HttpFile.class); 62 private String urlString; 63 64 public void setURL(String urlString) { 65 this.urlString = urlString; 66 } 67 68 public Map getProperties() { 69 return new Hashtable (); 70 } 71 72 public void validate() throws CruiseControlException { 73 ValidationHelper.assertIsSet(urlString, "url", this.getClass()); 74 try { 75 new URL (this.urlString); 76 } catch (MalformedURLException e) { 77 ValidationHelper.fail("'url' is not a valid connection string", e); 78 } 79 } 80 81 88 public List getModifications(Date lastBuild, Date now) { 89 long lastModified; 90 final URL url; 91 try { 92 url = new URL (this.urlString); 93 } catch (MalformedURLException e) { 94 return new ArrayList (); 96 } 97 try { 98 lastModified = getURLLastModified(url); 99 } catch (IOException e) { 100 log.error("Could not connect to 'url'", e); 101 return new ArrayList (); 102 } 103 List modifiedList = new ArrayList (); 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 (lastModified); 110 mod.comment = ""; 111 modifiedList.add(mod); 112 } 113 return modifiedList; 114 } 115 116 protected long getURLLastModified(final URL url) throws IOException { 117 final URLConnection con = url.openConnection(); 118 long lastModified = con.getLastModified(); 119 try { 120 con.getInputStream().close(); 121 } catch (IOException ignored) { 122 } 123 return lastModified; 124 } 125 } 126 | Popular Tags |