cn.sylinx.hbatis.plugin.sqlres.SqlResourcePreloadPlugin Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of hbatis-core Show documentation
Show all versions of hbatis-core Show documentation
hbatis is a simple orm framework
The newest version!
package cn.sylinx.hbatis.plugin.sqlres;
import java.util.ArrayList;
import java.util.List;
import cn.sylinx.hbatis.ext.res.ClasspathSqlResourceManager;
import cn.sylinx.hbatis.io.ClasspathResourceScanner;
import cn.sylinx.hbatis.kit.StrKit;
import cn.sylinx.hbatis.log.GLog;
import cn.sylinx.hbatis.plugin.IPlugin;
import cn.sylinx.hbatis.plugin.debug.DebugWrapper;
public class SqlResourcePreloadPlugin implements IPlugin {
private final static String defaultPath = "sql";
private List resourcePaths;
public SqlResourcePreloadPlugin() {
this(defaultPath);
}
public SqlResourcePreloadPlugin(String path) {
resourcePaths = new ArrayList<>();
resourcePaths.add(path);
}
public SqlResourcePreloadPlugin(List resourcePaths) {
if (resourcePaths != null) {
this.resourcePaths = resourcePaths;
}
}
@Override
public boolean start(Object... objects) {
load();
return true;
}
@Override
public boolean stop() {
ClasspathSqlResourceManager.clear();
return true;
}
private void load() {
if (DebugWrapper.ME.isDebug()) {
// 如果是debug模式,则无需预加载到内存
return;
}
if (resourcePaths == null) {
return;
}
resourcePaths.stream().filter(StrKit::isNotBlank).forEach(item -> {
boolean bl = load(item);
if (bl) {
GLog.info("resourcesPath:{} load ok..", item);
} else {
GLog.info("resourcesPath:{} load error..", item);
}
});
}
private boolean load(String resourcesPath) {
ClasspathResourceScanner crs = new ClasspathResourceScanner(resourcesPath, ".sql");
List rlist = null;
try {
rlist = crs.getResourceNameList();
} catch (Exception e) {
GLog.error("resource not found ", e);
return false;
}
if (rlist == null || rlist.isEmpty()) {
return false;
}
for (String r : rlist) {
ClasspathSqlResourceManager.loadAndGet(r);
GLog.info("resource:{} loaded ", r);
}
return true;
}
}