Java Resource Find findResourceBases(String baseResource, ClassLoader loader)

Here you can find the source of findResourceBases(String baseResource, ClassLoader loader)

Description

Find the classpath URLs for a specific classpath resource.

License

BSD License

Parameter

Parameter Description
baseResource a parameter

Declaration

public static URL[] findResourceBases(String baseResource, ClassLoader loader) 

Method Source Code

//package com.java2s;
/* Chronos - Game Development Toolkit for Java game developers. The
 * original source remains:/* w  w  w.j a  v a2  s . c  o  m*/
 * 
 * Copyright (c) 2013 Miguel Gonzalez http://my-reality.de
 * 
 * This source is provided under the terms of the BSD License.
 * 
 * Copyright (c) 2013, Chronos
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or 
 * without modification, are permitted provided that the following 
 * conditions are met:
 * 
 *  * Redistributions of source code must retain the above 
 *    copyright notice, this list of conditions and the 
 *    following disclaimer.
 *  * Redistributions in binary form must reproduce the above 
 *    copyright notice, this list of conditions and the following 
 *    disclaimer in the documentation and/or other materials provided 
 *    with the distribution.
 *  * Neither the name of the Chronos/my Reality Development nor the names of 
 *    its contributors may be used to endorse or promote products 
 *    derived from this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND 
 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS 
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, 
 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 
 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR 
 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 
 * OF SUCH DAMAGE.
 */

import java.io.IOException;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Enumeration;

public class Main {
    /**
     * Find the classpath URLs for a specific classpath resource. The classpath
     * URL is extracted from loader.getResources() using the baseResource.
     * 
     * @param baseResource
     * @return
     */
    public static URL[] findResourceBases(String baseResource, ClassLoader loader) {
        ArrayList<URL> list = new ArrayList<URL>();
        try {
            Enumeration<URL> urls = loader.getResources(baseResource);
            while (urls.hasMoreElements()) {
                URL url = urls.nextElement();
                list.add(findResourceBase(url, baseResource));
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return list.toArray(new URL[list.size()]);
    }

    /**
     * Find the classpath URLs for a specific classpath resource. The classpath
     * URL is extracted from loader.getResources() using the baseResource.
     * 
     * @param baseResource
     * @return
     */
    public static URL[] findResourceBases(String baseResource) {
        return findResourceBases(baseResource, Thread.currentThread().getContextClassLoader());
    }

    private static URL findResourceBase(URL url, String baseResource) {
        String urlString = url.toString();
        int idx = urlString.lastIndexOf(baseResource);
        urlString = urlString.substring(0, idx);
        URL deployUrl = null;
        try {
            deployUrl = new URL(urlString);
        } catch (MalformedURLException e) {
            throw new RuntimeException(e);
        }
        return deployUrl;
    }

    /**
     * Find the classpath URL for a specific classpath resource. The classpath
     * URL is extracted from
     * Thread.currentThread().getContextClassLoader().getResource() using the
     * baseResource.
     * 
     * @param baseResource
     * @return
     */
    public static URL findResourceBase(String baseResource) {
        return findResourceBase(baseResource, Thread.currentThread().getContextClassLoader());
    }

    /**
     * Find the classpath URL for a specific classpath resource. The classpath
     * URL is extracted from loader.getResource() using the baseResource.
     * 
     * @param baseResource
     * @param loader
     * @return
     */
    public static URL findResourceBase(String baseResource, ClassLoader loader) {
        URL url = loader.getResource(baseResource);
        return findResourceBase(url, baseResource);
    }
}

Related

  1. findResource(Class clazz)
  2. findResource(Class clazz)
  3. findResource(String name)
  4. findResource(String resource, ClassLoader classLoader)
  5. findResourceBase(String baseResource, ClassLoader loader)
  6. findResources(String name)