View Javadoc
1   /**
2    * Copyright 2010-2014 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  /**
32   * @deprecated
33   */
34  @Deprecated
35  public class MetaInfUtils {
36  
37  	private static final Logger logger = LoggerFactory.getLogger(MetaInfUtils.class);
38  
39  	public static void scanAndCreateFiles(List<org.kuali.common.util.metainf.MetaInfContext> contexts) throws IOException {
40  		for (org.kuali.common.util.metainf.MetaInfContext context : contexts) {
41  			List<File> files = getFiles(context);
42  			List<MetaInfResource> resources = getResources(context, files);
43  			doLocations(context, resources);
44  			if (context.isAddPropertiesFile()) {
45  				doProperties(context, resources);
46  			}
47  		}
48  	}
49  
50  	public static List<File> getFiles(org.kuali.common.util.metainf.MetaInfContext context) throws IOException {
51  		Assert.notNull(context.getBaseDir(), "baseDir is null");
52  		Assert.notNull(context.getOutputFile(), "outputFile is null");
53  		logger.debug("Examining " + LocationUtils.getCanonicalPath(context.getBaseDir()));
54  		logger.debug("Patterns - {}", getPatternLogMessage(context));
55  		List<String> includes = context.getIncludes();
56  		List<String> excludes = context.getExcludes();
57  		SimpleScanner scanner = new SimpleScanner(context.getBaseDir(), includes, excludes);
58  		return scanner.getFiles();
59  	}
60  
61  	protected static String getPatternLogMessage(org.kuali.common.util.metainf.MetaInfContext context) {
62  		StringBuilder sb = new StringBuilder();
63  		String incl = CollectionUtils.getSpaceSeparatedString(context.getIncludes());
64  		String excl = CollectionUtils.getSpaceSeparatedString(context.getExcludes());
65  		sb.append("[");
66  		if (!StringUtils.isBlank(incl)) {
67  			sb.append("include: ").append(incl);
68  		}
69  		if (!StringUtils.isBlank(excl)) {
70  			sb.append(", exclude:").append(excl);
71  		}
72  		boolean includeEverything = StringUtils.isBlank(incl) && StringUtils.isBlank(excl);
73  		if (includeEverything) {
74  			sb.append("include: *");
75  		}
76  		sb.append("]");
77  		return sb.toString();
78  	}
79  
80  	public static void doLocations(org.kuali.common.util.metainf.MetaInfContext context, List<MetaInfResource> resources) throws IOException {
81  		List<String> locations = getLocations(resources);
82  		if (context.isSort()) {
83  			Collections.sort(locations);
84  		}
85  		String path1 = LocationUtils.getCanonicalPath(context.getBaseDir());
86  		String path2 = LocationUtils.getCanonicalPath(context.getOutputFile());
87  		String path = StringUtils.remove(path2, path1);
88  		logger.info("Creating [" + path + "] - {} resources", locations.size());
89  		FileUtils.writeLines(context.getOutputFile(), locations);
90  	}
91  
92  	public static void doProperties(org.kuali.common.util.metainf.MetaInfContext context, List<MetaInfResource> resources) {
93  		logger.debug("doProperties()");
94  		Properties properties = getProperties(context, resources);
95  		File propertiesFile = new File(LocationUtils.getCanonicalPath(context.getOutputFile()) + ".properties");
96  		PropertyUtils.store(properties, propertiesFile, Encodings.UTF8);
97  	}
98  
99  	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 }