mirror of
https://gitee.com/chinabugotech/hutool.git
synced 2025-07-21 15:09:48 +08:00
add session
This commit is contained in:
@@ -21,6 +21,7 @@ import org.dromara.hutool.core.map.WeakConcurrentMap;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.WeakHashMap;
|
||||
import java.util.concurrent.locks.Lock;
|
||||
@@ -28,6 +29,7 @@ import java.util.concurrent.locks.ReadWriteLock;
|
||||
import java.util.concurrent.locks.ReentrantLock;
|
||||
import java.util.concurrent.locks.ReentrantReadWriteLock;
|
||||
import java.util.function.Predicate;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* 简单缓存,无超时实现,默认使用{@link WeakConcurrentMap}实现缓存自动清理
|
||||
@@ -212,4 +214,13 @@ public class SimpleCache<K, V> implements Iterable<Map.Entry<K, V>>, Serializabl
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取所有键
|
||||
*
|
||||
* @return 所有键
|
||||
*/
|
||||
public List<K> keys(){
|
||||
return this.rawMap.keySet().stream().map(Mutable::get).collect(Collectors.toList());
|
||||
}
|
||||
}
|
||||
|
@@ -14,6 +14,7 @@ package org.dromara.hutool.core.spi;
|
||||
|
||||
import org.dromara.hutool.core.cache.SimpleCache;
|
||||
import org.dromara.hutool.core.classloader.ClassLoaderUtil;
|
||||
import org.dromara.hutool.core.collection.ListUtil;
|
||||
import org.dromara.hutool.core.io.IORuntimeException;
|
||||
import org.dromara.hutool.core.io.resource.MultiResource;
|
||||
import org.dromara.hutool.core.io.resource.Resource;
|
||||
@@ -24,10 +25,7 @@ import org.dromara.hutool.core.text.StrUtil;
|
||||
import java.io.BufferedReader;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* 列表类型的服务加载器,用于替换JDK提供的{@link java.util.ServiceLoader}<br>
|
||||
@@ -136,6 +134,11 @@ public class ListServiceLoader<S> extends AbsServiceLoader<S> {
|
||||
return this.serviceNames.size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public List<String> getServiceNames(){
|
||||
return ListUtil.view(this.serviceNames);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定服务的实现类
|
||||
*
|
||||
@@ -148,7 +151,12 @@ public class ListServiceLoader<S> extends AbsServiceLoader<S> {
|
||||
return null;
|
||||
}
|
||||
|
||||
return ClassLoaderUtil.loadClass(serviceClassName);
|
||||
return getServiceClass(serviceClassName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<S> getServiceClass(final String serviceName) {
|
||||
return ClassLoaderUtil.loadClass(serviceName);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -162,7 +170,12 @@ public class ListServiceLoader<S> extends AbsServiceLoader<S> {
|
||||
if (null == serviceClassName) {
|
||||
return null;
|
||||
}
|
||||
return getServiceByName(serviceClassName);
|
||||
return getService(serviceClassName);
|
||||
}
|
||||
|
||||
@Override
|
||||
public S getService(final String serviceName) {
|
||||
return this.serviceCache.get(serviceName, () -> createService(serviceName));
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -177,7 +190,7 @@ public class ListServiceLoader<S> extends AbsServiceLoader<S> {
|
||||
|
||||
@Override
|
||||
public S next() {
|
||||
return getServiceByName(nameIter.next());
|
||||
return getService(nameIter.next());
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -223,7 +236,7 @@ public class ListServiceLoader<S> extends AbsServiceLoader<S> {
|
||||
line = line.substring(0, ci);
|
||||
}
|
||||
line = StrUtil.trim(line);
|
||||
if (line.length() > 0) {
|
||||
if (!line.isEmpty()) {
|
||||
// 检查行
|
||||
checkLine(resource, lineNo, line);
|
||||
// 不覆盖模式
|
||||
@@ -272,16 +285,6 @@ public class ListServiceLoader<S> extends AbsServiceLoader<S> {
|
||||
throw new SPIException(this.serviceClass + ":" + resource.getUrl() + ":" + lineNo + ": " + msg);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定class名对应的服务,使用缓存,多次调用只返回相同的服务对象
|
||||
*
|
||||
* @param serviceClassName 服务名称
|
||||
* @return 服务对象
|
||||
*/
|
||||
private S getServiceByName(final String serviceClassName) {
|
||||
return this.serviceCache.get(serviceClassName, () -> createService(serviceClassName));
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建服务,无缓存
|
||||
*
|
||||
|
@@ -14,6 +14,7 @@ package org.dromara.hutool.core.spi;
|
||||
|
||||
import org.dromara.hutool.core.cache.SimpleCache;
|
||||
import org.dromara.hutool.core.classloader.ClassLoaderUtil;
|
||||
import org.dromara.hutool.core.collection.ListUtil;
|
||||
import org.dromara.hutool.core.io.resource.ResourceUtil;
|
||||
import org.dromara.hutool.core.reflect.ConstructorUtil;
|
||||
import org.dromara.hutool.core.text.StrUtil;
|
||||
@@ -21,6 +22,7 @@ import org.dromara.hutool.core.text.StrUtil;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
|
||||
/**
|
||||
@@ -128,12 +130,12 @@ public class MapServiceLoader<S> extends AbsServiceLoader<S> {
|
||||
return this.serviceProperties.size();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定服务的实现类
|
||||
*
|
||||
* @param serviceName 服务名称
|
||||
* @return 服务名称对应的实现类
|
||||
*/
|
||||
@Override
|
||||
public List<String> getServiceNames() {
|
||||
return ListUtil.view(this.serviceCache.keys());
|
||||
}
|
||||
|
||||
@Override
|
||||
public Class<S> getServiceClass(final String serviceName) {
|
||||
final String serviceClassName = this.serviceProperties.getProperty(serviceName);
|
||||
if (StrUtil.isBlank(serviceClassName)) {
|
||||
@@ -143,12 +145,7 @@ public class MapServiceLoader<S> extends AbsServiceLoader<S> {
|
||||
return ClassLoaderUtil.loadClass(serviceClassName);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取指定名称对应的服务,使用缓存,多次调用只返回相同的服务对象
|
||||
*
|
||||
* @param serviceName 服务名称
|
||||
* @return 服务对象
|
||||
*/
|
||||
@Override
|
||||
public S getService(final String serviceName) {
|
||||
return this.serviceCache.get(serviceName, () -> createService(serviceName));
|
||||
}
|
||||
|
@@ -12,6 +12,8 @@
|
||||
|
||||
package org.dromara.hutool.core.spi;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* SPI服务加载接口<br>
|
||||
* 用户实现此接口用于制定不同的服务加载方式
|
||||
@@ -19,7 +21,7 @@ package org.dromara.hutool.core.spi;
|
||||
* @param <S> 服务对象类型
|
||||
* @author looly
|
||||
*/
|
||||
public interface ServiceLoader<S> extends Iterable<S>{
|
||||
public interface ServiceLoader<S> extends Iterable<S> {
|
||||
|
||||
/**
|
||||
* 加载服务
|
||||
@@ -32,4 +34,27 @@ public interface ServiceLoader<S> extends Iterable<S>{
|
||||
* @return 总数
|
||||
*/
|
||||
int size();
|
||||
|
||||
/**
|
||||
* 获取服务名称列表
|
||||
*
|
||||
* @return 服务名称列表
|
||||
*/
|
||||
List<String> getServiceNames();
|
||||
|
||||
/**
|
||||
* 获取指定服务的实现类
|
||||
*
|
||||
* @param serviceName 服务名称
|
||||
* @return 服务名称对应的实现类
|
||||
*/
|
||||
Class<S> getServiceClass(String serviceName);
|
||||
|
||||
/**
|
||||
* 获取指定名称对应的服务
|
||||
*
|
||||
* @param serviceName 服务名称
|
||||
* @return 服务对象
|
||||
*/
|
||||
S getService(String serviceName);
|
||||
}
|
||||
|
Reference in New Issue
Block a user