001 /** 002 * Copyright 2010-2013 The Kuali Foundation 003 * 004 * Licensed under the Educational Community License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.opensource.org/licenses/ecl2.php 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 package org.kuali.common.util; 017 018 import java.io.File; 019 import java.io.IOException; 020 import java.util.ArrayList; 021 import java.util.Arrays; 022 import java.util.Collections; 023 import java.util.List; 024 import java.util.Properties; 025 026 import org.apache.commons.io.FileUtils; 027 import org.apache.commons.lang3.StringUtils; 028 import org.slf4j.Logger; 029 import org.slf4j.LoggerFactory; 030 031 public class MetaInfUtils { 032 033 private static final Logger logger = LoggerFactory.getLogger(MetaInfUtils.class); 034 035 public static void scanAndCreateFiles(List<MetaInfContext> contexts) throws IOException { 036 for (MetaInfContext context : contexts) { 037 List<File> files = getFiles(context); 038 List<MetaInfResource> resources = getResources(context, files); 039 doLocations(context, resources); 040 if (context.isAddPropertiesFile()) { 041 doProperties(context, resources); 042 } 043 } 044 } 045 046 public static List<File> getFiles(MetaInfContext context) throws IOException { 047 Assert.notNull(context.getBaseDir(), "baseDir is null"); 048 Assert.notNull(context.getOutputFile(), "outputFile is null"); 049 logger.debug("Examining " + LocationUtils.getCanonicalPath(context.getBaseDir())); 050 logger.debug("Patterns - {}", getPatternLogMessage(context)); 051 List<String> includes = context.getIncludes(); 052 List<String> excludes = context.getExcludes(); 053 SimpleScanner scanner = new SimpleScanner(context.getBaseDir(), includes, excludes); 054 List<File> files = scanner.getFiles(); 055 return files; 056 } 057 058 protected static String getPatternLogMessage(MetaInfContext context) { 059 StringBuilder sb = new StringBuilder(); 060 String incl = CollectionUtils.getSpaceSeparatedString(context.getIncludes()); 061 String excl = CollectionUtils.getSpaceSeparatedString(context.getExcludes()); 062 sb.append("["); 063 if (!StringUtils.isBlank(incl)) { 064 sb.append("include: " + incl); 065 } 066 if (!StringUtils.isBlank(excl)) { 067 sb.append(", exclude:" + excl); 068 } 069 boolean includeEverything = StringUtils.isBlank(incl) && StringUtils.isBlank(excl); 070 if (includeEverything) { 071 sb.append("include: *"); 072 } 073 sb.append("]"); 074 return sb.toString(); 075 } 076 077 public static void doLocations(MetaInfContext context, List<MetaInfResource> resources) throws IOException { 078 List<String> locations = getLocations(resources); 079 if (context.isSort()) { 080 Collections.sort(locations); 081 } 082 String path1 = LocationUtils.getCanonicalPath(context.getBaseDir()); 083 String path2 = LocationUtils.getCanonicalPath(context.getOutputFile()); 084 String path = StringUtils.remove(path2, path1); 085 logger.info("Creating [" + path + "] - {} resources", locations.size()); 086 FileUtils.writeLines(context.getOutputFile(), locations); 087 } 088 089 public static void doProperties(MetaInfContext context, List<MetaInfResource> resources) { 090 logger.debug("doProperties()"); 091 Properties properties = getProperties(context, resources); 092 File propertiesFile = new File(LocationUtils.getCanonicalPath(context.getOutputFile()) + ".properties"); 093 PropertyUtils.store(properties, propertiesFile, "UTF-8"); 094 } 095 096 public static Properties getProperties(MetaInfContext context, List<MetaInfResource> resources) { 097 Properties properties = new Properties(); 098 for (MetaInfResource resource : resources) { 099 String sizeKey = resource.getKey() + ".size"; 100 properties.setProperty(sizeKey, resource.getSize() + ""); 101 if (context.isAddLineCount()) { 102 String linesKey = resource.getKey() + ".lines"; 103 properties.setProperty(linesKey, resource.getLines() + ""); 104 } 105 } 106 return properties; 107 } 108 109 public static String getPropertyKey(String location) { 110 String key = StringUtils.replace(location, ":", "."); 111 return StringUtils.replace(key, "/", "."); 112 } 113 114 public static void scanAndCreateFile(MetaInfContext context) throws IOException { 115 scanAndCreateFiles(Arrays.asList(context)); 116 } 117 118 public static List<MetaInfResource> getResources(MetaInfContext context, List<File> files) throws IOException { 119 List<MetaInfResource> resources = new ArrayList<MetaInfResource>(); 120 for (int i = 0; i < files.size(); i++) { 121 MetaInfResource resource = getResource(context, files.get(i)); 122 resources.add(resource); 123 } 124 return resources; 125 } 126 127 public static List<String> getLocations(List<MetaInfResource> resources) { 128 List<String> locations = new ArrayList<String>(); 129 for (MetaInfResource resource : resources) { 130 locations.add(resource.getLocation()); 131 } 132 return locations; 133 } 134 135 public static List<String> getLocations(File baseDir, List<File> files, String prefix) throws IOException { 136 List<String> locations = new ArrayList<String>(); 137 for (int i = 0; i < files.size(); i++) { 138 String location = getLocation(baseDir, files.get(i), prefix); 139 locations.add(location); 140 } 141 return locations; 142 } 143 144 public static MetaInfResource getResource(MetaInfContext context, File file) throws IOException { 145 String location = getLocation(context.getBaseDir(), file, context.getPrefix()); 146 long size = file.length(); 147 long lines = -1; 148 if (context.isAddLineCount()) { 149 lines = LocationUtils.getLineCount(file); 150 } 151 String key = getPropertyKey(location); 152 153 MetaInfResource resource = new MetaInfResource(); 154 resource.setLocation(location); 155 resource.setSize(size); 156 resource.setKey(key); 157 resource.setLines(lines); 158 return resource; 159 } 160 161 public static String getLocation(File baseDir, File file, String prefix) throws IOException { 162 String dir = baseDir.getCanonicalPath(); 163 String path = file.getCanonicalPath(); 164 int pos = dir.length() + 1; 165 return prefix + StringUtils.substring(path, pos); 166 } 167 168 }