Commit 3e7b2231 authored by o2null's avatar o2null
Browse files

Merge branch 'cherry-pick-5776d09c' into 'wrdp_java8'

update resource name

See merge request o2oa/o2oa!6350
parents 4a2e3aa8 fc8ba610
No related merge requests found
Showing with 127 additions and 106 deletions
+127 -106
......@@ -20,7 +20,7 @@
"###bindLogin": "是否启用扫描二维码登录,默认值:false###",
"###faceLogin": "是否启用刷脸登录,默认值:false###",
"###password": "注册初始密码,使用()调用脚本生成初始密码,默认为:(var v \\u003d person.getMobile();\\u000a return v.substring(v.length - 6))###",
"###passwordPeriod": "密码过期时间(天),0表示不过期,默认值:0.###",
"###passwordPeriod": "密码过期时间(天),0表示不过期(只对新用户有效),-1表示永不过期(对所有用户有效),默认值:0.###",
"###passwordRegex": "密码校验正则表达式,默认6位以上,包含数字和字母.###",
"###passwordRegexHint": "密码校验不通过的提示信息.###",
"###register": "是否允许用户自注册,disable:不允许,captcha通过验证码注册,code:通过短信注册,默认值:disable###",
......
......@@ -32,7 +32,7 @@ public class ScriptingFactory {
public static final String BINDING_NAME_WORKCONTEXT = "workContext";
public static final String BINDING_NAME_GSON = "gson";
public static final String BINDING_NAME_GSON1 = "gson";
public static final String BINDING_NAME_DATA = "data";
public static final String BINDING_NAME_ORGANIZATION = "organization";
public static final String BINDING_NAME_WEBSERVICESCLIENT = "webservicesClient";
......@@ -58,12 +58,12 @@ public class ScriptingFactory {
public static final String BINDING_NAME_RESOURCES = "java_resources";
public static final String BINDING_NAME_EFFECTIVEPERSON = "java_effectivePerson";
public static final String BINDING_NAME_CUSTOMRESPONSE = "java_customResponse";
public static final String BINDING_NAME_REQUESTTEXT = "java_requestText";
public static final String BINDING_NAME_REQUEST = "java_request";
public static final String BINDING_NAME_PARAMETERS = "java_parameters";
public static final String BINDING_NAME_SERVICE_RESOURCES = "java_resources";
public static final String BINDING_NAME_SERVICE_EFFECTIVEPERSON = "java_effectivePerson";
public static final String BINDING_NAME_SERVICE_CUSTOMRESPONSE = "java_customResponse";
public static final String BINDING_NAME_SERVICE_REQUESTTEXT = "java_requestText";
public static final String BINDING_NAME_SERVICE_REQUEST = "java_request";
public static final String BINDING_NAME_SERVICE_PARAMETERS = "java_parameters";
public static ScriptEngine newScriptEngine() {
return (new ScriptEngineManager()).getEngineByName(Config.SCRIPTING_ENGINE_NAME);
......
......@@ -61,10 +61,17 @@ class ActionEdit extends BaseAction {
private static final long serialVersionUID = 1571810726944802231L;
// static WrapCopier<Wi, Person> copier = WrapCopierFactory.wi(Wi.class, Person.class, null,
// ListTools.toList(JpaObject.FieldsUnmodify, "icon", "iconMdpi", "iconLdpi", "pinyin", "pinyinInitial",
// "password", "passwordExpiredTime", "lastLoginTime", "lastLoginAddress", "lastLoginClient",
// "superior", "controllerList"));
static WrapCopier<Wi, Person> copier = WrapCopierFactory.wi(Wi.class, Person.class, null,
ListTools.toList(JpaObject.FieldsUnmodify, "icon", "iconMdpi", "iconLdpi", "pinyin", "pinyinInitial",
"password", "passwordExpiredTime", "lastLoginTime", "lastLoginAddress", "lastLoginClient",
"superior", "controllerList"));
ListTools.toList(JpaObject.FieldsUnmodify, Person.icon_FIELDNAME, Person.iconMdpi_FIELDNAME,
Person.iconLdpi_FIELDNAME, Person.pinyin_FIELDNAME, Person.pinyinInitial_FIELDNAME,
Person.password_FIELDNAME, Person.passwordExpiredTime_FIELDNAME, Person.lastLoginTime_FIELDNAME,
Person.lastLoginAddress_FIELDNAME, Person.lastLoginClient_FIELDNAME, Person.superior_FIELDNAME,
Person.controllerList_FIELDNAME, Person.ipAddress_FIELDNAME));
}
public static class Wo extends WoId {
......
......@@ -62,7 +62,7 @@ class ActionExecute extends BaseAction {
resources.setOrganization(new Organization(ThisApplication.context()));
resources.setApplications(ThisApplication.context().applications());
resources.setWebservicesClient(new WebservicesClient());
bindings.put(ScriptingFactory.BINDING_NAME_RESOURCES, resources);
bindings.put(ScriptingFactory.BINDING_NAME_SERVICE_RESOURCES, resources);
CacheCategory cacheCategory = new CacheCategory(Agent.class);
CacheKey cacheKey = new CacheKey(ActionExecute.class, agent.getId());
......
......@@ -93,11 +93,11 @@ abstract class BaseAction extends StandardJaxrsAction {
resources.setOrganization(new Organization(ThisApplication.context()));
resources.setWebservicesClient(new WebservicesClient());
resources.setApplications(ThisApplication.context().applications());
bindings.put(ScriptingFactory.BINDING_NAME_RESOURCES, resources);
bindings.put(ScriptingFactory.BINDING_NAME_REQUESTTEXT, gson.toJson(jsonElement));
bindings.put(ScriptingFactory.BINDING_NAME_REQUEST, request);
bindings.put(ScriptingFactory.BINDING_NAME_EFFECTIVEPERSON, effectivePerson);
bindings.put(ScriptingFactory.BINDING_NAME_CUSTOMRESPONSE, customResponse);
bindings.put(ScriptingFactory.BINDING_NAME_SERVICE_RESOURCES, resources);
bindings.put(ScriptingFactory.BINDING_NAME_SERVICE_REQUESTTEXT, gson.toJson(jsonElement));
bindings.put(ScriptingFactory.BINDING_NAME_SERVICE_REQUEST, request);
bindings.put(ScriptingFactory.BINDING_NAME_SERVICE_EFFECTIVEPERSON, effectivePerson);
bindings.put(ScriptingFactory.BINDING_NAME_SERVICE_CUSTOMRESPONSE, customResponse);
}
protected CompiledScript getCompiledScript(CacheCategory cacheCategory, Invoke invoke) throws ScriptException {
......
package com.x.program.center.schedule;
import java.util.*;
import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.stream.Collectors;
......@@ -17,31 +20,30 @@ import javax.persistence.criteria.Root;
import javax.script.Bindings;
import javax.script.CompiledScript;
import javax.script.ScriptContext;
import javax.script.SimpleScriptContext;
import com.google.gson.JsonElement;
import com.x.base.core.project.config.CenterServer;
import com.x.base.core.project.config.Config;
import com.x.base.core.project.connection.ActionResponse;
import com.x.base.core.project.connection.CipherConnectionAction;
import com.x.base.core.project.exception.RunningException;
import com.x.base.core.project.jaxrs.WrapBoolean;
import com.x.base.core.project.tools.ListTools;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.concurrent.BasicThreadFactory;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import com.google.gson.JsonElement;
import com.x.base.core.container.EntityManagerContainer;
import com.x.base.core.container.factory.EntityManagerContainerFactory;
import com.x.base.core.project.cache.Cache.CacheCategory;
import com.x.base.core.project.cache.Cache.CacheKey;
import com.x.base.core.project.cache.CacheManager;
import com.x.base.core.project.config.CenterServer;
import com.x.base.core.project.config.Config;
import com.x.base.core.project.connection.ActionResponse;
import com.x.base.core.project.connection.CipherConnectionAction;
import com.x.base.core.project.logger.Logger;
import com.x.base.core.project.logger.LoggerFactory;
import com.x.base.core.project.script.AbstractResources;
import com.x.base.core.project.script.ScriptFactory;
import com.x.base.core.project.scripting.JsonScriptingExecutor;
import com.x.base.core.project.scripting.ScriptingFactory;
import com.x.base.core.project.tools.CronTools;
import com.x.base.core.project.tools.DateTools;
import com.x.base.core.project.tools.ListTools;
import com.x.base.core.project.webservices.WebservicesClient;
import com.x.organization.core.express.Organization;
import com.x.program.center.Business;
......@@ -51,6 +53,7 @@ import com.x.program.center.core.entity.Agent_;
/**
* 定时代理任务处理
*
* @author sword
*/
public class TriggerAgent extends BaseAction {
......@@ -59,7 +62,8 @@ public class TriggerAgent extends BaseAction {
private static final CopyOnWriteArrayList<String> LOCK = new CopyOnWriteArrayList<>();
private static final ExecutorService executorService = new ScheduledThreadPoolExecutor(Runtime.getRuntime().availableProcessors(),
private static final ExecutorService executorService = new ScheduledThreadPoolExecutor(
Runtime.getRuntime().availableProcessors(),
new BasicThreadFactory.Builder().namingPattern("triggerAgent-pool-%d").daemon(true).build());
@Override
......@@ -71,10 +75,8 @@ public class TriggerAgent extends BaseAction {
Business business = new Business(emc);
list = this.list(business);
}
if(list!=null) {
list.stream().forEach(p -> {
this.trigger(p);
});
if (list != null) {
list.stream().forEach(this::trigger);
}
}
} catch (Exception e) {
......@@ -122,20 +124,18 @@ public class TriggerAgent extends BaseAction {
CriteriaBuilder cb = em.getCriteriaBuilder();
CriteriaQuery<Tuple> cq = cb.createQuery(Tuple.class);
Root<Agent> root = cq.from(Agent.class);
Path<String> path_id = root.get(Agent_.id);
Path<String> path_name = root.get(Agent_.name);
Path<String> path_cron = root.get(Agent_.cron);
Path<Date> path_lastEndTime = root.get(Agent_.lastEndTime);
Path<Date> path_lastStartTime = root.get(Agent_.lastStartTime);
Path<String> pathId = root.get(Agent_.id);
Path<String> pathName = root.get(Agent_.name);
Path<String> pathCron = root.get(Agent_.cron);
Path<Date> pathLastEndTime = root.get(Agent_.lastEndTime);
Path<Date> pathLastStartTime = root.get(Agent_.lastStartTime);
Predicate p = cb.equal(root.get(Agent_.enable), true);
List<Tuple> list = em
.createQuery(
cq.multiselect(path_id, path_name, path_cron, path_lastEndTime, path_lastStartTime).where(p))
.createQuery(cq.multiselect(pathId, pathName, pathCron, pathLastEndTime, pathLastStartTime).where(p))
.getResultList();
List<Pair> pairs = list.stream().map(o -> {
return new Pair(o.get(path_id), o.get(path_name), o.get(path_cron), o.get(path_lastStartTime));
}).distinct().collect(Collectors.toList());
return pairs;
return list.stream()
.map(o -> new Pair(o.get(pathId), o.get(pathName), o.get(pathCron), o.get(pathLastStartTime)))
.distinct().collect(Collectors.toList());
}
class Pair {
......@@ -202,62 +202,76 @@ public class TriggerAgent extends BaseAction {
try {
LOCK.add(agent.getId());
Map.Entry<String, CenterServer> centerServer = getCenterServer();
if(centerServer==null){
try (EntityManagerContainer emc = EntityManagerContainerFactory.instance().create()) {
CacheCategory cacheCategory = new CacheCategory(Agent.class);
CacheKey cacheKey = new CacheKey(TriggerAgent.class, agent.getId());
CompiledScript compiledScript = null;
Optional<?> optional = CacheManager.get(cacheCategory, cacheKey);
if (optional.isPresent()) {
compiledScript = (CompiledScript) optional.get();
} else {
compiledScript = ScriptFactory.compile(ScriptFactory.functionalization(agent.getText()));
CacheManager.put(cacheCategory, cacheKey, compiledScript);
}
ScriptContext scriptContext = new SimpleScriptContext();
Bindings bindings = scriptContext.getBindings(ScriptContext.ENGINE_SCOPE);
Resources resources = new Resources();
resources.setEntityManagerContainer(emc);
resources.setContext(ThisApplication.context());
resources.setOrganization(new Organization(ThisApplication.context()));
resources.setApplications(ThisApplication.context().applications());
resources.setWebservicesClient(new WebservicesClient());
bindings.put(ScriptFactory.BINDING_NAME_RESOURCES, resources);
try {
ScriptFactory.initialServiceScriptText().eval(scriptContext);
compiledScript.eval(scriptContext);
} catch (Exception e) {
throw new ExceptionAgentEval(e, e.getMessage(), agent.getId(), agent.getName(),
agent.getAlias(), agent.getText());
}
} catch (Exception e) {
logger.error(e);
}
try (EntityManagerContainer emc = EntityManagerContainerFactory.instance().create()) {
Agent o = emc.find(agent.getId(), Agent.class);
if (null != o) {
emc.beginTransaction(Agent.class);
o.setLastEndTime(new Date());
emc.commit();
}
} catch (Exception e) {
logger.error(e);
}
}else{
try {
CipherConnectionAction.get(false, Config.url_x_program_center_jaxrs(centerServer, "agent", agent.getId(), "execute") + "?tt=" + System.currentTimeMillis());
} catch (Exception e) {
logger.warn("trigger agent {} on center {} error:{}", agent.getName(), centerServer.getKey(), e.getMessage());
}
if (centerServer == null) {
evalLocal();
} else {
evalRemote(centerServer);
}
} catch (Exception e) {
logger.error(e);
} finally {
LOCK.remove(agent.getId());
}
}
}
private Map.Entry<String, CenterServer> getCenterServer(){
private void evalRemote(Map.Entry<String, CenterServer> centerServer) {
try {
CipherConnectionAction.get(false,
Config.url_x_program_center_jaxrs(centerServer, "agent", agent.getId(), "execute") + "?tt="
+ System.currentTimeMillis());
} catch (Exception e) {
logger.warn("trigger agent {} on center {} error:{}", agent.getName(), centerServer.getKey(),
e.getMessage());
}
}
private void evalLocal() throws Exception {
CacheCategory cacheCategory = new CacheCategory(Agent.class);
CacheKey cacheKey = new CacheKey(TriggerAgent.class, agent.getId());
CompiledScript compiledScript = null;
Optional<?> optional = CacheManager.get(cacheCategory, cacheKey);
if (optional.isPresent()) {
compiledScript = (CompiledScript) optional.get();
} else {
compiledScript = ScriptingFactory.functionalizationCompile(agent.getText());
CacheManager.put(cacheCategory, cacheKey, compiledScript);
}
ScriptContext scriptContext = ScriptingFactory.scriptContextEvalInitialServiceScript();
Bindings bindings = scriptContext.getBindings(ScriptContext.ENGINE_SCOPE);
Resources resources = new Resources();
resources.setContext(ThisApplication.context());
resources.setOrganization(new Organization(ThisApplication.context()));
resources.setWebservicesClient(new WebservicesClient());
resources.setApplications(ThisApplication.context().applications());
bindings.put(ScriptingFactory.BINDING_NAME_SERVICE_RESOURCES, resources);
eval(compiledScript, scriptContext);
updateLastEndTime();
}
private void updateLastEndTime() {
try (EntityManagerContainer emc = EntityManagerContainerFactory.instance().create()) {
Agent o = emc.find(agent.getId(), Agent.class);
if (null != o) {
emc.beginTransaction(Agent.class);
o.setLastEndTime(new Date());
emc.commit();
}
} catch (Exception e) {
logger.error(e);
}
}
private void eval(CompiledScript compiledScript, ScriptContext scriptContext) throws ExceptionAgentEval {
try {
JsonScriptingExecutor.jsonElement(compiledScript, scriptContext);
} catch (Exception e) {
throw new ExceptionAgentEval(e, e.getMessage(), agent.getId(), agent.getName(), agent.getAlias(),
agent.getText());
}
}
private Map.Entry<String, CenterServer> getCenterServer() {
Map.Entry<String, CenterServer> centerServer = null;
try {
Map.Entry<String, CenterServer> entry;
......
......@@ -164,9 +164,9 @@ class ActionExecute extends BaseAction {
resources.setWebservicesClient(new WebservicesClient());
resources.setOrganization(new Organization(ThisApplication.context()));
Bindings bindings = scriptContext.getBindings(ScriptContext.ENGINE_SCOPE);
bindings.put(ScriptingFactory.BINDING_NAME_RESOURCES, resources);
bindings.put(ScriptingFactory.BINDING_NAME_EFFECTIVEPERSON, effectivePerson);
bindings.put(ScriptingFactory.BINDING_NAME_PARAMETERS, gson.toJson(runtime.getParameters()));
bindings.put(ScriptingFactory.BINDING_NAME_SERVICE_RESOURCES, resources);
bindings.put(ScriptingFactory.BINDING_NAME_SERVICE_EFFECTIVEPERSON, effectivePerson);
bindings.put(ScriptingFactory.BINDING_NAME_SERVICE_PARAMETERS, gson.toJson(runtime.getParameters()));
return scriptContext;
}
......
......@@ -234,9 +234,9 @@ class ActionExecuteV2 extends BaseAction {
resources.setWebservicesClient(new WebservicesClient());
resources.setOrganization(new Organization(ThisApplication.context()));
Bindings bindings = scriptContext.getBindings(ScriptContext.ENGINE_SCOPE);
bindings.put(ScriptingFactory.BINDING_NAME_RESOURCES, resources);
bindings.put(ScriptingFactory.BINDING_NAME_EFFECTIVEPERSON, effectivePerson);
bindings.put(ScriptingFactory.BINDING_NAME_PARAMETERS, gson.toJson(runtime.getParameters()));
bindings.put(ScriptingFactory.BINDING_NAME_SERVICE_RESOURCES, resources);
bindings.put(ScriptingFactory.BINDING_NAME_SERVICE_EFFECTIVEPERSON, effectivePerson);
bindings.put(ScriptingFactory.BINDING_NAME_SERVICE_PARAMETERS, gson.toJson(runtime.getParameters()));
return scriptContext;
}
......
......@@ -203,9 +203,9 @@ class ActionExecute extends BaseAction {
resources.setWebservicesClient(new WebservicesClient());
resources.setOrganization(new Organization(ThisApplication.context()));
Bindings bindings = scriptContext.getBindings(ScriptContext.ENGINE_SCOPE);
bindings.put(ScriptingFactory.BINDING_NAME_RESOURCES, resources);
bindings.put(ScriptingFactory.BINDING_NAME_EFFECTIVEPERSON, effectivePerson);
bindings.put(ScriptingFactory.BINDING_NAME_PARAMETERS, gson.toJson(runtime.getParameters()));
bindings.put(ScriptingFactory.BINDING_NAME_SERVICE_RESOURCES, resources);
bindings.put(ScriptingFactory.BINDING_NAME_SERVICE_EFFECTIVEPERSON, effectivePerson);
bindings.put(ScriptingFactory.BINDING_NAME_SERVICE_PARAMETERS, gson.toJson(runtime.getParameters()));
return scriptContext;
}
......
......@@ -251,9 +251,9 @@ class ActionExecuteV2 extends BaseAction {
resources.setWebservicesClient(new WebservicesClient());
resources.setOrganization(new Organization(ThisApplication.context()));
Bindings bindings = scriptContext.getBindings(ScriptContext.ENGINE_SCOPE);
bindings.put(ScriptingFactory.BINDING_NAME_RESOURCES, resources);
bindings.put(ScriptingFactory.BINDING_NAME_EFFECTIVEPERSON, effectivePerson);
bindings.put(ScriptingFactory.BINDING_NAME_PARAMETERS, gson.toJson(runtime.getParameters()));
bindings.put(ScriptingFactory.BINDING_NAME_SERVICE_RESOURCES, resources);
bindings.put(ScriptingFactory.BINDING_NAME_SERVICE_EFFECTIVEPERSON, effectivePerson);
bindings.put(ScriptingFactory.BINDING_NAME_SERVICE_PARAMETERS, gson.toJson(runtime.getParameters()));
return scriptContext;
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment