Java ClassLoader Load getConfigFileInputStream(String configFilePath)

Here you can find the source of getConfigFileInputStream(String configFilePath)

Description

get Config File Input Stream

License

Open Source License

Declaration

public static InputStream getConfigFileInputStream(String configFilePath) throws IOException 

Method Source Code


//package com.java2s;
/*//from   w w w  .ja va 2 s  . co  m
 * Copyright (c) 2013 Yahoo! Inc. All Rights Reserved.
 *
 * 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. See accompanying LICENSE file.
 */

import java.io.*;
import java.net.URL;
import java.util.*;

public class Main {
    public static InputStream getConfigFileInputStream(String configFilePath) throws IOException {
        if (null == configFilePath) {
            throw new IOException("Could not find config file, name not specified");
        }

        HashSet<URL> resources = new HashSet<URL>(findResources(configFilePath));

        if (resources.isEmpty()) {
            File configFile = new File(configFilePath);

            if (configFile.exists()) {
                return new FileInputStream(configFile);
            }
        } else if (resources.size() > 1) {
            throw new IOException("Found multiple " + configFilePath
                    + " resources. You're probably bundling the Storm jars with your topology jar. " + resources);
        } else {

            //          LOG.debug("Using "+configFilePath+" from resources");
            URL resource = resources.iterator().next();

            return resource.openStream();
        }

        return null;
    }

    public static List<URL> findResources(String name) {
        try {
            Enumeration<URL> resources = Thread.currentThread().getContextClassLoader().getResources(name);
            List<URL> ret = new ArrayList<URL>();

            while (resources.hasMoreElements()) {
                ret.add(resources.nextElement());
            }

            return ret;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

Related

  1. getAbsolutePathEssenceDesc(String file)
  2. getAllArquillianLibs()
  3. getAsProperties(final String name)
  4. getBaseDir()
  5. getBytes(File file)
  6. getContentFromFile(String fileName, Object context)
  7. getDirectories(String path)
  8. getFile(final String filename)
  9. getFile(final String name)