1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.common.util;
17
18 import java.io.File;
19 import java.io.IOException;
20 import java.util.ArrayList;
21 import java.util.Arrays;
22 import java.util.Collections;
23 import java.util.List;
24 import java.util.Properties;
25
26 import org.apache.commons.io.FileUtils;
27 import org.apache.commons.lang3.StringUtils;
28 import org.slf4j.Logger;
29 import org.slf4j.LoggerFactory;
30
31 public class MetaInfUtils {
32
33 private static final Logger logger = LoggerFactory.getLogger(MetaInfUtils.class);
34
35 public static void scanAndCreateFiles(List<MetaInfContext> contexts) throws IOException {
36 for (MetaInfContext context : contexts) {
37 List<File> files = getFiles(context);
38 List<MetaInfResource> resources = getResources(context, files);
39 doLocations(context, resources);
40 if (context.isAddPropertiesFile()) {
41 doProperties(context, resources);
42 }
43 }
44 }
45
46 public static List<File> getFiles(MetaInfContext context) throws IOException {
47 Assert.notNull(context.getBaseDir(), "baseDir is null");
48 Assert.notNull(context.getOutputFile(), "outputFile is null");
49 logger.debug("Examining " + LocationUtils.getCanonicalPath(context.getBaseDir()));
50 logger.debug("Patterns - {}", getPatternLogMessage(context));
51 List<String> includes = context.getIncludes();
52 List<String> excludes = context.getExcludes();
53 SimpleScanner scanner = new SimpleScanner(context.getBaseDir(), includes, excludes);
54 List<File> files = scanner.getFiles();
55 return files;
56 }
57
58 protected static String getPatternLogMessage(MetaInfContext context) {
59 StringBuilder sb = new StringBuilder();
60 String incl = CollectionUtils.getSpaceSeparatedString(context.getIncludes());
61 String excl = CollectionUtils.getSpaceSeparatedString(context.getExcludes());
62 sb.append("[");
63 if (!StringUtils.isBlank(incl)) {
64 sb.append("include: " + incl);
65 }
66 if (!StringUtils.isBlank(excl)) {
67 sb.append(", exclude:" + excl);
68 }
69 boolean includeEverything = StringUtils.isBlank(incl) && StringUtils.isBlank(excl);
70 if (includeEverything) {
71 sb.append("include: *");
72 }
73 sb.append("]");
74 return sb.toString();
75 }
76
77 public static void doLocations(MetaInfContext context, List<MetaInfResource> resources) throws IOException {
78 List<String> locations = getLocations(resources);
79 if (context.isSort()) {
80 Collections.sort(locations);
81 }
82 String path1 = LocationUtils.getCanonicalPath(context.getBaseDir());
83 String path2 = LocationUtils.getCanonicalPath(context.getOutputFile());
84 String path = StringUtils.remove(path2, path1);
85 logger.info("Creating [" + path + "] - {} resources", locations.size());
86 FileUtils.writeLines(context.getOutputFile(), locations);
87 }
88
89 public static void doProperties(MetaInfContext context, List<MetaInfResource> resources) {
90 logger.debug("doProperties()");
91 Properties properties = getProperties(context, resources);
92 File propertiesFile = new File(LocationUtils.getCanonicalPath(context.getOutputFile()) + ".properties");
93 PropertyUtils.store(properties, propertiesFile, "UTF-8");
94 }
95
96 public static Properties getProperties(MetaInfContext context, List<MetaInfResource> resources) {
97 Properties properties = new Properties();
98 for (MetaInfResource resource : resources) {
99 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 }