use of io.github.ihongs.cmdlet.anno.Cmdlet in project HongsCORE by ihongs.
the class LuceneCmdlet method create.
@Cmdlet("create")
public static void create(String[] args) throws HongsException {
Map opts = CmdletHelper.getOpts(args, new String[] { "conf=s", "form=s", "!A" });
args = (String[]) opts.get("");
String conf = Synt.asString(opts.remove("conf"));
String name = Synt.asString(opts.remove("name"));
LuceneRecord so = LuceneRecord.getInstance(conf, name);
String id;
Map rd;
try {
so.begin();
for (String rt : args) {
rd = data(rt);
id = (String) rd.get(Cnst.ID_KEY);
if (id != null && id.length() > 0) {
so.set(id, rd);
} else {
so.add(rd);
}
}
so.commit();
} catch (HongsException ex) {
so.revert();
throw ex;
} finally {
so.close();
}
}
use of io.github.ihongs.cmdlet.anno.Cmdlet in project HongsCORE by ihongs.
the class ServerCmdlet method start.
@Cmdlet("start")
public static void start(String[] args) throws HongsException {
int port = args.length > 0 ? Integer.parseInt(args[0]) : 8080;
String conf = Core.CORE_PATH + File.separator + "web.xml";
if (!(new File(conf)).exists()) {
conf = Core.CONF_PATH + File.separator + "web.xml";
}
String serd = Core.DATA_PATH + File.separator + "server";
File ppid = new File(serd + File.separator + port + ".pid");
File ppcd = new File(serd);
// 检查进程
if (ppid.exists() != false) {
System.err.println("ERROR: The server has not exit, or did not exit normally.");
System.exit(126);
return;
}
if (ppcd.exists() == false) {
ppcd.mkdirs();
}
try (FileWriter fw = new FileWriter(ppid, true)) {
fw.write(ManagementFactory.getRuntimeMXBean().getName().split("@", 2)[0]);
fw.close();
} catch (IOException e) {
throw new HongsException(e);
}
CoreConfig cc = CoreConfig.getInstance("defines");
/**
* 取消名称
* 日志中将记录各独立的线程名
* 以便于区分不同的非动作任务
*/
Core.ACTION_NAME.remove();
Server server;
WebAppContext webapp;
ServerConnector conner;
Connector[] connes;
server = new Server(new QueuedThreadPool(cc.getProperty("jetty.pool.max.threads", 254), cc.getProperty("jetty.pool.min.threads", 010), cc.getProperty("jetty.pool.idle.timeout", 30000)));
webapp = new WebAppContext();
conner = new ServerConnector(server);
connes = new Connector[] { conner };
conner.setPort(port);
conner.setHost(null);
conner.setIdleTimeout(cc.getProperty("jetty.conn.idle.timeout", 30000L));
conner.setAcceptQueueSize(cc.getProperty("jetty.conn.accept.queue.size", 254));
conner.setAcceptedSendBufferSize(cc.getProperty("jetty.conn.accept.sndbuf.size", -1));
conner.setAcceptedReceiveBufferSize(cc.getProperty("jetty.conn.accept.rcvbuf.size", -1));
server.setConnectors(connes);
webapp.setDescriptor(conf);
webapp.setContextPath(Core.SERV_PATH);
webapp.setResourceBase(Core.BASE_PATH);
webapp.setTempDirectory(new File(Core.DATA_PATH + "/server/temp"));
webapp.setPersistTempDirectory(true);
webapp.setParentLoaderPriority(true);
webapp.setThrowUnavailableOnStartupException(true);
// webapp.setMaxFormKeys(cc.getProperty("jetty.serv.max.form.keys", 10000));
// webapp.setMaxFormContentSize(cc.getProperty("jetty.serv.max.form.size", 200000));
server.setHandler(webapp);
String x;
// 默认微调
x = org.eclipse.jetty.servlet.DefaultServlet.CONTEXT_INIT;
webapp.setInitParameter(x + "useFileMappedBuffer", "false");
webapp.setInitParameter(x + "dirAllowed", "false");
/**
* 初始设置
* 光能外部配置参数还不够方便
* 可能需要替换 JSP 解析器或 Session 容器
* 可以设置 jetty.init 来注入 Initer 对象
*/
x = cc.getProperty("jetty.init");
if (null != x) {
String[] a = x.split(";");
for (String n : a) {
n = n.trim();
if (n.isEmpty()) {
continue;
}
try {
((Initer) Class.forName(n).getDeclaredConstructor().newInstance()).init(webapp);
} catch (ClassNotFoundException e) {
throw new HongsExemption(e);
} catch (NoSuchMethodException e) {
throw new HongsExemption(e);
} catch (InstantiationException e) {
throw new HongsExemption(e);
} catch (IllegalAccessException e) {
throw new HongsExemption(e);
} catch (InvocationTargetException e) {
throw new HongsExemption(e);
}
}
}
// 中止机制
Runtime.getRuntime().addShutdownHook(new Stoper(server, ppid));
// 启动服务
try {
server.start();
if (Core.DEBUG == 0) {
System.err.println("HTTP server is started.");
}
/**
* URL 会话参数等同于对应的 Cookie, 总是小写
* server.start_ 执行之前 web.xml 还没解析
*/
x = webapp.getInitParameter(SessionHandler.__SessionIdPathParameterNameProperty);
if (x == null || x.isEmpty()) {
x = webapp.getServletContext().getSessionCookieConfig().getName();
x = x.toLowerCase();
webapp.getSessionHandler().setSessionIdPathParameterName(x);
}
server.join();
} catch (Exception e) {
throw new HongsException(e);
} catch (Error e) {
throw new HongsExemption(e);
}
}
use of io.github.ihongs.cmdlet.anno.Cmdlet in project HongsCORE by ihongs.
the class SystemCmdlet method exec.
/**
* 维护命令
* @param args
* @throws HongsException
*/
@Cmdlet("__main__")
public static void exec(String[] args) throws HongsException {
Map<String, Object> opts;
opts = CmdletHelper.getOpts(args, "date:s", "!A");
args = (String[]) opts.remove("");
if (0 == args.length) {
System.err.println("Serve name required!");
return;
}
String fn = args[0];
File fu = new File(fn);
Date dt = new Date();
// 日期参数
if (opts.containsKey("date")) {
dt = getTin((String) opts.get("date"), dt);
}
// 相对且不存在则看作内部目录
if (!fu.isAbsolute() && !fu.exists()) {
fn = Core.CORE_PATH + File.separator + "bin" + File.separator + fn;
fu = new File(fn);
}
// 获取目录下全部待执行的文件
List<File> fxs = new ArrayList();
if (!fu.isFile()) {
File[] fus = fu.listFiles();
for (File fo : fus) {
if (!fo.isFile()) {
continue;
}
String fp = fo.getName();
if (fp.startsWith(".") || fp.startsWith("!") || fp.endsWith("~")) {
continue;
}
fxs.add(fo);
}
Collections.sort(fxs, new Sorter());
} else {
fxs.add(fu);
}
Looker lgr = new Looker();
String act = Core.ACTION_NAME.get();
long now = Core.ACTION_TIME.get();
// 逐个执行
for (File fo : fxs) {
String fp = fo.getPath();
try {
if (fp.endsWith(".cmd.xml")) {
runCmd(dt, fo, lgr);
} else if (fp.endsWith(".sql")) {
runSql(dt, fo);
}
} catch (Exception ex) {
lgr.error(ex);
} catch (Error ex) {
lgr.error(ex);
} finally {
// 放回名称和开始时间
// 避免时间或日志模糊
Core.ACTION_NAME.set(act);
Core.ACTION_TIME.set(now);
}
}
}
use of io.github.ihongs.cmdlet.anno.Cmdlet in project HongsCORE by ihongs.
the class Access method eval.
@Cmdlet("eval")
public static void eval(String[] args) throws HongsException {
Map<String, Object> opts;
opts = CmdletHelper.getOpts(args, "request:s", "context:s", "session:s", "cookies:s", "!A");
args = (String[]) opts.get("");
if (args.length == 0) {
CmdletHelper.ERR.get().println("Usage: ACTION_NAME [--request DATA] [--cookies DATA] [--session DATA] [--context DATA]\r\n\t" + "DATA can be JSON or URL search string.");
return;
}
// 请求参数
Map rep = new HashMap();
rep.put("act", args[0]);
if (opts.containsKey("request")) {
rep.put("request", text((String) opts.get("request")));
}
if (opts.containsKey("cookies")) {
rep.put("cookies", text((String) opts.get("cookies")));
}
if (opts.containsKey("session")) {
rep.put("session", text((String) opts.get("session")));
}
if (opts.containsKey("context")) {
rep.put("context", text((String) opts.get("context")));
}
String req = Dawn.toString(rep, true);
// 动作接口
String url = Core.SERV_HREF + Core.SERV_PATH + "/common/more/call" + Cnst.ACT_EXT;
try {
HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setConnectTimeout(0);
conn.setRequestMethod("POST");
conn.setRequestProperty("Accept", "application/json,text/html,*/*;q=0.8");
conn.setRequestProperty("Content-Type", "application/json");
conn.setRequestProperty("X-Requested-With", CoreConfig.getInstance().getProperty("core.powered.by"));
String ln;
PrintStream ps;
PrintWriter pw;
BufferedReader br;
pw = new PrintWriter(conn.getOutputStream());
pw.print(req);
pw.flush();
pw.close();
br = new BufferedReader(new InputStreamReader(conn.getInputStream()));
ps = CmdletHelper.OUT.get();
while ((ln = br.readLine()) != null) {
ps.print(ln);
}
ps.println();
} catch (UnsupportedEncodingException ex) {
throw new HongsException(ex);
} catch (MalformedURLException ex) {
throw new HongsException(ex);
} catch (IOException ex) {
throw new HongsException(ex);
}
}
use of io.github.ihongs.cmdlet.anno.Cmdlet in project HongsCORE by ihongs.
the class Access method call.
@Cmdlet("call")
public static void call(String[] args) throws HongsException {
Map<String, Object> opts;
opts = CmdletHelper.getOpts(args, "request:s", "context:s", "session:s", "cookies:s", "!A");
args = (String[]) opts.get("");
if (args.length == 0) {
CmdletHelper.ERR.get().println("Usage: ACTION_NAME [--request DATA] [--cookies DATA] [--session DATA] [--context DATA]\r\n\t" + "DATA can be JSON or URL search string.");
return;
}
// 请求参数
ActionHelper helper = new ActionHelper(data((String) opts.get("request")), data((String) opts.get("context")), data((String) opts.get("session")), data((String) opts.get("cookies")));
// 输出管道
PrintStream ps = CmdletHelper.OUT.get();
PrintWriter pw = new PrintWriter(ps);
helper.updateOutput(ps, pw);
// 将新动作助手对象放入全局以便跨层读取
String cn = ActionHelper.class.getName();
Core co = Core.getInstance();
Object ah = co.get(cn);
try {
co.set(cn, helper);
ActionRunner.newInstance(helper, args[0]).doActing();
helper.flush();
ps.println();
} finally {
if (null != ah) {
co.set(cn, ah);
} else {
co.unset(cn);
}
}
}
Aggregations