/** * Retrieves given resource as URL. * @see #getResourceUrl(String, ClassLoader) */ public static URL getResourceUrl(String resourceName) { return getResourceUrl(resourceName, null); }
/** * Retrieves given resource as URL. * @see #getResourceUrl(String, ClassLoader) */ public static URL getResourceUrl(final String resourceName) { return getResourceUrl(resourceName, null); }
/** * Opens a resource of the specified name for reading. * @see #getResourceUrl(String, ClassLoader) */ public static InputStream getResourceAsStream(String resourceName, ClassLoader callingClass) throws IOException { URL url = getResourceUrl(resourceName, callingClass); if (url != null) { return url.openStream(); } return null; }
/** * Opens a resource of the specified name for reading. * @see #getResourceUrl(String, ClassLoader) */ public static InputStream getResourceAsStream(final String resourceName, final ClassLoader callingClass) throws IOException { URL url = getResourceUrl(resourceName, callingClass); if (url != null) { return url.openStream(); } return null; }
/** * Retrieves resource as file. Resource is retrieved as {@link #getResourceUrl(String, ClassLoader) URL}, * than it is converted to URI so it can be used by File constructor. */ public static File getResourceFile(String resourceName, ClassLoader classLoader) { try { URL resourceUrl = getResourceUrl(resourceName, classLoader); if (resourceUrl == null) { return null; } return new File(resourceUrl.toURI()); } catch (URISyntaxException ignore) { return null; } }
/** * Opens a resource of the specified name for reading. Controls caching, * that is important when the same jar is reloaded using custom classloader. */ public static InputStream getResourceAsStream(String resourceName, ClassLoader callingClass, boolean useCache) throws IOException { URL url = getResourceUrl(resourceName, callingClass); if (url != null) { URLConnection urlConnection = url.openConnection(); urlConnection.setUseCaches(useCache); return urlConnection.getInputStream(); } return null; }
/** * Opens a resource of the specified name for reading. Controls caching, * that is important when the same jar is reloaded using custom classloader. */ public static InputStream getResourceAsStream(final String resourceName, final ClassLoader callingClass, final boolean useCache) throws IOException { URL url = getResourceUrl(resourceName, callingClass); if (url != null) { URLConnection urlConnection = url.openConnection(); urlConnection.setUseCaches(useCache); return urlConnection.getInputStream(); } return null; }
protected File readDataToFile(String fileName) throws URISyntaxException { String dataFolder = this.getClass().getPackage().getName() + ".data."; dataFolder = dataFolder.replace('.', '/'); final URL url = ClassLoaderUtil.getResourceUrl("/" + dataFolder + fileName); return new File(url.toURI()); }
@Override public void start() { initLogger(); final String resourceName = StringUtil.replaceChar(JoyPaths.class.getName(), '.', '/') + ".class"; URL url = ClassLoaderUtil.getResourceUrl(resourceName); if (url == null) { throw new JoyException("Failed to resolve app dir, missing: " + resourceName); } final String protocol = url.getProtocol(); if (!protocol.equals("file")) { try { url = new URL(url.getFile()); } catch (MalformedURLException ignore) { } } appDir = url.getFile(); final int ndx = appDir.indexOf("WEB-INF"); appDir = (ndx > 0) ? appDir.substring(0, ndx) : SystemUtil.info().getWorkingDir(); System.setProperty(APP_DIR, appDir); log.info("Application folder: " + appDir); }
@Test void testStream() throws IOException { InputStream is = ClassLoaderUtil.getClassAsStream(ClassLoaderUtilTest.class); assertNotNull(is); is.close(); is = ClassLoaderUtil.getClassAsStream(ClassLoaderUtilTest.class); assertNotNull(is); is.close(); URL url; final String resourceName = "jodd/util/Bits.class"; url = ClassLoaderUtil.getResourceUrl(resourceName); assertNotNull(url); is = ClassLoaderUtil.getResourceAsStream(resourceName); assertNotNull(is); is.close(); }
@Test void testWebJars() { URL url = ClassLoaderUtil.getResourceUrl("/META-INF/resources/webjars/jquery"); File containerFile = FileUtil.toContainerFile(url); final Value<String> jqueryName = Value.of(null); ClassScanner classScanner = new ClassScanner() { @Override protected void onEntry(ClassPathEntry entryData) { if (entryData.name().endsWith("jquery.js")) { jqueryName.set(entryData.name()); } } }; classScanner.includeResources(true); classScanner.scan(containerFile); classScanner.start(); assertNotNull(url); assertEquals("/META-INF/resources/webjars/jquery/2.2.4/jquery.js", jqueryName.get()); } }
/** * Retrieves given resource as URL. * @see #getResourceUrl(String, ClassLoader) */ public static URL getResourceUrl(final String resourceName) { return getResourceUrl(resourceName, null); }
/** * Opens a resource of the specified name for reading. * @see #getResourceUrl(String, ClassLoader) */ public static InputStream getResourceAsStream(final String resourceName, final ClassLoader callingClass) throws IOException { URL url = getResourceUrl(resourceName, callingClass); if (url != null) { return url.openStream(); } return null; }
/** * Opens a resource of the specified name for reading. Controls caching, * that is important when the same jar is reloaded using custom classloader. */ public static InputStream getResourceAsStream(final String resourceName, final ClassLoader callingClass, final boolean useCache) throws IOException { URL url = getResourceUrl(resourceName, callingClass); if (url != null) { URLConnection urlConnection = url.openConnection(); urlConnection.setUseCaches(useCache); return urlConnection.getInputStream(); } return null; }
@Override public void start() { initLogger(); final String resourceName = StringUtil.replaceChar(JoyPaths.class.getName(), '.', '/') + ".class"; URL url = ClassLoaderUtil.getResourceUrl(resourceName); if (url == null) { throw new JoyException("Failed to resolve app dir, missing: " + resourceName); } final String protocol = url.getProtocol(); if (!protocol.equals("file")) { try { url = new URL(url.getFile()); } catch (MalformedURLException ignore) { } } appDir = url.getFile(); final int ndx = appDir.indexOf("WEB-INF"); appDir = (ndx > 0) ? appDir.substring(0, ndx) : SystemUtil.info().getWorkingDir(); System.setProperty(APP_DIR, appDir); log.info("Application folder: " + appDir); }