1 package org.kuali.common.util; 2 3 import java.io.File; 4 import java.io.IOException; 5 import java.util.ArrayList; 6 import java.util.Arrays; 7 import java.util.Collections; 8 import java.util.List; 9 import java.util.Properties; 10 11 import org.apache.commons.io.FileUtils; 12 import org.codehaus.plexus.util.StringUtils; 13 import org.slf4j.Logger; 14 import org.slf4j.LoggerFactory; 15 16 public class MetaInfUtils { 17 18 private static final Logger logger = LoggerFactory.getLogger(MetaInfUtils.class); 19 20 public static void scanAndCreateFiles(List<MetaInfContext> contexts) throws IOException { 21 for (MetaInfContext context : contexts) { 22 List<File> files = getFiles(context); 23 List<MetaInfResource> resources = getResources(context, files); 24 doLocations(context, resources); 25 if (context.isAddPropertiesFile()) { 26 doProperties(context, resources); 27 } 28 } 29 } 30 31 public static List<File> getFiles(MetaInfContext context) throws IOException { 32 Assert.notNull(context.getBaseDir(), "baseDir is null"); 33 Assert.notNull(context.getOutputFile(), "outputFile is null"); 34 List<String> includes = context.getIncludes(); 35 List<String> excludes = context.getExcludes(); 36 logger.info("Examining - " + context.getBaseDir().getCanonicalPath()); 37 logger.info("Include - " + CollectionUtils.getSpaceSeparatedString(includes)); 38 logger.info("Exclude - " + CollectionUtils.getSpaceSeparatedString(excludes)); 39 SimpleScanner scanner = new SimpleScanner(context.getBaseDir(), includes, excludes); 40 List<File> files = scanner.getFiles(); 41 logger.info("Located " + files.size() + " files"); 42 return files; 43 } 44 45 public static void doLocations(MetaInfContext context, List<MetaInfResource> resources) throws IOException { 46 List<String> locations = getLocations(resources); 47 if (context.isSort()) { 48 Collections.sort(locations); 49 } 50 logger.info("Creating " + context.getOutputFile().getCanonicalPath()); 51 FileUtils.writeLines(context.getOutputFile(), locations); 52 } 53 54 public static void doProperties(MetaInfContext context, List<MetaInfResource> resources) { 55 logger.debug("doProperties()"); 56 Properties properties = getProperties(context, resources); 57 File propertiesFile = new File(LocationUtils.getCanonicalPath(context.getOutputFile()) + ".properties"); 58 PropertyUtils.store(properties, propertiesFile, "UTF-8"); 59 } 60 61 public static Properties getProperties(MetaInfContext context, List<MetaInfResource> resources) { 62 Properties properties = new Properties(); 63 for (MetaInfResource resource : resources) { 64 String sizeKey = resource.getKey() + ".size"; 65 properties.setProperty(sizeKey, resource.getSize() + ""); 66 if (context.isAddLineCount()) { 67 String linesKey = resource.getKey() + ".lines"; 68 properties.setProperty(linesKey, resource.getLines() + ""); 69 } 70 } 71 return properties; 72 } 73 74 public static String getPropertyKey(String location) { 75 String key = StringUtils.replace(location, ":", "."); 76 return StringUtils.replace(key, "/", "."); 77 } 78 79 public static void scanAndCreateFile(MetaInfContext context) throws IOException { 80 scanAndCreateFiles(Arrays.asList(context)); 81 } 82 83 public static List<MetaInfResource> getResources(MetaInfContext context, List<File> files) throws IOException { 84 List<MetaInfResource> resources = new ArrayList<MetaInfResource>(); 85 for (int i = 0; i < files.size(); i++) { 86 MetaInfResource resource = getResource(context, files.get(i)); 87 resources.add(resource); 88 } 89 return resources; 90 } 91 92 public static List<String> getLocations(List<MetaInfResource> resources) { 93 List<String> locations = new ArrayList<String>(); 94 for (MetaInfResource resource : resources) { 95 locations.add(resource.getLocation()); 96 } 97 return locations; 98 } 99 100 public static List<String> getLocations(File baseDir, List<File> files, String prefix) throws IOException { 101 List<String> locations = new ArrayList<String>(); 102 for (int i = 0; i < files.size(); i++) { 103 String location = getLocation(baseDir, files.get(i), prefix); 104 locations.add(location); 105 } 106 return locations; 107 } 108 109 public static MetaInfResource getResource(MetaInfContext context, File file) throws IOException { 110 String location = getLocation(context.getBaseDir(), file, context.getPrefix()); 111 long size = file.length(); 112 long lines = -1; 113 if (context.isAddLineCount()) { 114 lines = LocationUtils.getLineCount(file); 115 } 116 String key = getPropertyKey(location); 117 118 MetaInfResource resource = new MetaInfResource(); 119 resource.setLocation(location); 120 resource.setSize(size); 121 resource.setKey(key); 122 resource.setLines(lines); 123 return resource; 124 } 125 126 public static String getLocation(File baseDir, File file, String prefix) throws IOException { 127 String dir = baseDir.getCanonicalPath(); 128 String path = file.getCanonicalPath(); 129 int pos = dir.length() + 1; 130 return prefix + StringUtils.substring(path, pos); 131 } 132 133 }