This is an automated email from the git hooks/post-receive script. New commit to branch develop in repository eugene. See https://gitlab.nuiton.org/nuiton/eugene.git commit c65e5d29120dda0c64b0068f735dc7da9156cb4c Author: Tony CHEMIT <chemit@codelutin.com> Date: Fri Apr 8 16:53:17 2016 +0200 Add ResourcesHelper (See #3930) --- .../java/org/nuiton/eugene/ResourcesHelper.java | 88 ++++++++++++++++++++++ 1 file changed, 88 insertions(+) diff --git a/eugene/src/main/java/org/nuiton/eugene/ResourcesHelper.java b/eugene/src/main/java/org/nuiton/eugene/ResourcesHelper.java new file mode 100644 index 0000000..a0369a9 --- /dev/null +++ b/eugene/src/main/java/org/nuiton/eugene/ResourcesHelper.java @@ -0,0 +1,88 @@ +package org.nuiton.eugene; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import java.net.URL; + +/** + * Some useful methods to locate resources. + * + * Created on 08/04/16. + * + * @author Tony Chemit - chemit@codelutin.com + * @since 3.0 + */ +public class ResourcesHelper { + + /** Logger. */ + private static final Log log = LogFactory.getLog(ResourcesHelper.class); + + protected final ClassLoader classLoader; + protected final boolean verbose; + + public ResourcesHelper(ClassLoader classLoader, boolean verbose) { + this.classLoader = classLoader; + this.verbose = verbose; + } + + /** + * Checks if the given fully qualified name java file is in class-path and log a message that fqn will not generated if found. + * + * @param fqn fully qualified name to test + * @return {@code true} if java file was found in class-path, {@code false} otherwise. + */ + public boolean isJavaFileInClassPath(String fqn) { + return isFullyQualifiedNameInClassPath(fqn, ".java"); + } + + /** + * Checks if the given fully qualified name (path separated by {@code pathSeparator} and optionaly suffixed by + * {@code extension}) is in class-path and log a message that fqn will not generated if found. + * + * @param extension file extension + * @param fqn fully qualified name to test + * @return {@code true} if resource was found in class-path, {@code false} otherwise. + */ + public boolean isFullyQualifiedNameInClassPath(String fqn, String extension) { + return isInClassPath(fqn,"\\.", extension); + } + + /** + * Checks if the given fully qualified name (path separated by {@code pathSeparator} and optionaly suffixed by + * {@code extension}) is in class-path and log a message that fqn will not generated if found. + * + * @param extension file extension + * @param fqn fully qualified name to test + * @return {@code true} if resource was found in class-path, {@code false} otherwise. + */ + public boolean isInClassPath(String fqn, String pathSeparator, String extension) { + + URL fileLocation = getFileInClassPath(fqn, pathSeparator, extension); + + if (fileLocation != null) { + + // there is already a existing file in class-path, skip + + if (log.isDebugEnabled()) { + log.debug("Will not generate [" + fqn + "], already found in class-path at location : " + fileLocation); + } else if (verbose) { + log.info("Will not generate [" + fqn + "], already found in class-path."); + } + + return true; + } + + // is not found + return false; + } + + protected URL getFileInClassPath(String fqn, String pathSeparator, String extension) { + String resourceName = fqn.replaceAll(pathSeparator, "/") + extension; + URL fileLocation = classLoader.getResource(resourceName); + if (log.isDebugEnabled()) { + log.debug("Look for resource : " + resourceName + " = " + fileLocation); + } + return fileLocation; + } +} -- To stop receiving notification emails like this one, please contact nuiton.org SCM administrator <admin+scm@nuiton.org>.