Example usage for org.springframework.core.io ClassPathResource exists

List of usage examples for org.springframework.core.io ClassPathResource exists

Introduction

In this page you can find the example usage for org.springframework.core.io ClassPathResource exists.

Prototype

@Override
public boolean exists() 

Source Link

Document

This implementation checks for the resolution of a resource URL.

Usage

From source file:org.springframework.test.context.support.TestPropertySourceAttributes.java

/**
 * Detect a default properties file for the supplied class, as specified
 * in the class-level Javadoc for {@link TestPropertySource}.
 *//*from   w  w  w . java  2  s .c om*/
private static String detectDefaultPropertiesFile(Class<?> testClass) {
    String resourcePath = ClassUtils.convertClassNameToResourcePath(testClass.getName()) + ".properties";
    String prefixedResourcePath = ResourceUtils.CLASSPATH_URL_PREFIX + resourcePath;
    ClassPathResource classPathResource = new ClassPathResource(resourcePath);

    if (classPathResource.exists()) {
        if (logger.isInfoEnabled()) {
            logger.info(String.format("Detected default properties file \"%s\" for test class [%s]",
                    prefixedResourcePath, testClass.getName()));
        }
        return prefixedResourcePath;
    } else {
        String msg = String.format(
                "Could not detect default properties file for test [%s]: "
                        + "%s does not exist. Either declare the 'locations' or 'properties' attributes "
                        + "of @TestPropertySource or make the default properties file available.",
                testClass.getName(), classPathResource);
        logger.error(msg);
        throw new IllegalStateException(msg);
    }
}

From source file:piecework.content.concrete.ClasspathContentProvider.java

@Override
public ContentResource findByLocation(ContentProfileProvider modelProvider, String rawPath)
        throws PieceworkException {

    if (!rawPath.startsWith(ClasspathContentResource.PREFIX)) {
        LOG.error("Should not be looking for a classpath resource without the classpath prefix");
        return null;
    }/*from  w  ww . j  ava  2 s  .  co m*/

    String path = rawPath.substring(ClasspathContentResource.PREFIX.length());

    ContentProfile contentProfile = modelProvider.contentProfile();
    // Show never use ClasspathContentProvider unless the content profile explicitly
    // specifies a base classpath
    if (contentProfile == null || StringUtils.isEmpty(contentProfile.getBaseClasspath()))
        return null;

    String baseClasspath = contentProfile.getBaseClasspath();

    if (!ContentUtility.validateClasspath(baseClasspath, path)) {
        accessTracker.alarm(AlarmSeverity.MINOR,
                "Attempt to access classpath " + path + " outside of " + baseClasspath + " forbidden",
                modelProvider.principal());
        throw new ForbiddenError();
    }

    ClassPathResource resource = new ClassPathResource(path);

    if (!resource.exists()) {
        LOG.warn("No classpath resource exists for " + resource.getPath());
        return null;
    }

    return new ClasspathContentResource(resource);
}

From source file:piecework.util.UserInterfaceUtility.java

public static ContentResource template(File templatesDirectory, String templateName) throws NotFoundError {
    ContentResource resource = null;/*  w  w  w .  ja v  a  2  s. c om*/
    if (templatesDirectory != null) {
        File file = new File(templatesDirectory, templateName);
        FileSystemResource fileSystemResource = new FileSystemResource(file);

        if (!fileSystemResource.exists())
            throw new NotFoundError();

        resource = new FileSystemContentResource(fileSystemResource);
    } else {
        ClassPathResource classPathResource = new ClassPathResource(TEMPLATES_CLASSPATH_PREFIX + templateName);

        if (!classPathResource.exists())
            throw new NotFoundError();

        resource = new ClasspathContentResource(classPathResource);
    }

    return resource;
}