1 /* 2 * Copyright (C) 2000 - 2008 Grid Systems, S.A. 3 * 4 * This program is free software; you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License, version 2, as 6 * published by the Free Software Foundation. 7 * 8 * This program is distributed in the hope that it will be useful, 9 * but WITHOUT ANY WARRANTY; without even the implied warranty of 10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 11 * GNU General Public License for more details. 12 * 13 * You should have received a copy of the GNU General Public License 14 * along with this program; if not, write to the Free Software 15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 16 */ 17 package com.gridsystems.maven.furabuild; 18 19 import java.io.File; 20 import java.util.Collection; 21 22 /** 23 * <p>Checks the given resource tree (File), checks that it is valid, and 24 * removes it if it is not.</p> 25 * 26 * <p>The resource tree is considered valid if it is older than a given 27 * source file set.</p> 28 * 29 * @author Rodrigo Ruiz 30 */ 31 public class ResourceValidator { 32 33 /** 34 * Checks that a resource is older than a set of sources. 35 * 36 * @param resources File to check 37 * @param sources Collection of files to compare with 38 * @param purge Whether to remove invalid resources or not 39 * @return <tt>true</tt> if the resource is still valid; <tt>false</tt> otherwise 40 */ 41 public boolean isValid(Collection<File> resources, Collection<File> sources, 42 boolean purge) { 43 if (resources == null) { 44 return true; 45 } else if (purge) { 46 // If purge is active, all resources must be iterated 47 boolean valid = true; 48 for (File f : resources) { 49 boolean result = isValid(f, sources, purge); 50 valid &= result; 51 } 52 return valid; 53 } else { 54 // If purge is not active, it is enough to stop on first "false" 55 for (File f : resources) { 56 if (!isValid(f, sources, purge)) { 57 return false; 58 } 59 } 60 return true; 61 } 62 } 63 64 /** 65 * Checks that a resource is older than a set of sources. 66 * 67 * @param resource File to check 68 * @param sources Collection of files to compare with 69 * @param purge Whether to remove invalid resources or not 70 * @return <tt>true</tt> if the resource is still valid; <tt>false</tt> otherwise 71 */ 72 public boolean isValid(File resource, Collection<File> sources, boolean purge) { 73 if (resource == null) { 74 return true; 75 } else if (!resource.exists()) { 76 return false; 77 } else { 78 long time = getLastModified(resource); 79 if (sources != null) { 80 for (File src : sources) { 81 long tsrc = getLastModified(src); 82 if (tsrc > time) { 83 if (purge) { 84 purge(resource); 85 } 86 return false; 87 } 88 } 89 } 90 91 return true; 92 } 93 } 94 95 /** 96 * Gets the last modified timestamp of a file. If a directory is specified, 97 * it recursively iterates over the directory contents and returns the 98 * earliest timestamp found. 99 * 100 * @param f The file or directory to process 101 * @return The earliest "last-modified" timestamp in <tt>f</tt> 102 */ 103 protected long getLastModified(File f) { 104 assert f != null; 105 106 long t = f.lastModified(); 107 if (f.isDirectory()) { 108 File[] files = f.listFiles(); 109 if (files != null) { 110 for (File ff : files) { 111 t = Math.max(t, getLastModified(ff)); 112 } 113 } 114 } 115 return t; 116 } 117 118 /** 119 * Recursively deletes a resource. 120 * 121 * @param resource The resource (file) to delete 122 */ 123 private void purge(File resource) { 124 if (resource.isDirectory()) { 125 File[] files = resource.listFiles(); 126 if (files != null) { 127 for (File f : files) { 128 if (f.isFile()) { 129 f.delete(); 130 } else { 131 purge(f); 132 } 133 } 134 } 135 } 136 resource.delete(); 137 } 138 }