List of usage examples for java.lang ClassLoader getResource
public URL getResource(String name)
From source file:com.dianping.resource.io.util.ResourceUtils.java
/** * Resolve the given resource location to a {@code java.io.File}, * i.e. to a file in the file system.//from ww w . ja v a 2s . c o m * <p>Does not check whether the file actually exists; simply returns * the File that the given location would correspond to. * @param resourceLocation the resource location to resolve: either a * "classpath:" pseudo URL, a "file:" URL, or a plain file path * @return a corresponding File object * @throws java.io.FileNotFoundException if the resource cannot be resolved to * a file in the file system */ public static File getFile(String resourceLocation) throws FileNotFoundException { Assert.notNull(resourceLocation, "Resource location must not be null"); if (resourceLocation.startsWith(CLASSPATH_URL_PREFIX)) { String path = resourceLocation.substring(CLASSPATH_URL_PREFIX.length()); String description = "class path resource [" + path + "]"; ClassLoader cl = ClassUtils.getDefaultClassLoader(); URL url = (cl != null ? cl.getResource(path) : ClassLoader.getSystemResource(path)); if (url == null) { throw new FileNotFoundException(description + " cannot be resolved to absolute file path " + "because it does not reside in the file system"); } return getFile(url, description); } try { // try URL return getFile(new URL(resourceLocation)); } catch (MalformedURLException ex) { // no URL -> treat as file path return new File(resourceLocation); } }
From source file:nl.knaw.dans.common.lang.ResourceLocator.java
/** * Get the URL for the given location, or <code>null</code> if no resource exists on given location. * /*from w w w. j a v a 2 s .co m*/ * @param location * a relative path on the class path, separated with "/" * @return URL of the resource or <code>null</code> */ public static URL getURL(final String location) { URL url = null; if (HOME_DIRECTORIES != null) { for (HomeDirectory home : HOME_DIRECTORIES) { File file = new File(home.getHomeDirectory(), location); if (file.exists()) { try { return file.toURI().toURL(); } catch (MalformedURLException e) { // we return null "if no resource exists on given location." } } } } if (url == null) // try to locate the resource on the classpath. { final ClassLoader classLoader = ResourceLocator.class.getClassLoader(); if (classLoader != null) { url = classLoader.getResource(location); } } return url; }
From source file:org.jboss.as.test.integration.web.security.WebSecurityCERTTestCase.java
public static void createTestConnector(final ModelControllerClient client) throws Exception { final List<ModelNode> updates = new ArrayList<ModelNode>(); ModelNode op = new ModelNode(); op.get(OP).set(ADD);//from w w w. j a v a 2 s . c o m op.get(OP_ADDR).add("socket-binding-group", "standard-sockets"); op.get(OP_ADDR).add("socket-binding", "https-test"); op.get("interface").set("default"); op.get("port").set(8380); updates.add(op); op = new ModelNode(); op.get(OP).set(ADD); op.get(OP_ADDR).add(SUBSYSTEM, "web"); op.get(OP_ADDR).add("connector", "testConnector"); op.get("socket-binding").set("https-test"); op.get("enabled").set(true); op.get("protocol").set("HTTP/1.1"); op.get("scheme").set("https"); op.get("secure").set(true); ModelNode ssl = new ModelNode(); ssl.setEmptyObject(); ssl.get("name").set("https-test"); ssl.get("key-alias").set("test"); ssl.get("password").set("changeit"); ClassLoader tccl = Thread.currentThread().getContextClassLoader(); URL keystore = tccl.getResource("security/server.keystore"); ssl.get("certificate-key-file").set(keystore.getPath()); ssl.get("protocol").set("TLS"); ssl.get("verify-client").set(true); op.get("ssl").set(ssl); updates.add(op); applyUpdates(updates, client); }
From source file:mzmatch.util.Tool.java
/** * //w w w .j a v a 2s .c o m * * @param pckgname * @return * @throws ClassNotFoundException */ public static Vector<Class<? extends Object>> getAllClasses(String pckgname) throws ClassNotFoundException { Vector<Class<? extends Object>> classes = new Vector<Class<? extends Object>>(); // load the current package ClassLoader cld = Thread.currentThread().getContextClassLoader(); if (cld == null) throw new ClassNotFoundException("Can't get class loader."); String path = pckgname.replace('.', '/'); URL resource = cld.getResource(path); if (resource == null) throw new ClassNotFoundException("No resource for " + path); // parse the directory File directory = new File(resource.getFile()); if (directory.isDirectory() && directory.exists()) { for (File f : directory.listFiles()) { if (f.getName().endsWith(".class")) classes.add(Class.forName(pckgname + '.' + f.getName().substring(0, f.getName().length() - 6))); } for (File f : directory.listFiles()) { if (f.isDirectory()) classes.addAll(getAllClasses(pckgname + "." + f.getName())); } } else throw new ClassNotFoundException(pckgname + " does not appear to be a valid package"); return classes; }
From source file:com.dianping.resource.io.util.ResourceUtils.java
/** * Resolve the given resource location to a {@code java.net.URL}. * <p>Does not check whether the URL actually exists; simply returns * the URL that the given location would correspond to. * @param resourceLocation the resource location to resolve: either a * "classpath:" pseudo URL, a "file:" URL, or a plain file path * @return a corresponding URL object//from w ww . j a v a 2 s .c o m * @throws java.io.FileNotFoundException if the resource cannot be resolved to a URL */ public static URL getURL(String resourceLocation) throws FileNotFoundException { Assert.notNull(resourceLocation, "Resource location must not be null"); if (resourceLocation.startsWith(CLASSPATH_URL_PREFIX)) { String path = resourceLocation.substring(CLASSPATH_URL_PREFIX.length()); ClassLoader cl = ClassUtils.getDefaultClassLoader(); URL url = (cl != null ? cl.getResource(path) : ClassLoader.getSystemResource(path)); if (url == null) { String description = "class path resource [" + path + "]"; throw new FileNotFoundException( description + " cannot be resolved to URL because it does not exist"); } return url; } try { // try URL return new URL(resourceLocation); } catch (MalformedURLException ex) { // no URL -> treat as file path try { return new File(resourceLocation).toURI().toURL(); } catch (MalformedURLException ex2) { throw new FileNotFoundException("Resource location [" + resourceLocation + "] is neither a URL not a well-formed file path"); } } }
From source file:org.jboss.as.test.integration.security.loginmodules.common.Utils.java
public static URL getResource(String name) { ClassLoader tccl = Thread.currentThread().getContextClassLoader(); return tccl.getResource(name); }
From source file:com.sunchenbin.store.feilong.core.lang.ClassLoaderUtil.java
/** * ???.//from w ww .ja va 2 s . c om * <p> * "",classes * </p> * e.q:<br> * * <blockquote> * <table border="1" cellspacing="0" cellpadding="4"> * <tr style="background-color:#ccccff"> * <th align="left"></th> * <th align="left">(maven)</th> * <th align="left">web,(??jar)</th> * </tr> * <tr valign="top"> * <td>{@code getResource("")}</td> * <td>file:/E:/Workspaces/feilong/feilong-platform/feilong-common/target/test-classes/</td> * <td>file:/E:/Workspaces/feilong/feilong-platform/feilong-spring-test-2.5/src/main/webapp/WEB-INF/classes/</td> * </tr> * <tr valign="top" style="background-color:#eeeeff"> * <td>{@code getResource("com")}</td> * <td>file:/E:/Workspaces/feilong/feilong-platform/feilong-common/target/test-classes/com</td> * <td>file:/E:/Workspaces/feilong/feilong-platform/feilong-spring-test-2.5/src/main/webapp/WEB-INF/classes/com/</td> * </tr> * </table> * </blockquote> * * @param classLoader * the class loader * @param resourceName * the resource name * @return the resource * @since 1.2.1 */ public static URL getResource(ClassLoader classLoader, String resourceName) { return classLoader.getResource(resourceName); }
From source file:com.github.fge.jackson.JsonLoader.java
/** * Read a {@link JsonNode} from a resource path. * * <p>This method first tries and loads the resource using {@link * Class#getResource(String)}; if not found, is tries and uses the context * classloader and if this is not found, this class's classloader.</p> * * <p>This method throws an {@link IOException} if the resource does not * exist.</p>/* ww w . ja v a 2 s .c o m*/ * * @param resource the path to the resource (<strong>must</strong> begin * with a {@code /}) * @return the JSON document at the resource * @throws IllegalArgumentException resource path does not begin with a * {@code /} * @throws IOException there was a problem loading the resource, or the JSON * document is invalid */ public static JsonNode fromResource(@Nonnull final String resource) throws IOException { Preconditions.checkNotNull(resource); Preconditions.checkArgument(resource.startsWith("/"), "resource path does not start with a '/'"); URL url; url = JsonLoader.class.getResource(resource); if (url == null) { final ClassLoader classLoader = Objects.firstNonNull(Thread.currentThread().getContextClassLoader(), JsonLoader.class.getClassLoader()); final String s = INITIAL_SLASH.matcher(resource).replaceFirst(""); url = classLoader.getResource(s); } if (url == null) throw new IOException("resource " + resource + " not found"); final Closer closer = Closer.create(); final JsonNode ret; final InputStream in; try { in = closer.register(url.openStream()); ret = READER.fromInputStream(in); } finally { closer.close(); } return ret; }
From source file:com.stratio.crossdata.core.utils.ParserUtils.java
/** * Get a replacement token for a given one. * * @param target The parser token./*from w w w . ja va2 s.c o m*/ * @return A string with the replacement token. */ private static String getReplacement(String target) { String targetToken = target.substring(2); String replacement = ""; BufferedReader bufferedReaderF = null; try { ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); FileInputStream tokensFile = new FileInputStream( classLoader.getResource("com/stratio/crossdata/parser/tokens.txt").getFile()); bufferedReaderF = new BufferedReader(new InputStreamReader(tokensFile, Charset.forName("UTF-8"))); String line = bufferedReaderF.readLine(); while (line != null) { if (line.startsWith(targetToken)) { replacement = line.substring(line.indexOf(':') + 1); replacement = replacement.replace("[#", "\""); replacement = replacement.replace("#]", "\""); replacement = replacement.replace(" ", ""); break; } line = bufferedReaderF.readLine(); } } catch (IOException ex) { LOG.error("Cannot read replacement file", ex); } finally { try { if (bufferedReaderF != null) { bufferedReaderF.close(); } } catch (IOException e) { LOG.error("Cannot close replacement file", e); } } return replacement; }
From source file:Resources.java
/** * Returns the URL of the resource on the classpath * * @param loader The classloader used to load the resource * @param resource The resource to find//from w ww .j a va 2s. c om * @return The resource * @throws IOException If the resource cannot be found or read */ public static URL getResourceURL(ClassLoader loader, String resource) throws IOException { URL url = null; if (loader != null) url = loader.getResource(resource); if (url == null) url = ClassLoader.getSystemResource(resource); if (url == null) throw new IOException("Could not find resource " + resource); return url; }