List of usage examples for java.lang ClassLoader getResource
public URL getResource(String name)
From source file:io.apiman.manager.api.jdbc.JdbcMetricsAccessorTest.java
/** * Initialize the DB with the apiman gateway DDL. * @param connection// www.ja v a2 s . c om */ private static void initDB(Connection connection) throws Exception { ClassLoader cl = JdbcMetricsAccessorTest.class.getClassLoader(); URL resource = cl.getResource("ddls/apiman-gateway_h2.ddl"); try (InputStream is = resource.openStream()) { System.out.println("======================================="); System.out.println("Initializing database."); DdlParser ddlParser = new DdlParser(); List<String> statements = ddlParser.parse(is); for (String sql : statements) { System.out.println(sql); PreparedStatement statement = connection.prepareStatement(sql); statement.execute(); } System.out.println("======================================="); } System.out.println("--------------------------------------"); System.out.println("Adding test data to the database."); resource = cl.getResource("JdbcMetricsAccessorTest/bulk-data.ddl"); try (InputStream is = resource.openStream()) { DdlParser ddlParser = new DdlParser(); List<String> statements = ddlParser.parse(is); for (String sql : statements) { System.out.println(sql); PreparedStatement statement = connection.prepareStatement(sql); statement.execute(); } } System.out.println("--------------------------------------"); }
From source file:io.apiman.gateway.test.junit.servlet.ServletGatewayTestServer.java
/** * Initialize the DB with the apiman gateway DDL. * @param connection/*from w w w .j av a2 s .com*/ */ private static void initDB(Connection connection) throws Exception { ClassLoader cl = ServletGatewayTestServer.class.getClassLoader(); URL resource = cl.getResource("ddls/apiman-gateway_h2.ddl"); try (InputStream is = resource.openStream()) { DdlParser ddlParser = new DdlParser(); List<String> statements = ddlParser.parse(is); for (String sql : statements) { PreparedStatement statement = connection.prepareStatement(sql); statement.execute(); } } }
From source file:org.agiso.core.lang.util.ClassUtils.java
public static String locate(String name, ClassLoader cl) /*throws ClassNotFoundException*/ { final URL location; final String classLocation = name.replace('.', '/') + ".class"; if (cl == null) { location = ClassLoader.getSystemResource(classLocation); } else {/*from w w w . j a v a2 s . co m*/ location = cl.getResource(classLocation); } if (location != null) { Pattern p = Pattern.compile("^.*:(.*)!.*$"); Matcher m = p.matcher(location.toString()); if (m.find()) { return m.group(1); } else { if (logger.isLoggable(Level.WARNING)) { logger.warning( "Cannot parse location of '" + location + "'. " + "Probably not loaded from a Jar"); } // throw new ClassNotFoundException("Cannot parse location of '" // + location + "'. Probably not loaded from a Jar"); return null; } } else { if (logger.isLoggable(Level.WARNING)) { logger.warning("Cannot find class '" + name + " using the " + cl); } // throw new ClassNotFoundException("Cannot find class '" // + name + " using the " + cl); return null; } }
From source file:ffx.Main.java
/** * Print out help for the command line version of Force Field X. *///from w w w.jav a 2s . com private static void commandLineInterfaceHelp() { logger.info(" usage: ffxc [-D<property=value>] <command> [-options] <PDB|XYZ>"); logger.info("\n where commands include:\n"); ClassLoader classLoader = ClassLoader.getSystemClassLoader(); classLoader.getResource("List all scripts"); logger.info("\n For help on a spcific command: ffxc command -h\n"); System.exit(0); }
From source file:org.apache.axis2.util.Loader.java
/** * Gets the resource with the specified class loader. * * @param loader//from w ww. j av a2 s .co m * @param resource * @return Returns URL. * @throws ClassNotFoundException */ static public URL getResource(ClassLoader loader, String resource) throws ClassNotFoundException { URL url = null; try { if (loader != null) { log.debug("Trying to find [" + resource + "] using " + loader + " class loader."); url = loader.getResource(resource); if (url != null) { return url; } } } catch (Throwable t) { log.warn("Caught Exception while in Loader.getResource. This may be innocuous.", t); } return getResource(resource); }
From source file:io.apiman.gateway.test.junit.servlet.ServletGatewayTestServer.java
/** * Loads the config file./* ww w .ja v a 2 s . c o m*/ * @param configFile */ private static Properties loadConfigFile(String configFile) { // Try loading as a URL first. try { URL url = new URL(configFile); try (InputStream is = url.openStream()) { Properties props = new Properties(); props.load(is); return props; } } catch (IOException e) { // Move on to the next type. System.out.println("Tried to load config file as a URL but failed: " + configFile); } // Now try loading as a resource. ClassLoader cl = ServletGatewayTestServer.class.getClassLoader(); URL resource = cl.getResource(configFile); if (resource == null) { resource = cl.getResource("test-configs/" + configFile); } try { try (InputStream is = resource.openStream()) { Properties props = new Properties(); props.load(is); return props; } } catch (Exception e) { // Move on to the next type. System.out.println("Tried to load config file as a resource but failed: " + configFile); } throw new RuntimeException("Failed to load referenced config: " + configFile); }
From source file:com.appleframework.jmx.connector.framework.ConnectorRegistry.java
@SuppressWarnings("deprecation") private static ConnectorRegistry create(ApplicationConfig config) throws Exception { Map<String, String> paramValues = config.getParamValues(); String appId = config.getApplicationId(); String connectorId = (String) paramValues.get("connectorId"); File file1 = new File(CoreUtils.getConnectorDir() + File.separatorChar + connectorId); URL[] urls = new URL[] { file1.toURL() }; ClassLoader cl = ClassLoaderRepository.getClassLoader(urls, true); //URLClassLoader cl = new URLClassLoader(urls, // ConnectorMBeanRegistry.class.getClassLoader()); Registry.MODELER_MANIFEST = MBEANS_DESCRIPTOR; URL res = cl.getResource(MBEANS_DESCRIPTOR); logger.info("Application ID : " + appId); logger.info("Connector Archive: " + file1.getAbsoluteFile()); logger.info("MBean Descriptor : " + res.toString()); //Thread.currentThread().setContextClassLoader(cl); //Registry.setUseContextClassLoader(true); ConnectorRegistry registry = new ConnectorRegistry(); registry.loadMetadata(cl);//from ww w.j a v a 2s . co m String[] mbeans = registry.findManagedBeans(); MBeanServer server = MBeanServerFactory.newMBeanServer(DOMAIN_CONNECTOR); for (int i = 0; i < mbeans.length; i++) { ManagedBean managed = registry.findManagedBean(mbeans[i]); String clsName = managed.getType(); String domain = managed.getDomain(); if (domain == null) { domain = DOMAIN_CONNECTOR; } Class<?> cls = Class.forName(clsName, true, cl); Object objMBean = null; // Use the factory method when it is defined. Method[] methods = cls.getMethods(); for (Method method : methods) { if (method.getName().equals(FACTORY_METHOD) && Modifier.isStatic(method.getModifiers())) { objMBean = method.invoke(null); logger.info("Create MBean using factory method."); break; } } if (objMBean == null) { objMBean = cls.newInstance(); } // Call the initialize method if the MBean extends ConnectorSupport class if (objMBean instanceof ConnectorSupport) { Method method = cls.getMethod("initialize", new Class[] { Map.class }); Map<String, String> props = config.getParamValues(); method.invoke(objMBean, new Object[] { props }); } ModelMBean mm = managed.createMBean(objMBean); String beanObjName = domain + ":name=" + mbeans[i]; server.registerMBean(mm, new ObjectName(beanObjName)); } registry.setMBeanServer(server); entries.put(appId, registry); return registry; }
From source file:com.meyling.telnet.startup.Loader.java
/** This method will search for <code>resource</code> in different places. The search order is as follows: //w w w. j a va 2 s. com <ol> <p><li>Search for <code>resource</code> using the thread context class loader under Java2. If that fails, search for <code>resource</code> using the class loader that loaded this class (<code>Loader</code>). Under JDK 1.1, only the the class loader that loaded this class (<code>Loader</code>) is used. <p><li>Try one last time with <code>ClassLoader.getSystemResource(resource)</code>, that is is using the system class loader in JDK 1.2 and virtual machine's built-in class loader in JDK 1.1. </ol> */ static public URL getResource(String resource) { ClassLoader classLoader = null; URL url = null; try { if (!java1) { classLoader = getTCL(); if (classLoader != null) { trace.debug("Trying to find [" + resource + "] using context classloader " + classLoader + "."); url = classLoader.getResource(resource); if (url != null) { return url; } } } // We could not find resource. Ler us now try with the // classloader that loaded this class. classLoader = Loader.class.getClassLoader(); if (classLoader != null) { trace.debug("Trying to find [" + resource + "] using " + classLoader + " class loader."); url = classLoader.getResource(resource); if (url != null) { return url; } } } catch (Throwable t) { trace.warn(TSTR, t); } // Last ditch attempt: get the resource from the class path. It // may be the case that clazz was loaded by the Extentsion class // loader which the parent of the system class loader. Hence the // code below. trace.debug("Trying to find [" + resource + "] using ClassLoader.getSystemResource()."); return ClassLoader.getSystemResource(resource); }
From source file:Main.java
private static URL getRootUrlForClass(Class<?> cls) { ClassLoader classLoader = cls.getClassLoader(); String resource = cls.getName().replace('.', '/') + ".class"; if (classLoader == null) { // A null class loader means the bootstrap class loader. In this case we use the // system class loader. This is safe since we can assume that the system class // loader uses parent first as delegation policy. classLoader = ClassLoader.getSystemClassLoader(); }//from w w w.ja va 2 s.c o m URL url = classLoader.getResource(resource); if (url == null) { return null; } String file = url.getFile(); if (file.endsWith(resource)) { try { return new URL(url.getProtocol(), url.getHost(), url.getPort(), file.substring(0, file.length() - resource.length())); } catch (MalformedURLException ex) { return null; } } else { return null; } }
From source file:org.callimachusproject.rdfa.test.RDFaGenerationTest.java
public static TestSuite suite() throws URISyntaxException { ClassLoader cl = Thread.currentThread().getContextClassLoader(); URL path = cl.getResource(test_dir); File testDir = new File(path.toURI()); if (testDir.exists() && testDir.isDirectory()) { TestSuite suite = listCases(testDir); suite.setName(RDFaGenerationTest.class.getName()); return suite; }/*from w w w .ja v a 2 s .c o m*/ return new TestSuite(RDFaGenerationTest.class.getName()); }