List of usage examples for java.net URLClassLoader getResourceAsStream
public InputStream getResourceAsStream(String name)
From source file:com.katsu.dwm.reflection.JarUtils.java
/** * Return as stream a resource in a jar/*from ww w . ja v a 2s . c om*/ * @param jar * @param resource */ public static InputStream getResourceAsStream(File jar, String resource) { try { URLClassLoader urlClassLoader = new URLClassLoader(new URL[] { jar.toURI().toURL() }, null); return urlClassLoader.getResourceAsStream(resource); } catch (Exception e) { logger.error(e); } return null; }
From source file:io.fabric8.forge.ipaas.repository.NexusConnectionRepository.java
private static String loadCamelConnectorJSonSchema(URL url) { try {/*from w ww.java2 s . c o m*/ // is it a JAR file URLClassLoader child = new URLClassLoader(new URL[] { url }); InputStream is = child.getResourceAsStream("camel-connector.json"); if (is != null) { return loadText(is); } IOHelpers.close(is); } catch (Throwable e) { e.printStackTrace(); // ignore } return null; }
From source file:edu.uci.ics.asterix.event.service.AsterixEventServiceUtil.java
private static void injectAsterixLogPropertyFile(String asterixInstanceDir, AsterixInstance asterixInstance) throws IOException, EventException { final String asterixJarPath = asterixJarPath(asterixInstance, asterixInstanceDir); File sourceJar1 = new File(asterixJarPath); Properties txnLogProperties = new Properties(); URLClassLoader urlClassLoader = new URLClassLoader(new URL[] { sourceJar1.toURI().toURL() }); InputStream in = urlClassLoader.getResourceAsStream(TXN_LOG_CONFIGURATION_FILE); if (in != null) { txnLogProperties.load(in);/*from ww w.j ava2s .c o m*/ } writeAsterixLogConfigurationFile(asterixInstance, txnLogProperties); File sourceJar2 = new File(asterixJarPath); File replacementFile = new File(asterixInstanceDir + File.separator + "log.properties"); replaceInJar(sourceJar2, TXN_LOG_CONFIGURATION_FILE, replacementFile); new File(asterixInstanceDir + File.separator + "log.properties").delete(); }
From source file:io.fabric8.forge.camel.commands.project.AbstractCamelProjectCommand.java
public static Properties loadComponentProperties(Dependency dependency) { Properties answer = new Properties(); try {/*from w w w .j a va2 s . c om*/ // 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; }
From source file:io.fabric8.forge.camel.commands.project.AbstractCamelProjectCommand.java
public static String extractComponentJavaType(Dependency dependency, String scheme) { try {//from www .j av a 2 s .c o m // 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/" + scheme); if (is != null) { Properties props = new Properties(); props.load(is); return (String) props.get("class"); } } } catch (Throwable e) { // ignore } return null; }
From source file:io.fabric8.forge.camel.commands.project.AbstractCamelProjectCommand.java
public static String loadComponentJSonSchema(Dependency dependency, String scheme) { String answer = null;//from ww w .java2 s. c o m 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; }
From source file:com.wavemaker.commons.io.ResourceURLTest.java
@Test public void shouldWorkViaClassLoader() throws Exception { Folder jail = this.root.getFolder("jail").jail(); URLClassLoader classLoader = new URLClassLoader(new URL[] { ResourceURL.get(jail) }); assertThat(IOUtils.toString(classLoader.getResourceAsStream("/a/b/c.txt")), Matchers.is(Matchers.equalTo("c"))); assertThat(classLoader.getResource("/x/y/z.txt").toString(), Matchers.is(Matchers.equalTo("rfs:/x/y/z.txt"))); }
From source file:com.varaneckas.hawkscope.plugin.PluginManager.java
private void processPlugin(final File pluginDir, final String jar) { try {// ww w . java2 s . co m final File jarFile = new File(pluginDir.getAbsolutePath() + "/" + jar); final URLClassLoader classLoader = new URLClassLoader(new URL[] { jarFile.toURI().toURL() }); final Reader r = new InputStreamReader(classLoader.getResourceAsStream("plugin.loader")); String pluginClass = ""; int c = 0; while ((c = r.read()) != -1) { pluginClass += (char) c; } r.close(); log.debug("Processing Plugin: " + pluginClass); if (!isPluginEnabled(pluginClass)) { log.debug("Plugin disabled, skipping"); getAllPlugins().add(new DisabledPlugin(pluginClass)); return; } Class<?> p = classLoader.loadClass(pluginClass); Method creator = null; try { creator = p.getMethod("getInstance", new Class[] {}); } catch (final NoSuchMethodException no) { log.debug("Plugin does not implement a singleton getter"); } Plugin plugin; if (creator == null) { plugin = (Plugin) p.newInstance(); } else { plugin = (Plugin) creator.invoke(p, new Object[] {}); } creator = null; p = null; if (plugin != null) { log.debug("Adding plugin: " + plugin); getAllPlugins().add(plugin); } } catch (final Exception e) { log.warn("Failed loading plugin: " + jar, e); } }
From source file:mobac.mapsources.loader.MapPackManager.java
public void loadMapPack(File mapPackFile, MapSourcesManager mapSourcesManager) throws CertificateException, IOException, MapSourceCreateException { // testMapPack(mapPackFile); URLClassLoader urlCl; URL url = mapPackFile.toURI().toURL(); urlCl = new MapPackClassLoader(url, ClassLoader.getSystemClassLoader()); InputStream manifestIn = urlCl.getResourceAsStream("META-INF/MANIFEST.MF"); String rev = null;/*from ww w . j av a2 s.c o m*/ if (manifestIn != null) { Manifest mf = new Manifest(manifestIn); rev = mf.getMainAttributes().getValue("MapPackRevision"); manifestIn.close(); if (rev != null) { if ("exported".equals(rev)) { rev = ProgramInfo.getRevisionStr(); } else { rev = Integer.toString(Utilities.parseSVNRevision(rev)); } } mf = null; } MapSourceLoaderInfo loaderInfo = new MapSourceLoaderInfo(LoaderType.MAPPACK, mapPackFile, rev); final Iterator<MapSource> iterator = ServiceLoader.load(MapSource.class, urlCl).iterator(); while (iterator.hasNext()) { try { MapSource ms = iterator.next(); ms.setLoaderInfo(loaderInfo); mapSourcesManager.addMapSource(ms); log.trace("Loaded map source: " + ms.toString() + " (name: " + ms.getName() + ")"); } catch (Error e) { urlCl = null; throw new MapSourceCreateException("Failed to load a map sources from map pack: " + mapPackFile.getName() + " " + e.getMessage(), e); } } }
From source file:io.fabric8.ConnectorMojo.java
/** * Finds and embeds the Camel component JSon schema file *///from ww w .j av a 2s .c o m private File embedCamelComponentSchema(File file) throws MojoExecutionException { try { List<String> json = loadFile(file); String scheme = extractScheme(json); String groupId = extractGroupId(json); String artifactId = extractArtifactId(json); String version = extractVersion(json); // version not in use // find the artifact on the classpath that has the Camel component this connector is using // then we want to grab its json schema file to embed in this JAR so we have all files together if (scheme != null && groupId != null && artifactId != null) { for (Artifact artifact : getProject().getDependencyArtifacts()) { if ("jar".equals(artifact.getType())) { if (groupId.equals(artifact.getGroupId()) && artifactId.equals(artifact.getArtifactId())) { // load the component file inside the file URL url = new URL("file:" + artifact.getFile()); URLClassLoader child = new URLClassLoader(new URL[] { url }, this.getClass().getClassLoader()); InputStream is = child .getResourceAsStream("META-INF/services/org/apache/camel/component/" + scheme); if (is != null) { List<String> lines = loadFile(is); String fqn = extractClass(lines); is.close(); // only keep package String pck = fqn.substring(0, fqn.lastIndexOf(".")); String name = pck.replace(".", "/") + "/" + scheme + ".json"; is = child.getResourceAsStream(name); if (is != null) { List<String> schema = loadFile(is); is.close(); // write schema to file File out = new File(classesDirectory, "camel-component-schema.json"); FileOutputStream fos = new FileOutputStream(out, false); for (String line : schema) { fos.write(line.getBytes()); fos.write("\n".getBytes()); } fos.close(); getLog().info("Embedded camel-component-schema.json file for Camel component " + scheme); return out; } } } } } } } catch (Exception e) { throw new MojoExecutionException("Cannot read file camel-connector.json", e); } return null; }