Search in sources :

Example 91 with PathMatchingResourcePatternResolver

use of org.springframework.core.io.support.PathMatchingResourcePatternResolver 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)

Example 92 with PathMatchingResourcePatternResolver

use of org.springframework.core.io.support.PathMatchingResourcePatternResolver in project pigatron-web by pigatron-industries.

the class AdminResourceConfig method findWebResources.

public WebResources findWebResources(String path) throws IOException {
    WebResources webResources = new WebResources();
    ClassLoader cl = this.getClass().getClassLoader();
    ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver(cl);
    Resource[] resources = resolver.getResources("classpath*:/static/" + path + "/*.js");
    for (Resource resource : resources) {
        webResources.addResource(resource.getFilename(), WebResourceType.JS);
    }
    resources = resolver.getResources("classpath*:/static/" + path + "/*.css");
    for (Resource resource : resources) {
        webResources.addResource(resource.getFilename(), WebResourceType.CSS);
    }
    webResources.sort();
    return webResources;
}
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 93 with PathMatchingResourcePatternResolver

use of org.springframework.core.io.support.PathMatchingResourcePatternResolver in project web3sdk by FISCO-BCOS.

the class ChannelConnections method startConnect.

public void startConnect() {
    if (running) {
        logger.debug("服务已启动");
        return;
    }
    logger.debug("初始化connections connect");
    // 初始化netty
    EventLoopGroup workerGroup = new NioEventLoopGroup();
    bootstrap.group(workerGroup);
    bootstrap.channel(NioSocketChannel.class);
    bootstrap.option(ChannelOption.SO_KEEPALIVE, true);
    final ChannelConnections selfService = this;
    final ThreadPoolTaskExecutor selfThreadPool = threadPool;
    ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
    final Resource keystoreResource = resolver.getResource(getClientKeystorePath());
    final Resource caResource = resolver.getResource(getCaCertPath());
    bootstrap.handler(new ChannelInitializer<SocketChannel>() {

        @Override
        public void initChannel(SocketChannel ch) throws Exception {
            KeyStore ks = KeyStore.getInstance("JKS");
            InputStream ksInputStream = keystoreResource.getInputStream();
            ks.load(ksInputStream, getKeystorePassWord().toCharArray());
            /*
				 * 每次连接使用新的handler 连接信息从socketChannel中获取
				 */
            ChannelHandler handler = new ChannelHandler();
            handler.setConnections(selfService);
            handler.setIsServer(false);
            handler.setThreadPool(selfThreadPool);
            SslContext sslCtx = SslContextBuilder.forClient().trustManager(caResource.getFile()).keyManager((PrivateKey) ks.getKey("client", getClientCertPassWord().toCharArray()), (X509Certificate) ks.getCertificate("client")).build();
            ch.pipeline().addLast(sslCtx.newHandler(ch.alloc()), new LengthFieldBasedFrameDecoder(1024 * 1024 * 4, 0, 4, -4, 0), new IdleStateHandler(idleTimeout, idleTimeout, idleTimeout, TimeUnit.MILLISECONDS), handler);
        }
    });
    running = true;
    Thread loop = new Thread() {

        public void run() {
            try {
                while (true) {
                    if (!running) {
                        return;
                    }
                    // 尝试重连
                    reconnect();
                    Thread.sleep(heartBeatDelay);
                }
            } catch (InterruptedException e) {
                logger.error("系统错误", e);
            }
        }
    };
    loop.start();
}
Also used : NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) SocketChannel(io.netty.channel.socket.SocketChannel) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) PathMatchingResourcePatternResolver(org.springframework.core.io.support.PathMatchingResourcePatternResolver) ResourcePatternResolver(org.springframework.core.io.support.ResourcePatternResolver) InputStream(java.io.InputStream) Resource(org.springframework.core.io.Resource) KeyStore(java.security.KeyStore) EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) IdleStateHandler(io.netty.handler.timeout.IdleStateHandler) ThreadPoolTaskExecutor(org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor) PathMatchingResourcePatternResolver(org.springframework.core.io.support.PathMatchingResourcePatternResolver) LengthFieldBasedFrameDecoder(io.netty.handler.codec.LengthFieldBasedFrameDecoder) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) SslContext(io.netty.handler.ssl.SslContext)

Example 94 with PathMatchingResourcePatternResolver

use of org.springframework.core.io.support.PathMatchingResourcePatternResolver in project web3sdk by FISCO-BCOS.

the class ChannelConnections method initSslContext.

private SslContext initSslContext() throws SSLException {
    SslContext sslCtx;
    try {
        if (!isEnableOpenSSL()) {
            String property = System.getProperty("jdk.tls.namedGroups", "");
            if (property == null || "".equals(property)) {
                System.setProperty("jdk.tls.namedGroups", "secp256k1");
                logger.info("jdk.tls.namedGroups has not been set, property: {}", property);
            } else if (!property.contains("secp256k1")) {
                System.setProperty("jdk.tls.namedGroups", property + ",secp256k1");
                logger.info("jdk.tls.namedGroups not including secp256k1 has been set, property: {}", property);
            } else {
                logger.info("jdk.tls.namedGroups including secp256k1 has been set, property: {}", property);
            }
        }
        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        // check ssl cert file
        Resource caResource = getCaCert();
        Resource keystorecaResource = getSslCert();
        Resource keystorekeyResource = getSslKey();
        // check if ca.crt exist
        if (Objects.isNull(caResource) || !caResource.exists()) {
            Resource resource = resolver.getResource(CA_CERT);
            if (Objects.nonNull(resource) && resource.exists()) {
                caResource = resource;
            } else {
                throw new RuntimeException((Objects.nonNull(caResource) ? "ca.crt" : caResource.getFilename()) + " not exist ");
            }
        }
        // check if sdk.crt exist, if not , check the default value node.crt
        if (Objects.isNull(keystorecaResource) || !keystorecaResource.exists()) {
            Resource resource = resolver.getResource(SSL_CERT);
            if (Objects.nonNull(resource) && resource.exists()) {
                keystorecaResource = resource;
            } else {
                throw new RuntimeException((Objects.nonNull(keystorecaResource) ? "sdk.crt" : keystorecaResource.getFilename()) + " not exist ");
            }
        }
        // check if sdk.key exist, if not, check the default value sdk.key
        if (Objects.isNull(keystorekeyResource) || !keystorekeyResource.exists()) {
            Resource resource = resolver.getResource(SSL_KEY);
            if (Objects.nonNull(resource) && resource.exists()) {
                keystorekeyResource = resource;
            } else {
                throw new RuntimeException((Objects.nonNull(keystorekeyResource) ? "sdk.key" : keystorekeyResource.getFilename()) + " not exist ");
            }
        }
        logger.info(" ca certificate: {}, sdk certificate: {}, sdk key: {}, enableOpenSsl: {}", caResource.getFilename(), keystorecaResource.getFilename(), keystorekeyResource.getFilename(), isEnableOpenSSL());
        sslCtx = SslContextBuilder.forClient().trustManager(caResource.getInputStream()).keyManager(keystorecaResource.getInputStream(), keystorekeyResource.getInputStream()).sslProvider(isEnableOpenSSL() ? SslProvider.OPENSSL : SslProvider.JDK).build();
    } catch (Exception e) {
        logger.error(" Failed to initialize the SSLContext, e: {} ", e.getCause());
        throw new SSLException(" Failed to initialize the SSLContext: " + e.getMessage());
    }
    return sslCtx;
}
Also used : Resource(org.springframework.core.io.Resource) PathMatchingResourcePatternResolver(org.springframework.core.io.support.PathMatchingResourcePatternResolver) SSLException(javax.net.ssl.SSLException) SSLException(javax.net.ssl.SSLException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) InvalidKeySpecException(java.security.spec.InvalidKeySpecException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) NoSuchProviderException(java.security.NoSuchProviderException) SslContext(io.netty.handler.ssl.SslContext)

Example 95 with PathMatchingResourcePatternResolver

use of org.springframework.core.io.support.PathMatchingResourcePatternResolver in project web3sdk by FISCO-BCOS.

the class ChannelConnections method initSMSslContext.

/**
 * init sm ssl context object
 *
 * @return
 * @throws IOException
 */
public SslContext initSMSslContext() throws IOException, InvalidKeySpecException, CertificateException, NoSuchAlgorithmException, NoSuchProviderException {
    // The client's SM SSL certificate exists
    if ((getGmCaCert() != null && getGmCaCert().exists()) || (getGmEnSslCert() != null && getGmEnSslCert().exists()) || (getGmEnSslKey() != null && getGmEnSslKey().exists()) || (getGmSslCert() != null && getGmSslCert().exists()) || (getGmSslKey() != null && getGmSslKey().exists())) {
        return SMSslClientContextFactory.build(getGmCaCert().getInputStream(), getGmEnSslCert().getInputStream(), getGmEnSslKey().getInputStream(), getGmSslCert().getInputStream(), getGmSslKey().getInputStream());
    }
    PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
    Resource gmCaResource = resolver.getResource(GM_CA_CERT);
    Resource gmSslResource = resolver.getResource(GM_SSL_CERT);
    Resource gmSslKeyResource = resolver.getResource(GM_SSL_KEY);
    Resource gmEnSslResource = resolver.getResource(GM_EN_SSL_CERT);
    Resource gmEnSslKeyResource = resolver.getResource(GM_EN_SSL_KEY);
    // The client's SM SSL default certificate exists
    if (gmCaResource.exists() && gmSslResource.exists() && gmSslKeyResource.exists() && gmEnSslKeyResource.exists() && gmEnSslResource.exists()) {
        logger.info(" build SM ssl context by default certificate configuration ");
        return SMSslClientContextFactory.build(gmCaResource.getInputStream(), gmEnSslResource.getInputStream(), gmEnSslKeyResource.getInputStream(), gmSslResource.getInputStream(), gmSslKeyResource.getInputStream());
    }
    logger.info(" there is no SM ssl certificate configuration ");
    // The client's SM SSL certificate not exists or not configured correctly
    return initSslContext();
}
Also used : Resource(org.springframework.core.io.Resource) PathMatchingResourcePatternResolver(org.springframework.core.io.support.PathMatchingResourcePatternResolver)

Aggregations

PathMatchingResourcePatternResolver (org.springframework.core.io.support.PathMatchingResourcePatternResolver)135 Resource (org.springframework.core.io.Resource)86 ResourcePatternResolver (org.springframework.core.io.support.ResourcePatternResolver)53 IOException (java.io.IOException)37 Bean (org.springframework.context.annotation.Bean)30 SqlSessionFactoryBean (org.mybatis.spring.SqlSessionFactoryBean)29 ArrayList (java.util.ArrayList)16 Properties (java.util.Properties)14 ClassPathResource (org.springframework.core.io.ClassPathResource)14 File (java.io.File)13 InputStream (java.io.InputStream)12 Test (org.junit.jupiter.api.Test)12 JndiDataSourceLookup (org.springframework.jdbc.datasource.lookup.JndiDataSourceLookup)11 PageHelper (com.github.pagehelper.PageHelper)10 PersistenceUnitInfo (jakarta.persistence.spi.PersistenceUnitInfo)10 Primary (org.springframework.context.annotation.Primary)9 CachingMetadataReaderFactory (org.springframework.core.type.classreading.CachingMetadataReaderFactory)9 MetadataReader (org.springframework.core.type.classreading.MetadataReader)9 MetadataReaderFactory (org.springframework.core.type.classreading.MetadataReaderFactory)7 URL (java.net.URL)6