All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.kuali.common.util.metainf.service.MetaInfUtils Maven / Gradle / Ivy

There is a newer version: 4.4.17
Show newest version
/**
 * Copyright 2010-2014 The Kuali Foundation
 *
 * Licensed under the Educational Community License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.opensource.org/licenses/ecl2.php
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.kuali.common.util.metainf.service;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.kuali.common.util.SimpleScanner;
import org.kuali.common.util.Str;
import org.kuali.common.util.metainf.model.PathComparator;
import org.kuali.common.util.metainf.spring.MetaInfDataLocation;
import org.kuali.common.util.metainf.spring.MetaInfDataType;
import org.kuali.common.util.metainf.spring.MetaInfGroup;
import org.kuali.common.util.project.KualiUtilProjectConstants;
import org.kuali.common.util.project.ProjectUtils;
import org.kuali.common.util.project.model.Build;
import org.kuali.common.util.project.model.Project;
import org.kuali.common.util.project.model.ProjectIdentifier;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.util.ResourceUtils;

import com.google.common.base.Optional;
import com.google.common.collect.Lists;

public class MetaInfUtils {

	public static final String RESOURCES_FILENAME_EXTENSION = "resources";
	public static final String METAINF_DIRECTORY_NAME = "META-INF";

	/**
	 * @deprecated
	 */
	@Deprecated
	public static final org.kuali.common.util.project.model.FeatureIdentifier FEATURE_ID = new org.kuali.common.util.project.model.FeatureIdentifier(
			KualiUtilProjectConstants.PROJECT_ID, "metainf");

	public static final String PROPERTY_PREFIX = "metainf";

	/**
	 * ${project.build.outputDirectory}/META-INF/org/kuali/util/kuali-util/[group].resources
	 */
	public static File getOutputFile(Project project, Build build, MetaInfGroup group) {
		return getOutputFile(project, build, group.name().toLowerCase());
	}

	/**
	 * ${project.build.outputDirectory}/META-INF/org/kuali/util/kuali-util/[filename].resources
	 */
	public static File getOutputFile(Project project, Build build, String filename) {
		return getOutputFile(project, build, Optional. absent(), filename);
	}

	/**
	 * ${project.build.outputDirectory}/META-INF/org/kuali/util/kuali-util/[qualifier]/[group].resources
	 */
	public static File getOutputFile(Project project, Build build, String qualifier, MetaInfGroup group) {
		return getOutputFile(project, build, Optional.of(qualifier), group.name().toLowerCase());
	}

	/**
	 * ${project.build.outputDirectory}/META-INF/org/kuali/util/kuali-util/[qualifier]/[filename].resources
	 */
	public static File getOutputFile(Project project, Build build, String qualifier, String filename) {
		return getOutputFile(project, build, Optional.of(qualifier), filename);
	}

	/**
	 * ${project.build.outputDirectory}/META-INF/org/kuali/util/kuali-util/[qualifier]/[filename].resources where [qualifier] is optional
	 */
	public static File getOutputFile(Project project, Build build, Optional qualifier, String filename) {
		return getOutputFile(project, build, qualifier, Optional. absent(), Optional. absent(), filename);
	}

	/**
	 * ${project.build.outputDirectory}/META-INF/org/kuali/util/kuali-util/[qualifier]/[location]/[type]/[filename].resources where [qualifier],
	 * [location], and [type] are optional
	 */
	public static File getOutputFile(Project project, Build build, Optional qualifier, Optional location, Optional type,
			String filename) {
		StringBuilder sb = new StringBuilder();
		sb.append(getResourcePrefix(project));
		if (qualifier.isPresent()) {
			sb.append("/");
			sb.append(qualifier.get());
		}
		if (location.isPresent()) {
			sb.append("/");
			sb.append(location.get().name().toLowerCase());
		}
		if (type.isPresent()) {
			sb.append("/");
			sb.append(type.get().name().toLowerCase());
		}
		sb.append("/");
		sb.append(getFilename(filename));
		return new File(build.getOutputDir(), sb.toString());
	}

	/**
	 * classpath:META-INF/org/kuali/util/kuali-util/[group].resources
	 */
	public static String getClasspathResource(ProjectIdentifier project, MetaInfGroup group) {
		return getClasspathResource(project.getGroupId(), project.getArtifactId(), group);
	}

	/**
	 * classpath:META-INF/org/kuali/util/kuali-util/[filename].resources
	 */
	public static String getClasspathResource(ProjectIdentifier project, String filename) {
		return getClasspathResource(project.getGroupId(), project.getArtifactId(), filename);
	}

	/**
	 * classpath:META-INF/org/kuali/util/kuali-util/[qualifier]/[group].resources
	 */
	public static String getClasspathResource(ProjectIdentifier project, String qualifier, MetaInfGroup group) {
		return getClasspathResource(project.getGroupId(), project.getArtifactId(), Optional.of(qualifier), group.name().toLowerCase());
	}

	/**
	 * classpath:META-INF/org/kuali/util/kuali-util/[qualifier]/[filename].resources
	 */
	public static String getClasspathResource(ProjectIdentifier project, String qualifier, String filename) {
		return getClasspathResource(project.getGroupId(), project.getArtifactId(), Optional.of(qualifier), filename);
	}

	/**
	 * classpath:META-INF/org/kuali/util/kuali-util/[qualifier]/[filename].resources where [qualifier], [location], and [type]
	 * are optional
	 */
	public static String getClasspathResource(ProjectIdentifier project, Optional qualifier, Optional location, Optional type,
			String filename) {
		return getClasspathResource(project.getGroupId(), project.getArtifactId(), qualifier, location, type, filename);
	}

	/**
	 * classpath:META-INF/org/kuali/util/kuali-util/[group].resources
	 */
	public static String getClasspathResource(Project project, MetaInfGroup group) {
		return getClasspathResource(project.getGroupId(), project.getArtifactId(), group);
	}

	/**
	 * classpath:META-INF/org/kuali/util/kuali-util/[filename].resources
	 */
	public static String getClasspathResource(Project project, String filename) {
		return getClasspathResource(project.getGroupId(), project.getArtifactId(), filename);
	}

	/**
	 * classpath:META-INF/org/kuali/util/kuali-util/[qualifier]/[group].resources
	 */
	public static String getClasspathResource(Project project, String qualifier, MetaInfGroup group) {
		return getClasspathResource(project.getGroupId(), project.getArtifactId(), Optional.of(qualifier), group.name().toLowerCase());
	}

	/**
	 * classpath:META-INF/org/kuali/util/kuali-util/[qualifier]/[filename].resources
	 */
	public static String getClasspathResource(Project project, String qualifier, String filename) {
		return getClasspathResource(project.getGroupId(), project.getArtifactId(), Optional.of(qualifier), filename);
	}

	/**
	 * classpath:META-INF/org/kuali/util/kuali-util/[qualifier]/[filename].resources where [qualifier], [location], and [type]
	 * are optional
	 */
	public static String getClasspathResource(Project project, Optional qualifier, Optional location, Optional type, String filename) {
		return getClasspathResource(project.getGroupId(), project.getArtifactId(), qualifier, location, type, filename);
	}

	/**
	 * classpath:META-INF/org/kuali/util/kuali-util/[group].resources
	 */
	public static String getClasspathResource(String groupId, String artifactId, MetaInfGroup group) {
		return getClasspathResource(groupId, artifactId, Optional. absent(), group.name().toLowerCase());
	}

	/**
	 * classpath:META-INF/org/kuali/util/kuali-util/[filename].resources
	 */
	public static String getClasspathResource(String groupId, String artifactId, String filename) {
		return getClasspathResource(groupId, artifactId, Optional. absent(), filename);
	}

	/**
	 * classpath:META-INF/org/kuali/util/kuali-util/[qualifier]/[filename].resources where [qualifier] is optional
	 */
	public static String getClasspathResource(String groupId, String artifactId, Optional qualifier, String filename) {
		return getClasspathResource(groupId, artifactId, qualifier, Optional. absent(), Optional. absent(), filename);
	}

	/**
	 * classpath:META-INF/org/kuali/util/kuali-util/[qualifier]/[filename].resources where [qualifier], [location], and [type]
	 * are optional
	 */
	public static String getClasspathResource(String groupId, String artifactId, Optional qualifier, Optional location,
			Optional type, String filename) {
		StringBuilder sb = new StringBuilder();
		sb.append(ResourceUtils.CLASSPATH_URL_PREFIX);
		sb.append(getResourcePrefix(groupId, artifactId));
		if (qualifier.isPresent()) {
			sb.append("/");
			sb.append(qualifier.get());
		}
		if (location.isPresent()) {
			sb.append("/");
			sb.append(location.get().name().toLowerCase());
		}
		if (type.isPresent()) {
			sb.append("/");
			sb.append(type.get().name().toLowerCase());
		}
		sb.append("/");
		sb.append(getFilename(filename));
		return sb.toString();
	}

	/**
	 * List of classpath:META-INF/org/kuali/util/kuali-util/[filename].resources where [filename] can contain an Ant-style pattern
	 */
	public static List getPatternedClasspathResources(ProjectIdentifier project, String filename) {
		return getPatternedClasspathResources(project.getGroupId(), project.getArtifactId(), filename);
	}

	/**
	 * List of classpath:META-INF/org/kuali/util/kuali-util/[qualifier]/[group].resources where [qualifier] can contain an Ant-style pattern
	 */
	public static List getPatternedClasspathResources(ProjectIdentifier project, String qualifier, MetaInfGroup group) {
		return getPatternedClasspathResources(project.getGroupId(), project.getArtifactId(), Optional.of(qualifier), group.name().toLowerCase());
	}

	/**
	 * List of classpath:META-INF/org/kuali/util/kuali-util/[qualifier]/[filename].resources where [qualifier] and [filename] can contain an
	 * Ant-style pattern
	 */
	public static List getPatternedClasspathResources(ProjectIdentifier project, String qualifier, String filename) {
		return getPatternedClasspathResources(project.getGroupId(), project.getArtifactId(), Optional.of(qualifier), filename);
	}

	/**
	 * List of classpath:META-INF/org/kuali/util/kuali-util/[qualifier]/[location]/[type]/[filename].resources where [qualifier], [location],
	 * and [type] are optional and [qualifier] and [filename] can contain an Ant-style pattern
	 */
	public static List getPatternedClasspathResources(ProjectIdentifier project, Optional qualifier, Optional location,
			Optional type, String filename) {
		return getPatternedClasspathResources(project.getGroupId(), project.getArtifactId(), qualifier, location, type, filename);
	}

	/**
	 * List of classpath:META-INF/org/kuali/util/kuali-util/[filename].resources where [filename] can contain an Ant-style pattern
	 */
	public static List getPatternedClasspathResources(Project project, String filename) {
		return getPatternedClasspathResources(project.getGroupId(), project.getArtifactId(), filename);
	}

	/**
	 * List of classpath:META-INF/org/kuali/util/kuali-util/[qualifier]/[group].resources where [qualifier] can contain an Ant-style pattern
	 */
	public static List getPatternedClasspathResources(Project project, String qualifier, MetaInfGroup group) {
		return getPatternedClasspathResources(project.getGroupId(), project.getArtifactId(), Optional.of(qualifier), group.name().toLowerCase());
	}

	/**
	 * List of classpath:META-INF/org/kuali/util/kuali-util/[qualifier]/[filename].resources where [qualifier] and [filename] can contain an
	 * Ant-style pattern
	 */
	public static List getPatternedClasspathResources(Project project, String qualifier, String filename) {
		return getPatternedClasspathResources(project.getGroupId(), project.getArtifactId(), Optional.of(qualifier), filename);
	}

	/**
	 * List of classpath:META-INF/org/kuali/util/kuali-util/[qualifier]/[location]/[type]/[filename].resources where [qualifier], [location],
	 * and [type] are optional and [qualifier] and [filename] can contain an Ant-style pattern
	 */
	public static List getPatternedClasspathResources(Project project, Optional qualifier, Optional location, Optional type,
			String filename) {
		return getPatternedClasspathResources(project.getGroupId(), project.getArtifactId(), qualifier, location, type, filename);
	}

	/**
	 * List of classpath:META-INF/org/kuali/util/kuali-util/[filename].resources where [filename] can contain an Ant-style pattern
	 */
	public static List getPatternedClasspathResources(String groupId, String artifactId, String filename) {
		return getPatternedClasspathResources(groupId, artifactId, Optional. absent(), filename);
	}

	/**
	 * List of classpath:META-INF/org/kuali/util/kuali-util/[qualifier]/[filename].resources where [qualifier] is optional * and [qualifier]
	 * and [filename] can contain an Ant-style pattern
	 */
	public static List getPatternedClasspathResources(String groupId, String artifactId, Optional qualifier, String filename) {
		return getPatternedClasspathResources(groupId, artifactId, qualifier, Optional. absent(), Optional. absent(), filename);
	}

	/**
	 * List of classpath:META-INF/org/kuali/util/kuali-util/[qualifier]/[filename].resources where [qualifier], [location], and
	 * [type] are optional and [qualifier] and [filename] can contain an Ant-style pattern
	 */
	public static List getPatternedClasspathResources(String groupId, String artifactId, Optional qualifier, Optional location,
			Optional type, String filename) {
		List patterenedClasspathResources = new ArrayList();
		String classpathResource = getClasspathResource(groupId, artifactId, qualifier, location, type, filename);
		PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
		try {
			Resource[] resources = resolver.getResources(classpathResource);
			if (ArrayUtils.isNotEmpty(resources)) {
				for (Resource resource : resources) {
					if (resource instanceof ClassPathResource) {
						ClassPathResource classPathResource = (ClassPathResource) resource;
						patterenedClasspathResources.add(ResourceUtils.CLASSPATH_URL_PREFIX + classPathResource.getPath());
					}
				}
			}
		} catch (IOException ioe) {
			throw new IllegalStateException("Unexpected IO error", ioe);
		}
		return patterenedClasspathResources;
	}

	/**
	 * Returns [group].resources (always lowercase)
	 */
	public static String getFilename(MetaInfGroup group) {
		return getFilename(group.name().toLowerCase());
	}

	/**
	 * Returns [filename].resources
	 */
	public static String getFilename(String filename) {
		return filename + "." + RESOURCES_FILENAME_EXTENSION;
	}

	/**
	 * META-INF/org/kuali/util
	 */
	public static String getGroupPrefix(Project project) {
		return getGroupPrefix(project.getGroupId());
	}

	/**
	 * META-INF/org/kuali/util
	 */
	public static String getGroupPrefix(String groupId) {
		return METAINF_DIRECTORY_NAME + "/" + Str.getPath(groupId);
	}

	/**
	 * classpath:META-INF
	 */
	public static String getClasspathPrefix() {
		return ResourceUtils.CLASSPATH_URL_PREFIX + METAINF_DIRECTORY_NAME;
	}

	/**
	 * META-INF/org/kuali/util/kuali-util
	 */
	public static String getResourcePrefix(Project project) {
		return getResourcePrefix(project.getGroupId(), project.getArtifactId());
	}

	/**
	 * META-INF/org/kuali/util/kuali-util
	 */
	public static String getResourcePrefix(String groupId, String artifactId) {
		return getGroupPrefix(groupId) + "/" + artifactId;
	}

	/**
	 * META-INF/org/kuali/util/kuali-util
	 */
	public static String getResourcePrefix(ProjectIdentifier project) {
		return getResourcePrefix(project.getGroupId(), project.getArtifactId());
	}

	/**
	 * classpath:META-INF/org/kuali/util/kuali-util
	 */
	public static String getClasspathResourcePrefix(ProjectIdentifier project) {
		StringBuilder sb = new StringBuilder();
		sb.append(ResourceUtils.CLASSPATH_URL_PREFIX);
		sb.append(getResourcePrefix(project.getGroupId(), project.getArtifactId()));
		return sb.toString();
	}

	/**
	 * classpath:META-INF/org/kuali/util/kuali-util
	 */
	public static String getClasspathResourcePrefix(Project project) {
		StringBuilder sb = new StringBuilder();
		sb.append(ResourceUtils.CLASSPATH_URL_PREFIX);
		sb.append(getResourcePrefix(project.getGroupId(), project.getArtifactId()));
		return sb.toString();
	}

	public static List getQualifiers(File baseDirectory, ProjectIdentifier project, List includes, List excludes) {
		String resourcePath = ProjectUtils.getResourcePath(project.getGroupId(), project.getArtifactId());
		File resourceDirectory = FileUtils.getFile(baseDirectory, resourcePath);
		return getQualifiers(resourceDirectory, includes, excludes);
	}

	public static List getQualifiers(File baseDirectory, Project project, List includes, List excludes) {
		String resourcePath = ProjectUtils.getResourcePath(project.getGroupId(), project.getArtifactId());
		File resourceDirectory = FileUtils.getFile(baseDirectory, resourcePath);
		return getQualifiers(resourceDirectory, includes, excludes);
	}

	public static List getQualifiers(File baseDirectory, List includes, List excludes) {
		List qualifiers = Lists.newArrayList();
		SimpleScanner scanner = new SimpleScanner(baseDirectory, includes, excludes);
		List directories = scanner.getDirectories();
		Collections.sort(directories, new PathComparator());
		for (String directory : directories) {
			if (qualifiers.isEmpty()) {
				qualifiers.add(directory);
			} else {
				boolean matches = false;
				for (String qualifier : qualifiers) {
					matches |= StringUtils.startsWith(directory, qualifier);
				}
				if (!matches) {
					qualifiers.add(directory);
				}
			}
		}
		return qualifiers;
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy