List of usage examples for java.lang ClassLoader getResourceAsStream
public InputStream getResourceAsStream(String name)
From source file:com.vangent.hieos.services.xds.bridge.utils.JUnitHelper.java
/** * Method description// www.j a v a 2 s . co m * * * * * @param file * @param count * @param documentIds * @return * * @throws Exception */ public static OMElement createOMRequest(String file, int count, String[] documentIds) throws Exception { ObjectFactory factory = new ObjectFactory(); SubmitDocumentRequest sdr = factory.createSubmitDocumentRequest(); IdType pid = factory.createIdType(); pid.setRoot("1.3.6.1.4.1.21367.2005.3.7.6fa11e467880478"); sdr.setPatientId(pid); ClassLoader classLoader = JUnitHelper.class.getClassLoader(); DocumentsType documents = factory.createDocumentsType(); for (int i = 0; i < count; ++i) { DocumentType document = factory.createDocumentType(); if ((documentIds != null) && (documentIds.length > i)) { document.setId(documentIds[i]); } CodeType type = factory.createCodeType(); type.setCode("51855-5"); type.setCodeSystem("2.16.840.1.113883.6.1"); document.setType(type); ByteArrayOutputStream bos = new ByteArrayOutputStream(); InputStream is = classLoader.getResourceAsStream(file); assertNotNull(is); IOUtils.copy(is, bos); document.setContent(bos.toByteArray()); documents.getDocument().add(document); } sdr.setDocuments(documents); QName qname = new QName(URIConstants.XDSBRIDGE_URI, "SubmitDocumentRequest"); JAXBContext jc = JAXBContext.newInstance(SubmitDocumentRequest.class); Marshaller marshaller = jc.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE); JAXBElement element = new JAXBElement(qname, sdr.getClass(), sdr); StringWriter sw = new StringWriter(); marshaller.marshal(element, sw); String xml = sw.toString(); logger.debug(xml); OMElement result = AXIOMUtil.stringToOM(OMAbstractFactory.getOMFactory(), xml); List<OMElement> list = XPathHelper.selectNodes(result, "./ns:Documents/ns:Document/ns:Content", URIConstants.XDSBRIDGE_URI); for (OMElement contentNode : list) { OMText binaryNode = (OMText) contentNode.getFirstOMChild(); if (binaryNode != null) { binaryNode.setOptimize(true); } } return result; }
From source file:PropertyLoader.java
public static Properties loadProperties(String name, ClassLoader loader) throws Exception { if (name.startsWith("/")) name = name.substring(1);/*from ww w. j av a 2s . c om*/ if (name.endsWith(SUFFIX)) name = name.substring(0, name.length() - SUFFIX.length()); Properties result = new Properties(); InputStream in = null; if (loader == null) loader = ClassLoader.getSystemClassLoader(); if (LOAD_AS_RESOURCE_BUNDLE) { name = name.replace('/', '.'); ResourceBundle rb = ResourceBundle.getBundle(name, Locale.getDefault(), loader); for (Enumeration keys = rb.getKeys(); keys.hasMoreElements();) { result.put((String) keys.nextElement(), rb.getString((String) keys.nextElement())); } } else { name = name.replace('.', '/'); if (!name.endsWith(SUFFIX)) name = name.concat(SUFFIX); in = loader.getResourceAsStream(name); if (in != null) { result = new Properties(); result.load(in); // can throw IOException } } in.close(); return result; }
From source file:de.sub.goobi.forms.IndexingForm.java
private static String readMapping() throws ParseException { JSONParser parser = new JSONParser(); ClassLoader classloader = Thread.currentThread().getContextClassLoader(); try (InputStream inputStream = classloader.getResourceAsStream("mapping.json")) { String mapping = IOUtils.toString(inputStream, "UTF-8"); Object object = parser.parse(mapping); JSONObject jsonObject = (JSONObject) object; return jsonObject.toJSONString(); } catch (IOException e) { logger.error(e);// w ww.j a v a 2 s . c o m return ""; } }
From source file:oracle.kv.hadoop.table.TableRecordReaderBase.java
/** * Convenience method that retrieves the login configuration and trust * credentials as resources from the classpath, and writes that * information to corresponding files on the user's local file system. * The login file and trust file that are created by this method can * then be used when attempting to interact with a secure KVStore. * <p>//from w ww . ja v a2 s . c om * This method returns a string containing the fully qualified path to * the login file that is created. The login file will reference the * name of the trust file that is created; which must be written to * the same directory in which the login file resides. */ private static String createLocalKVSecurity(final String loginFlnm, final String trustFlnm) throws IOException { if (loginFlnm == null) { return null; } if (trustFlnm == null) { return null; } /* * If loginFile and/or trustFile is a filename and not an absolute * path, then generate local versions of the file. */ final File userSecurityDirFd = new File(USER_SECURITY_DIR); if (!userSecurityDirFd.exists()) { if (!userSecurityDirFd.mkdirs()) { throw new IOException("failed to create " + userSecurityDirFd); } } InputStream loginStream = null; InputStream trustStream = null; final ClassLoader cl = TableRecordReaderBase.class.getClassLoader(); if (cl != null) { loginStream = cl.getResourceAsStream(loginFlnm); trustStream = cl.getResourceAsStream(trustFlnm); } else { loginStream = ClassLoader.getSystemResourceAsStream(loginFlnm); trustStream = ClassLoader.getSystemResourceAsStream(trustFlnm); } /* * Retrieve the login configuration as a resource from the classpath, * and write that information to the user's local file system. */ final Properties loginProps = new Properties(); if (loginStream != null) { loginProps.load(loginStream); } /* Strip off the path of the trust file. */ final String trustProp = loginProps.getProperty(KVSecurityConstants.SSL_TRUSTSTORE_FILE_PROPERTY); if (trustProp != null) { final File trustPropFd = new File(trustProp); if (!trustPropFd.exists()) { loginProps.setProperty(KVSecurityConstants.SSL_TRUSTSTORE_FILE_PROPERTY, trustPropFd.getName()); } } final File loginFd = new File(USER_SECURITY_DIR + FILE_SEP + loginFlnm); final FileOutputStream loginFos = new FileOutputStream(loginFd); loginProps.store(loginFos, null); loginFos.close(); /* * Retrieve the trust credentials as a resource from the classpath, * and write that information to the user's local file system. */ final File trustFd = new File(USER_SECURITY_DIR + FILE_SEP + trustFlnm); final FileOutputStream trustFlnmFos = new FileOutputStream(trustFd); if (trustStream != null) { int nextByte = trustStream.read(); while (nextByte != -1) { trustFlnmFos.write(nextByte); nextByte = trustStream.read(); } } trustFlnmFos.close(); return loginFd.toString(); }
From source file:org.apache.synapse.transport.http.access.AccessConfiguration.java
/** * Loads the properties from a given property file path * * @param filePath Path of the property file * @return Properties loaded from given file *///from w w w. ja v a 2 s. c om private static Properties loadProperties(String filePath) { Properties properties = new Properties(); ClassLoader cl = Thread.currentThread().getContextClassLoader(); if (log.isTraceEnabled()) { log.debug("Loading the file '" + filePath + "' from classpath"); } InputStream in = null; //if we reach to this assume that the we may have to looking to the customer provided external location for the //given properties if (System.getProperty(CONF_LOCATION) != null) { try { in = new FileInputStream(System.getProperty(CONF_LOCATION) + File.separator + filePath); } catch (FileNotFoundException e) { String msg = "Error loading properties from file: " + filePath; log.warn(msg); } } if (in == null) { in = cl.getResourceAsStream(filePath); if (log.isTraceEnabled()) { log.debug("Unable to load file '" + filePath + "'"); } filePath = "conf" + File.separatorChar + filePath; if (log.isTraceEnabled()) { log.debug("Loading the file '" + filePath + "'"); } in = cl.getResourceAsStream(filePath); if (in == null) { if (log.isTraceEnabled()) { log.debug("Unable to load file '" + filePath + "'"); } } } if (in != null) { try { properties.load(in); } catch (IOException e) { String msg = "Error loading properties from a file at : " + filePath; log.error(msg, e); } } return properties; }
From source file:com.depas.utils.FileUtils.java
public static Properties getProperties(String filePath) throws IOException { InputStream inStream = null;/*from w w w . j a va2s.c om*/ try { String decodedFilePath = URLDecoder.decode(filePath, "UTF-8"); // inStream=new FileInputStream(decodedFilePath); ClassLoader cl = Thread.currentThread().getContextClassLoader(); if (cl.getResource(decodedFilePath) == null) { throw new FileNotFoundException( "Ignite was unable to find the resource [" + decodedFilePath + "]."); } inStream = cl.getResourceAsStream(decodedFilePath); Properties properties = new Properties(); properties.load(inStream); return properties; } finally { if (inStream != null) { try { inStream.close(); } catch (Exception ex) { logger.warn("Error closing InputStream [filePath=" + filePath + "]: " + ex); } } } }
From source file:com.ibm.devops.dra.AbstractDevOpsAction.java
public static void printPluginVersion(ClassLoader loader, PrintStream printStream) { final Properties properties = new Properties(); try {// w ww. j a v a2s. c o m properties.load(loader.getResourceAsStream("plugin.properties")); printStream.println("[IBM Cloud DevOps] version: " + properties.getProperty("version")); } catch (IOException e) { e.printStackTrace(); } }
From source file:com.blackducksoftware.integration.hub.jenkins.tests.ScanIntegrationTest.java
@BeforeClass public static void init() throws Exception { basePath = ScanIntegrationTest.class.getProtectionDomain().getCodeSource().getLocation().getPath(); basePath = basePath.substring(0, basePath.indexOf(File.separator + "target")); basePath = basePath + File.separator + "test-workspace"; testWorkspace = URLDecoder.decode(basePath + File.separator + "workspace", "UTF-8"); testProperties = new Properties(); final ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); final InputStream is = classLoader.getResourceAsStream("test.properties"); try {//ww w .j a v a 2 s .co m testProperties.load(is); } catch (final IOException e) { System.err.println("reading test.properties failed!"); } // p.load(new FileReader(new File("test.properties"))); System.out.println(testProperties.getProperty("TEST_HUB_SERVER_URL")); System.out.println(testProperties.getProperty("TEST_USERNAME")); System.out.println(testProperties.getProperty("TEST_PASSWORD")); restHelper = new JenkinsHubIntTestHelper(testProperties.getProperty("TEST_HUB_SERVER_URL")); restHelper.setTimeout(300); restHelper.setCookies(testProperties.getProperty("TEST_USERNAME"), testProperties.getProperty("TEST_PASSWORD")); projectCleanup(); }
From source file:com.depas.utils.FileUtils.java
/** * Returns content of resource that exists on classpath *///from w ww.j ava 2 s. c om public static String getTextFromResource(String resourceName) throws IOException { ClassLoader cl = Thread.currentThread().getContextClassLoader(); if (cl.getResource(resourceName) == null) { throw new FileNotFoundException( "The resource " + resourceName + " could not be found on the current classpath."); } StringBuffer buf = new StringBuffer(); BufferedInputStream bis = null; try { InputStream is = cl.getResourceAsStream(resourceName); bis = new BufferedInputStream(is); int i; while ((i = bis.read()) != -1) { buf.append((char) i); } } finally { try { bis.close(); } catch (Exception ex) { } } return buf.toString(); }
From source file:io.trivium.glue.binding.http.StaticResourceHandler.java
@Override public void handle(HttpExchange httpExchange) throws IOException { Session session = new Session(httpExchange); String origURI = httpExchange.getRequestURI().getPath(); String uri = origURI.replace(httpUri, packageUri); ClassLoader cl = ClassLoader.getSystemClassLoader(); InputStream is = cl.getResourceAsStream(uri); if (is != null) { byte[] buf = IOUtils.toByteArray(is); String ending = uri.substring(uri.lastIndexOf('.') + 1); String contentType = MimeTypes.getMimeType(ending, "text/plain"); session.ok(contentType, new String(buf)); return;/*from w w w. jav a 2s . c om*/ } //if no response was send so far session.error(404, "resource not found"); }