Example usage for java.lang ClassLoader getResource

List of usage examples for java.lang ClassLoader getResource

Introduction

In this page you can find the example usage for java.lang ClassLoader getResource.

Prototype

public URL getResource(String name) 

Source Link

Document

Finds the resource with the given name.

Usage

From source file:org.jboss.as.test.integration.web.security.cert.WebSecurityCERTTestCase.java

public static HttpClient wrapClient(HttpClient base, String alias) {
    try {//ww w . j a v  a 2 s  . c o  m
        SSLContext ctx = SSLContext.getInstance("TLS");
        JBossJSSESecurityDomain jsseSecurityDomain = new JBossJSSESecurityDomain("client-cert");
        jsseSecurityDomain.setKeyStorePassword("changeit");
        ClassLoader tccl = Thread.currentThread().getContextClassLoader();
        URL keystore = tccl.getResource("security/client.keystore");
        jsseSecurityDomain.setKeyStoreURL(keystore.getPath());
        jsseSecurityDomain.setClientAlias(alias);
        jsseSecurityDomain.reloadKeyAndTrustStore();
        KeyManager[] keyManagers = jsseSecurityDomain.getKeyManagers();
        TrustManager[] trustManagers = jsseSecurityDomain.getTrustManagers();
        ctx.init(keyManagers, trustManagers, null);
        X509HostnameVerifier verifier = new X509HostnameVerifier() {

            @Override
            public void verify(String s, SSLSocket sslSocket) throws IOException {
            }

            @Override
            public void verify(String s, X509Certificate x509Certificate) throws SSLException {
            }

            @Override
            public void verify(String s, String[] strings, String[] strings1) throws SSLException {
            }

            @Override
            public boolean verify(String string, SSLSession ssls) {
                return true;
            }
        };
        SSLSocketFactory ssf = new SSLSocketFactory(ctx, verifier);//SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        ClientConnectionManager ccm = base.getConnectionManager();
        SchemeRegistry sr = ccm.getSchemeRegistry();
        sr.register(new Scheme("https", 8380, ssf));
        return new DefaultHttpClient(ccm, base.getParams());
    } catch (Exception ex) {
        ex.printStackTrace();
        return null;
    }
}

From source file:com.rapid7.diskstorage.dynamodb.TestGraphUtil.java

public static Configuration loadProperties() {
    ClassLoader classLoader = Client.class.getClassLoader();
    URL resource = classLoader.getResource("META-INF/dynamodb_store_manager_test.properties");
    PropertiesConfiguration storageConfig;
    try {/*  w  ww. ja  va2s . co  m*/
        storageConfig = new PropertiesConfiguration(resource.getFile());
    } catch (ConfigurationException e) {
        throw new RuntimeException(e);
    }
    return storageConfig;
}

From source file:com.cloud.utils.PropertiesUtil.java

/**
 * Searches the class path and local paths to find the config file.
 * @param path path to find.  if it starts with / then it's absolute path.
 * @return File or null if not found at all.
 *///from w  ww  .j av  a2  s  .c  o m

public static File findConfigFile(String path) {
    ClassLoader cl = PropertiesUtil.class.getClassLoader();
    URL url = cl.getResource(path);
    if (url != null && "file".equals(url.getProtocol())) {
        return new File(url.getFile());
    }

    url = ClassLoader.getSystemResource(path);
    if (url != null && "file".equals(url.getProtocol())) {
        return new File(url.getFile());
    }

    File file = new File(path);
    if (file.exists()) {
        return file;
    }

    String newPath = "conf" + (path.startsWith(File.separator) ? "" : "/") + path;
    url = ClassLoader.getSystemResource(newPath);
    if (url != null && "file".equals(url.getProtocol())) {
        return new File(url.getFile());
    }

    url = cl.getResource(newPath);
    if (url != null && "file".equals(url.getProtocol())) {
        return new File(url.getFile());
    }

    newPath = "conf" + (path.startsWith(File.separator) ? "" : File.separator) + path;
    file = new File(newPath);
    if (file.exists()) {
        return file;
    }

    newPath = System.getProperty("catalina.home");
    if (newPath == null) {
        newPath = System.getenv("CATALINA_HOME");
    }

    if (newPath == null) {
        newPath = System.getenv("CATALINA_BASE");
    }

    if (newPath == null) {
        return null;
    }

    file = new File(newPath + File.separator + "conf" + File.separator + path);
    if (file.exists()) {
        return file;
    }

    return null;
}

From source file:io.apiman.test.common.util.TestUtil.java

/**
 * Loads a rest test from a classpath resource.
 * @param resourcePath//from  w  ww  .  j  a v a 2  s  .c om
 * @param cl
 */
public static final RestTest loadRestTest(String resourcePath, ClassLoader cl) {
    InputStream is = null;
    try {
        URL url = cl.getResource(resourcePath);
        if (url == null)
            throw new RuntimeException("Rest Test not found: " + resourcePath); //$NON-NLS-1$
        is = url.openStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        return parseRestTest(reader);
    } catch (Throwable e) {
        throw new RuntimeException(e);
    } finally {
        IOUtils.closeQuietly(is);
    }
}

From source file:Loader.java

public static URL getResource(Class loadClass, String name, boolean checkParents)
        throws ClassNotFoundException {
    URL url = null;//from ww  w  .  j a v a  2  s  .  com
    ClassLoader loader = Thread.currentThread().getContextClassLoader();
    while (url == null && loader != null) {
        url = loader.getResource(name);
        loader = (url == null && checkParents) ? loader.getParent() : null;
    }

    loader = loadClass == null ? null : loadClass.getClassLoader();
    while (url == null && loader != null) {
        url = loader.getResource(name);
        loader = (url == null && checkParents) ? loader.getParent() : null;
    }

    if (url == null) {
        url = ClassLoader.getSystemResource(name);
    }

    return url;
}

From source file:org.apache.openejb.resource.jdbc.BasicDataSourceUtil.java

/**
 * Create a {@link PasswordCipher} instance from the
 *  passwordCipher class name./* w w  w  .  jav  a2s.  c  o  m*/
 * 
 * @param passwordCipherClass the password cipher to look for
 * @return the password cipher from the passwordCipher class name
 *         optionally set.
 * @throws SQLException
 *             if the driver can not be found.
 */
public static PasswordCipher getPasswordCipher(String passwordCipherClass) throws SQLException {
    // Load the password cipher class
    Class<? extends PasswordCipher> pwdCipher = null;

    // try looking for implementation in /META-INF/org.apache.openejb.resource.jdbc.PasswordCipher
    ResourceFinder finder = new ResourceFinder("META-INF/");
    Map<String, Class<? extends PasswordCipher>> impls;
    try {
        impls = finder.mapAllImplementations(PasswordCipher.class);

    } catch (Throwable t) {
        String message = "Password cipher '" + passwordCipherClass
                + "' not found in META-INF/org.apache.openejb.resource.jdbc.PasswordCipher.";
        throw new SQLNestedException(message, t);
    }
    pwdCipher = impls.get(passwordCipherClass);

    //
    final ClassLoader tccl = Thread.currentThread().getContextClassLoader();
    final URL url = tccl
            .getResource("META-INF/org.apache.openejb.resource.jdbc.PasswordCipher/" + passwordCipherClass);
    if (url != null) {
        try {
            final String clazz = new BufferedReader(new InputStreamReader(url.openStream())).readLine().trim();
            pwdCipher = tccl.loadClass(clazz).asSubclass(PasswordCipher.class);
        } catch (Exception e) {
            // ignored
        }
    }

    // if not found in META-INF/org.apache.openejb.resource.jdbc.PasswordCipher
    // we can try to load the class.
    if (null == pwdCipher) {
        try {
            try {
                pwdCipher = Class.forName(passwordCipherClass).asSubclass(PasswordCipher.class);

            } catch (ClassNotFoundException cnfe) {
                pwdCipher = tccl.loadClass(passwordCipherClass).asSubclass(PasswordCipher.class);
            }
        } catch (Throwable t) {
            String message = "Cannot load password cipher class '" + passwordCipherClass + "'";
            throw new SQLNestedException(message, t);
        }
    }

    // Create an instance
    PasswordCipher cipher = null;
    try {
        cipher = pwdCipher.newInstance();

    } catch (Throwable t) {
        String message = "Cannot create password cipher instance";
        throw new SQLNestedException(message, t);
    }

    return cipher;
}

From source file:FileLocator.java

/**
 * Search for a file using the properties: user.dir, user.home, java.home
 * Returns URL or null.//from ww  w  . j  a v a2 s. c  o  m
 */
private static URL locateByResource(String findFile) {
    ClassLoader loader = Thread.currentThread().getContextClassLoader();
    URL url = loader.getResource(findFile);
    if (url == null) {
        url = FileLocator.class.getResource("/" + findFile);
    }
    // System.err.println("Search succeeded via getResource()");
    return url;
}

From source file:fr.xebia.demo.wicket.blog.service.AbstractServiceTest.java

@BeforeClass
public static void setUpClass() {
    randomizer = new Random();
    long startTime = System.currentTimeMillis();
    logger.info("Initializing Services");
    ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
    URL configuration = contextClassLoader.getResource("applicationContext-service.xml");
    factory = new XmlBeanFactory(new UrlResource(configuration));
    long endTime = System.currentTimeMillis();
    logger.info("Initialisation des services en " + ((endTime - startTime) / 1000.0) + " s");
}

From source file:com.autentia.tnt.xml.UtilitiesXML.java

/**
 * Esta funcion retorna en un List todos los ficheros de la carpeta report
 * @return//from   w w w.  ja  v a 2s.  co m
 */
public static List filesFromFolder(String path) {
    File[] filesList = null;
    List list = new ArrayList();
    ClassLoader loader = Thread.currentThread().getContextClassLoader();
    File f = null;
    try {
        f = new File(loader.getResource(path).toURI());
    } catch (URISyntaxException e) {
        log.error("Error en filesFromFolder", e);
    }
    if (f != null && f.isDirectory()) {
        filesList = f.listFiles();
        for (File file : filesList) {
            int i = file.getAbsolutePath().lastIndexOf(".");
            String format = file.getAbsolutePath().substring(i + 1);
            if (file.isFile() && (format.equals("jrxml"))) {
                list.add(path + File.separator + file.getName());
            }
        }
    }
    return list;
}

From source file:com.netsteadfast.greenstep.util.TemplateUtils.java

/**
 * ???? META-INF/  template /*from ww w  .j a  v  a 2s.c  om*/
 * 
 * @param classLoader
 * @param metaInfFile
 * @return
 */
public static String getResourceSrc(ClassLoader classLoader, String metaInfFile) {
    String out = "";
    try {
        out = IOUtils.toString(classLoader.getResource(metaInfFile).openStream(), Constants.BASE_ENCODING);
    } catch (IOException e) {
        e.printStackTrace();
    }
    return out;
}