Java Code Examples for com.google.common.reflect.ClassPath#ResourceInfo

The following examples show how to use com.google.common.reflect.ClassPath#ResourceInfo . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ClasspathResourceRepository.java    From jweb-cms with GNU Affero General Public License v3.0 6 votes vote down vote up
@Override
public List<Resource> list(String directory) {
    try {
        ClassPath classpath = ClassPath.from(Thread.currentThread().getContextClassLoader());
        List<Resource> resources = Lists.newArrayList();
        for (ClassPath.ResourceInfo resourceInfo : classpath.getResources()) {
            if (resourceInfo.getResourceName().startsWith(basePath)) {
                String name = resourceInfo.getResourceName();
                String path = name.substring(basePath.length());
                if (path.startsWith(directory)) {
                    resources.add(new ClasspathResource(path, name));
                }
            }
        }
        return resources;
    } catch (IOException e) {
        throw new ResourceException(e);
    }
}
 
Example 2
Source File: AllureMain.java    From allure1 with Apache License 2.0 6 votes vote down vote up
/**
 * Unpack report face to given directory.
 *
 * @param outputDirectory the output directory to unpack face.
 * @throws IOException if any occurs.
 */
private static void unpackFace(File outputDirectory) throws IOException {
    ClassLoader loader = AllureMain.class.getClassLoader();
    for (ClassPath.ResourceInfo info : ClassPath.from(loader).getResources()) {
        Matcher matcher = REPORT_RESOURCE_PATTERN.matcher(info.getResourceName());
        if (matcher.find()) {
            String resourcePath = matcher.group(1);
            File dest = new File(outputDirectory, resourcePath);
            Files.createParentDirs(dest);
            try (FileOutputStream output = new FileOutputStream(dest);
                 InputStream input = info.url().openStream()) {
                IOUtils.copy(input, output);
                LOGGER.debug("{} successfully copied.", resourcePath);
            }
        }
    }
}
 
Example 3
Source File: ClassPathResourceTest.java    From wisdom with Apache License 2.0 6 votes vote down vote up
@Test
public void test() throws Exception {
    File file = new File("src/test/resources/foo.txt");
    ClassPath.ResourceInfo info = getResourceForName("foo.txt");
    assertThat(info).isNotNull();
    ClassPathResource resource = new ClassPathResource(info);
    assertThat(resource.getExtra()).isNullOrEmpty();
    assertThat(resource.lastModified()).isGreaterThanOrEqualTo(file.lastModified());
    assertThat(resource.size()).isEqualTo(file.length());

    InputStream stream = resource.openInputStream();
    String content = IOUtils.toString(stream);
    IOUtils.closeQuietly(stream);

    assertThat(content).isEqualTo(FileUtils.readFileToString(file));

    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    resource.write(bos);
    content = bos.toString();
    IOUtils.closeQuietly(bos);

    assertThat(content).isEqualTo(FileUtils.readFileToString(file));

}
 
Example 4
Source File: ScriptRunner.java    From purplejs with Apache License 2.0 5 votes vote down vote up
private List<ResourcePath> findResources()
    throws Exception
{
    final List<Pattern> patterns = compileFilePatterns();
    final List<ResourcePath> list = Lists.newArrayList();

    for ( final ClassPath.ResourceInfo info : CP.getResources() )
    {
        matchesResource( info, patterns, list );
    }

    return list;
}
 
Example 5
Source File: ClassPathResource.java    From wisdom with Apache License 2.0 5 votes vote down vote up
/**
 * Adds all the resources found in the given classpath to the given jar. Are excluded resources matching the
 * 'doNotCopy' pattern.
 *
 * @param jar       the jar in which the resources are added
 * @param classpath the classpath
 * @param doNotCopy the do not copy pattern
 */
public static void build(Jar jar, ClassPath classpath, Pattern doNotCopy) {
    ImmutableSet<ClassPath.ResourceInfo> resources = classpath.getResources();
    for (ClassPath.ResourceInfo resource : resources) {
        if (doNotCopy != null && doNotCopy.matcher(resource.getResourceName()).matches()) {
            continue;
        }
        jar.putResource(resource.getResourceName(), new ClassPathResource(resource));
    }
}
 
Example 6
Source File: ClassPathResourceTest.java    From wisdom with Apache License 2.0 5 votes vote down vote up
private static ClassPath.ResourceInfo getResourceForName(String name) throws IOException {
    ClassPath cp = ClassPath.from(ClassPathResourceTest.class.getClassLoader());
    ImmutableSet<ClassPath.ResourceInfo> resources = cp.getResources();
    for (ClassPath.ResourceInfo info : resources) {
        if (info.getResourceName().equals(name)) {
            return info;
        }
    }
    return null;
}
 
Example 7
Source File: ScriptRunner.java    From purplejs with Apache License 2.0 4 votes vote down vote up
private void matchesResource( final ClassPath.ResourceInfo info, final List<Pattern> patterns, final List<ResourcePath> list )
{
    final String path = "/" + info.getResourceName();
    patterns.stream().filter( pattern -> pattern.matcher( path ).find() ).forEach( pattern -> list.add( ResourcePath.from( path ) ) );
}
 
Example 8
Source File: ClassPathResource.java    From wisdom with Apache License 2.0 2 votes vote down vote up
/**
 * Creates the Classpath resource from the given resource.
 *
 * @param resource the wrapped resource
 */
public ClassPathResource(ClassPath.ResourceInfo resource) {
    this.resource = resource;
}