@Test public void testGetResourceViaClasspathWithoutPrefix() throws Exception { InputStream stream = utils.getResourceFromUrl("/brooklyn/config/sample.properties"); assertNotNull(stream); }
public InputStream getResourceFromUrl(String url) { try { if (Strings.isBlank(url)) throw new IllegalArgumentException("Cannot read from empty string"); String orig = url; String protocol = Urls.getProtocol(url); if (protocol!=null) { if ("classpath".equals(protocol)) { try { return getResourceViaClasspath(url); } catch (IOException e) { throw new IOException("Error accessing "+orig+": "+e, e); } } if ("sftp".equals(protocol)) { try { return getResourceViaSftp(url); } catch (IOException e) { throw new IOException("Error accessing "+orig+": "+e, e); } } if ("file".equals(protocol)) url = tidyFileUrl(url); if ("data".equals(protocol)) { return new DataUriSchemeParser(url).lax().parse().getDataAsInputStream(); } if ("http".equals(protocol) || "https".equals(protocol)) { return getResourceViaHttp(url); } return new URL(url).openStream(); } try { try { URL u = getLoader().getResource(url); if (u!=null) return u.openStream(); } catch (IllegalArgumentException e) { } if (url.startsWith("/")) { String urlNoSlash = url; while (urlNoSlash.startsWith("/")) urlNoSlash = urlNoSlash.substring(1); URL u = getLoader().getResource(urlNoSlash); if (u!=null) return u.openStream(); } File f; if (url.startsWith("~/")) { f = new File(Os.home(), url.substring(2)); } else if (url.startsWith("~\\")) { f = new File(Os.home(), url.substring(2)); } else { f = new File(url); } if (f.exists()) return new FileInputStream(f); } catch (IOException e) { throw new IOException("Error accessing "+orig+": "+e, e); } throw new IOException("'"+orig+"' not found on classpath or filesystem"); } catch (Exception e) { if (context!=null) { throw new RuntimeException("Error getting resource '"+url+"' for "+context+": "+e, e); } else { throw Exceptions.propagate(e); } } }
ResourceUtils { public InputStream getResourceFromUrl(String url) { try { if (Strings.isBlank(url)) throw new IllegalArgumentException("Cannot read from empty string"); String orig = url; String protocol = Urls.getProtocol(url); if (protocol!=null) { if ("classpath".equals(protocol)) { try { return getResourceViaClasspath(url); } catch (IOException e) { throw new IOException("Error accessing "+orig+": "+e, e); } } if ("sftp".equals(protocol)) { try { return getResourceViaSftp(url); } catch (IOException e) { throw new IOException("Error accessing "+orig+": "+e, e); } } if ("file".equals(protocol)) url = tidyFileUrl(url); if ("data".equals(protocol)) { return new DataUriSchemeParser(url).lax().parse().getDataAsInputStream(); } if ("http".equals(protocol) || "https".equals(protocol)) { return getResourceViaHttp(url); } return new URL(url).openStream(); } try { try { URL u = getLoader().getResource(url); if (u!=null) return u.openStream(); } catch (IllegalArgumentException e) { } if (url.startsWith("/")) { String urlNoSlash = url; while (urlNoSlash.startsWith("/")) urlNoSlash = urlNoSlash.substring(1); URL u = getLoader().getResource(urlNoSlash); if (u!=null) return u.openStream(); } File f; if (url.startsWith("~/")) { f = new File(Os.home(), url.substring(2)); } else if (url.startsWith("~\\")) { f = new File(Os.home(), url.substring(2)); } else { f = new File(url); } if (f.exists()) return new FileInputStream(f); } catch (IOException e) { throw new IOException("Error accessing "+orig+": "+e, e); } throw new IOException("'"+orig+"' not found on classpath or filesystem"); } catch (Exception e) { if (context!=null) { throw new RuntimeException("Error getting resource '"+url+"' for "+context+": "+e, e); } else { throw Exceptions.propagate(e); } } } }
ResourceUtils { public InputStream getResourceFromUrl(String url) { try { if (Strings.isBlank(url)) throw new IllegalArgumentException("Cannot read from empty string"); String orig = url; String protocol = Urls.getProtocol(url); if (protocol!=null) { if ("classpath".equals(protocol)) { try { return getResourceViaClasspath(url); } catch (IOException e) { throw new IOException("Error accessing "+orig+": "+e, e); } } if ("sftp".equals(protocol)) { try { return getResourceViaSftp(url); } catch (IOException e) { throw new IOException("Error accessing "+orig+": "+e, e); } } if ("file".equals(protocol)) url = tidyFileUrl(url); if ("data".equals(protocol)) { return new DataUriSchemeParser(url).lax().parse().getDataAsInputStream(); } if ("http".equals(protocol) || "https".equals(protocol)) { return getResourceViaHttp(url); } return new URL(url).openStream(); } try { try { URL u = getLoader().getResource(url); if (u!=null) return u.openStream(); } catch (IllegalArgumentException e) { } if (url.startsWith("/")) { String urlNoSlash = url; while (urlNoSlash.startsWith("/")) urlNoSlash = urlNoSlash.substring(1); URL u = getLoader().getResource(urlNoSlash); if (u!=null) return u.openStream(); } File f; if (url.startsWith("~/")) { f = new File(Os.home(), url.substring(2)); } else if (url.startsWith("~\\")) { f = new File(Os.home(), url.substring(2)); } else { f = new File(url); } if (f.exists()) return new FileInputStream(f); } catch (IOException e) { throw new IOException("Error accessing "+orig+": "+e, e); } throw new IOException("'"+orig+"' not found on classpath or filesystem"); } catch (Exception e) { if (context!=null) { throw new RuntimeException("Error getting resource '"+url+"' for "+context+": "+e, e); } else { throw Exceptions.propagate(e); } } } ResourceUtils(ClassLoader loader, Object contextObject, String contextMessage);  ResourceUtils(BrooklynClassLoadingContext loader, Object contextObject, String contextMessage);  ResourceUtils(Object contextObject, String contextMessage);  ResourceUtils(Object contextObject); }
ResourceUtils { public InputStream getResourceFromUrl(String url) { try { if (Strings.isBlank(url)) throw new IllegalArgumentException("Cannot read from empty string"); String orig = url; String protocol = Urls.getProtocol(url); if (protocol!=null) { if ("classpath".equals(protocol)) { try { return getResourceViaClasspath(url); } catch (IOException e) { throw new IOException("Error accessing "+orig+": "+e, e); } } if ("sftp".equals(protocol)) { try { return getResourceViaSftp(url); } catch (IOException e) { throw new IOException("Error accessing "+orig+": "+e, e); } } if ("file".equals(protocol)) url = tidyFileUrl(url); if ("data".equals(protocol)) { return new DataUriSchemeParser(url).lax().parse().getDataAsInputStream(); } if ("http".equals(protocol) || "https".equals(protocol)) { return getResourceViaHttp(url); } return new URL(url).openStream(); } try { try { URL u = getLoader().getResource(url); if (u!=null) return u.openStream(); } catch (IllegalArgumentException e) { } if (url.startsWith("/")) { String urlNoSlash = url; while (urlNoSlash.startsWith("/")) urlNoSlash = urlNoSlash.substring(1); URL u = getLoader().getResource(urlNoSlash); if (u!=null) return u.openStream(); } File f; if (url.startsWith("~/")) { f = new File(Os.home(), url.substring(2)); } else if (url.startsWith("~\\")) { f = new File(Os.home(), url.substring(2)); } else { f = new File(url); } if (f.exists()) return new FileInputStream(f); } catch (IOException e) { throw new IOException("Error accessing "+orig+": "+e, e); } throw new IOException("'"+orig+"' not found on classpath or filesystem"); } catch (Exception e) { if (context!=null) { throw new RuntimeException("Error getting resource '"+url+"' for "+context+": "+e, e); } else { throw Exceptions.propagate(e); } } } ResourceUtils(ClassLoader loader, Object contextObject, String contextMessage);  ResourceUtils(BrooklynClassLoadingContext loader, Object contextObject, String contextMessage);  ResourceUtils(Object contextObject, String contextMessage);  ResourceUtils(Object contextObject); static final ResourceUtils create(ClassLoader loader, Object contextObject, String contextMessage); static final ResourceUtils create(BrooklynClassLoadingContext loader, Object contextObject, String contextMessage); static final ResourceUtils create(Object contextObject, String contextMessage); static final ResourceUtils create(Object contextObject); static final ResourceUtils create(); static void addClassLoaderProvider(Function<Object,BrooklynClassLoadingContext> provider); Iterable<URL> getResources(String name); InputStream getResourceFromUrl(String url); static URL tidy(URL url); static String tidyFileUrl(String url); @Deprecated static String mergeFilePaths(String... items); @Deprecated static String tidyFilePath(String path); @Deprecated static String getProtocol(String url); String getResourceAsString(String url); String checkUrlExists(String url); String checkUrlExists(String url, String message); boolean doesUrlExist(String url); Optional<String> firstAvailableUrl(String ...urls); String getClassLoaderDir(); String getClassLoaderDir(String resourceInThatDir); static URL getContainerUrl(URL url, String resourceInThatDir); @Deprecated static String readFullyString(InputStream is); @Deprecated static byte[] readFullyBytes(InputStream is); @Deprecated static void copy(InputStream input, OutputStream output); @Deprecated static File mkdirs(File dir); @Deprecated static File writeToTempFile(InputStream is, String prefix, String suffix); @Deprecated static File writeToTempFile(InputStream is, File tempDir, String prefix, String suffix); @Deprecated static File writeToTempFile(Properties props, String prefix, String suffix); @Deprecated static File writeToTempFile(Properties props, File tempDir, String prefix, String suffix); @Deprecated static Thread addShutdownHook(final Runnable task); @Deprecated static boolean removeShutdownHook(Thread hook); @Deprecated static String mergePaths(String ...items); }
ResourceUtils { public InputStream getResourceFromUrl(String url) { try { if (Strings.isBlank(url)) throw new IllegalArgumentException("Cannot read from empty string"); String orig = url; String protocol = Urls.getProtocol(url); if (protocol!=null) { if ("classpath".equals(protocol)) { try { return getResourceViaClasspath(url); } catch (IOException e) { throw new IOException("Error accessing "+orig+": "+e, e); } } if ("sftp".equals(protocol)) { try { return getResourceViaSftp(url); } catch (IOException e) { throw new IOException("Error accessing "+orig+": "+e, e); } } if ("file".equals(protocol)) url = tidyFileUrl(url); if ("data".equals(protocol)) { return new DataUriSchemeParser(url).lax().parse().getDataAsInputStream(); } if ("http".equals(protocol) || "https".equals(protocol)) { return getResourceViaHttp(url); } return new URL(url).openStream(); } try { try { URL u = getLoader().getResource(url); if (u!=null) return u.openStream(); } catch (IllegalArgumentException e) { } if (url.startsWith("/")) { String urlNoSlash = url; while (urlNoSlash.startsWith("/")) urlNoSlash = urlNoSlash.substring(1); URL u = getLoader().getResource(urlNoSlash); if (u!=null) return u.openStream(); } File f; if (url.startsWith("~/")) { f = new File(Os.home(), url.substring(2)); } else if (url.startsWith("~\\")) { f = new File(Os.home(), url.substring(2)); } else { f = new File(url); } if (f.exists()) return new FileInputStream(f); } catch (IOException e) { throw new IOException("Error accessing "+orig+": "+e, e); } throw new IOException("'"+orig+"' not found on classpath or filesystem"); } catch (Exception e) { if (context!=null) { throw new RuntimeException("Error getting resource '"+url+"' for "+context+": "+e, e); } else { throw Exceptions.propagate(e); } } } ResourceUtils(ClassLoader loader, Object contextObject, String contextMessage);  ResourceUtils(BrooklynClassLoadingContext loader, Object contextObject, String contextMessage);  ResourceUtils(Object contextObject, String contextMessage);  ResourceUtils(Object contextObject); static final ResourceUtils create(ClassLoader loader, Object contextObject, String contextMessage); static final ResourceUtils create(BrooklynClassLoadingContext loader, Object contextObject, String contextMessage); static final ResourceUtils create(Object contextObject, String contextMessage); static final ResourceUtils create(Object contextObject); static final ResourceUtils create(); static void addClassLoaderProvider(Function<Object,BrooklynClassLoadingContext> provider); Iterable<URL> getResources(String name); InputStream getResourceFromUrl(String url); static URL tidy(URL url); static String tidyFileUrl(String url); @Deprecated static String mergeFilePaths(String... items); @Deprecated static String tidyFilePath(String path); @Deprecated static String getProtocol(String url); String getResourceAsString(String url); String checkUrlExists(String url); String checkUrlExists(String url, String message); boolean doesUrlExist(String url); Optional<String> firstAvailableUrl(String ...urls); String getClassLoaderDir(); String getClassLoaderDir(String resourceInThatDir); static URL getContainerUrl(URL url, String resourceInThatDir); @Deprecated static String readFullyString(InputStream is); @Deprecated static byte[] readFullyBytes(InputStream is); @Deprecated static void copy(InputStream input, OutputStream output); @Deprecated static File mkdirs(File dir); @Deprecated static File writeToTempFile(InputStream is, String prefix, String suffix); @Deprecated static File writeToTempFile(InputStream is, File tempDir, String prefix, String suffix); @Deprecated static File writeToTempFile(Properties props, String prefix, String suffix); @Deprecated static File writeToTempFile(Properties props, File tempDir, String prefix, String suffix); @Deprecated static Thread addShutdownHook(final Runnable task); @Deprecated static boolean removeShutdownHook(Thread hook); @Deprecated static String mergePaths(String ...items);  }