Search in sources :

Example 56 with ResourcePatternResolver

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

the class CoreResources method copyBaseToDest.

private void copyBaseToDest(ResourceLoader resourceLoader) {
    ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(resourceLoader);
    Resource[] resources;
    try {
        /*
             * Use classpath* to search for resources that match this pattern in ALL of the jars in the application
             * class path. See:
             * http://static.springsource.org/spring/docs/3.0.x/spring-framework-reference/html/resources
             * .html#resources-classpath-wildcards
             */
        resources = resolver.getResources("classpath*:properties/xslt/*.xsl");
    } catch (IOException ioe) {
        logger.debug(ioe.getMessage(), ioe);
        throw new OpenClinicaSystemException("Unable to read source files", ioe);
    }
    File dest = new File(getField("filePath") + "xslt");
    if (!dest.exists()) {
        if (!dest.mkdirs()) {
            throw new OpenClinicaSystemException("Copying files, Could not create direcotry: " + dest.getAbsolutePath() + ".");
        }
    }
    for (Resource r : resources) {
        File f = new File(dest, r.getFilename());
        try {
            FileOutputStream out = new FileOutputStream(f);
            IOUtils.copy(r.getInputStream(), out);
            out.close();
        } catch (IOException ioe) {
            logger.debug(ioe.getMessage(), ioe);
            throw new OpenClinicaSystemException("Unable to copy file: " + r.getFilename() + " to " + f.getAbsolutePath(), ioe);
        }
    }
}
Also used : PathMatchingResourcePatternResolver(org.springframework.core.io.support.PathMatchingResourcePatternResolver) ResourcePatternResolver(org.springframework.core.io.support.ResourcePatternResolver) FileOutputStream(java.io.FileOutputStream) Resource(org.springframework.core.io.Resource) IOException(java.io.IOException) PathMatchingResourcePatternResolver(org.springframework.core.io.support.PathMatchingResourcePatternResolver) OpenClinicaSystemException(org.akaza.openclinica.exception.OpenClinicaSystemException) File(java.io.File)

Example 57 with ResourcePatternResolver

use of org.springframework.core.io.support.ResourcePatternResolver in project commons by terran4j.

the class Classes method scanClasses.

/**
 * 扫描指定的包,将有指定注解的类找出来。
 *
 * @param basePackageClass 待扫描的包中的直接任意类,也就是根据这个类找到它的包。
 * @param recursively      是否递归的方式在子包中找, true表示要在所有子包中找,false表示只在本包中找。
 * @param filter           指定的过滤条件,只有符合过滤条件的类才会被找出来;如果为 null,则不过滤。
 * @param classLoader      类加载器。
 * @return 符合条件的类集合。
 * @throws IOException            IO异常
 * @throws ClassNotFoundException 类找不到。
 */
public static final Set<Class<?>> scanClasses(Class<?> basePackageClass, boolean recursively, TypeFilter filter, ClassLoader classLoader) throws IOException, ClassNotFoundException {
    if (basePackageClass == null) {
        throw new NullPointerException("basePackageClass is null.");
    }
    final Set<Class<?>> classes = new HashSet<Class<?>>();
    String packageName = basePackageClass.getPackage().getName();
    final String resourcePattern = recursively ? "/**/*.class" : "/*.class";
    String pattern = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + ClassUtils.convertClassNameToResourcePath(packageName) + resourcePattern;
    ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
    MetadataReaderFactory readerFactory = new CachingMetadataReaderFactory(resourcePatternResolver);
    final String KEY_maxCount = "terran4j.util.maxScanClassCount";
    String maxCountText = System.getProperty(KEY_maxCount, "1024").trim();
    int maxCount = 1024;
    try {
        maxCount = Integer.parseInt(maxCountText);
    } catch (NumberFormatException e) {
        throw new RuntimeException("Can't parse as int of java property[" + KEY_maxCount + "]: " + maxCountText);
    }
    Resource[] resources = resourcePatternResolver.getResources(pattern);
    for (Resource resource : resources) {
        if (resource.isReadable()) {
            // 找到这个类。
            MetadataReader reader = readerFactory.getMetadataReader(resource);
            String className = reader.getClassMetadata().getClassName();
            boolean matched = false;
            if (filter != null) {
                matched = filter.match(reader, readerFactory);
            } else {
                matched = true;
            }
            if (matched) {
                Class<?> clazz = classLoader.loadClass(className);
                classes.add(clazz);
                // 调用方指定的范围太大的话,
                if (classes.size() > maxCount) {
                    throw new RuntimeException("too many classes be scaned, can't more than: " + maxCount);
                }
            }
        }
    }
    // 输出日志
    if (log.isInfoEnabled()) {
        if (filter == null) {
            log.info("Found classes in package[{}]: \n{}", packageName, Joiner.on("\n").join(classes.iterator()));
        } else {
            log.info("Found classes with filter[{}] in package[{}]: \n{}", filter, packageName, Joiner.on("\n").join(classes.iterator()));
        }
    }
    return classes;
}
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) Resource(org.springframework.core.io.Resource) MetadataReader(org.springframework.core.type.classreading.MetadataReader) CachingMetadataReaderFactory(org.springframework.core.type.classreading.CachingMetadataReaderFactory) PathMatchingResourcePatternResolver(org.springframework.core.io.support.PathMatchingResourcePatternResolver)

Example 58 with ResourcePatternResolver

use of org.springframework.core.io.support.ResourcePatternResolver in project commons by terran4j.

the class Classes method scanResources.

/**
 * 搜索匹配路径的搜索。
 *
 * @param pathPattern 路径匹配模式。
 * @return 找到的资源文件。
 * @throws IOException IO异常。
 */
public static final Resource[] scanResources(String pathPattern) throws IOException {
    String pattern = ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX + pathPattern;
    ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
    Resource[] resources = resolver.getResources(pattern);
    return resources;
}
Also used : PathMatchingResourcePatternResolver(org.springframework.core.io.support.PathMatchingResourcePatternResolver) ResourcePatternResolver(org.springframework.core.io.support.ResourcePatternResolver) Resource(org.springframework.core.io.Resource) PathMatchingResourcePatternResolver(org.springframework.core.io.support.PathMatchingResourcePatternResolver)

Example 59 with ResourcePatternResolver

use of org.springframework.core.io.support.ResourcePatternResolver in project cosmic by MissionCriticalCloud.

the class ClasspathModuleDefinitionLocator method locateModules.

@Override
public Collection<ModuleDefinition> locateModules(final String context) throws IOException {
    final ResourcePatternResolver resolver = getResolver();
    final Map<String, ModuleDefinition> allModules = discoverModules(context, resolver);
    return allModules.values();
}
Also used : DefaultModuleDefinition(com.cloud.spring.module.model.impl.DefaultModuleDefinition) ModuleDefinition(com.cloud.spring.module.model.ModuleDefinition) PathMatchingResourcePatternResolver(org.springframework.core.io.support.PathMatchingResourcePatternResolver) ResourcePatternResolver(org.springframework.core.io.support.ResourcePatternResolver)

Example 60 with ResourcePatternResolver

use of org.springframework.core.io.support.ResourcePatternResolver in project syndesis by syndesisio.

the class Application method generateIntegrationProject.

private void generateIntegrationProject(File project) throws IOException {
    final ReadApiClientData reader = new ReadApiClientData();
    final ArrayList<Step> steps = new ArrayList<>();
    String deploymentText;
    try (InputStream is = resourceLoader.getResource("io/syndesis/server/dao/deployment.json").getInputStream()) {
        deploymentText = reader.from(is);
    }
    final List<ModelData<?>> modelList = reader.readDataFromString(deploymentText);
    for (final ModelData<?> model : modelList) {
        if (model.getKind() == Kind.Connector) {
            final Connector connector = (Connector) model.getData();
            for (final Action action : connector.getActions()) {
                steps.add(new Step.Builder().stepKind(StepKind.endpoint).connection(new Connection.Builder().connector(connector).connectorId(connector.getId().get()).build()).action(action).build());
            }
        }
        if (model.getKind() == Kind.ConnectorTemplate) {
            final ConnectorTemplate template = (ConnectorTemplate) model.getData();
            steps.add(new Step.Builder().stepKind(StepKind.endpoint).connection(new Connection.Builder().connectorId("connector-" + template.getId()).build()).action(new ConnectorAction.Builder().descriptor(new ConnectorDescriptor.Builder().camelConnectorGAV(template.getCamelConnectorGAV()).camelConnectorPrefix(template.getCamelConnectorPrefix()).build()).build()).build());
        }
    }
    try {
        final ResourcePatternResolver resolver = ResourcePatternUtils.getResourcePatternResolver(resourceLoader);
        final Resource[] resources = resolver.getResources("classpath:/META-INF/syndesis/connector/*.json");
        if (resources != null) {
            for (Resource resource : resources) {
                Connector connector = Json.reader().forType(Connector.class).readValue(resource.getInputStream());
                if (connector != null) {
                    for (final Action action : connector.getActions()) {
                        steps.add(new Step.Builder().stepKind(StepKind.endpoint).connection(new Connection.Builder().connector(connector).connectorId(connector.getId().get()).build()).action(action).build());
                    }
                }
            }
        }
    } catch (FileNotFoundException ignored) {
    // ignore
    }
    Integration integration = new Integration.Builder().id("Integration").name("Integration").description("This integration is used to prime the .m2 repo").steps(steps).build();
    generate(integration, project);
}
Also used : ModelData(io.syndesis.common.model.ModelData) Connector(io.syndesis.common.model.connection.Connector) ConnectorAction(io.syndesis.common.model.action.ConnectorAction) Action(io.syndesis.common.model.action.Action) Integration(io.syndesis.common.model.integration.Integration) ResourcePatternResolver(org.springframework.core.io.support.ResourcePatternResolver) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) Connection(io.syndesis.common.model.connection.Connection) ClassPathResource(org.springframework.core.io.ClassPathResource) Resource(org.springframework.core.io.Resource) FileNotFoundException(java.io.FileNotFoundException) Step(io.syndesis.common.model.integration.Step) ConnectorTemplate(io.syndesis.common.model.connection.ConnectorTemplate) ConnectorAction(io.syndesis.common.model.action.ConnectorAction) ReadApiClientData(io.syndesis.server.dao.init.ReadApiClientData)

Aggregations

ResourcePatternResolver (org.springframework.core.io.support.ResourcePatternResolver)68 PathMatchingResourcePatternResolver (org.springframework.core.io.support.PathMatchingResourcePatternResolver)65 Resource (org.springframework.core.io.Resource)52 IOException (java.io.IOException)15 ArrayList (java.util.ArrayList)11 Bean (org.springframework.context.annotation.Bean)11 PageHelper (com.github.pagehelper.PageHelper)9 File (java.io.File)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 Properties (java.util.Properties)8 InputStream (java.io.InputStream)6 FileOutputStream (java.io.FileOutputStream)5 URL (java.net.URL)5 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