001/** 002 * Copyright 2010-2014 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 */ 016package org.kuali.common.util; 017 018import java.io.File; 019import java.io.IOException; 020import java.util.ArrayList; 021import java.util.Arrays; 022import java.util.Collections; 023import java.util.List; 024import java.util.Properties; 025 026import org.apache.commons.io.FileUtils; 027import org.apache.commons.lang3.StringUtils; 028import org.slf4j.Logger; 029import org.slf4j.LoggerFactory; 030 031/** 032 * @deprecated 033 */ 034@Deprecated 035public class MetaInfUtils { 036 037 private static final Logger logger = LoggerFactory.getLogger(MetaInfUtils.class); 038 039 public static void scanAndCreateFiles(List<org.kuali.common.util.metainf.MetaInfContext> contexts) throws IOException { 040 for (org.kuali.common.util.metainf.MetaInfContext context : contexts) { 041 List<File> files = getFiles(context); 042 List<MetaInfResource> resources = getResources(context, files); 043 doLocations(context, resources); 044 if (context.isAddPropertiesFile()) { 045 doProperties(context, resources); 046 } 047 } 048 } 049 050 public static List<File> getFiles(org.kuali.common.util.metainf.MetaInfContext context) throws IOException { 051 Assert.notNull(context.getBaseDir(), "baseDir is null"); 052 Assert.notNull(context.getOutputFile(), "outputFile is null"); 053 logger.debug("Examining " + LocationUtils.getCanonicalPath(context.getBaseDir())); 054 logger.debug("Patterns - {}", getPatternLogMessage(context)); 055 List<String> includes = context.getIncludes(); 056 List<String> excludes = context.getExcludes(); 057 SimpleScanner scanner = new SimpleScanner(context.getBaseDir(), includes, excludes); 058 return scanner.getFiles(); 059 } 060 061 protected static String getPatternLogMessage(org.kuali.common.util.metainf.MetaInfContext context) { 062 StringBuilder sb = new StringBuilder(); 063 String incl = CollectionUtils.getSpaceSeparatedString(context.getIncludes()); 064 String excl = CollectionUtils.getSpaceSeparatedString(context.getExcludes()); 065 sb.append("["); 066 if (!StringUtils.isBlank(incl)) { 067 sb.append("include: ").append(incl); 068 } 069 if (!StringUtils.isBlank(excl)) { 070 sb.append(", exclude:").append(excl); 071 } 072 boolean includeEverything = StringUtils.isBlank(incl) && StringUtils.isBlank(excl); 073 if (includeEverything) { 074 sb.append("include: *"); 075 } 076 sb.append("]"); 077 return sb.toString(); 078 } 079 080 public static void doLocations(org.kuali.common.util.metainf.MetaInfContext context, List<MetaInfResource> resources) throws IOException { 081 List<String> locations = getLocations(resources); 082 if (context.isSort()) { 083 Collections.sort(locations); 084 } 085 String path1 = LocationUtils.getCanonicalPath(context.getBaseDir()); 086 String path2 = LocationUtils.getCanonicalPath(context.getOutputFile()); 087 String path = StringUtils.remove(path2, path1); 088 logger.info("Creating [" + path + "] - {} resources", locations.size()); 089 FileUtils.writeLines(context.getOutputFile(), locations); 090 } 091 092 public static void doProperties(org.kuali.common.util.metainf.MetaInfContext context, List<MetaInfResource> resources) { 093 logger.debug("doProperties()"); 094 Properties properties = getProperties(context, resources); 095 File propertiesFile = new File(LocationUtils.getCanonicalPath(context.getOutputFile()) + ".properties"); 096 PropertyUtils.store(properties, propertiesFile, Encodings.UTF8); 097 } 098 099 public static Properties getProperties(org.kuali.common.util.metainf.MetaInfContext context, List<MetaInfResource> resources) { 100 Properties properties = new Properties(); 101 for (MetaInfResource resource : resources) { 102 String sizeKey = resource.getKey() + ".size"; 103 properties.setProperty(sizeKey, resource.getSize() + ""); 104 if (context.isAddLineCount()) { 105 String linesKey = resource.getKey() + ".lines"; 106 properties.setProperty(linesKey, resource.getLines() + ""); 107 } 108 } 109 return properties; 110 } 111 112 public static String getPropertyKey(String location) { 113 String key = StringUtils.replace(location, ":", "."); 114 return StringUtils.replace(key, "/", "."); 115 } 116 117 public static void scanAndCreateFile(org.kuali.common.util.metainf.MetaInfContext context) throws IOException { 118 scanAndCreateFiles(Arrays.asList(context)); 119 } 120 121 public static List<MetaInfResource> getResources(org.kuali.common.util.metainf.MetaInfContext context, List<File> files) throws IOException { 122 List<MetaInfResource> resources = new ArrayList<MetaInfResource>(); 123 for (File file : files) { 124 MetaInfResource resource = getResource(context, file); 125 resources.add(resource); 126 } 127 return resources; 128 } 129 130 public static List<String> getLocations(List<MetaInfResource> resources) { 131 List<String> locations = new ArrayList<String>(); 132 for (MetaInfResource resource : resources) { 133 locations.add(resource.getLocation()); 134 } 135 return locations; 136 } 137 138 public static List<String> getLocations(File baseDir, List<File> files, String prefix) throws IOException { 139 List<String> locations = new ArrayList<String>(); 140 for (File file : files) { 141 String location = getLocation(baseDir, file, prefix); 142 locations.add(location); 143 } 144 return locations; 145 } 146 147 public static MetaInfResource getResource(org.kuali.common.util.metainf.MetaInfContext context, File file) throws IOException { 148 String location = getLocation(context.getBaseDir(), file, context.getPrefix()); 149 long size = file.length(); 150 long lines = -1; 151 if (context.isAddLineCount()) { 152 lines = LocationUtils.getLineCount(file); 153 } 154 String key = getPropertyKey(location); 155 156 MetaInfResource resource = new MetaInfResource(); 157 resource.setLocation(location); 158 resource.setSize(size); 159 resource.setKey(key); 160 resource.setLines(lines); 161 return resource; 162 } 163 164 public static String getLocation(File baseDir, File file, String prefix) throws IOException { 165 String dir = baseDir.getCanonicalPath(); 166 String path = file.getCanonicalPath(); 167 int pos = dir.length() + 1; 168 return prefix + StringUtils.substring(path, pos); 169 } 170 171}