List of usage examples for java.lang ClassLoader getResourceAsStream
public InputStream getResourceAsStream(String name)
From source file:org.n52.movingcode.runtime.processors.config.ProcessorConfig.java
/** * Properties reader/*from w w w . ja v a 2 s.c o m*/ * * @return */ private static final HashMap<String, ProcessorDescription> readProperties() { ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); InputStream is = classLoader.getResourceAsStream(configFile); HashMap<String, ProcessorDescription> processorMap = new HashMap<String, ProcessorDescription>(); JsonFactory f = new JsonFactory(); JsonParser jp; try { jp = f.createParser(is); jp.nextToken(); // will return JsonToken.START_OBJECT while (jp.nextToken() != END_OBJECT) { String field = jp.getCurrentName(); if (field.equalsIgnoreCase(KEY_PROCESSORS)) { // get next token, make sure it is the beginning of an array if (jp.nextToken() != START_ARRAY) { break; } while (jp.nextToken() != END_ARRAY) { // do the parsing if (jp.getCurrentToken() == START_OBJECT) { ProcessorDescription p = parseProcessorJSON(jp); // only add those processor that have a valid ID if (p.getId() != null) { processorMap.put(p.getId(), p); } } } } else { if (field.equalsIgnoreCase(KEY_DEFAULTS)) { // parse defaults ProcessorDescription p = parseProcessorJSON(jp); p.setId(DEFAULT_PROCESSOR_CONFIG_ID); processorMap.put(p.getId(), p); } } } } catch (JsonParseException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return processorMap; }
From source file:Main.java
public static InputStream getResourceAsStream(String name, Class clazz) { ClassLoader loader; InputStream retval = null;/*from w w w . j a v a 2 s . co m*/ // https://issues.jboss.org/browse/JGRP-1762: load the classloader from the defining class first if (clazz != null) { try { loader = clazz.getClassLoader(); if (loader != null) { retval = loader.getResourceAsStream(name); if (retval != null) return retval; } } catch (Throwable t) { } } try { loader = Thread.currentThread().getContextClassLoader(); if (loader != null) { retval = loader.getResourceAsStream(name); if (retval != null) return retval; } } catch (Throwable t) { } try { loader = ClassLoader.getSystemClassLoader(); if (loader != null) { return loader.getResourceAsStream(name); } } catch (Throwable t) { } return retval; }
From source file:com.cablevision.util.sso.UtilSSO.java
/** * Creates a PublicKey from the specified public key file and algorithm. * Returns null if failure to generate PublicKey. * /* www. j av a2 s. c o m*/ * @param publicKeyFilepath location of public key file * @param algorithm algorithm of specified key file * @return PublicKey object representing contents of specified public key * file, null if error in generating key or invalid file specified */ public static PublicKey getPublicKey(String publicKeyFilepath, String algorithm) throws SamlException { try { InputStream pubKey = null; ClassLoader cl = Thread.currentThread().getContextClassLoader(); pubKey = cl.getResourceAsStream(publicKeyFilepath); byte[] bytes = IOUtils.toByteArray(pubKey); pubKey.close(); System.out.println("Private bytes: " + Arrays.toString(bytes)); pubKey.close(); X509EncodedKeySpec pubSpec = new X509EncodedKeySpec(bytes); KeyFactory factory = KeyFactory.getInstance(algorithm); return factory.generatePublic(pubSpec); } catch (FileNotFoundException e) { throw new SamlException("ERROR: Public key file not found - " + publicKeyFilepath); } catch (IOException e) { throw new SamlException("ERROR: Invalid public key file - " + e.getMessage()); } catch (NoSuchAlgorithmException e) { throw new SamlException("ERROR: Invalid public key algorithm - " + e.getMessage()); } catch (InvalidKeySpecException e) { throw new SamlException("ERROR: Invalid public key spec - " + e.getMessage()); } }
From source file:com.cablevision.util.sso.UtilSSO.java
/** * Creates a PrivateKey from the specified public key file and algorithm. * Returns null if failure to generate PrivateKey. * //from w ww .ja v a 2 s. com * @param PrivateKeyFilepath location of public key file * @param algorithm algorithm of specified key file * @return PrivateKey object representing contents of specified private key * file, null if error in generating key or invalid file specified */ public static PrivateKey getPrivateKey(String privateKeyFilepath, String algorithm) throws SamlException { try { InputStream privKey = null; ClassLoader cl = Thread.currentThread().getContextClassLoader(); privKey = cl.getResourceAsStream(privateKeyFilepath); byte[] bytes = IOUtils.toByteArray(privKey); privKey.close(); System.out.println("Private bytes: " + Arrays.toString(bytes)); PKCS8EncodedKeySpec privSpec = new PKCS8EncodedKeySpec(bytes); KeyFactory factory = KeyFactory.getInstance(algorithm); return factory.generatePrivate(privSpec); } catch (FileNotFoundException e) { throw new SamlException("ERROR: Private key file not found - " + privateKeyFilepath); } catch (IOException e) { throw new SamlException("ERROR: Invalid private key file - " + e.getMessage()); } catch (NoSuchAlgorithmException e) { throw new SamlException("ERROR: Invalid private key algorithm - " + e.getMessage()); } catch (InvalidKeySpecException e) { throw new SamlException("ERROR: Invalid private key spec - " + e.getMessage()); } }
From source file:org.apache.pdfbox.pdmodel.font.encoding.GlyphList.java
/** * Loads a glyph list from disk.// w ww . j av a 2 s .c o m */ private static GlyphList load(String filename) { ClassLoader loader = GlyphList.class.getClassLoader(); String path = "org/apache/pdfbox/resources/glyphlist/"; try { return new GlyphList(loader.getResourceAsStream(path + filename)); } catch (IOException e) { throw new RuntimeException(e); } }
From source file:org.apache.hadoop.hbase.zookeeper.ZKConfig.java
/** * Make a Properties object holding ZooKeeper config. * Parses the corresponding config options from the HBase XML configs * and generates the appropriate ZooKeeper properties. * @param conf Configuration to read from. * @return Properties holding mappings representing ZooKeeper config file. *//*from w ww . ja va 2 s.co m*/ public static Properties makeZKProps(Configuration conf) { if (conf.getBoolean(HConstants.HBASE_CONFIG_READ_ZOOKEEPER_CONFIG, false)) { LOG.warn("Parsing ZooKeeper's " + HConstants.ZOOKEEPER_CONFIG_NAME + " file for ZK properties " + "has been deprecated. Please instead place all ZK related HBase " + "configuration under the hbase-site.xml, using prefixes " + "of the form '" + HConstants.ZK_CFG_PROPERTY_PREFIX + "', and " + "set property '" + HConstants.HBASE_CONFIG_READ_ZOOKEEPER_CONFIG + "' to false"); // First check if there is a zoo.cfg in the CLASSPATH. If so, simply read // it and grab its configuration properties. ClassLoader cl = HQuorumPeer.class.getClassLoader(); final InputStream inputStream = cl.getResourceAsStream(HConstants.ZOOKEEPER_CONFIG_NAME); if (inputStream != null) { try { return parseZooCfg(conf, inputStream); } catch (IOException e) { LOG.warn("Cannot read " + HConstants.ZOOKEEPER_CONFIG_NAME + ", loading from XML files", e); } } } else { if (LOG.isTraceEnabled()) { LOG.trace("Skipped reading ZK properties file '" + HConstants.ZOOKEEPER_CONFIG_NAME + "' since '" + HConstants.HBASE_CONFIG_READ_ZOOKEEPER_CONFIG + "' was not set to true"); } } // Otherwise, use the configuration options from HBase's XML files. Properties zkProperties = new Properties(); // Directly map all of the hbase.zookeeper.property.KEY properties. for (Entry<String, String> entry : new Configuration(conf)) { // copy for mt safety String key = entry.getKey(); if (key.startsWith(HConstants.ZK_CFG_PROPERTY_PREFIX)) { String zkKey = key.substring(HConstants.ZK_CFG_PROPERTY_PREFIX_LEN); String value = entry.getValue(); // If the value has variables substitutions, need to do a get. if (value.contains(VARIABLE_START)) { value = conf.get(key); } zkProperties.put(zkKey, value); } } // If clientPort is not set, assign the default. if (zkProperties.getProperty(HConstants.CLIENT_PORT_STR) == null) { zkProperties.put(HConstants.CLIENT_PORT_STR, HConstants.DEFAULT_ZOOKEPER_CLIENT_PORT); } // Create the server.X properties. int peerPort = conf.getInt("hbase.zookeeper.peerport", 2888); int leaderPort = conf.getInt("hbase.zookeeper.leaderport", 3888); final String[] serverHosts = conf.getStrings(HConstants.ZOOKEEPER_QUORUM, HConstants.LOCALHOST); for (int i = 0; i < serverHosts.length; ++i) { String serverHost = serverHosts[i]; String address = serverHost + ":" + peerPort + ":" + leaderPort; String key = "server." + i; zkProperties.put(key, address); } return zkProperties; }
From source file:TestXjc.java
/** * @param input//w w w. j av a 2 s . com * @return * @throws InstantiationException * @throws IllegalAccessException * @throws ClassNotFoundException * @throws XQException * @throws XMLStreamException * @throws ParserConfigurationException * @throws SAXException * @throws IOException */ /*@SuppressWarnings("restriction") private static String processXquery1(String input) throws InstantiationException, IllegalAccessException, ClassNotFoundException, XQException, XMLStreamException, ParserConfigurationException, SAXException, IOException{ XQDataSource xqds = new SaxonXQDataSource(); XQConnection conn = xqds.getConnection(); String str=""; XQExpression xqe; XQSequence xqs; xqe = conn.createExpression(); xqs=xqe.executeQuery("doc('src/main/resources/example/i2b2/i2b2medspod.txt')//observation"); while (xqs.next()) { System.out.println(xqs.getSequenceAsString(null)); //str+=xqs.getAtomicValue(); } //return str; // InputStream query; query = new FileInputStream("src/main/resources/transform/I2b2ToFhir/i2b2MedsToFHIRMeds.xquery"); xqe = conn.createExpression(); xqs = xqe.executeQuery(query); // String query = getFile("transform/I2b2ToFhir/i2b2MedsToFHIRMeds.xquery"); XQPreparedExpression expr = conn.prepareExpression(query); input=getFile("example/i2b2/i2b2medspod.txt"); //System.out.println("processsing:"+input); DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); DocumentBuilder parser = factory.newDocumentBuilder(); InputStream stream = new ByteArrayInputStream(input.getBytes(StandardCharsets.UTF_8)); Document domDocument = parser.parse(stream); expr.bindNode(new QName("doc"), domDocument,null); xqs=expr.executeQuery(); while (xqs.next()) { System.out.println(xqs.getSequenceAsString(null)); //str+=xqs.getAtomicValue(); } return str; // Reader reader = new StringReader(input); XMLInputFactory factory = XMLInputFactory.newInstance(); // Or newFactory() XMLStreamReader xmlReader = factory.createXMLStreamReader(reader); expr.bindDocument(new QName("doc"), xmlReader, null); String query = "declare variable $x as xs:integer external; " +sep+" for $n in $x"+ sep +" return <test>$n+1</test>"; XQPreparedExpression expr = conn.prepareExpression(query); expr.bindInt(new QName("x"), 21, null); // // Bind a value (21) to an external variable with the QName x // Execute the XQuery expression //XQExpression expr = conn.createExpression(); //XQSequence result = expr.executeQuery(); //System.out.println(result.getSequenceAsString(null)); //return result.getSequenceAsString(null); } */ public static String getFile(String fileName) { String result = ""; ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); try { result = IOUtils.toString(classLoader.getResourceAsStream(fileName)); } catch (IOException e) { e.printStackTrace(); } return result; }
From source file:com.cablevision.util.sso.UtilSSO.java
/** * Returns a String containing the contents of the file located at the * specified path.// w w w .j ava 2 s . c om * * @param path location of file to be read * @return String containing contents of file, null if error reading file * @throws IOException */ public static String readFileContents(String path) throws SamlException { StringBuffer contents = new StringBuffer(); BufferedReader input = null; try { ClassLoader cl = Thread.currentThread().getContextClassLoader(); java.io.InputStream is = cl.getResourceAsStream(path); input = new BufferedReader(new InputStreamReader(is)); String line = null; while ((line = input.readLine()) != null) { contents.append(line); } input.close(); return contents.toString(); } catch (FileNotFoundException e) { throw new SamlException("File not found: " + path); } catch (IOException e) { throw new SamlException("Error reading file: " + path); } }
From source file:biz.netcentric.cq.tools.actool.configReader.YamlConfigReaderTest.java
static String getTestConfigAsString(final String resourceName) throws IOException { final ClassLoader classloader = Thread.currentThread().getContextClassLoader(); final InputStream is = classloader.getResourceAsStream(resourceName); final StringWriter stringWriter = new StringWriter(); IOUtils.copy(is, stringWriter, "UTF-8"); return stringWriter.toString(); }
From source file:org.apache.hadoop.hbase.zookeeper.HQuorumPeer.java
/** * Make a Properties object holding ZooKeeper config equivalent to zoo.cfg. * If there is a zoo.cfg in the classpath, simply read it in. Otherwise parse * the corresponding config options from the HBase XML configs and generate * the appropriate ZooKeeper properties. * @param conf Configuration to read from. * @return Properties holding mappings representing ZooKeeper zoo.cfg file. *///from w ww . j a va2s . co m public static Properties makeZKProps(Configuration conf) { // First check if there is a zoo.cfg in the CLASSPATH. If so, simply read // it and grab its configuration properties. ClassLoader cl = HQuorumPeer.class.getClassLoader(); final InputStream inputStream = cl.getResourceAsStream(HConstants.ZOOKEEPER_CONFIG_NAME); if (inputStream != null) { try { return parseZooCfg(conf, inputStream); } catch (IOException e) { LOG.warn("Cannot read " + HConstants.ZOOKEEPER_CONFIG_NAME + ", loading from XML files", e); } } // Otherwise, use the configuration options from HBase's XML files. Properties zkProperties = new Properties(); // Directly map all of the hbase.zookeeper.property.KEY properties. for (Entry<String, String> entry : conf) { String key = entry.getKey(); if (key.startsWith(HConstants.ZK_CFG_PROPERTY_PREFIX)) { String zkKey = key.substring(ZK_CLIENT_PORT_KEY_PREFIX_LENGTH); String value = entry.getValue(); // If the value has variables substitutions, need to do a get. if (value.contains(VARIABLE_START)) { value = conf.get(key); } zkProperties.put(zkKey, value); } } // If clientPort is not set, assign the default. if (zkProperties.getProperty(HConstants.CLIENT_PORT_STR) == null) { zkProperties.put(HConstants.CLIENT_PORT_STR, HConstants.DEFAULT_ZOOKEPER_CLIENT_PORT); } // Create the server.X properties. int peerPort = conf.getInt("hbase.zookeeper.peerport", 2888); int leaderPort = conf.getInt("hbase.zookeeper.leaderport", 3888); final String[] serverHosts = conf.getStrings(HConstants.ZOOKEEPER_QUORUM, "localhost"); for (int i = 0; i < serverHosts.length; ++i) { String serverHost = serverHosts[i]; String address = serverHost + ":" + peerPort + ":" + leaderPort; String key = "server." + i; zkProperties.put(key, address); } LOG.trace("Created ZK properties: " + zkProperties); return zkProperties; }