View Javadoc

1   /**
2    * Copyright 2010-2013 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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  		List<String> includes = context.getIncludes();
50  		List<String> excludes = context.getExcludes();
51  		logger.debug("Examining - " + context.getBaseDir().getCanonicalPath());
52  		String incl = CollectionUtils.getSpaceSeparatedString(includes);
53  		String excl = CollectionUtils.getSpaceSeparatedString(excludes);
54  		StringBuilder sb = new StringBuilder();
55  		sb.append("include: " + incl);
56  		if (!StringUtils.isBlank(excl)) {
57  			sb.append(", exclude:" + excl);
58  		}
59  		logger.info("[" + sb.toString() + "]");
60  		SimpleScanner scanner = new SimpleScanner(context.getBaseDir(), includes, excludes);
61  		List<File> files = scanner.getFiles();
62  		logger.debug("Located " + files.size() + " files");
63  		return files;
64  	}
65  
66  	public static void doLocations(MetaInfContext context, List<MetaInfResource> resources) throws IOException {
67  		List<String> locations = getLocations(resources);
68  		if (context.isSort()) {
69  			Collections.sort(locations);
70  		}
71  		String path1 = LocationUtils.getCanonicalPath(context.getBaseDir());
72  		String path2 = LocationUtils.getCanonicalPath(context.getOutputFile());
73  		String path = StringUtils.remove(path2, path1);
74  		logger.info("Creating [" + path + "] - {} resources", locations.size());
75  		FileUtils.writeLines(context.getOutputFile(), locations);
76  	}
77  
78  	public static void doProperties(MetaInfContext context, List<MetaInfResource> resources) {
79  		logger.debug("doProperties()");
80  		Properties properties = getProperties(context, resources);
81  		File propertiesFile = new File(LocationUtils.getCanonicalPath(context.getOutputFile()) + ".properties");
82  		PropertyUtils.store(properties, propertiesFile, "UTF-8");
83  	}
84  
85  	public static Properties getProperties(MetaInfContext context, List<MetaInfResource> resources) {
86  		Properties properties = new Properties();
87  		for (MetaInfResource resource : resources) {
88  			String sizeKey = resource.getKey() + ".size";
89  			properties.setProperty(sizeKey, resource.getSize() + "");
90  			if (context.isAddLineCount()) {
91  				String linesKey = resource.getKey() + ".lines";
92  				properties.setProperty(linesKey, resource.getLines() + "");
93  			}
94  		}
95  		return properties;
96  	}
97  
98  	public static String getPropertyKey(String location) {
99  		String key = StringUtils.replace(location, ":", ".");
100 		return StringUtils.replace(key, "/", ".");
101 	}
102 
103 	public static void scanAndCreateFile(MetaInfContext context) throws IOException {
104 		scanAndCreateFiles(Arrays.asList(context));
105 	}
106 
107 	public static List<MetaInfResource> getResources(MetaInfContext context, List<File> files) throws IOException {
108 		List<MetaInfResource> resources = new ArrayList<MetaInfResource>();
109 		for (int i = 0; i < files.size(); i++) {
110 			MetaInfResource resource = getResource(context, files.get(i));
111 			resources.add(resource);
112 		}
113 		return resources;
114 	}
115 
116 	public static List<String> getLocations(List<MetaInfResource> resources) {
117 		List<String> locations = new ArrayList<String>();
118 		for (MetaInfResource resource : resources) {
119 			locations.add(resource.getLocation());
120 		}
121 		return locations;
122 	}
123 
124 	public static List<String> getLocations(File baseDir, List<File> files, String prefix) throws IOException {
125 		List<String> locations = new ArrayList<String>();
126 		for (int i = 0; i < files.size(); i++) {
127 			String location = getLocation(baseDir, files.get(i), prefix);
128 			locations.add(location);
129 		}
130 		return locations;
131 	}
132 
133 	public static MetaInfResource getResource(MetaInfContext context, File file) throws IOException {
134 		String location = getLocation(context.getBaseDir(), file, context.getPrefix());
135 		long size = file.length();
136 		long lines = -1;
137 		if (context.isAddLineCount()) {
138 			lines = LocationUtils.getLineCount(file);
139 		}
140 		String key = getPropertyKey(location);
141 
142 		MetaInfResource resource = new MetaInfResource();
143 		resource.setLocation(location);
144 		resource.setSize(size);
145 		resource.setKey(key);
146 		resource.setLines(lines);
147 		return resource;
148 	}
149 
150 	public static String getLocation(File baseDir, File file, String prefix) throws IOException {
151 		String dir = baseDir.getCanonicalPath();
152 		String path = file.getCanonicalPath();
153 		int pos = dir.length() + 1;
154 		return prefix + StringUtils.substring(path, pos);
155 	}
156 
157 }