use of io.github.ihongs.Core in project HongsCORE by ihongs.
the class ActionDriver method destroy.
/**
* 公共销毁
*/
@Override
public void destroy() {
if (!FIRST) {
return;
}
long time = System.currentTimeMillis() - Core.STARTS_TIME;
Core core = Core.GLOBAL_CORE;
CoreLogger.info("Action server is stopped" + "\r\n\tSERVER_ID : {}" + "\r\n\tRuntime : {}" + "\r\n\tObjects : {}", Core.SERVER_ID, Syno.humanTime(time), core.toString());
if (!SETUP) {
return;
}
try {
Core.GLOBAL_CORE.reset();
} catch (Throwable e) {
CoreLogger.error(e);
}
}
use of io.github.ihongs.Core in project HongsCORE by ihongs.
the class ActionDriver method doFinish.
private void doFinish(Core core, ActionHelper hlpr, HttpServletRequest req) {
try {
if (4 == (4 & Core.DEBUG)) {
/**
* 提取必要的客户相关标识
* 以便判断用户和模拟登录
*/
HttpSession ses;
Object uid;
String mem;
String tim;
req = hlpr.getRequest();
ses = req.getSession(false);
tim = Syno.humanTime(System.currentTimeMillis() - Core.ACTION_TIME.get());
uid = hlpr.getSessibute(Cnst.UID_SES);
if (uid != null) {
mem = "uid=" + uid.toString();
} else if (ses != null) {
mem = "sid=" + ses.getId();
} else {
mem = "-";
}
StringBuilder sb = new StringBuilder("...");
sb.append("\r\n\tACTION_NAME : ").append(Core.ACTION_NAME.get()).append("\r\n\tACTION_TIME : ").append(Core.ACTION_TIME.get()).append("\r\n\tACTION_LANG : ").append(Core.ACTION_LANG.get()).append("\r\n\tACTION_ZONE : ").append(Core.ACTION_ZONE.get()).append("\r\n\tThread : ").append(Thread.currentThread().getName()).append("\r\n\tMethod : ").append(req.getMethod()).append("\r\n\tMember : ").append(mem).append("\r\n\tRuntime : ").append(tim).append("\r\n\tObjects : ").append(core.toString());
if (Synt.declare(System.getProperty("show.request"), false)) {
Map rd = null;
try {
rd = hlpr.getRequestData();
} catch (RuntimeException ex) {
CoreLogger.debug(ex.getMessage());
}
if (rd != null && !rd.isEmpty()) {
sb.append("\r\n\tRequest : ").append(Syno.indent(Dawn.toString(rd)).substring(1));
}
}
if (Synt.declare(System.getProperty("show.results"), false)) {
Map xd = hlpr.getResponseData();
if (xd == null) {
xd = (Map) req.getAttribute(Cnst.RESPON_ATTR);
}
if (xd != null && !xd.isEmpty()) {
sb.append("\r\n\tResults : ").append(Syno.indent(Dawn.toString(xd)).substring(1));
}
}
if (Synt.declare(System.getProperty("show.session"), false) && ses != null) {
Map map = new HashMap();
Enumeration<String> nms = ses.getAttributeNames();
while (nms.hasMoreElements()) {
String nme = nms.nextElement();
map.put(nme, ses.getAttribute(nme));
}
if (!map.isEmpty()) {
sb.append("\r\n\tSession : ").append(Syno.indent(Dawn.toString(map)).substring(1));
}
}
if (Synt.declare(System.getProperty("show.context"), false)) {
Map map = new HashMap();
Enumeration<String> nms = req.getAttributeNames();
while (nms.hasMoreElements()) {
String nme = nms.nextElement();
map.put(nme, req.getAttribute(nme));
}
if (!map.isEmpty()) {
sb.append("\r\n\tContext : ").append(Syno.indent(Dawn.toString(map)).substring(1));
}
}
if (Synt.declare(System.getProperty("show.headers"), false)) {
Map map = new HashMap();
Enumeration<String> nms = req.getHeaderNames();
while (nms.hasMoreElements()) {
String nme = nms.nextElement();
map.put(nme, req.getHeader(nme));
}
if (!map.isEmpty()) {
sb.append("\r\n\tHeaders : ").append(Syno.indent(Dawn.toString(map)).substring(1));
}
}
if (Synt.declare(System.getProperty("show.cookies"), false)) {
Map map = new HashMap();
Cookie[] cks = req.getCookies();
for (Cookie cke : cks) {
map.put(cke.getName(), cke.getValue());
}
if (!map.isEmpty()) {
sb.append("\r\n\tCookies : ").append(Syno.indent(Dawn.toString(map)).substring(1));
}
}
CoreLogger.debug(sb.toString());
}
// 删除上传的临时文件
Map<String, List<Part>> ud = Synt.asMap(hlpr.getAttribute(Cnst.UPLOAD_ATTR));
if (ud != null) {
for (List<Part> pa : ud.values()) {
for (Part pr : pa) {
try {
pr.delete();
} catch (IOException ex) {
CoreLogger.error(ex);
}
}
}
}
} finally {
// 销毁此周期内的对象
try {
core.reset();
Core.THREAD_CORE.remove();
req.removeAttribute(Core.class.getName());
} catch (Error e) {
CoreLogger.error(e);
} catch (Exception e) {
CoreLogger.error(e);
}
}
}
use of io.github.ihongs.Core in project HongsCORE by ihongs.
the class ConfAction method service.
/**
* 服务方法
* 判断配置和消息有没有生成, 如果没有则生成; 消息按客户语言存放
* @param req
* @param rsp
* @throws java.io.IOException
* @throws javax.servlet.ServletException
*/
@Override
public void service(HttpServletRequest req, HttpServletResponse rsp) throws ServletException, IOException {
Core core = ActionDriver.getActualCore(req);
ActionHelper helper = core.got(ActionHelper.class);
String name = req.getPathInfo();
if (name == null || name.length() == 0) {
helper.error(400, "Path info required");
return;
}
int p = name.lastIndexOf('.');
if (p < 0) {
helper.error(400, "File type required");
return;
}
String type = name.substring(1 + p);
name = name.substring(1, p);
if (!"js".equals(type) && !"json".equals(type)) {
helper.error(400, "Wrong file type: " + type);
return;
}
/**
* 如果指定配置的数据并没有改变
* 则直接返回 304 Not modified
*/
long m = helper.getRequest().getDateHeader("If-Modified-Since");
if (ConfAction.MTIMES.containsKey(name) && MTIMES.get(name) <= m) {
helper.getResponse().setStatus(HttpServletResponse.SC_NOT_MODIFIED);
return;
}
/**
* 如果没有配置
* 则调用工厂方法构造 JS 代码
*/
String s;
if (!ConfAction.CACHES.containsKey(name)) {
try {
s = this.makeConf(name);
} catch (HongsExemption ex) {
helper.error(404, ex.getMessage());
return;
}
// HTTP 时间精确到秒
m = System.currentTimeMillis() / 1000L * 1000L;
ConfAction.CACHES.put(name, s);
ConfAction.MTIMES.put(name, m);
} else {
s = ConfAction.CACHES.get(name);
m = ConfAction.MTIMES.get(name);
}
// 标明修改时间
helper.getResponse().setDateHeader("Last-Modified", m);
// 输出配置信息
if ("json".equals(type)) {
helper.write("application/json", s);
} else {
String c = req.getParameter("callback");
if (c != null && !c.isEmpty()) {
if (!c.matches("^[a-zA-Z_\\$][a-zA-Z0-9_]*$")) {
helper.error(400, "Illegal callback function name!");
return;
}
helper.write("text/javascript", c + "(" + s + ");");
} else {
c = "self.HsCONF=Object.assign(self.HsCONF||{}";
helper.write("text/javascript", c + "," + s + ");");
}
}
}
use of io.github.ihongs.Core in project HongsCORE by ihongs.
the class NaviMap method getInstance.
public static NaviMap getInstance(String name) throws HongsException {
Core core = Core.getInstance();
String code = NaviMap.class.getName() + ":" + name;
NaviMap inst = (NaviMap) core.get(code);
if (inst == null) {
inst = new NaviMap(name);
core.set(code, inst);
}
return inst;
}
Aggregations