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.WebSecurityCERTTestCase.java

@Deployment
public static WebArchive deployment() {
    // FIXME hack to get things prepared before the deployment happens
    try {/*from   w  w  w  .j av a 2s. co m*/
        final ModelControllerClient client = ModelControllerClient.Factory
                .create(InetAddress.getByName("localhost"), 9999);
        // create the test connector
        createTestConnector(client);
        // create required security domains
        createSecurityDomains(client);
    } catch (Exception e) {
        // ignore
    }

    ClassLoader tccl = Thread.currentThread().getContextClassLoader();
    URL webxml = tccl.getResource("web-secure-client-cert.war/web.xml");
    WebArchive war = create("web-secure-client-cert.war", SecuredServlet.class, webxml);
    WebSecurityPasswordBasedBase.printWar(war);
    return war;
}

From source file:com.eincs.decanter.render.soy.SoyAcre.java

/**
 * /*from ww w .  ja  va 2 s .  co  m*/
 * @return
 */
public static SoyAcre forClasspath() {
    final Reflections reflections = new Reflections(new ConfigurationBuilder()
            .addUrls(ClasspathHelper.forJavaClassPath()).setScanners(new ResourcesScanner()));
    final Set<String> soyFiles = reflections.getResources(Pattern.compile(SOY_FILE_NAME_PATTERN));
    final ClassLoader clazzLoader = ClassLoader.getSystemClassLoader();

    return new SoyAcre() {
        @Override
        public void addToFileSet(SoyFileSet.Builder fileSetBuilder) {
            for (String soyFile : soyFiles) {
                fileSetBuilder.add(clazzLoader.getResource(soyFile));
            }
        }
    };
}

From source file:com.rodaxsoft.mailgun.MailgunManager.java

private static boolean hasProperties() {
    final ClassLoader cl = MailgunManager.class.getClassLoader();
    return cl.getResource(MAILGUN_PROPERTIES) != null;
}

From source file:com.confighub.core.auth.Auth.java

private static String readLocalFile(String fileName) throws ConfigException {
    try {/*from   w  ww  .  ja v a 2 s.  c  om*/
        ClassLoader classLoader = Auth.class.getClassLoader();
        File file = new File(classLoader.getResource("auth/" + fileName).getFile());
        InputStream inputStream = new FileInputStream(file);

        if (null != inputStream) {
            String s = IOUtils.toString(inputStream, "UTF-8");
            if (null != s) {
                return s.replaceAll("\n", "");
            }
        }

        throw new ConfigException("Cannot read file: " + file);
    } catch (IOException e) {
        e.printStackTrace();
        throw new ConfigException(Error.Code.INTERNAL_ERROR);
    }
}

From source file:com.eincs.decanter.render.soy.SoyAcre.java

/**
 * //from w w w.  jav a  2  s  .  c  om
 * @param packageName
 * @return
 */
public static SoyAcre forPakckage(String packageName) {
    final Reflections reflections = new Reflections(new ConfigurationBuilder()
            .addUrls(ClasspathHelper.forPackage(packageName, ClassLoader.getSystemClassLoader()))
            .setScanners(new ResourcesScanner()));
    final Set<String> soyFiles = reflections.getResources(Pattern.compile(SOY_FILE_NAME_PATTERN));
    final ClassLoader clazzLoader = ClassLoader.getSystemClassLoader();

    return new SoyAcre() {
        @Override
        public void addToFileSet(SoyFileSet.Builder fileSetBuilder) {
            for (String soyFile : soyFiles) {
                fileSetBuilder.add(clazzLoader.getResource(soyFile));
            }
        }
    };
}

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

/**
 * Base method to create a {@link WebArchive}
 *
 * @param name Name of the war file//  w  ww . j ava  2 s. c o m
 * @param servletClass a class that is the servlet
 * @param webxml {@link URL} to the web.xml. This can be null
 * @return  the web archive
 */
public static WebArchive create(String name, Class<?> servletClass, URL webxml) {
    WebArchive war = ShrinkWrap.create(WebArchive.class, name);
    war.addClass(servletClass);

    ClassLoader tccl = Thread.currentThread().getContextClassLoader();

    war.addAsResource(tccl.getResource("web-secure-client-cert.war/roles.properties"), "roles.properties");
    war.addAsWebInfResource(tccl.getResource("web-secure-client-cert.war/jboss-web.xml"), "jboss-web.xml");

    if (webxml != null) {
        war.setWebXML(webxml);
    }

    return war;
}

From source file:org.jboss.as.test.integration.web.customerrors.CustomErrorsUnitTestCase.java

@Deployment(name = "error-producer.war", testable = false)
public static WebArchive producerDeployment() {
    ClassLoader tccl = Thread.currentThread().getContextClassLoader();
    String resourcesLocation = "org/jboss/as/test/integration/web/customerrors/resources/";

    WebArchive war = ShrinkWrap.create(WebArchive.class, "error-producer.war");
    war.setWebXML(tccl.getResource(resourcesLocation + "error-producer-web.xml"));
    war.addClass(ErrorGeneratorServlet.class);
    war.addClass(ContextForwardServlet.class);

    System.out.println(war.toString(true));
    return war;// ww w  . ja v a  2 s .c o  m
}

From source file:org.jboss.as.test.integration.web.customerrors.CustomErrorsUnitTestCase.java

@Deployment(name = "custom-errors.war", testable = false)
public static WebArchive errorDeployment() {
    ClassLoader tccl = Thread.currentThread().getContextClassLoader();
    String resourcesLocation = "org/jboss/as/test/integration/web/customerrors/resources/";

    WebArchive war = ShrinkWrap.create(WebArchive.class, "custom-errors.war");
    war.setWebXML(tccl.getResource(resourcesLocation + "custom-errors-web.xml"));
    war.addAsWebResource(tccl.getResource(resourcesLocation + "403.jsp"), "403.jsp");
    war.addAsWebResource(tccl.getResource(resourcesLocation + "404.jsp"), "404.jsp");
    war.addAsWebResource(tccl.getResource(resourcesLocation + "500.jsp"), "500.jsp");
    war.addClass(ErrorGeneratorServlet.class);

    System.out.println(war.toString(true));
    return war;//from  ww w.  j  a  v  a 2s  . c  o  m
}

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

public static void createSecurityDomains(final ModelControllerClient client) throws Exception {
    final List<ModelNode> updates = new ArrayList<ModelNode>();
    ModelNode op = new ModelNode();
    op.get(OP).set(ADD);//from w ww.ja  va  2  s  .co  m
    op.get(OP_ADDR).add(SUBSYSTEM, "security");
    op.get(OP_ADDR).add(SECURITY_DOMAIN, "cert-test");
    ModelNode loginModule = op.get(AUTHENTICATION).add();
    loginModule.get(CODE).set("CertificateRoles");
    loginModule.get(FLAG).set("required");
    ModelNode moduleOptions = loginModule.get(MODULE_OPTIONS);
    moduleOptions.add("securityDomain", "cert");
    updates.add(op);

    op = new ModelNode();
    op.get(OP).set(ADD);
    op.get(OP_ADDR).add(SUBSYSTEM, "security");
    op.get(OP_ADDR).add(SECURITY_DOMAIN, "cert");
    ModelNode jsse = op.get(JSSE);
    jsse.get(TRUSTSTORE_PASSWORD).set("changeit");
    ClassLoader tccl = Thread.currentThread().getContextClassLoader();
    URL keystore = tccl.getResource("security/jsse.keystore");
    jsse.get(TRUSTSTORE_URL).set(keystore.getPath());
    updates.add(op);

    applyUpdates(updates, client);
}

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

public static HttpClient wrapClient(HttpClient base, String alias) {
    try {/*  w  w 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);
        SSLSocketFactory ssf = new SSLSocketFactory(ctx, 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;
    }
}