List of usage examples for java.lang ClassLoader getResource
public URL getResource(String name)
From source file:com.ibm.iotf.client.AbstractClient.java
static SSLSocketFactory getSocketFactory(final String caCrtFile, final String crtFile, final String keyFile, final String password) throws IOException, KeyStoreException, NoSuchAlgorithmException, CertificateException, UnrecoverableKeyException, KeyManagementException { Security.addProvider(new BouncyCastleProvider()); X509Certificate caCert = null; if (caCrtFile != null) { // load CA certificate PEMReader reader = new PEMReader( new InputStreamReader(new ByteArrayInputStream(Files.readAllBytes(Paths.get(caCrtFile))))); caCert = (X509Certificate) reader.readObject(); reader.close();// w ww .j a v a 2s . co m } else { ClassLoader classLoader = AbstractClient.class.getClassLoader(); PEMReader reader = new PEMReader( new InputStreamReader(classLoader.getResource(SERVER_MESSAGING_PEM).openStream())); caCert = (X509Certificate) reader.readObject(); reader.close(); } PEMReader reader = new PEMReader( new InputStreamReader(new ByteArrayInputStream(Files.readAllBytes(Paths.get(crtFile))))); X509Certificate cert = (X509Certificate) reader.readObject(); reader.close(); // load client private key reader = new PEMReader( new InputStreamReader(new ByteArrayInputStream(Files.readAllBytes(Paths.get(keyFile))))); KeyPair key = (KeyPair) reader.readObject(); reader.close(); TrustManagerFactory tmf = null; if (caCert != null) { // CA certificate is used to authenticate server KeyStore caKs = KeyStore.getInstance("JKS"); //caKs.load(null, null); caKs.load(null, null); caKs.setCertificateEntry("ca-certificate", caCert); tmf = TrustManagerFactory.getInstance("PKIX"); tmf.init(caKs); } // client key and certificates are sent to server so it can authenticate us KeyStore ks = KeyStore.getInstance("JKS"); ks.load(null, null); ks.setCertificateEntry("certificate", cert); ks.setKeyEntry("private-key", key.getPrivate(), password.toCharArray(), new java.security.cert.Certificate[] { cert }); KeyManagerFactory kmf = KeyManagerFactory.getInstance("PKIX"); kmf.init(ks, password.toCharArray()); // finally, create SSL socket factory SSLContext context = SSLContext.getInstance("TLSv1.2"); if (tmf != null) { context.init(kmf.getKeyManagers(), tmf.getTrustManagers(), null); } else { context.init(kmf.getKeyManagers(), null, null); } return context.getSocketFactory(); }
From source file:com.panet.imeta.core.config.DigesterConfigManager.java
/** * Loads the configuration parameters by delegating to commons digester. */// w ww . java 2 s . co m public Collection<T> load() throws KettleConfigException { ClassLoader loader = Thread.currentThread().getContextClassLoader(); Digester digester = DigesterLoader.createDigester(loader.getResource(rulesURL)); final Set<T> configObjs = new LinkedHashSet<T>(); digester.addRule(setNext, new SetNextRule("") { @SuppressWarnings("unchecked") public void end(String nameSpace, String name) throws Exception { configObjs.add((T) digester.peek()); } }); try { digester.parse(loader.getResource(configURL)); } catch (Exception e) { throw new KettleConfigException(e); } return configObjs; }
From source file:ffx.xray.CIFFilterTest.java
@Test public void testCIFFilter2DRM() { String filename = "ffx/xray/structures/2DRM.cif"; ClassLoader cl = this.getClass().getClassLoader(); File cifFile = new File(cl.getResource(filename).getPath()); // load any properties associated with it CompositeConfiguration properties = Keyword.loadProperties(cifFile); CIFFilter cifFilter = new CIFFilter(); ReflectionList reflectionList = cifFilter.getReflectionList(cifFile); assertNull(" Reflection list should be null", reflectionList); Crystal crystal = new Crystal(29.969, 37.861, 44.506, 90.28, 90.11, 90.64, "P1"); Resolution resolution = new Resolution(1.30); reflectionList = new ReflectionList(crystal, resolution); DiffractionRefinementData refinementData = new DiffractionRefinementData(properties, reflectionList); assertTrue(" CIF data not read correctly", cifFilter.readFile(cifFile, reflectionList, refinementData, properties)); HKL hkl = reflectionList.getHKL(-21, -6, 7); assertEquals("-21 -6 7 F", 18.6, refinementData.getF(hkl.index()), 0.01); assertEquals("-21 -6 7 sigF", 3.6, refinementData.getSigF(hkl.index()), 0.01); assertEquals("-21 -6 7 freeR value", 0, refinementData.freer[hkl.index()]); hkl = reflectionList.getHKL(-21, -6, 8); assertEquals("-21 -6 7 F", 20.2, refinementData.getF(hkl.index()), 0.01); assertEquals("-21 -6 7 sigF", 5.0, refinementData.getSigF(hkl.index()), 0.01); assertEquals("-21 -6 7 freeR value", 1, refinementData.freer[hkl.index()]); }
From source file:io.apiman.gateway.engine.jdbc.JdbcInitializer.java
/** * Do the initialization work./*from ww w. ja va 2 s .c o m*/ */ @SuppressWarnings("nls") private void doInit() { QueryRunner run = new QueryRunner(ds); Boolean isInitialized; try { isInitialized = run.query("SELECT * FROM gw_apis", new ResultSetHandler<Boolean>() { @Override public Boolean handle(ResultSet rs) throws SQLException { return true; } }); } catch (SQLException e) { isInitialized = false; } if (isInitialized) { System.out.println("============================================"); System.out.println("Apiman Gateway database already initialized."); System.out.println("============================================"); return; } ClassLoader cl = JdbcInitializer.class.getClassLoader(); URL resource = cl.getResource("ddls/apiman-gateway_" + dbType + ".ddl"); try (InputStream is = resource.openStream()) { System.out.println("======================================="); System.out.println("Initializing apiman Gateway database."); DdlParser ddlParser = new DdlParser(); List<String> statements = ddlParser.parse(is); for (String sql : statements) { System.out.println(sql); run.update(sql); } System.out.println("======================================="); } catch (Exception e) { throw new RuntimeException(e); } }
From source file:de.micromata.genome.gwiki.plugin.CombinedClassLoader.java
@Override public URL findResource(String name) { for (ClassLoader cl : parents) { URL resource = cl.getResource(name); if (resource != null) return resource; }/*from ww w .j av a 2 s.co m*/ return null; // return super.findResource(name); }
From source file:de.micromata.genome.gwiki.plugin.CombinedClassLoader.java
@Override public URL getResource(String name) { for (ClassLoader cl : parents) { URL resource = cl.getResource(name); if (resource != null) return resource; }/*from w w w. java 2s . co m*/ // return super.getResource(name); return null; }
From source file:io.klerch.alexa.tellask.util.resource.ResourceUtteranceReader.java
/** * {@inheritDoc}/*from ww w . j a v a2 s .c om*/ */ @Override public InputStream read(final String locale) { Validate.notNull(locale, "Locale must not be blank."); final String resourcePath = leadingPath + locale + resourceLocation; final String resourcePath2 = resourcePath.startsWith("/") ? resourcePath.substring(1) : resourcePath; final ClassLoader cl = getClass().getClassLoader(); Validate.notNull(cl.getResource(resourcePath2), "Resource " + resourcePath2 + " does not exist in current context."); return cl.getResourceAsStream(resourcePath2); }
From source file:de.tuttas.servlets.MailSender.java
private String getFile(String fileName) { StringBuilder result = new StringBuilder(""); //Get file from resources folder ClassLoader classLoader = getClass().getClassLoader(); File file = new File(classLoader.getResource(fileName).getFile()); try (Scanner scanner = new Scanner(file)) { while (scanner.hasNextLine()) { String line = scanner.nextLine(); result.append(line).append("\n"); }/*from w ww .j a va2s . co m*/ scanner.close(); } catch (IOException e) { e.printStackTrace(); } return result.toString(); }
From source file:com.nec.core.container.ContextAwareContainer.java
private ApplicationContext getApplicationContext() { ClassLoader classLoader = ContextAwareContainer.class.getClassLoader(); if (classLoader.getResource(ContextAwareContainer.APPLICATION_CONFIGURATION) == null) { throw new ConfigurationNotFoundException( "File " + ContextAwareContainer.APPLICATION_CONFIGURATION + " not found"); }/* w w w .j a v a 2s . co m*/ return new ClassPathXmlApplicationContext(new String[] { ContextAwareContainer.APPLICATION_CONFIGURATION }); }
From source file:com.cloudera.nav.sdk.client.SSLUtilsTest.java
@Before public void setUp() throws Exception { Map<String, Object> confMap = Maps.newHashMap(); confMap.put(ClientConfigFactory.APP_URL, "localhost"); confMap.put(ClientConfigFactory.NAV_URL, "localhost"); confMap.put(ClientConfigFactory.NAMESPACE, "test"); confMap.put(ClientConfigFactory.USERNAME, "user"); confMap.put(ClientConfigFactory.PASSWORD, "pass"); confMap.put(ClientConfigFactory.API_VERSION, 9); config = (new ClientConfigFactory()).fromConfigMap(confMap); KeyStore keyStore = KeyStore.getInstance("jks"); ClassLoader classLoader = getClass().getClassLoader(); String keyStoreLocation = classLoader.getResource("client.jks").getFile(); try (InputStream is = new FileInputStream(keyStoreLocation)) { keyStore.load(is, "clientP".toCharArray()); }/*from w w w . ja v a 2 s .c o m*/ certs = Maps.newHashMap(); Enumeration<String> aliasesEn = keyStore.aliases(); String alias; while (aliasesEn.hasMoreElements()) { alias = aliasesEn.nextElement(); certs.put(alias, keyStore.getCertificate(alias)); } }