Example usage for org.springframework.core.io Resource isReadable

List of usage examples for org.springframework.core.io Resource isReadable

Introduction

In this page you can find the example usage for org.springframework.core.io Resource isReadable.

Prototype

default boolean isReadable() 

Source Link

Document

Indicate whether non-empty contents of this resource can be read via #getInputStream() .

Usage

From source file:com.baifendian.swordfish.webserver.service.storage.FileSystemStorageService.java

@Override
public Resource loadAsResource(String filename) {
    try {/*from   ww  w  .  j av a 2 s  .c o m*/
        Path file = Paths.get(filename);

        Resource resource = new UrlResource(file.toUri());
        if (resource.exists() || resource.isReadable()) {
            return resource;
        } else {
            throw new StorageFileNotFoundException("Could not read file: " + filename);

        }
    } catch (MalformedURLException e) {
        throw new StorageFileNotFoundException("Could not read file: " + filename, e);
    }
}

From source file:com.agileapes.couteau.maven.resource.ClassPathScanningClassProvider.java

public Set<Class> findCandidateClasses(String basePackage) {
    Set<Class> candidates = new TreeSet<Class>(new Comparator<Class>() {
        @Override//from ww w.j  a  v a  2  s  . c o  m
        public int compare(Class dis, Class dat) {
            if (dis == null && dat == null) {
                return 0;
            } else if (dis == null) {
                return -1;
            } else if (dat == null) {
                return 1;
            } else {
                return dis.getName().compareTo(dat.getName());
            }
        }
    });
    try {
        final String packageSearchPath = classpathUrlPrefix + resolveBasePackage(basePackage) + "/"
                + DEFAULT_RESOURCE_PATTERN;
        final Resource[] resources = ((ResourcePatternResolver) this.getResourceLoader())
                .getResources(packageSearchPath);
        if (resources == null) {
            return Collections.emptySet();
        }
        for (Resource resource : resources) {
            if (resource.isReadable() && resource.getURL().getPath() != null) {
                if (isCandidateComponent(this.metadataReaderFactory.getMetadataReader(resource))) {
                    candidates.add(resolveClass(resource, basePackage));
                }
            }
        }
    } catch (IOException ex) {
        throw new RuntimeException("I/O failure during classpath scanning", ex);
    } catch (ClassNotFoundException e) {
        throw new RuntimeException("System failure during classpath scanning. No class were found.", e);
    }
    return candidates;

}

From source file:com.epam.ta.reportportal.database.search.CriteriaMapFactory.java

/**
 * Find Classes into specified package/* w ww .j a v a  2s.  c  o m*/
 */
private List<Class<?>> findNeededTypes(String basePackage) {
    ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
    MetadataReaderFactory metadataReaderFactory = new CachingMetadataReaderFactory(resourcePatternResolver);

    List<Class<?>> candidates = new LinkedList<>();

    try {
        String packageSearchPath = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX
                + resolveBasePackage(basePackage) + "/" + "**/*.class";
        Resource[] resources = resourcePatternResolver.getResources(packageSearchPath);
        for (Resource resource : resources) {
            if (resource.isReadable()) {
                MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(resource);
                if (isCandidate(metadataReader)) {
                    candidates.add(Class.forName(metadataReader.getClassMetadata().getClassName()));
                }
            }
        }
        return candidates;
    } catch (Exception e) {
        throw new IllegalArgumentException("Unable to scan base package '" + basePackage + "'", e);
    }
}

From source file:org.opentides.persistence.hibernate.PersistenceScanner.java

/**
 * //from   ww  w. j  a  v a  2  s.  c  o  m
 * Perform Spring-based scanning for entity classes.
 * 
 * @see #setPackagesToScan
 */

protected String[] scanPackages() {
    Set<String> entities = new HashSet<String>();
    if (this.packagesToScan != null) {
        try {
            for (String pkg : this.packagesToScan) {
                String pattern = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX
                        + ClassUtils.convertClassNameToResourcePath(pkg) + RESOURCE_PATTERN;
                Resource[] resources = this.resourcePatternResolver.getResources(pattern);
                MetadataReaderFactory readerFactory = new CachingMetadataReaderFactory(
                        this.resourcePatternResolver);
                for (Resource resource : resources) {
                    if (resource.isReadable()) {
                        MetadataReader reader = readerFactory.getMetadataReader(resource);
                        String className = reader.getClassMetadata().getClassName();
                        if (matchesFilter(reader, readerFactory)) {
                            entities.add(className);
                        }
                    }
                }
            }
        } catch (IOException ex) {
            throw new MappingException("Failed to scan classpath for unlisted classes", ex);
        }
    }
    return entities.toArray(new String[entities.size()]);
}

From source file:cn.wanghaomiao.seimi.core.SeimiScanner.java

@SafeVarargs
public final Set<Class<?>> scan(String[] confPkgs, Class<? extends Annotation>... annotationTags) {
    Set<Class<?>> resClazzSet = new HashSet<>();
    List<AnnotationTypeFilter> typeFilters = new LinkedList<>();
    if (ArrayUtils.isNotEmpty(annotationTags)) {
        for (Class<? extends Annotation> annotation : annotationTags) {
            typeFilters.add(new AnnotationTypeFilter(annotation, false));
        }/*  www. j  av  a2  s.  c o m*/
    }
    if (ArrayUtils.isNotEmpty(confPkgs)) {
        for (String pkg : confPkgs) {
            String pattern = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX
                    + String.format(RESOURCE_PATTERN, ClassUtils.convertClassNameToResourcePath(pkg));
            try {
                Resource[] resources = this.resourcePatternResolver.getResources(pattern);
                MetadataReaderFactory readerFactory = new CachingMetadataReaderFactory(
                        this.resourcePatternResolver);
                for (Resource resource : resources) {
                    if (resource.isReadable()) {
                        MetadataReader reader = readerFactory.getMetadataReader(resource);
                        String className = reader.getClassMetadata().getClassName();
                        if (ifMatchesEntityType(reader, readerFactory, typeFilters)) {
                            Class<?> curClass = Thread.currentThread().getContextClassLoader()
                                    .loadClass(className);
                            context.register(curClass);
                            resClazzSet.add(curClass);
                        }
                    }
                }
            } catch (Exception e) {
                logger.error("?????[{}][{}]", pattern,
                        StringUtils.join(typeFilters, ","));
            }
        }
    }
    if (!context.isActive()) {
        context.refresh();
    }
    return resClazzSet;
}

From source file:com.github.ukase.config.WebConfig.java

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    String location = "classpath:static/";

    if (devMode) {
        Path path = Paths.get(getProjectRootRequired(), "ui", "target", "dist");
        location = path.toUri().toString();
    }/*  w ww  . ja  va  2 s.com*/

    newResourceHandler(registry, "/bundle.*.js", location).addTransformer(new AppCacheManifestTransformer());

    newResourceHandler(registry, "/cache*.appcache", location);

    newResourceHandler(registry, "/**", location + "index.html").addResolver(new PathResourceResolver() {
        @Override
        protected Resource getResource(String resourcePath, Resource location) throws IOException {
            Resource resource = location.createRelative(resourcePath);
            return resource.exists() && resource.isReadable() ? resource : null;
        }
    });
}

From source file:net.seedboxer.web.utils.mapper.Mapper.java

@PostConstruct
public void init() throws ClassNotFoundException {
    if (packages != null) {
        mappings = new HashMap<Class<?>, Class<?>>();
        List<Class<?>> annotated = new ArrayList<Class<?>>();

        try {// w w w.  j a  v a 2s  .c  o m
            for (String p : packages) {
                String packageSearchPath = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX
                        + ClassUtils.convertClassNameToResourcePath(SystemPropertyUtils.resolvePlaceholders(p))
                        + "/" + CLASS_RESOURCE_PATTERN;

                Resource[] resources = resourcePatternResolver.getResources(packageSearchPath);

                for (Resource resource : resources) {
                    if (resource.isReadable()) {
                        MetadataReader metadataReader = this.metadataReaderFactory.getMetadataReader(resource);

                        if (this.annotationFilter.match(metadataReader, metadataReaderFactory)) {
                            annotated.add(Class.forName(metadataReader.getAnnotationMetadata().getClassName()));
                        }
                    }
                }
            }
        } catch (IOException ex) {
            throw new RuntimeException("I/O failure during classpath scanning", ex);
        }

        for (Class<?> clazz : annotated) {
            Class<?> toMap = clazz.getAnnotation(MAPPER_ANNOTATION).value();

            // Binding One-to-One
            mappings.put(clazz, toMap);
            mappings.put(toMap, clazz);
        }
    }
}

From source file:com.sxj.jsonrpc.client.spring.AutoJsonRpcClientProxyCreator.java

public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
    SimpleMetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory(applicationContext);
    DefaultListableBeanFactory dlbf = (DefaultListableBeanFactory) beanFactory;
    String resolvedPath = resolvePackageToScan();
    LOG.debug(format("Scanning '%s' for JSON-RPC service interfaces.", resolvedPath));
    try {/*from w  w w.  j a  v a2 s  .com*/
        for (Resource resource : applicationContext.getResources(resolvedPath)) {
            if (resource.isReadable()) {
                MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(resource);
                ClassMetadata classMetadata = metadataReader.getClassMetadata();
                AnnotationMetadata annotationMetadata = metadataReader.getAnnotationMetadata();
                String jsonRpcPathAnnotation = JsonRpcService.class.getName();
                if (annotationMetadata.isAnnotated(jsonRpcPathAnnotation)) {
                    String className = classMetadata.getClassName();
                    String path = (String) annotationMetadata.getAnnotationAttributes(jsonRpcPathAnnotation)
                            .get("value");
                    boolean useNamedParams = (Boolean) annotationMetadata
                            .getAnnotationAttributes(jsonRpcPathAnnotation).get("useNamedParams");
                    LOG.debug(format("Found JSON-RPC service to proxy [%s] on path '%s'.", className, path));
                    registerJsonProxyBean(dlbf, className, path, useNamedParams);
                }
            }
        }
    } catch (IOException e) {
        throw new RuntimeException(format("Cannot scan package '%s' for classes.", resolvedPath), e);
    }
}

From source file:com.lexicalscope.javabeanhelpers.generator.plugin.JavaBeanHelpersGeneratorMojo.java

private List<Class<?>> findEligableTypes(final String basePackage)
        throws IOException, ClassNotFoundException, DependencyResolutionRequiredException {
    if (basePackage == null) {
        return Collections.emptyList();
    }//from ww w .  j a va  2 s .  c  om

    final ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver(
            getClassLoader());
    final MetadataReaderFactory metadataReaderFactory = new CachingMetadataReaderFactory(
            resourcePatternResolver);

    final List<Class<?>> candidates = new ArrayList<Class<?>>();
    final String packageSearchPath = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX
            + resolveBasePackage(basePackage) + "/" + "**/*.class";
    final Resource[] resources = resourcePatternResolver.getResources(packageSearchPath);
    for (final Resource resource : resources) {
        if (resource.isReadable()) {
            final MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(resource);
            final Class<?> loadedClass = getClassLoader()
                    .loadClass(metadataReader.getClassMetadata().getClassName());
            if (isCandidate(loadedClass)) {
                getLog().info("found resource " + resource);
                candidates.add(loadedClass);
            }
        }
    }
    return candidates;
}

From source file:com.googlecode.jsonrpc4j.spring.AutoJsonRpcClientProxyCreator.java

@Override
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
    SimpleMetadataReaderFactory metadataReaderFactory = new SimpleMetadataReaderFactory(applicationContext);
    DefaultListableBeanFactory dlbf = (DefaultListableBeanFactory) beanFactory;
    String resolvedPath = resolvePackageToScan();
    LOG.debug(format("Scanning '%s' for JSON-RPC service interfaces.", resolvedPath));
    try {/*from   ww w  .ja  va2  s.c om*/
        for (Resource resource : applicationContext.getResources(resolvedPath)) {
            if (resource.isReadable()) {
                MetadataReader metadataReader = metadataReaderFactory.getMetadataReader(resource);
                ClassMetadata classMetadata = metadataReader.getClassMetadata();
                AnnotationMetadata annotationMetadata = metadataReader.getAnnotationMetadata();
                String jsonRpcPathAnnotation = JsonRpcService.class.getName();
                if (annotationMetadata.isAnnotated(jsonRpcPathAnnotation)) {
                    String className = classMetadata.getClassName();
                    String path = (String) annotationMetadata.getAnnotationAttributes(jsonRpcPathAnnotation)
                            .get("value");
                    boolean useNamedParams = (Boolean) annotationMetadata
                            .getAnnotationAttributes(jsonRpcPathAnnotation).get("useNamedParams");
                    LOG.debug(format("Found JSON-RPC service to proxy [%s] on path '%s'.", className, path));
                    registerJsonProxyBean(dlbf, className, path, useNamedParams);
                }
            }
        }
    } catch (IOException e) {
        throw new RuntimeException(format("Cannot scan package '%s' for classes.", resolvedPath), e);
    }
}