Java Path File Name nio findRootPathForResource(String resourceName, ClassLoader classLoader)

Here you can find the source of findRootPathForResource(String resourceName, ClassLoader classLoader)

Description

Find the root path for the given resource.

License

Apache License

Parameter

Parameter Description
resourceName relative path of the resource to search for. E.G. "scripts/myscript.groovy"
classLoader the ClassLoader to search

Return

absolute path of the root of the resource.

Declaration

@Nullable
public static Path findRootPathForResource(String resourceName,
        ClassLoader classLoader) 

Method Source Code

//package com.java2s;
/*/* ww w  . j  a v  a 2  s . c o m*/
 *
 *  Copyright 2013 Netflix, Inc.
 *
 *     Licensed under the Apache 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.apache.org/licenses/LICENSE-2.0
 *
 *     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.
 *
 */

import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Path;
import java.nio.file.Paths;

import java.util.Objects;

import javax.annotation.Nullable;

public class Main {
    /**
     * Find the root path for the given resource. If the resource is found in a Jar file, then the
     * result will be an absolute path to the jar file. If the resource is found in a directory,
     * then the result will be the parent path of the given resource.
     *
     * For example, if the resourceName is given as "scripts/myscript.groovy", and the path to the file is
     * "/root/sub1/script/myscript.groovy", then this method will return "/root/sub1"
     *
     * @param resourceName relative path of the resource to search for. E.G. "scripts/myscript.groovy"
     * @param classLoader the {@link ClassLoader} to search
     * @return absolute path of the root of the resource.
     */
    @Nullable
    public static Path findRootPathForResource(String resourceName,
            ClassLoader classLoader) {
        Objects.requireNonNull(resourceName, "resourceName");
        Objects.requireNonNull(classLoader, "classLoader");

        URL resource = classLoader.getResource(resourceName);
        if (resource != null) {
            String protocol = resource.getProtocol();
            if (protocol.equals("jar")) {
                return getJarPathFromUrl(resource);
            } else if (protocol.equals("file")) {
                return getRootPathFromDirectory(resourceName, resource);
            } else {
                throw new IllegalStateException(
                        "Unsupported URL protocol: " + protocol);
            }
        }
        return null;
    }

    /**
     * Find the jar containing the given resource.
     *
     * @param jarUrl URL that came from a jar that needs to be parsed
     * @return {@link Path} to the Jar containing the resource.
     */
    public static Path getJarPathFromUrl(URL jarUrl) {
        try {
            String pathString = jarUrl.getPath();
            // for Jar URL, the path is in the form of: file:/path/to/groovy/myJar.jar!/path/to/resource/myResource.txt
            int endIndex = pathString.lastIndexOf("!");
            return Paths.get(new URI(pathString.substring(0, endIndex)));
        } catch (URISyntaxException e) {
            throw new IllegalStateException("Unsupported URL syntax: "
                    + jarUrl.getPath(), e);
        }
    }

    private static Path getRootPathFromDirectory(String resourceName,
            URL resource) {
        try {
            Path result = Paths.get(resource.toURI());
            int relativePathSize = Paths.get(resourceName).getNameCount();
            for (int i = 0; i < relativePathSize; i++) {
                result = result.getParent();
            }
            return result;
        } catch (URISyntaxException e) {
            throw new IllegalStateException("Unsupported URL syntax: "
                    + resource, e);
        }
    }
}

Related

  1. filterJarContainingClass(Collection jarsPaths, String className)
  2. findExecutablePaths(String executableName)
  3. findFile(Path directoryPath, String fileName)
  4. findFile(String basePath, final String fileName)
  5. findNextFileName(Path folder, String name)
  6. findUpward(String filename, Path startingPath)
  7. formatPathName(Path path)
  8. getAbsolutePath(String fileName)
  9. getAbsolutePath(String name)