List of usage examples for java.lang ClassLoader getResourceAsStream
public InputStream getResourceAsStream(String name)
From source file:io.mindmaps.graql.GraqlShell.java
private void printLicense() { StringBuilder result = new StringBuilder(""); //Get file from resources folder ClassLoader classloader = Thread.currentThread().getContextClassLoader(); InputStream is = classloader.getResourceAsStream(LICENSE_LOCATION); Scanner scanner = new Scanner(is); while (scanner.hasNextLine()) { String line = scanner.nextLine(); result.append(line).append("\n"); }/* w w w . j a v a 2s.c o m*/ result.append("\n"); scanner.close(); this.print(result.toString()); }
From source file:ca.oson.json.util.ObjectUtil.java
/** * Returns a list containing one parameter name for each argument accepted * by the given constructor. If the class was compiled with debugging * symbols, the parameter names will match those provided in the Java source * code. Otherwise, a generic "arg" parameter name is generated ("arg0" for * the first argument, "arg1" for the second...). * * This method relies on the constructor's class loader to locate the * bytecode resource that defined its class. * * @param constructor the constructor to get a list of parameter names * @return a list of parameter names/* w ww. jav a 2 s. c o m*/ * @throws IOException io exception to throw */ private static List<String> getParameterNamesBytecode(Constructor<?> constructor) throws IOException { Class<?> declaringClass = constructor.getDeclaringClass(); ClassLoader declaringClassLoader = declaringClass.getClassLoader(); if (declaringClassLoader == null) { return null; } org.objectweb.asm.Type declaringType = org.objectweb.asm.Type.getType(declaringClass); String constructorDescriptor = org.objectweb.asm.Type.getConstructorDescriptor(constructor); String url = declaringType.getInternalName() + ".class"; InputStream classFileInputStream = declaringClassLoader.getResourceAsStream(url); if (classFileInputStream == null) { // throw new IllegalArgumentException("The constructor's class loader cannot find the bytecode that defined the constructor's class (URL: " + url + ")"); return null; } ClassNode classNode; try { classNode = new ClassNode(); ClassReader classReader = new ClassReader(classFileInputStream); classReader.accept(classNode, 0); } finally { classFileInputStream.close(); } @SuppressWarnings("unchecked") List<MethodNode> methods = classNode.methods; for (MethodNode method : methods) { if (method.name.equals("<init>") && method.desc.equals(constructorDescriptor)) { org.objectweb.asm.Type[] argumentTypes = org.objectweb.asm.Type.getArgumentTypes(method.desc); List<String> parameterNames = new ArrayList<String>(argumentTypes.length); @SuppressWarnings("unchecked") List<LocalVariableNode> localVariables = method.localVariables; for (int i = 0; i < argumentTypes.length && i < localVariables.size() - 1; i++) { // The first local variable actually represents the "this" object parameterNames.add(localVariables.get(i + 1).name); } return parameterNames; } } return null; }
From source file:com.quinsoft.zeidon.standardoe.ApplicationImpl.java
@Override public List<String> getLodNameList(Task task) { ClassLoader loader = this.getClass().getClassLoader(); final String resourceDir = getObjectDir() + "/"; Pattern pattern = Pattern.compile("(.*)(\\.xod$)", Pattern.CASE_INSENSITIVE); try {/* w ww. j a v a 2 s. c o m*/ return (List<String>) IOUtils.readLines(loader.getResourceAsStream(resourceDir), StandardCharsets.UTF_8) .stream().map(resourceName -> pattern.matcher(resourceName)) // Create a matcher .filter(matcher -> matcher.matches()) // Keep only ones that match. .map(matcher -> matcher.group(1)) // Get the base filename. .collect(Collectors.toList()); } catch (IOException e) { throw ZeidonException.wrapException(e).appendMessage("XOD resource dir: %s", resourceDir); } }
From source file:nl.toolforge.karma.core.cmd.impl.AbstractBuildCommand.java
private final File getBuildFile(String buildFile) throws IOException { File tmp = null;//from w ww .jav a2 s. c om tmp = MyFileUtils.createTempDirectory(); tmp.deleteOnExit(); ClassLoader loader = this.getClass().getClassLoader(); BufferedReader in = new BufferedReader( new InputStreamReader(loader.getResourceAsStream("ant/" + buildFile))); BufferedWriter out = new BufferedWriter(new FileWriter(new File(tmp, buildFile))); String str; while ((str = in.readLine()) != null) { out.write(str); } out.close(); in.close(); // Return a temp reference to the file // return new File(tmp, buildFile); }
From source file:org.apache.hama.manager.util.UITemplate.java
/** * read template file contents/*ww w .java2s. c o m*/ * * @param template fileName */ protected String loadTemplateResource(String fileName) { ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); if (classLoader == null) { classLoader = this.getClass().getClassLoader(); } InputStream is = classLoader.getResourceAsStream(fileName); InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr); StringBuffer sb = new StringBuffer(); String line; try { while ((line = br.readLine()) != null) { sb.append(line); } } catch (Exception e) { e.printStackTrace(); } finally { if (br != null) { try { br.close(); } catch (IOException e) { e.printStackTrace(); } } if (isr != null) { try { isr.close(); } catch (IOException e) { e.printStackTrace(); } } if (is != null) { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } } return sb.toString(); }
From source file:com.joyent.manta.client.MantaClientPutIT.java
@Test public final void testPutWithJPGFile() throws IOException { final String name = UUID.randomUUID().toString(); final String path = testPathPrefix + name; File temp = File.createTempFile("upload", ".jpg"); FileUtils.forceDeleteOnExit(temp);/* w w w .ja v a2 s . co m*/ final ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); try (InputStream testDataInputStream = classLoader.getResourceAsStream(TEST_FILENAME); OutputStream out = new FileOutputStream(temp)) { IOUtils.copy(testDataInputStream, out); MantaObject response = mantaClient.put(path, temp); String contentType = response.getContentType(); Assert.assertEquals(contentType, "image/jpeg", "Content type wasn't detected correctly"); try (InputStream in = mantaClient.getAsInputStream(path)) { byte[] actual = IOUtils.toByteArray(in); byte[] expected = FileUtils.readFileToByteArray(temp); Assert.assertTrue(Arrays.equals(actual, expected), "Uploaded file isn't the same as actual file"); } } }
From source file:architecture.ee.component.core.lifecycle.RepositoryImpl.java
private Resource getRootResource() { if (initailized) return rootResource; if (getState() != State.INITIALIZED || getState() == State.INITIALIZING) { ClassLoader classloader = Thread.currentThread().getContextClassLoader(); try {// w w w .java 2 s .co m InputStream input = classloader.getResourceAsStream("application-init.xml"); XmlProperties prop = new XmlProperties(input); String path = prop.getProperty("home"); if (!StringUtils.isEmpty(path)) { Resource resource = resoruceLoader.getResource(path); log.debug(L10NUtils.format("003001", resource.getURI())); this.rootResource = resource; setState(State.INITIALIZED); initailized = true; } } catch (Throwable e) { } } return rootResource; }
From source file:com.liferay.portal.model.ModelHintsImpl.java
public void read(ClassLoader classLoader, String source) throws Exception { read(classLoader, source, classLoader.getResourceAsStream(source)); }
From source file:org.suren.autotest.web.framework.code.DefaultXmlSuiteRunnerGenerator.java
@Override public void generate(String srcCoding, String outputDir) { this.srcCoding = srcCoding; ClassLoader classLoader = this.getClass().getClassLoader(); //??// w w w . j a v a 2 s . c om try (InputStream confInput = classLoader.getResourceAsStream(srcCoding)) { generate(confInput, outputDir, null); } catch (IOException e) { logger.error(String.format("Main config [%s] parse process error.", srcCoding), e); } }
From source file:com.kolich.curacao.embedded.mappers.ClasspathFileReturnMapper.java
@Override public final void render(final AsyncContext context, final HttpServletResponse response, final @Nonnull File entity) throws Exception { // Get the path, and remove the leading slash. final String path = entity.getAbsolutePath().substring(1); final ClassLoader loader = Thread.currentThread().getContextClassLoader(); final URL resource = loader.getResource(path); if (resource == null) { logger__.warn("Resource not found: {}", path); response.setStatus(HttpServletResponse.SC_NOT_FOUND); } else {//from w w w .j ava 2 s.co m logger__.debug("Serving resource from classpath: {}", path); try (final InputStream in = loader.getResourceAsStream(path); final OutputStream out = response.getOutputStream()) { IOUtils.copyLarge(in, out); } } }