Java Code Examples for java.net.URLClassLoader#getResourceAsStream()

The following examples show how to use java.net.URLClassLoader#getResourceAsStream() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JarHelper.java    From uavstack with Apache License 2.0 6 votes vote down vote up
/**
 * 提取资源为InputStream
 * 
 * @param jarFilePath
 * @param resourcePath
 * @return
 */
public static InputStream getResourceAsStream(String jarFilePath, String resourcePath) {

    File file = new File(jarFilePath);

    if (!file.exists()) {
        return null;
    }

    try {
        @SuppressWarnings("resource")
        URLClassLoader cl = new URLClassLoader(new URL[] { file.toURI().toURL() });

        InputStream is = cl.getResourceAsStream(resourcePath);

        return is;

    }
    catch (Exception e) {
        // ignore
    }

    return null;
}
 
Example 2
Source File: CamelService.java    From camel-idea-plugin with Apache License 2.0 6 votes vote down vote up
private static Properties loadComponentProperties(URLClassLoader classLoader, boolean legacyScan) {
    Properties answer = new Properties();
    try {
        // load the component files using the recommended way by a component.properties file
        InputStream is = classLoader.getResourceAsStream("META-INF/services/org/apache/camel/component.properties");
        if (is != null) {
            answer.load(is);
        } else if (legacyScan) {
            // okay then try to load using a fallback using legacy classpath scanning
            loadComponentPropertiesClasspathScan(classLoader, answer);
        }
    } catch (Throwable e) {
        LOG.warn("Error loading META-INF/services/org/apache/camel/component.properties file", e);
    }
    return answer;
}
 
Example 3
Source File: AbstractCamelProjectCommand.java    From fabric8-forge with Apache License 2.0 6 votes vote down vote up
public static Properties loadComponentProperties(Dependency dependency) {
    Properties answer = new Properties();

    try {
        // is it a JAR file
        File file = dependency.getArtifact().getUnderlyingResourceObject();
        if (file != null && file.getName().toLowerCase().endsWith(".jar")) {
            URL url = new URL("file:" + file.getAbsolutePath());
            URLClassLoader child = new URLClassLoader(new URL[]{url});

            InputStream is = child.getResourceAsStream("META-INF/services/org/apache/camel/component.properties");
            if (is != null) {
                answer.load(is);
            }
        }
    } catch (Throwable e) {
        // ignore
    }

    return answer;
}
 
Example 4
Source File: CamelProjectHelper.java    From fabric8-forge with Apache License 2.0 6 votes vote down vote up
public static boolean isCamelComponentArtifact(Dependency dependency) {
    try {
        // is it a JAR file
        File file = dependency.getArtifact().getUnderlyingResourceObject();
        if (file != null && file.getName().toLowerCase().endsWith(".jar")) {
            URL url = new URL("file:" + file.getAbsolutePath());
            URLClassLoader child = new URLClassLoader(new URL[]{url});

            // custom component
            InputStream is = child.getResourceAsStream("META-INF/services/org/apache/camel/component.properties");
            if (is != null) {
                IOHelpers.close(is);
                return true;
            }
        }
    } catch (Throwable e) {
        // ignore
    }

    return false;
}
 
Example 5
Source File: JarIndexMergeForClassLoaderTest.java    From jdk8u_jdk with GNU General Public License v2.0 6 votes vote down vote up
private static void assertResource(URLClassLoader classLoader, String file,
        String expectedContent) throws IOException {
    InputStream fileStream = classLoader.getResourceAsStream(file);

    if (fileStream == null && expectedContent == null) {
        return;
    }
    if (fileStream == null && expectedContent != null) {
        throw new RuntimeException(
                buildMessage(file, expectedContent, null));
    }
    try {
        String actualContent = readAsString(fileStream);

        if (fileStream != null && expectedContent == null) {
            throw new RuntimeException(buildMessage(file, null,
                    actualContent));
        }
        if (!expectedContent.equals(actualContent)) {
            throw new RuntimeException(buildMessage(file, expectedContent,
                    actualContent));
        }
    } finally {
        fileStream.close();
    }
}
 
Example 6
Source File: DoctorKafkaWebServer.java    From doctorkafka with Apache License 2.0 6 votes vote down vote up
@Override
protected void doGet(HttpServletRequest request,
                     HttpServletResponse response) throws ServletException,
                                                          IOException {
  response.setContentType("text/html");
  try {
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    URL resource = classLoader.getResource(indexPagePath);
    URLClassLoader urlClassLoader = new URLClassLoader(new URL[]{resource});

    InputStream input = urlClassLoader.getResourceAsStream(indexPagePath);
    String result = CharStreams.toString(new InputStreamReader(input, Charsets.UTF_8));
    response.setStatus(HttpServletResponse.SC_OK);
    response.getWriter().print(result);
  } catch (Exception e) {
    response.getWriter().print(e);
    LOG.error("error : ", e);
  }
}
 
Example 7
Source File: JarUtil.java    From uavstack with Apache License 2.0 6 votes vote down vote up
/**
 * 提取资源为InputStream
 * 
 * @param jarFilePath
 * @param resourcePath
 * @return
 */
public static InputStream getResourceAsStream(String jarFilePath, String resourcePath) {

    File file = new File(jarFilePath);

    if (!file.exists()) {
        return null;
    }

    try {
        @SuppressWarnings("resource")
        URLClassLoader cl = new URLClassLoader(new URL[] { file.toURI().toURL() });

        InputStream is = cl.getResourceAsStream(resourcePath);

        return is;

    }
    catch (Exception e) {
        // ignore
    }

    return null;
}
 
Example 8
Source File: CamelService.java    From camel-idea-plugin with Apache License 2.0 6 votes vote down vote up
private static String loadComponentJSonSchema(URLClassLoader classLoader, String scheme) {
    String answer = null;

    String path = null;
    String javaType = extractComponentJavaType(classLoader, scheme);
    if (javaType != null) {
        int pos = javaType.lastIndexOf(".");
        path = javaType.substring(0, pos);
        path = path.replace('.', '/');
        path = path + "/" + scheme + ".json";
    }

    if (path != null) {
        try {
            InputStream is = classLoader.getResourceAsStream(path);
            if (is != null) {
                answer = loadText(is);
            }
        } catch (Throwable e) {
            LOG.warn("Error loading " + path + " file", e);
        }
    }

    return answer;
}
 
Example 9
Source File: JarIndexMergeForClassLoaderTest.java    From jdk8u-jdk with GNU General Public License v2.0 6 votes vote down vote up
private static void assertResource(URLClassLoader classLoader, String file,
        String expectedContent) throws IOException {
    InputStream fileStream = classLoader.getResourceAsStream(file);

    if (fileStream == null && expectedContent == null) {
        return;
    }
    if (fileStream == null && expectedContent != null) {
        throw new RuntimeException(
                buildMessage(file, expectedContent, null));
    }
    try {
        String actualContent = readAsString(fileStream);

        if (fileStream != null && expectedContent == null) {
            throw new RuntimeException(buildMessage(file, null,
                    actualContent));
        }
        if (!expectedContent.equals(actualContent)) {
            throw new RuntimeException(buildMessage(file, expectedContent,
                    actualContent));
        }
    } finally {
        fileStream.close();
    }
}
 
Example 10
Source File: JarIndexMergeForClassLoaderTest.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
private static void assertResource(URLClassLoader classLoader, String file,
        String expectedContent) throws IOException {
    InputStream fileStream = classLoader.getResourceAsStream(file);

    if (fileStream == null && expectedContent == null) {
        return;
    }
    if (fileStream == null && expectedContent != null) {
        throw new RuntimeException(
                buildMessage(file, expectedContent, null));
    }
    try {
        String actualContent = readAsString(fileStream);

        if (fileStream != null && expectedContent == null) {
            throw new RuntimeException(buildMessage(file, null,
                    actualContent));
        }
        if (!expectedContent.equals(actualContent)) {
            throw new RuntimeException(buildMessage(file, expectedContent,
                    actualContent));
        }
    } finally {
        fileStream.close();
    }
}
 
Example 11
Source File: JarIndexMergeForClassLoaderTest.java    From jdk8u-dev-jdk with GNU General Public License v2.0 6 votes vote down vote up
private static void assertResource(URLClassLoader classLoader, String file,
        String expectedContent) throws IOException {
    InputStream fileStream = classLoader.getResourceAsStream(file);

    if (fileStream == null && expectedContent == null) {
        return;
    }
    if (fileStream == null && expectedContent != null) {
        throw new RuntimeException(
                buildMessage(file, expectedContent, null));
    }
    try {
        String actualContent = readAsString(fileStream);

        if (fileStream != null && expectedContent == null) {
            throw new RuntimeException(buildMessage(file, null,
                    actualContent));
        }
        if (!expectedContent.equals(actualContent)) {
            throw new RuntimeException(buildMessage(file, expectedContent,
                    actualContent));
        }
    } finally {
        fileStream.close();
    }
}
 
Example 12
Source File: JarIndexMergeForClassLoaderTest.java    From openjdk-jdk8u with GNU General Public License v2.0 6 votes vote down vote up
private static void assertResource(URLClassLoader classLoader, String file,
        String expectedContent) throws IOException {
    InputStream fileStream = classLoader.getResourceAsStream(file);

    if (fileStream == null && expectedContent == null) {
        return;
    }
    if (fileStream == null && expectedContent != null) {
        throw new RuntimeException(
                buildMessage(file, expectedContent, null));
    }
    try {
        String actualContent = readAsString(fileStream);

        if (fileStream != null && expectedContent == null) {
            throw new RuntimeException(buildMessage(file, null,
                    actualContent));
        }
        if (!expectedContent.equals(actualContent)) {
            throw new RuntimeException(buildMessage(file, expectedContent,
                    actualContent));
        }
    } finally {
        fileStream.close();
    }
}
 
Example 13
Source File: JarIndexMergeForClassLoaderTest.java    From jdk8u60 with GNU General Public License v2.0 6 votes vote down vote up
private static void assertResource(URLClassLoader classLoader, String file,
        String expectedContent) throws IOException {
    InputStream fileStream = classLoader.getResourceAsStream(file);

    if (fileStream == null && expectedContent == null) {
        return;
    }
    if (fileStream == null && expectedContent != null) {
        throw new RuntimeException(
                buildMessage(file, expectedContent, null));
    }
    try {
        String actualContent = readAsString(fileStream);

        if (fileStream != null && expectedContent == null) {
            throw new RuntimeException(buildMessage(file, null,
                    actualContent));
        }
        if (!expectedContent.equals(actualContent)) {
            throw new RuntimeException(buildMessage(file, expectedContent,
                    actualContent));
        }
    } finally {
        fileStream.close();
    }
}
 
Example 14
Source File: JarIndexMergeForClassLoaderTest.java    From openjdk-8 with GNU General Public License v2.0 6 votes vote down vote up
private static void assertResource(URLClassLoader classLoader, String file,
        String expectedContent) throws IOException {
    InputStream fileStream = classLoader.getResourceAsStream(file);

    if (fileStream == null && expectedContent == null) {
        return;
    }
    if (fileStream == null && expectedContent != null) {
        throw new RuntimeException(
                buildMessage(file, expectedContent, null));
    }
    try {
        String actualContent = readAsString(fileStream);

        if (fileStream != null && expectedContent == null) {
            throw new RuntimeException(buildMessage(file, null,
                    actualContent));
        }
        if (!expectedContent.equals(actualContent)) {
            throw new RuntimeException(buildMessage(file, expectedContent,
                    actualContent));
        }
    } finally {
        fileStream.close();
    }
}
 
Example 15
Source File: JarIndexMergeForClassLoaderTest.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
private static void assertResource(URLClassLoader classLoader, String file,
        String expectedContent) throws IOException {
    InputStream fileStream = classLoader.getResourceAsStream(file);

    if (fileStream == null && expectedContent == null) {
        return;
    }
    if (fileStream == null && expectedContent != null) {
        throw new RuntimeException(
                buildMessage(file, expectedContent, null));
    }
    try {
        String actualContent = readAsString(fileStream);

        if (fileStream != null && expectedContent == null) {
            throw new RuntimeException(buildMessage(file, null,
                    actualContent));
        }
        if (!expectedContent.equals(actualContent)) {
            throw new RuntimeException(buildMessage(file, expectedContent,
                    actualContent));
        }
    } finally {
        fileStream.close();
    }
}
 
Example 16
Source File: CoordinatorUtils.java    From usergrid with Apache License 2.0 5 votes vote down vote up
/**
 *
 * @param runnerJar
 * @param resource
 * @return
 */
public static InputStream getResourceAsStreamFromRunnerJar( File runnerJar, String resource ) {
    try {
        // Access the jar file resources after adding it to a new ClassLoader
        URLClassLoader classLoader = new URLClassLoader( new URL[] { runnerJar.toURL() },
                Thread.currentThread().getContextClassLoader() );

        return classLoader.getResourceAsStream( resource );
    }
    catch ( Exception e ) {
        LOG.warn( "Error while reading {} from runner.jar resources", resource, e );
        return null;
    }
}
 
Example 17
Source File: FabricArchetypeCatalogFactory.java    From fabric8-forge with Apache License 2.0 5 votes vote down vote up
@Override
public ArchetypeCatalog getArchetypeCatalog() {
    if (cachedArchetypes == null) {
        String version = VersionHelper.fabric8ArchetypesVersion();

        Coordinate coordinate = CoordinateBuilder.create()
                .setGroupId("io.fabric8.archetypes")
                .setArtifactId("archetypes-catalog")
                .setVersion(version)
                .setPackaging("jar");

        // load the archetype-catalog.xml from inside the JAR
        Dependency dependency = resolver.get().resolveArtifact(DependencyQueryBuilder.create(coordinate));
        if (dependency != null) {
            try {
                String name = dependency.getArtifact().getFullyQualifiedName();
                URL url = new URL("file", null, name);
                URLClassLoader loader = new URLClassLoader(new URL[]{url});
                InputStream is = loader.getResourceAsStream("archetype-catalog.xml");
                if (is != null) {
                    cachedArchetypes = new ArchetypeCatalogXpp3Reader().read(is);
                }
            } catch (Exception e) {
                LOG.log(Level.WARNING, "Error while retrieving archetypes due " + e.getMessage(), e);
            }
        }
    }
    return cachedArchetypes;
}
 
Example 18
Source File: AbstractCamelProjectCommand.java    From fabric8-forge with Apache License 2.0 5 votes vote down vote up
public static String loadComponentJSonSchema(Dependency dependency, String scheme) {
    String answer = null;

    String path = null;
    String javaType = extractComponentJavaType(dependency, scheme);
    if (javaType != null) {
        int pos = javaType.lastIndexOf(".");
        path = javaType.substring(0, pos);
        path = path.replace('.', '/');
        path = path + "/" + scheme + ".json";
    }

    if (path != null) {
        try {
            // is it a JAR file
            File file = dependency.getArtifact().getUnderlyingResourceObject();
            if (file != null && file.getName().toLowerCase().endsWith(".jar")) {
                URL url = new URL("file:" + file.getAbsolutePath());
                URLClassLoader child = new URLClassLoader(new URL[]{url});

                InputStream is = child.getResourceAsStream(path);
                if (is != null) {
                    answer = loadText(is);
                }
            }
        } catch (Throwable e) {
            // ignore
        }
    }

    return answer;
}
 
Example 19
Source File: JARSoundbankReader.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@Override
@SuppressWarnings("deprecation")
public Soundbank getSoundbank(URL url)
        throws InvalidMidiDataException, IOException {
    if (!isZIP(url))
        return null;
    ArrayList<Soundbank> soundbanks = new ArrayList<>();
    URLClassLoader ucl = URLClassLoader.newInstance(new URL[]{url});
    InputStream stream = ucl.getResourceAsStream(
            "META-INF/services/javax.sound.midi.Soundbank");
    if (stream == null)
        return null;
    try
    {
        BufferedReader r = new BufferedReader(new InputStreamReader(stream));
        String line = r.readLine();
        while (line != null) {
            if (!line.startsWith("#")) {
                try {
                    Class<?> c = Class.forName(line.trim(), false, ucl);
                    if (Soundbank.class.isAssignableFrom(c)) {
                        ReflectUtil.checkPackageAccess(c);
                        Object o = c.newInstance();
                        soundbanks.add((Soundbank) o);
                    }
                } catch (ReflectiveOperationException ignored) {
                }
            }
            line = r.readLine();
        }
    }
    finally
    {
        stream.close();
    }
    if (soundbanks.size() == 0)
        return null;
    if (soundbanks.size() == 1)
        return soundbanks.get(0);
    SimpleSoundbank sbk = new SimpleSoundbank();
    for (Soundbank soundbank : soundbanks)
        sbk.addAllInstruments(soundbank);
    return sbk;
}
 
Example 20
Source File: JARSoundbankReader.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
@Override
@SuppressWarnings("deprecation")
public Soundbank getSoundbank(URL url)
        throws InvalidMidiDataException, IOException {
    if (!isZIP(url))
        return null;
    ArrayList<Soundbank> soundbanks = new ArrayList<>();
    URLClassLoader ucl = URLClassLoader.newInstance(new URL[]{url});
    InputStream stream = ucl.getResourceAsStream(
            "META-INF/services/javax.sound.midi.Soundbank");
    if (stream == null)
        return null;
    try
    {
        BufferedReader r = new BufferedReader(new InputStreamReader(stream));
        String line = r.readLine();
        while (line != null) {
            if (!line.startsWith("#")) {
                try {
                    Class<?> c = Class.forName(line.trim(), false, ucl);
                    if (Soundbank.class.isAssignableFrom(c)) {
                        ReflectUtil.checkPackageAccess(c);
                        Object o = c.newInstance();
                        soundbanks.add((Soundbank) o);
                    }
                } catch (ReflectiveOperationException ignored) {
                }
            }
            line = r.readLine();
        }
    }
    finally
    {
        stream.close();
    }
    if (soundbanks.size() == 0)
        return null;
    if (soundbanks.size() == 1)
        return soundbanks.get(0);
    SimpleSoundbank sbk = new SimpleSoundbank();
    for (Soundbank soundbank : soundbanks)
        sbk.addAllInstruments(soundbank);
    return sbk;
}