Example usage for org.springframework.core.io.support PathMatchingResourcePatternResolver getResources

List of usage examples for org.springframework.core.io.support PathMatchingResourcePatternResolver getResources

Introduction

In this page you can find the example usage for org.springframework.core.io.support PathMatchingResourcePatternResolver getResources.

Prototype

@Override
    public Resource[] getResources(String locationPattern) throws IOException 

Source Link

Usage

From source file:eu.esdihumboldt.hale.common.align.model.impl.AbstractCellExplanation.java

/**
 * Determine the locales a resource is available for.
 * //from   ww  w .j a  v a2  s .  c o  m
 * @param clazz the clazz the resource resides next to
 * @param baseName the base name of the resource
 * @param suffix the suffix of the resource file, e.g.
 *            <code>properties</code>
 * @param defaultLocale the default locale to be assumed for an unqualified
 *            resource
 * @return the set of locales the resource is available for
 * @throws IOException if an error occurs trying to determine the resource
 *             files
 */
public static Set<Locale> findLocales(final Class<?> clazz, final String baseName, final String suffix,
        Locale defaultLocale) throws IOException {
    PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(
            clazz.getClassLoader());
    String pkg = clazz.getPackage().getName().replaceAll("\\.", "/");
    String pattern = pkg + "/" + baseName + "*." + suffix;
    return Arrays.stream(resolver.getResources(pattern)).map(resource -> {
        String fileName = resource.getFilename();

        if (fileName != null && fileName.startsWith(baseName)) {
            fileName = fileName.substring(baseName.length());
            if (fileName.endsWith("." + suffix)) {
                if (fileName.length() == suffix.length() + 1) {
                    // default locale file
                    return defaultLocale;
                } else {
                    String localeIdent = fileName.substring(0, fileName.length() - suffix.length() - 1);

                    String language = "";
                    String country = "";
                    String variant = "";

                    String[] parts = localeIdent.split("_");
                    int index = 0;
                    if (parts.length > index && parts[index].isEmpty()) {
                        index++;
                    }

                    if (parts.length > index) {
                        language = parts[index++];
                    }

                    if (parts.length > index) {
                        country = parts[index++];
                    }

                    if (parts.length > index) {
                        variant = parts[index++];
                    }

                    return new Locale(language, country, variant);
                }
            } else {
                log.error("Invalid resource encountered");
                return null;
            }
        } else {
            log.error("Invalid resource encountered");
            return null;
        }
    }).filter(locale -> locale != null).collect(Collectors.toSet());
}

From source file:org.apache.uima.ruta.descriptor.RutaDescriptorBuilder.java

public static URL checkImportExistence(String candidate, String extension, ClassLoader classloader)
        throws IOException {
    String p = candidate.replaceAll("[.]", "/");
    p += extension;/* www. j a  v a2s . c o  m*/
    PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(classloader);
    String prefix = "classpath*:";
    String pattern = prefix + p;
    Resource[] resources = resolver.getResources(pattern);
    if (resources == null || resources.length == 0) {
        return null;
    } else {
        Resource resource = resources[0];
        URL url = resource.getURL();
        return url;
    }
}

From source file:ch.rasc.edsutil.optimizer.WebResourceProcessor.java

private static List<Resource> enumerateResourcesFromClasspath(final String classpath, final String path,
        final String suffix) throws IOException {
    if (path.endsWith("/")) {
        PathMatchingResourcePatternResolver resourceResolver = new PathMatchingResourcePatternResolver();
        String location = classpath + path + "**/*" + suffix;

        Resource[] resources = resourceResolver.getResources(location);
        return Arrays.asList(resources);
    }/*from   w w  w.j av a 2  s.co  m*/

    if (path.endsWith(suffix)) {
        return Collections.singletonList(new ClassPathResource(classpath + path));
    }

    return Collections.emptyList();
}

From source file:de.tudarmstadt.ukp.dkpro.core.api.resources.ResourceUtils.java

/**
 * Make a given classpath location available as a folder. A temporary folder is created and
 * deleted upon a regular shutdown of the JVM. This method must not be used for creating
 * executable binaries. For this purpose, getUrlAsExecutable should be used.
 *
 * @param aClasspathBase/*from   ww w.ja  va2s  .c o  m*/
 *            a classpath location as used by
 *            {@link PathMatchingResourcePatternResolver#getResources(String)}
 * @param aCache
 *            use the cache or not.
 * @see PathMatchingResourcePatternResolver
 */
public static File getClasspathAsFolder(String aClasspathBase, boolean aCache) throws IOException {
    synchronized (classpathFolderCache) {
        File folder = classpathFolderCache.get(aClasspathBase);

        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();

        if (!aCache || (folder == null) || !folder.exists()) {
            folder = File.createTempFile("dkpro-package", "");
            folder.delete();
            FileUtils.forceMkdir(folder);

            Resource[] roots = resolver.getResources(aClasspathBase);
            for (Resource root : roots) {
                String base = root.getURL().toString();
                Resource[] resources = resolver.getResources(base + "/**/*");
                for (Resource resource : resources) {
                    if (!resource.isReadable()) {
                        // This is true for folders/packages
                        continue;
                    }

                    // Relativize
                    String res = resource.getURL().toString();
                    if (!res.startsWith(base)) {
                        throw new IOException("Resource location does not start with base location");
                    }
                    String relative = resource.getURL().toString().substring(base.length());

                    // Make sure the target folder exists
                    File target = new File(folder, relative).getAbsoluteFile();
                    if (target.getParentFile() != null) {
                        FileUtils.forceMkdir(target.getParentFile());
                    }

                    // Copy data
                    InputStream is = null;
                    OutputStream os = null;
                    try {
                        is = resource.getInputStream();
                        os = new FileOutputStream(target);
                        IOUtils.copyLarge(is, os);
                    } finally {
                        IOUtils.closeQuietly(is);
                        IOUtils.closeQuietly(os);
                    }

                    // WORKAROUND: folders get written as files if inside jars
                    // delete files of size zero
                    if (target.length() == 0) {
                        FileUtils.deleteQuietly(target);
                    }
                }
            }

            if (aCache) {
                classpathFolderCache.put(aClasspathBase, folder);
            }
        }

        return folder;
    }
}

From source file:com.xsb.conf.Application.java

@Bean
public SqlSessionFactory sqlSessionFactory() throws Exception {
    SqlSessionFactoryBean ssfb = new SqlSessionFactoryBean();

    ssfb.setDataSource(database());/*from  w  w w .  j av a2s  .  c  o m*/

    PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();

    ssfb.setMapperLocations(resolver.getResources("classpath:com/xsb/dao/mapper/*.xml"));

    return ssfb.getObject();
}

From source file:com.jaxio.celerio.template.pack.ClasspathResourceUncryptedPackLoader.java

public List<TemplatePack> getPacks() {
    List<TemplatePack> packs = newArrayList();
    try {/*w  w  w  .  ja  v a 2 s .  c  o  m*/
        PathMatchingResourcePatternResolver o = new PathMatchingResourcePatternResolver();
        Resource infos[] = o.getResources(CLASSPATH_CELERIO_PACK);

        for (Resource info : infos) {
            CelerioPack celerioPack = celerioPackConfigLoader.load(info.getInputStream());
            TemplatePackInfo templatePackInfo = new TemplatePackInfo(celerioPack);
            Resource templatesAsResources[] = o
                    .getResources("classpath*:/celerio/" + templatePackInfo.getName() + "/**/*");
            packs.add(new ClasspathResourceUncryptedPack(templatePackInfo, templatesAsResources));
        }

        return packs;
    } catch (IOException e) {
        throw new RuntimeException("Could not load the template packs", e);
    }
}

From source file:com.logsniffer.util.grok.GrokAppConfig.java

@Bean
public GroksRegistry groksRegistry() throws IOException, GrokException {
    GroksRegistry registry = new GroksRegistry();
    PathMatchingResourcePatternResolver pathMatcher = new PathMatchingResourcePatternResolver();
    Resource[] classPathPatterns = pathMatcher.getResources("classpath*:/grok-patterns/*");
    Arrays.sort(classPathPatterns, new Comparator<Resource>() {
        @Override//from   ww  w.jav  a  2s  .  co  m
        public int compare(final Resource o1, final Resource o2) {
            return o1.getFilename().compareTo(o2.getFilename());
        }
    });
    LinkedHashMap<String, String[]> grokBlocks = new LinkedHashMap<String, String[]>();
    for (Resource r : classPathPatterns) {
        grokBlocks.put(r.getFilename(), IOUtils.readLines(r.getInputStream()).toArray(new String[0]));
    }
    registry.registerPatternBlocks(grokBlocks);
    return registry;
}

From source file:com.jaxio.celerio.template.pack.ClasspathTemplatePackInfoLoader.java

public List<TemplatePackInfo> resolveTopLevelPacks() {
    List<TemplatePackInfo> packInfos = newArrayList();
    PathMatchingResourcePatternResolver o = new PathMatchingResourcePatternResolver();

    try {// w  ww.  ja  v a2 s .c  o m
        Resource packInfosAsResource[] = o.getResources(CLASSPATH_CELERIO_PACK);
        for (Resource r : packInfosAsResource) {
            CelerioPack celerioPack = celerioPackConfigLoader.load(r.getInputStream());
            packInfos.add(new TemplatePackInfo(celerioPack));
        }

        return packInfos;
    } catch (IOException ioe) {
        throw new RuntimeException(
                "Error while searching for Celerio template pack having a META-INF/celerio-pack.xml file!",
                ioe);
    }
}

From source file:org.web4thejob.module.SafetyJoblet.java

@Override
public List<Resource> getResources() {
    List<Resource> resources = new ArrayList<Resource>();

    PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
    try {/*from   w w w  .j a  v a  2s  .  com*/
        for (Resource resource : resolver.getResources("classpath*:com/safetyjoblet/**/*.hbm.xml")) {
            resources.add(resource);
        }
    } catch (IOException e) {
        e.printStackTrace();
        throw new RuntimeException(e);
    }

    return resources;
}

From source file:my.joblet.MyJoblet.java

@Override
public List<Resource> getResources() {
    List<Resource> resources = new ArrayList<Resource>();

    PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
    try {/*from   ww w  .  j av a2 s .c  o m*/
        for (Resource resource : resolver.getResources("classpath*:my/joblet/**/*.hbm.xml")) {
            resources.add(resource);
        }
    } catch (IOException e) {
        e.printStackTrace();
        throw new RuntimeException(e);
    }

    return resources;
}