View Javadoc
1   package org.kuali.common.util.metainf.spring;
2   
3   import java.io.File;
4   import java.util.ArrayList;
5   import java.util.Comparator;
6   import java.util.List;
7   import java.util.Map;
8   
9   import org.kuali.common.util.metainf.model.MetaInfContext;
10  import org.kuali.common.util.metainf.model.MetaInfResource;
11  import org.kuali.common.util.metainf.model.MetaInfResourceLocationComparator;
12  import org.kuali.common.util.metainf.model.MetaInfResourcePathComparator;
13  import org.kuali.common.util.metainf.service.MetaInfUtils;
14  import org.kuali.common.util.nullify.NullUtils;
15  import org.kuali.common.util.project.ProjectUtils;
16  import org.kuali.common.util.project.model.Build;
17  import org.kuali.common.util.project.model.Project;
18  import org.kuali.common.util.project.spring.AutowiredProjectConfig;
19  import org.kuali.common.util.spring.SpringUtils;
20  import org.kuali.common.util.spring.env.EnvironmentService;
21  import org.kuali.common.util.spring.service.SpringServiceConfig;
22  import org.springframework.beans.factory.annotation.Autowired;
23  import org.springframework.context.annotation.Bean;
24  import org.springframework.context.annotation.Configuration;
25  import org.springframework.context.annotation.Import;
26  
27  import com.google.common.collect.Maps;
28  
29  /**
30   * TODO Should not be a class called {@code RiceXmlConfig} down here in kuali-util. Create a rice-util and move this there? Main issue preventing this from living in the rice-xml
31   * module itself is that it gets tricky having software used very early in the build lifecycle reside in the same project that makes use of it.
32   */
33  @Configuration
34  @Import({ AutowiredProjectConfig.class, MetaInfExecutableConfig.class, SpringServiceConfig.class })
35  public class RiceXmlConfig implements MetaInfContextsConfig {
36  
37  	private static final boolean DEFAULT_GENERATE_RELATIVE_PATHS = true;
38  	private static final String RELATIVE_KEY = MetaInfUtils.PROPERTY_PREFIX + ".xml.relative";
39  	private static final String PREFIX = "xml";
40  
41  	// This is used in the rice-xml module to help locate the correct .resources file containing the XML to ingest
42  	public static final String INGEST_FILENAME = "ingest";
43  
44  	@Autowired
45  	EnvironmentService env;
46  
47  	@Autowired
48  	Project project;
49  
50  	@Autowired
51  	Build build;
52  
53  	@Override
54  	@Bean
55  	public List<MetaInfContext> metaInfContexts() {
56  		List<MetaInfContext> contexts = new ArrayList<MetaInfContext>();
57  		MetaInfContext context = getMetaInfContext(MetaInfGroup.OTHER);
58  		contexts.add(context);
59  		return contexts;
60  	}
61  
62  	protected MetaInfContext getMetaInfContext(MetaInfGroup group) {
63  		Map<MetaInfGroup, String> defaultIncludes = getDefaultIncludes(project);
64  		Map<MetaInfGroup, String> defaultExcludes = getDefaultExcludes();
65  		boolean relativePaths = env.getBoolean(RELATIVE_KEY, DEFAULT_GENERATE_RELATIVE_PATHS);
66  		File outputFile = MetaInfUtils.getOutputFile(project, build, INGEST_FILENAME);
67  		String includesKey = MetaInfConfigUtils.getIncludesKey(group, PREFIX);
68  		String excludesKey = MetaInfConfigUtils.getExcludesKey(group, PREFIX);
69  		List<String> includes = SpringUtils.getNoneSensitiveListFromCSV(env, includesKey, defaultIncludes.get(group));
70  		List<String> excludes = SpringUtils.getNoneSensitiveListFromCSV(env, excludesKey, defaultExcludes.get(group));
71  		File scanDir = build.getOutputDir();
72  		String encoding = build.getEncoding();
73  		Comparator<MetaInfResource> comparator = getComparator(group);
74  		return new MetaInfContext.Builder(outputFile, encoding, scanDir).comparator(comparator).includes(includes).excludes(excludes).relativePaths(relativePaths).build();
75  	}
76  
77  	protected Comparator<MetaInfResource> getComparator(MetaInfGroup group) {
78  		if (MetaInfGroup.OTHER.equals(group)) {
79  			// The upgrades folder for Rice has a nested directory structure - [bootstrap|demo|test] and also [files...|sub-directories] inside each area
80  			// The sorting of XML located inside the "upgrades" folder for Rice sorts by the directory structure first, and then by filenames in each directory.
81  			// All sorting is done lexicographically.
82  			// Files in any given directory come first, followed by any files in sub-directories.
83  			return new MetaInfResourcePathComparator();
84  		} else {
85  			return new MetaInfResourceLocationComparator();
86  		}
87  	}
88  
89  	protected Map<MetaInfGroup, String> getDefaultIncludes(Project project) {
90  		String resourcePath = ProjectUtils.getResourcePath(project.getGroupId(), project.getArtifactId());
91  		Map<MetaInfGroup, String> map = Maps.newHashMap();
92  		map.put(MetaInfGroup.OTHER, resourcePath + "/upgrades/**/*.xml");
93  		return map;
94  	}
95  
96  	protected Map<MetaInfGroup, String> getDefaultExcludes() {
97  		Map<MetaInfGroup, String> map = Maps.newHashMap();
98  		// No need to exclude any of the "upgrades" XML
99  		map.put(MetaInfGroup.OTHER, NullUtils.NONE);
100 		return map;
101 	}
102 
103 }