This commit is contained in:
Redkale
2016-09-14 09:36:33 +08:00
parent 2c75c04b7e
commit a81c9f7d21
4 changed files with 36 additions and 16 deletions

View File

@@ -175,6 +175,7 @@
<!--
当Server为HTTP协议时ResourceServlet才有效. 默认存在一个有默认属性的resource-servlet节点
webroot: web资源的根目录, 默认取server节点中的root值
servlet: 静态资源HttpServlet的实现默认使用HttpResourceServlet
index : 启始页默认值index.html
-->
<resource-servlet webroot="root" index="index.html">

View File

@@ -23,13 +23,15 @@ import org.redkale.watch.*;
*
* @author zhangjx
*/
public final class HttpPrepareServlet extends PrepareServlet<String, HttpContext, HttpRequest, HttpResponse, HttpServlet> {
public class HttpPrepareServlet extends PrepareServlet<String, HttpContext, HttpRequest, HttpResponse, HttpServlet> {
private SimpleEntry<Predicate<String>, HttpServlet>[] regArray = new SimpleEntry[0];
protected final Logger logger = Logger.getLogger(this.getClass().getSimpleName());
private HttpServlet resourceHttpServlet = new HttpResourceServlet();
protected SimpleEntry<Predicate<String>, HttpServlet>[] regArray = new SimpleEntry[0];
private final Map<String, Class> allMapStrings = new HashMap<>();
protected HttpServlet resourceHttpServlet = new HttpResourceServlet();
protected final Map<String, Class> allMapStrings = new HashMap<>();
@Override
public void init(HttpContext context, AnyValue config) {
@@ -47,10 +49,16 @@ public final class HttpPrepareServlet extends PrepareServlet<String, HttpContext
watch.inject(s);
});
}
AnyValue resConfig = null;
if (config != null) {
AnyValue ssConfig = config.getAnyValue("servlets");
AnyValue resConfig = null;
if (ssConfig != null) {
String resServlet = config.getValue("servlet", HttpResourceServlet.class.getName());
try {
this.resourceHttpServlet = (HttpServlet) Class.forName(resServlet).newInstance();
} catch (Exception e) {
logger.log(Level.WARNING, "init HttpResourceSerlvet(" + resServlet + ") error", e);
}
resConfig = ssConfig.getAnyValue("resource-servlet");
if ((resConfig instanceof DefaultAnyValue) && resConfig.getValue("webroot") == null) {
((DefaultAnyValue) resConfig).addValue("webroot", config.getValue("root"));
@@ -70,8 +78,9 @@ public final class HttpPrepareServlet extends PrepareServlet<String, HttpContext
}
resConfig = dresConfig;
}
this.resourceHttpServlet.init(context, resConfig);
}
if (this.resourceHttpServlet == null) this.resourceHttpServlet = new HttpResourceServlet();
this.resourceHttpServlet.init(context, resConfig);
}
@Override

View File

@@ -27,7 +27,7 @@ import org.redkale.util.AnyValue;
*/
public final class HttpResourceServlet extends HttpServlet {
private static final Logger logger = Logger.getLogger(HttpResourceServlet.class.getSimpleName());
protected static final Logger logger = Logger.getLogger(HttpResourceServlet.class.getSimpleName());
protected class WatchThread extends Thread {
@@ -63,7 +63,12 @@ public final class HttpResourceServlet extends HttpServlet {
} else if (event.kind() == ENTRY_MODIFY) {
FileEntry en = files.get(uri);
if (en != null) {
Thread.sleep(5000L); //等待update file完毕
long d; //等待update file完毕
for (;;) {
d = en.file.lastModified();
Thread.sleep(2000L);
if (d == en.file.lastModified()) break;
}
en.update();
}
}
@@ -81,16 +86,16 @@ public final class HttpResourceServlet extends HttpServlet {
protected final boolean finest = logger.isLoggable(Level.FINEST);
protected final LongAdder cachedLength = new LongAdder();
//缓存总大小, 默认0
protected long cachelimit = 0 * 1024 * 1024L;
//最大可缓存的文件大小, 大于该值的文件将不被缓存
protected long cachelengthmax = 1 * 1024 * 1024;
//是否监控缓存文件的变化, 默认不监控
protected boolean watch = false;
protected File root = new File("./root/");
protected String indexHtml = "index.html";
@@ -162,7 +167,7 @@ public final class HttpResourceServlet extends HttpServlet {
}
}
private static long parseLenth(String value, long defValue) {
protected static long parseLenth(String value, long defValue) {
if (value == null) return defValue;
value = value.toUpperCase().replace("B", "");
if (value.endsWith("G")) return Long.decode(value.replace("G", "")) * 1024 * 1024 * 1024;
@@ -204,7 +209,7 @@ public final class HttpResourceServlet extends HttpServlet {
}
}
private FileEntry createFileEntry(String uri) {
protected FileEntry createFileEntry(String uri) {
File rfile = new File(root, uri);
File file = rfile;
if (file.isDirectory()) file = new File(rfile, this.indexHtml);
@@ -221,9 +226,9 @@ public final class HttpResourceServlet extends HttpServlet {
return en;
}
private static final class FileEntry {
protected static class FileEntry {
final File file;
protected final File file;
private final HttpResourceServlet servlet;

View File

@@ -129,7 +129,12 @@ public class JDBCPoolSource {
try {
while (!this.isInterrupted()) {
final WatchKey key = watcher.take();
Thread.sleep(3000); //防止文件正在更新过程中去读取
long d; //防止文件正在更新过程中去读取
for (;;) {
d = f.lastModified();
Thread.sleep(2000L);
if (d == f.lastModified()) break;
}
final Map<String, Properties> m = loadProperties(new FileInputStream(file));
key.pollEvents().stream().forEach((event) -> {
if (event.kind() != ENTRY_MODIFY) return;