use of org.springframework.core.io.support.ResourcePatternResolver in project littlefisher-system by littlefishercoder.
the class PackageUtil method scanTypePackage.
/**
* 扫描获取指定包路径所有类
*
* @param typePackage 扫描类包路径
* @return Set<Class>
*/
public static Set<Class> scanTypePackage(String typePackage) {
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
MetadataReaderFactory metadataReaderFactory = new CachingMetadataReaderFactory(resolver);
String pkg = ClassUtils.convertClassNameToResourcePath(typePackage) + ".class";
/*
* 将加载多个绝对匹配的所有Resource
* 将首先通过ClassLoader.getResource("META-INF")加载非模式路径部分,然后进行遍历模式匹配,排除重复包路径
*/
try {
Set<Class> set = Sets.newHashSet();
Resource[] resources = resolver.getResources(pkg);
if (ArrayUtils.isNotEmpty(resources)) {
MetadataReader metadataReader;
for (Resource resource : resources) {
if (resource.isReadable()) {
metadataReader = metadataReaderFactory.getMetadataReader(resource);
set.add(Class.forName(metadataReader.getClassMetadata().getClassName()));
}
}
}
return set;
} catch (Exception e) {
throw new BaseAppException("CORE-000007", null, e);
}
}
use of org.springframework.core.io.support.ResourcePatternResolver in project leopard by tanhaichao.
the class ConfigClasspathImpl method find.
@Override
public InputStream find() throws IOException {
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
Resource[] resources;
try {
resources = resolver.getResources("classpath*:/dev/dns.properties");
} catch (IOException e) {
e.printStackTrace();
throw new FileNotFoundException("classpath*:/dev/dns.properties");
}
for (Resource resource : resources) {
try {
InputStream is = resource.getInputStream();
return is;
} catch (IOException e) {
e.printStackTrace();
throw new RuntimeException(e.getMessage(), e);
}
}
throw new FileNotFoundException("classpath*:/dev/dns.properties");
}
use of org.springframework.core.io.support.ResourcePatternResolver in project jaffa-framework by jaffa-projects.
the class ConfigApiHelper method unregisterResources.
/**
* Unregister configuration files in IManager implementations, based on the source of the method call
* @param file The configuration file to be registered or unregistered
* @return Success or failure of the operation
*/
public static boolean unregisterResources(File file, FileContentsHelper fileContents) {
boolean isSuccess = true;
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
for (IManager manager : ManagerRepositoryService.getInstance().getManagerMap().values()) {
Resource resource = getMetaInfResource(file, resolver, manager);
try {
if (resource.getFile().exists()) {
manager.unregisterResource(resource, fileContents.getContextSalience(), fileContents.getVariationSalience());
log.debug(resource.getFilename() + " was successfully unregistered from " + manager);
}
} catch (Exception e) {
isSuccess = false;
log.warn("The resource " + resource.getFilename() + " failed to unregister", e);
}
}
return isSuccess;
}
use of org.springframework.core.io.support.ResourcePatternResolver in project web3sdk by FISCO-BCOS.
the class ChannelConnections method startListen.
public void startListen(Integer port) {
if (running) {
logger.debug("服务已启动");
return;
}
logger.debug("初始化connections listen");
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();
final ChannelConnections selfService = this;
final ThreadPoolTaskExecutor selfThreadPool = threadPool;
try {
serverBootstrap.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).option(ChannelOption.SO_BACKLOG, 100).handler(new LoggingHandler(LogLevel.INFO)).childHandler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
KeyStore ks = KeyStore.getInstance("JKS");
ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
Resource keystoreResource = resolver.getResource(getClientKeystorePath());
Resource caResource = resolver.getResource(getCaCertPath());
ks.load(keystoreResource.getInputStream(), getKeystorePassWord().toCharArray());
/*
* 每次连接使用新的handler
* 连接信息从socketChannel中获取
*/
ChannelHandler handler = new ChannelHandler();
handler.setConnections(selfService);
handler.setIsServer(true);
handler.setThreadPool(selfThreadPool);
SslContext sslCtx = SslContextBuilder.forServer((PrivateKey) ks.getKey("client", getClientCertPassWord().toCharArray()), (X509Certificate) ks.getCertificate("client")).trustManager(caResource.getFile()).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);
}
});
ChannelFuture future = serverBootstrap.bind(port);
future.get();
running = true;
} catch (Exception e) {
logger.error("系统错误", e);
}
}
use of org.springframework.core.io.support.ResourcePatternResolver in project jeesuite-libs by vakinge.
the class TopicConsumerSpringProvider method scanAndRegisterAnnotationTopics.
private void scanAndRegisterAnnotationTopics(String[] scanBasePackages) {
String RESOURCE_PATTERN = "/**/*.class";
ResourcePatternResolver resourcePatternResolver = new PathMatchingResourcePatternResolver();
for (String scanBasePackage : scanBasePackages) {
logger.info(">>begin scan package [{}] with Annotation[ConsumerHandler] MessageHanlder ", 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(ConsumerHandler.class)) {
ConsumerHandler annotation = clazz.getAnnotation(ConsumerHandler.class);
MessageHandler hander = (MessageHandler) context.getBean(clazz);
if (!topicHandlers.containsKey(annotation.topic())) {
topicHandlers.put(annotation.topic(), hander);
logger.info("register new MessageHandler:{}-{}", annotation.topic(), clazz.getName());
}
}
}
}
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);
}
}
}
Aggregations