/** * @param basePath The path to load resources * @return The resouce loader */ public ResourceLoader forBase(String basePath) { return new DefaultClassPathResourceLoader(classLoader, basePath); }
/** * Obtains a resource as a stream. * * @param path The path * @return An optional resource */ public Optional<InputStream> getResourceAsStream(String path) { if (!isDirectory(path)) { return Optional.ofNullable(classLoader.getResourceAsStream(prefixPath(path))); } return Optional.empty(); }
/** * Use when resources should have a standard base path. * * @param classLoader The class loader for loading resources * @param basePath The path to look for resources under */ public DefaultClassPathResourceLoader(ClassLoader classLoader, String basePath) { this.classLoader = classLoader; this.basePath = normalize(basePath); }
/** * Obtains a stream of resource URLs. * * @param path The path * @return A resource stream */ public Stream<URL> getResources(String path) { Enumeration<URL> all; try { all = classLoader.getResources(prefixPath(path)); } catch (IOException e) { return Stream.empty(); } Stream.Builder<URL> builder = Stream.builder(); while (all.hasMoreElements()) { URL url = all.nextElement(); builder.accept(url); } return builder.build(); }
@SuppressWarnings("ConstantConditions") private boolean isDirectory(String path) { return isDirectoryCache.computeIfAbsent(path, s -> { URL url = classLoader.getResource(prefixPath(path)); if (url != null) { try {
/** * Return the default {@link ClassPathResourceLoader} for the given class loader. * * @param classLoader The classloader * @return The default loader */ static ClassPathResourceLoader defaultLoader(@Nullable ClassLoader classLoader) { if (classLoader == null) { classLoader = Thread.currentThread().getContextClassLoader(); } if (classLoader == null) { classLoader = ClassPathResourceLoader.class.getClassLoader(); } if (classLoader == null) { classLoader = ClassLoader.getSystemClassLoader(); } return new DefaultClassPathResourceLoader(classLoader); } }
/** * Obtains a resource URL. * * @param path The path * @return An optional resource */ public Optional<URL> getResource(String path) { boolean isDirectory = isDirectory(path); if (!isDirectory) { URL url = classLoader.getResource(prefixPath(path)); return Optional.ofNullable(url); } return Optional.empty(); }
/** * @return The class path resource loader */ @Singleton @Bean @BootstrapContextCompatible protected @Nonnull ClassPathResourceLoader getClassPathResourceLoader() { return new DefaultClassPathResourceLoader(classLoader); }
/** * @return The class path resource loader */ @Singleton @Bean @BootstrapContextCompatible ClassPathResourceLoader getClassPathResourceLoader() { return new DefaultClassPathResourceLoader(ResourceLoaderFactory.class.getClassLoader()); }