Search in sources :

Example 41 with ResourcePatternResolver

use of org.springframework.core.io.support.ResourcePatternResolver in project nd4j by deeplearning4j.

the class TFGraphTestAllHelper method readVars.

protected static Map<String, INDArray> readVars(String modelName, String base_dir, String pattern) throws IOException {
    Map<String, INDArray> varMap = new HashMap<>();
    String modelDir = base_dir + "/" + modelName;
    ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(new ClassPathResource(modelDir).getClassLoader());
    Resource[] resources = resolver.getResources("classpath*:" + modelDir + "/" + pattern + ".shape");
    val dtype = Nd4j.dataType();
    for (int i = 0; i < resources.length; i++) {
        String fileName = resources[i].getFilename();
        String varPath = modelDir + "/" + fileName;
        String[] varNameArr = fileName.split("\\.");
        String varName = String.join(".", Arrays.copyOfRange(varNameArr, 0, varNameArr.length - 2));
        int[] varShape = Nd4j.readNumpy(new ClassPathResource(varPath).getInputStream(), ",").data().asInt();
        try {
            float[] varContents = Nd4j.readNumpy(new ClassPathResource(varPath.replace(".shape", ".csv")).getInputStream(), ",").data().asFloat();
            INDArray varValue;
            if (varShape.length == 1) {
                if (varShape[0] == 0) {
                    varValue = Nd4j.trueScalar(varContents[0]);
                } else {
                    varValue = Nd4j.trueVector(varContents);
                }
            } else {
                varValue = Nd4j.create(varContents, varShape);
            }
            // varValue = Nd4j.readNumpy(new ClassPathResource(varPath.replace(".shape", ".csv")).getInputStream(), ",").reshape(varShape);
            if (varName.contains("____")) {
                // these are intermediate node outputs
                varMap.put(varName.replaceAll("____", "/"), varValue);
            } else {
                varMap.put(varName, varValue);
            }
        } catch (NumberFormatException e) {
            // FIXME: we can't parse boolean arrays right now :(
            continue;
        }
    }
    return varMap;
}
Also used : lombok.val(lombok.val) PathMatchingResourcePatternResolver(org.springframework.core.io.support.PathMatchingResourcePatternResolver) ResourcePatternResolver(org.springframework.core.io.support.ResourcePatternResolver) Resource(org.springframework.core.io.Resource) ClassPathResource(org.nd4j.linalg.io.ClassPathResource) ClassPathResource(org.nd4j.linalg.io.ClassPathResource) INDArray(org.nd4j.linalg.api.ndarray.INDArray) PathMatchingResourcePatternResolver(org.springframework.core.io.support.PathMatchingResourcePatternResolver)

Example 42 with ResourcePatternResolver

use of org.springframework.core.io.support.ResourcePatternResolver in project nd4j by deeplearning4j.

the class TFGraphTestAllHelper method modelDirNames.

private static String[] modelDirNames(String base_dir, ExecuteWith executeWith) throws IOException {
    ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(new ClassPathResource(base_dir).getClassLoader());
    Resource[] resources = resolver.getResources("classpath*:" + base_dir + "/**/frozen_model.pb");
    String[] exampleNames = new String[resources.length];
    for (int i = 0; i < resources.length; i++) {
        String nestedName = resources[i].getURL().toString().split(base_dir + "/")[1];
        exampleNames[i] = nestedName.replaceAll(Pattern.quote(executeWith.getDefaultBaseDir()), "").replaceAll("/frozen_model.pb", "");
    }
    return exampleNames;
}
Also used : PathMatchingResourcePatternResolver(org.springframework.core.io.support.PathMatchingResourcePatternResolver) ResourcePatternResolver(org.springframework.core.io.support.ResourcePatternResolver) Resource(org.springframework.core.io.Resource) ClassPathResource(org.nd4j.linalg.io.ClassPathResource) PathMatchingResourcePatternResolver(org.springframework.core.io.support.PathMatchingResourcePatternResolver) ClassPathResource(org.nd4j.linalg.io.ClassPathResource)

Example 43 with ResourcePatternResolver

use of org.springframework.core.io.support.ResourcePatternResolver in project service-proxy by membrane.

the class ClassFinder method find.

public static List<Class<?>> find(ClassLoader loader, String scannedPackage) throws IOException, ClassNotFoundException {
    String scannedPath = scannedPackage.replace(PKG_SEPARATOR, DIR_SEPARATOR);
    ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(loader);
    Resource[] resources = resolver.getResources("classpath:" + scannedPath + "/*");
    List<Class<?>> classes = new ArrayList<Class<?>>();
    for (Resource resource : resources) {
        String filename = resource.getFilename();
        Class clazz = getClasses(resolver, scannedPackage, filename);
        if (clazz != null)
            classes.add(clazz);
    }
    return classes;
}
Also used : PathMatchingResourcePatternResolver(org.springframework.core.io.support.PathMatchingResourcePatternResolver) ResourcePatternResolver(org.springframework.core.io.support.ResourcePatternResolver) Resource(org.springframework.core.io.Resource) ArrayList(java.util.ArrayList) PathMatchingResourcePatternResolver(org.springframework.core.io.support.PathMatchingResourcePatternResolver)

Example 44 with ResourcePatternResolver

use of org.springframework.core.io.support.ResourcePatternResolver in project openmrs-core by openmrs.

the class MutableResourceBundleMessageSource method findPropertiesFiles.

/**
 * Searches the filesystem for message properties files. ABKTODO: consider caching this, rather
 * than searching every time
 *
 * @return an array of property file names
 */
private Resource[] findPropertiesFiles() {
    Set<Resource> resourceSet = new HashSet<>();
    try {
        String pattern = "classpath*:messages*.properties";
        ResourcePatternResolver resourceResolver = new PathMatchingResourcePatternResolver(OpenmrsClassLoader.getInstance());
        Resource[] propertiesFiles = resourceResolver.getResources(pattern);
        Collections.addAll(resourceSet, propertiesFiles);
        for (ModuleClassLoader moduleClassLoader : ModuleFactory.getModuleClassLoaders()) {
            resourceResolver = new PathMatchingResourcePatternResolver(moduleClassLoader);
            propertiesFiles = resourceResolver.getResources(pattern);
            Collections.addAll(resourceSet, propertiesFiles);
        }
    } catch (IOException e) {
        log.error("Error generated", e);
    }
    if (log.isWarnEnabled() && (resourceSet.isEmpty())) {
        log.warn("No properties files found.");
    }
    return resourceSet.toArray(new Resource[resourceSet.size()]);
}
Also used : PathMatchingResourcePatternResolver(org.springframework.core.io.support.PathMatchingResourcePatternResolver) ResourcePatternResolver(org.springframework.core.io.support.ResourcePatternResolver) ModuleClassLoader(org.openmrs.module.ModuleClassLoader) Resource(org.springframework.core.io.Resource) IOException(java.io.IOException) PathMatchingResourcePatternResolver(org.springframework.core.io.support.PathMatchingResourcePatternResolver) HashSet(java.util.HashSet)

Example 45 with ResourcePatternResolver

use of org.springframework.core.io.support.ResourcePatternResolver in project jeesuite-libs by vakinge.

the class SchedulerFactoryBeanWrapper method scanAndRegisterAnnotationJobs.

private void scanAndRegisterAnnotationJobs(String[] scanBasePackages) {
    String RESOURCE_PATTERN = "/**/*.class";
    ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
    for (String scanBasePackage : scanBasePackages) {
        logger.info(">>begin scan package [{}] with Annotation[ScheduleConf] jobs ", scanBasePackage);
        try {
            String pattern = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + ClassUtils.convertClassNameToResourcePath(scanBasePackage) + RESOURCE_PATTERN;
            org.springframework.core.io.Resource[] resources = resourcePatternResolver.getResources(pattern);
            MetadataReaderFactory readerFactory = new CachingMetadataReaderFactory(resourcePatternResolver);
            for (org.springframework.core.io.Resource resource : resources) {
                if (resource.isReadable()) {
                    MetadataReader reader = readerFactory.getMetadataReader(resource);
                    String className = reader.getClassMetadata().getClassName();
                    Class<?> clazz = Class.forName(className);
                    if (clazz.isAnnotationPresent(ScheduleConf.class)) {
                        ScheduleConf annotation = clazz.getAnnotation(ScheduleConf.class);
                        AbstractJob job = (AbstractJob) context.getBean(clazz);
                        job.setCronExpr(annotation.cronExpr());
                        job.setExecuteOnStarted(annotation.executeOnStarted());
                        job.setGroup(groupName);
                        job.setJobName(annotation.jobName());
                        job.setRetries(annotation.retries());
                        if (!schedulers.contains(job)) {
                            schedulers.add(job);
                            logger.info("register new job:{}", ToStringBuilder.reflectionToString(job, ToStringStyle.JSON_STYLE));
                        } else {
                            logger.info("Job[{}] is registered", job.getJobName());
                        }
                    }
                }
            }
            logger.info("<<scan package[" + scanBasePackage + "] finished!");
        } catch (Exception e) {
            if (e instanceof org.springframework.beans.factory.NoSuchBeanDefinitionException) {
                throw (org.springframework.beans.factory.NoSuchBeanDefinitionException) e;
            }
            logger.error("<<scan package[" + scanBasePackage + "] error", e);
        }
    }
}
Also used : PathMatchingResourcePatternResolver(org.springframework.core.io.support.PathMatchingResourcePatternResolver) ResourcePatternResolver(org.springframework.core.io.support.ResourcePatternResolver) CachingMetadataReaderFactory(org.springframework.core.type.classreading.CachingMetadataReaderFactory) MetadataReaderFactory(org.springframework.core.type.classreading.MetadataReaderFactory) MetadataReader(org.springframework.core.type.classreading.MetadataReader) CachingMetadataReaderFactory(org.springframework.core.type.classreading.CachingMetadataReaderFactory) BeansException(org.springframework.beans.BeansException) ScheduleConf(com.jeesuite.scheduler.annotation.ScheduleConf) PathMatchingResourcePatternResolver(org.springframework.core.io.support.PathMatchingResourcePatternResolver)

Aggregations

ResourcePatternResolver (org.springframework.core.io.support.ResourcePatternResolver)65 PathMatchingResourcePatternResolver (org.springframework.core.io.support.PathMatchingResourcePatternResolver)62 Resource (org.springframework.core.io.Resource)49 IOException (java.io.IOException)15 Bean (org.springframework.context.annotation.Bean)11 ArrayList (java.util.ArrayList)10 PageHelper (com.github.pagehelper.PageHelper)9 SqlSessionFactoryBean (org.mybatis.spring.SqlSessionFactoryBean)9 CachingMetadataReaderFactory (org.springframework.core.type.classreading.CachingMetadataReaderFactory)9 MetadataReader (org.springframework.core.type.classreading.MetadataReader)9 MetadataReaderFactory (org.springframework.core.type.classreading.MetadataReaderFactory)9 File (java.io.File)8 Properties (java.util.Properties)8 InputStream (java.io.InputStream)5 URL (java.net.URL)5 FileOutputStream (java.io.FileOutputStream)3 OpenClinicaSystemException (org.akaza.openclinica.exception.OpenClinicaSystemException)3 EventLoopGroup (io.netty.channel.EventLoopGroup)2 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)2 SocketChannel (io.netty.channel.socket.SocketChannel)2