FlowManager.java 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799
  1. package com.yihu.hos.system.service;
  2. import com.yihu.hos.common.constants.ContextAttributes;
  3. import com.yihu.hos.config.BeanConfig;
  4. import com.yihu.hos.core.datatype.ClassFileUtil;
  5. import com.yihu.hos.core.file.FileUtil;
  6. import com.yihu.hos.interceptor.LocalContext;
  7. import com.yihu.hos.services.ServiceFlowEventService;
  8. import com.yihu.hos.system.dao.FlowClassDao;
  9. import com.yihu.hos.system.dao.FlowDao;
  10. import com.yihu.hos.system.dao.FlowTempDao;
  11. import com.yihu.hos.system.model.SystemServiceFlow;
  12. import com.yihu.hos.system.model.SystemServiceFlowClass;
  13. import com.yihu.hos.system.model.SystemServiceFlowTemp;
  14. import com.yihu.hos.web.framework.constant.ServiceFlowConstant;
  15. import com.yihu.hos.web.framework.model.ActionResult;
  16. import com.yihu.hos.web.framework.model.DictItem;
  17. import com.yihu.hos.web.framework.model.Result;
  18. import com.yihu.hos.web.framework.model.bo.ServiceFlow;
  19. import org.apache.commons.lang3.StringUtils;
  20. import org.springframework.beans.factory.annotation.Autowired;
  21. import org.springframework.beans.factory.annotation.Value;
  22. import org.springframework.stereotype.Service;
  23. import org.springframework.transaction.annotation.Transactional;
  24. import javax.annotation.Resource;
  25. import java.io.File;
  26. import java.io.InputStream;
  27. import java.nio.charset.StandardCharsets;
  28. import java.util.*;
  29. /**
  30. * 系统流程管理业务类
  31. *
  32. * @author HZY
  33. * @vsrsion 1.0
  34. * Created at 2016/8/19.
  35. */
  36. @Service("flowManager")
  37. public class FlowManager {
  38. public static final String BEAN_ID = "flowManager";
  39. @Autowired
  40. ServiceFlowEventService serviceFlowEventService;
  41. @Autowired
  42. private BeanConfig beanConfig;
  43. @Resource(name = "flowDao")
  44. private FlowDao flowDao;
  45. @Resource(name = FlowClassDao.BEAN_ID)
  46. private FlowClassDao flowClassDao;
  47. @Resource(name = FlowTempDao.BEAN_ID)
  48. private FlowTempDao flowTempDao;
  49. @Value("${hos.filesystem.url}")
  50. private String fsUrl;
  51. /**
  52. * 生成Route流程的java文件
  53. *
  54. * @param routeId 流程Id
  55. * @param className 模板类名
  56. * @param tempFilePath 模板文件路径
  57. * @param newCron cron表达式
  58. * @return
  59. */
  60. public String genRouteJavaFile(String jobId, String routeId, String className, String tempFilePath, String newCron) {
  61. try {
  62. //TODO 临时文件夹
  63. String newFileName = className + routeId + ".java";
  64. String newFilePath = System.getProperty("java.io.tmpdir") + System.getProperty("file.separator") + newFileName;
  65. String uploadUrl = fsUrl + "/" + tempFilePath;
  66. byte[] content = ClassFileUtil.down(uploadUrl);
  67. if (content == null) {
  68. return null;
  69. }
  70. String text = new String(content, StandardCharsets.UTF_8);
  71. if (text.contains("?cron=")) {
  72. String oldStr = text.substring(text.indexOf("?cron=") + 6);
  73. String cron = oldStr.substring(0, oldStr.indexOf("\""));
  74. text = text.replace(cron, newCron);
  75. }
  76. //修改java类名
  77. if (text.contains(className)) {
  78. text = text.replace(className, className + routeId);//新类名规则=旧类名+routeId
  79. }
  80. //修改routeId;模板规则 routeId("routeId")
  81. text = text.replace("routeId(\"routeId\")", "routeId(\"" + routeId + "\")");
  82. //修改jobId;模板规则 "{\"jobId\": \"jobId\"}"
  83. text = text.replace("{\\\"jobId\\\": \\\"jobId\\\"}", "{\\\"jobId\\\": \\\"" + jobId + "\\\"}");
  84. boolean succ = FileUtil.writeFile(newFilePath, text, "UTF-8");
  85. if (succ) {
  86. newFileName = ClassFileUtil.uploadFile(beanConfig.getFsUrl(), new File(newFilePath), newFileName);
  87. } else {
  88. return null;
  89. }
  90. return newFileName;
  91. } catch (Exception e) {
  92. System.out.println("修改Route的java文件操作出错");
  93. e.printStackTrace();
  94. }
  95. return null;
  96. }
  97. /**
  98. * 生成processor的java文件
  99. *
  100. * @param jobId 任务Id
  101. * @param routeId 流程Code
  102. * @param tempFilePath 模板文件名
  103. * @param className 模板类名
  104. * @return
  105. */
  106. public String genProcessorJavaFile(String jobId, String routeId, String tempFilePath, String className) {
  107. try {
  108. String newFileName = className + routeId + ".java";
  109. //TODO 临时文件夹设置
  110. String newFilePath = System.getProperty("java.io.tmpdir") + System.getProperty("file.separator") + newFileName;
  111. String uploadUrl = fsUrl + "/" + tempFilePath;
  112. byte[] content = ClassFileUtil.down(uploadUrl);
  113. if (content == null) {
  114. return null;
  115. }
  116. String text = new String(content, StandardCharsets.UTF_8);
  117. text = text.replace("jobId=jobId", "jobId=" + jobId);
  118. boolean succ = FileUtil.writeFile(newFilePath, text, "UTF-8");
  119. //TODO 上传到GridFS
  120. if (succ) {
  121. newFileName = ClassFileUtil.uploadFile(beanConfig.getFsUrl(), new File(newFilePath), newFileName);
  122. return newFileName;
  123. }
  124. } catch (Exception e) {
  125. System.out.println("生成processor的java文件操作出错");
  126. e.printStackTrace();
  127. }
  128. return null;
  129. }
  130. public Result getFlowList(Map<String, Object> params) throws Exception {
  131. return flowDao.getFlowList(params);
  132. }
  133. /**
  134. * @return List<ServiceFlow> 返回所有可运行流程
  135. * @throws Exception ...
  136. */
  137. public List<ServiceFlow> getServiceFlowList() throws Exception {
  138. List<ServiceFlow> serviceFlowList = new ArrayList<>();
  139. List<SystemServiceFlow> classFlowList = flowDao.getFlowList(ServiceFlowConstant.CLASS);
  140. for (SystemServiceFlow systemServiceFlow : classFlowList) {
  141. String tenant = LocalContext.getContext().getAttachment(ContextAttributes.TENANT_NAME);
  142. ServiceFlow serviceFlow = new ServiceFlow();
  143. serviceFlow.setRouteCode(systemServiceFlow.getCode());
  144. serviceFlow.setFlowType(systemServiceFlow.getFileType());
  145. serviceFlow.setTenant(tenant);
  146. List<SystemServiceFlowClass> classList = flowClassDao.getFlowClassByFlowId(systemServiceFlow.getId());
  147. ArrayList<ServiceFlow.HandleFile> handleFileList = new ArrayList<>();
  148. for (SystemServiceFlowClass flowClass : classList) {
  149. ServiceFlow.HandleFile handleFile = new ServiceFlow.HandleFile();
  150. handleFile.setFileType(ServiceFlowConstant.CLASS);
  151. handleFile.setClassName(flowClass.getClassName());
  152. handleFile.setPackageName(flowClass.getPackageName());
  153. handleFile.setFilePath(flowClass.getClassPath());
  154. handleFile.setUsage(flowClass.getType());
  155. handleFile.setRouteCode(serviceFlow.getRouteCode());
  156. handleFileList.add(handleFile);
  157. }
  158. serviceFlow.setHandleFiles(handleFileList);
  159. serviceFlowList.add(serviceFlow);
  160. }
  161. return serviceFlowList;
  162. }
  163. /* =================================== flowClass 部分================================================*/
  164. public SystemServiceFlow getFlowById(Integer id) throws Exception {
  165. return flowDao.getEntity(SystemServiceFlow.class, id);
  166. }
  167. @Transactional
  168. public Result addFlow(SystemServiceFlow obj) throws Exception {
  169. obj.setCreateDate(new Date());
  170. flowDao.saveEntity(obj);
  171. if (ServiceFlowConstant.CLASS.equals(obj.getFileType())) {
  172. List<SystemServiceFlowClass> flowClassList = obj.getFlowClassArray();
  173. ServiceFlow serviceFlow = new ServiceFlow();
  174. serviceFlow.setRouteCode(obj.getCode());
  175. serviceFlow.setFlowType(ServiceFlowConstant.CLASS);
  176. for (SystemServiceFlowClass flowClass : flowClassList) {
  177. flowClass.setFlowId(obj.getId());
  178. flowDao.saveEntity(flowClass);
  179. ServiceFlow.HandleFile handleFile = new ServiceFlow.HandleFile();
  180. handleFile.setRouteCode(obj.getCode());
  181. handleFile.setFileType(ServiceFlowConstant.CLASS);
  182. handleFile.setPackageName(flowClass.getPackageName());
  183. handleFile.setClassName(flowClass.getClassName());
  184. handleFile.setFilePath(flowClass.getClassPath());
  185. handleFile.setUsage(flowClass.getType());
  186. serviceFlow.addHandleFile(handleFile);
  187. }
  188. serviceFlowEventService.serviceFlowAdded(serviceFlow);
  189. } else if (ServiceFlowConstant.JAVA.equals(obj.getFileType())) {
  190. List<SystemServiceFlowTemp> flowTempList = obj.getFlowTempArray();
  191. for (SystemServiceFlowTemp flowTemp : flowTempList) {
  192. flowTemp.setFlowId(obj.getId());
  193. flowDao.saveEntity(flowTemp);
  194. }
  195. }
  196. return Result.success("保存成功");
  197. }
  198. @Transactional
  199. public Result updateFlow(SystemServiceFlow obj) throws Exception {
  200. ServiceFlow serviceFlow = new ServiceFlow();
  201. SystemServiceFlow flow = flowDao.getEntity(SystemServiceFlow.class, obj.getId());
  202. flow.setCode(obj.getCode());
  203. flow.setName(obj.getName());
  204. flow.setPath(obj.getPath());
  205. flow.setChart(obj.getChart());
  206. flow.setValid(obj.getValid());
  207. flow.setFileType(obj.getFileType());
  208. if (ServiceFlowConstant.JAVA.equals(flow.getFileType())) {
  209. List<SystemServiceFlowTemp> flowTempList = obj.getFlowTempArray();
  210. boolean succ = flowTempDao.deleteFlowTempByFlowId(obj.getId());
  211. if (succ) {
  212. for (SystemServiceFlowTemp flowTemp : flowTempList) {
  213. flowTempDao.saveEntity(flowTemp);
  214. }
  215. }
  216. } else if (ServiceFlowConstant.CLASS.equals(flow.getFileType())) {
  217. List<Integer> classIds = flowClassDao.getFlowClassIds(obj.getId());//原flowclass集合
  218. List<SystemServiceFlowClass> flowClassList = obj.getFlowClassArray();
  219. SystemServiceFlowClass flowClassRoute = null;
  220. serviceFlow.setRouteCode(obj.getCode());
  221. serviceFlow.setFlowType(ServiceFlowConstant.CLASS);
  222. for (SystemServiceFlowClass flowClass : flowClassList) {
  223. if (flowClass.getId() != null) {
  224. classIds.remove(flowClass.getId());
  225. flowClassDao.updateEntity(flowClass);
  226. } else {
  227. if (!flowClass.getType().equals(ServiceFlowConstant.FLOW_TYPE_ROUTE)) {
  228. flowClassDao.saveEntity(flowClass);
  229. }
  230. }
  231. ServiceFlow.HandleFile handleFile = new ServiceFlow.HandleFile();
  232. handleFile.setRouteCode(obj.getCode());
  233. handleFile.setFileType(ServiceFlowConstant.CLASS);
  234. handleFile.setPackageName(flowClass.getPackageName());
  235. handleFile.setClassName(flowClass.getClassName());
  236. handleFile.setFilePath(flowClass.getClassPath());
  237. handleFile.setUsage(flowClass.getType());
  238. serviceFlow.addHandleFile(handleFile);
  239. }
  240. serviceFlowEventService.serviceFlowModifiedAdd(serviceFlow);
  241. //删除判断
  242. if (classIds != null && classIds.size() > 0) {
  243. for (Integer id : classIds) {
  244. SystemServiceFlowClass flowClass = getFlowClassById(id);
  245. flowClassDao.deleteEntity(flowClass);
  246. if (flowClass.getType().equals(ServiceFlowConstant.FLOW_TYPE_ROUTE)) {
  247. flowClassRoute = flowClass;
  248. flowClassRoute.setIsUpdate("1");
  249. }
  250. }
  251. }
  252. }
  253. flowDao.updateEntity(flow);
  254. return Result.success("更新成功");
  255. }
  256. @Transactional
  257. public Result deleteFlow(Integer id) throws Exception {
  258. SystemServiceFlow flow = flowDao.getEntity(SystemServiceFlow.class, id);
  259. if (flow == null) {
  260. return Result.error("删除流程失败");
  261. }
  262. ArrayList<ServiceFlow.HandleFile> handleFiles = new ArrayList<>();
  263. List<SystemServiceFlowClass> flowClassList = flowClassDao.getFlowClassByFlowId(id);
  264. if (ServiceFlowConstant.JAVA.equals(flow.getFileType())) {
  265. flowTempDao.deleteFlowTempByFlowId(id);
  266. } else {
  267. ServiceFlow serviceFlow = new ServiceFlow();
  268. serviceFlow.setRouteCode(flow.getCode());
  269. serviceFlow.setFlowType(ServiceFlowConstant.CLASS);
  270. for (SystemServiceFlowClass flowClass : flowClassList) {
  271. ServiceFlow.HandleFile handleFile = new ServiceFlow.HandleFile();
  272. handleFile.setRouteCode(flow.getCode());
  273. handleFile.setFileType(ServiceFlowConstant.JAVA);
  274. handleFile.setPackageName(flowClass.getPackageName());
  275. handleFile.setClassName(flowClass.getClassName());
  276. handleFile.setFilePath(flowClass.getClassPath());
  277. handleFile.setUsage(flowClass.getType());
  278. handleFiles.add(handleFile);
  279. flowClassDao.deleteEntity(flowClass);
  280. }
  281. serviceFlow.setHandleFiles(handleFiles);
  282. serviceFlowEventService.serviceFlowDelete(serviceFlow);
  283. }
  284. flowDao.deleteEntity(flow);
  285. return Result.success("删除流程成功");
  286. }
  287. public Result getFlowClassList(Map<String, Object> params) throws Exception {
  288. return null;
  289. }
  290. public List<SystemServiceFlowClass> getFlowClassByFlowId(Integer flowId) throws Exception {
  291. return flowClassDao.getFlowClassByFlowId(flowId);
  292. }
  293. public SystemServiceFlowClass getFlowClassById(Integer id) throws Exception {
  294. return flowClassDao.getEntity(SystemServiceFlowClass.class, id);
  295. }
  296. @Transactional
  297. public Result addFlowClass(SystemServiceFlowClass obj) throws Exception {
  298. flowDao.saveEntity(obj);
  299. return Result.success("保存成功");
  300. }
  301. @Transactional
  302. public Result updateFlowClass(SystemServiceFlowClass obj) throws Exception {
  303. return null;
  304. }
  305. @Transactional
  306. public boolean deleteFlowClassByFlowId(Integer flowId) {
  307. boolean succ = flowClassDao.deleteFlowClassByFlowId(flowId);
  308. return succ;
  309. }
  310. public List<SystemServiceFlowTemp> getFlowTempByFlowId(Integer id) throws Exception {
  311. return flowTempDao.getFlowTempByFlowId(id);
  312. }
  313. //TODO
  314. public boolean genFlewByflowTempId(Integer flowTempId) throws Exception {
  315. SystemServiceFlow flow = getFlowById(flowTempId);
  316. //生成新的route类
  317. //添加新processor记录
  318. return false;
  319. }
  320. /**
  321. * 发送MQ消息-更新路由
  322. *
  323. * @param flowCode 服务流程Code标识
  324. * @param flowClass
  325. * @param operate
  326. */
  327. public void sendUpdateMessage(String flowCode, SystemServiceFlowClass flowClass, String operate) {
  328. if ("1".equals(flowClass.getIsUpdate())) {
  329. ServiceFlow serviceFlow = new ServiceFlow();
  330. serviceFlow.setRouteCode(flowCode);
  331. serviceFlow.setFlowType(ServiceFlowConstant.CLASS);
  332. ServiceFlow.HandleFile handleFile = new ServiceFlow.HandleFile();
  333. handleFile.setRouteCode(flowCode);
  334. handleFile.setFileType(ServiceFlowConstant.CLASS);
  335. handleFile.setPackageName(flowClass.getPackageName());
  336. handleFile.setClassName(flowClass.getClassName());
  337. handleFile.setFilePath(flowClass.getClassPath());
  338. handleFile.setUsage(flowClass.getType());
  339. serviceFlow.addHandleFile(handleFile);
  340. switch (operate) {
  341. case ServiceFlowConstant.FLOW_OP_ADD:
  342. case ServiceFlowConstant.FLOW_OP_UPDATE:
  343. serviceFlowEventService.serviceFlowModifiedAdd(serviceFlow);
  344. break;
  345. case ServiceFlowConstant.FLOW_OP_DELETE:
  346. serviceFlowEventService.serviceFlowModifiedReduce(serviceFlow);
  347. break;
  348. default:
  349. break;
  350. }
  351. }
  352. }
  353. /**
  354. * 获取流程列表
  355. *
  356. * @param type 流程的文件类型
  357. * @return
  358. * @throws Exception
  359. */
  360. public ActionResult getFlowList(String type) throws Exception {
  361. List<SystemServiceFlow> flowList = flowDao.getFlowList(type);
  362. ActionResult re = new ActionResult();
  363. if (flowList != null && flowList.size() > 0) {
  364. List<DictItem> dictList = new ArrayList<>();
  365. for (SystemServiceFlow item : flowList) {
  366. DictItem dict = new DictItem();
  367. dict.setCode(item.getId().toString());
  368. dict.setValue(item.getName());
  369. dict.setExtend("");
  370. dictList.add(dict);
  371. }
  372. re.setData(dictList);
  373. }
  374. return re;
  375. }
  376. /**
  377. * 发送消息到broker,生成camel相关文件
  378. *
  379. * @param flowTempId
  380. * @param newCron
  381. * @throws Exception
  382. */
  383. public Integer genCamelFile(String jobId, Integer flowTempId, String newCron) throws Exception {
  384. Long timestamp = System.currentTimeMillis();
  385. //发送生成processor文件的消息
  386. Integer newFlowId = sendAddProcessor(jobId, flowTempId, timestamp, newCron);
  387. if (newFlowId != null) {
  388. //发送生成route文件的消息
  389. // newFlowId = sendAddRoute(flowTempId, newFlowId, newCron);
  390. // if (newFlowId != null) {
  391. // return newFlowId;
  392. // } else {
  393. // System.out.println("生成route文件失败");
  394. // return null;
  395. // }
  396. return newFlowId;
  397. } else {
  398. System.out.println("生成processor文件失败");
  399. return null;
  400. }
  401. }
  402. /**
  403. * 修改任务,修改camel相关文件
  404. *
  405. * @param flowId 流程ID
  406. * @param newCron 新cron
  407. * @return
  408. * @throws Exception
  409. */
  410. public Integer updateCamelFile(String jobId, Integer flowTempId, Integer flowId, String newCron) throws Exception {
  411. /* 修改route文件无需重新生成flowClass记录,文件名根据className+routeId 生成;*/
  412. List<SystemServiceFlowTemp> flowTempRouters = flowTempDao.getFlowTemps(flowTempId, ServiceFlowConstant.FLOW_TYPE_ROUTE);
  413. List<SystemServiceFlowTemp> flowTempProces = flowTempDao.getFlowTemps(flowTempId, ServiceFlowConstant.FLOW_TYPE_PROCESSOR);
  414. SystemServiceFlow flow = flowDao.getEntity(SystemServiceFlow.class, flowId);
  415. //route模板文件记录是否存在。不存在就返回。
  416. if (!flowTempRouters.isEmpty()) {
  417. SystemServiceFlowTemp flowTemp = flowTempRouters.get(0);
  418. ArrayList<ServiceFlow.HandleFile> handleFiles = new ArrayList<>();
  419. ServiceFlow serviceFlow = new ServiceFlow();
  420. //新增processor记录
  421. for (SystemServiceFlowTemp process : flowTempProces) {
  422. String newPath = genProcessorJavaFile(jobId, flow.getCode(), process.getClassPath(), process.getClassName());
  423. if (newPath != null) {
  424. //发送消息
  425. serviceFlow.setRouteCode(flow.getCode());
  426. serviceFlow.setFlowType(ServiceFlowConstant.CLASS);
  427. ServiceFlow.HandleFile handleFile = new ServiceFlow.HandleFile();
  428. handleFile.setRouteCode(flow.getCode());
  429. handleFile.setFileType(ServiceFlowConstant.JAVA);
  430. handleFile.setPackageName(process.getPackageName());
  431. handleFile.setClassName(process.getClassName());
  432. handleFile.setFilePath(newPath);
  433. handleFile.setUsage(ServiceFlowConstant.FLOW_TYPE_PROCESSOR);
  434. handleFiles.add(handleFile);
  435. serviceFlow.setHandleFiles(handleFiles);
  436. } else {
  437. System.out.println("生成processor的java文件过程出错");
  438. return null;
  439. }
  440. }
  441. //生成新的route文件
  442. String newPath = genRouteJavaFile(jobId, flow.getCode(), flowTemp.getClassName(), flowTemp.getClassPath(), newCron);
  443. serviceFlow.setRouteCode(flow.getCode());
  444. serviceFlow.setFlowType(ServiceFlowConstant.JAVA);
  445. ServiceFlow.HandleFile handleFile = new ServiceFlow.HandleFile();
  446. handleFile.setRouteCode(flow.getCode());
  447. handleFile.setFileType(ServiceFlowConstant.JAVA);
  448. handleFile.setPackageName(flowTemp.getPackageName());
  449. handleFile.setClassName(flowTemp.getClassName() + flow.getCode());
  450. handleFile.setFilePath(newPath);
  451. handleFile.setUsage(ServiceFlowConstant.FLOW_TYPE_ROUTE);
  452. handleFiles.add(handleFile);
  453. serviceFlow.setHandleFiles(handleFiles);
  454. serviceFlowEventService.serviceFlowModifiedAdd(serviceFlow);
  455. return flowId;
  456. }
  457. return null;
  458. }
  459. /* ********************* 发送消息方式生成文件 ********************************/
  460. public Integer sendAddRoute(Integer tempId, Integer flowId, String newCron) throws Exception {
  461. List<SystemServiceFlowTemp> flowTempRouters = flowTempDao.getFlowTemps(tempId, ServiceFlowConstant.FLOW_TYPE_ROUTE);
  462. SystemServiceFlow newFlow = getFlowById(flowId);
  463. //route模板文件记录是否存在。不存在就返回。
  464. if (!flowTempRouters.isEmpty()) {
  465. SystemServiceFlowTemp flowTemp = flowTempRouters.get(0);
  466. StringBuilder basePath = new StringBuilder();
  467. ;
  468. if (flowTemp.getPackageName() != null) {
  469. String packagePath[] = flowTemp.getPackageName().split("\\.");
  470. for (int i = 0; i < packagePath.length; i++) {
  471. basePath.append(packagePath[i]).append("/");
  472. }
  473. }
  474. //新增processor记录
  475. String newPath = genRouteJavaFile("", newFlow.getCode(), flowTemp.getClassName(), flowTemp.getClassPath(), newCron);
  476. SystemServiceFlowClass newFlowClass = new SystemServiceFlowClass();
  477. newFlowClass.setPackageName(flowTemp.getPackageName());
  478. newFlowClass.setClassName(flowTemp.getClassName() + newFlow.getCode());
  479. newFlowClass.setClassPath(flowTemp.getClassPath());
  480. newFlowClass.setFlowId(newFlow.getId());
  481. newFlowClass.setType(ServiceFlowConstant.FLOW_TYPE_ROUTE);
  482. flowClassDao.saveEntity(newFlowClass);
  483. newFlowClass.setIsUpdate("1");
  484. if (newPath != null) {
  485. ServiceFlow serviceFlow = new ServiceFlow();
  486. serviceFlow.setRouteCode(newFlow.getCode());
  487. serviceFlow.setFlowType(ServiceFlowConstant.CLASS);
  488. ServiceFlow.HandleFile handleFile = new ServiceFlow.HandleFile();
  489. handleFile.setRouteCode(newFlow.getCode());
  490. handleFile.setFileType(ServiceFlowConstant.JAVA);
  491. handleFile.setPackageName(flowTemp.getPackageName());
  492. handleFile.setClassName(flowTemp.getClassName() + newFlow.getCode());
  493. handleFile.setFilePath(newPath);
  494. handleFile.setUsage(ServiceFlowConstant.FLOW_TYPE_ROUTE);
  495. ArrayList<ServiceFlow.HandleFile> handleFiles = new ArrayList<>();
  496. handleFiles.add(handleFile);
  497. serviceFlow.setHandleFiles(handleFiles);
  498. serviceFlowEventService.serviceFlowModifiedAdd(serviceFlow);
  499. return newFlow.getId();
  500. } else {
  501. System.out.println("生成route的java文件过程出错");
  502. return null;
  503. }
  504. }
  505. return null;
  506. }
  507. public Integer sendAddRoute(String code, String name, String javaName, String packageName, String fileInfo) throws Exception {
  508. String fileName = javaName + ".java";
  509. String filePath = this.getClass().getProtectionDomain().getClassLoader().getResource("").getPath() + "temp/" + fileName;
  510. boolean succ = FileUtil.writeFile(filePath, fileInfo, "UTF-8");
  511. String path = "";
  512. if (succ) {
  513. path = ClassFileUtil.uploadFile(beanConfig.getFsUrl(), new File(filePath), fileName);
  514. } else {
  515. return null;
  516. }
  517. if (fileName != null) {
  518. SystemServiceFlow systemServiceFlow = new SystemServiceFlow();
  519. systemServiceFlow.setName(name);
  520. systemServiceFlow.setCode(code);
  521. systemServiceFlow.setCreateDate(new Date());
  522. systemServiceFlow.setValid(1);
  523. systemServiceFlow.setFileType(ServiceFlowConstant.JAVA);
  524. flowDao.saveEntity(systemServiceFlow);
  525. ServiceFlow serviceFlow = new ServiceFlow();
  526. serviceFlow.setRouteCode(code);
  527. serviceFlow.setFlowType(ServiceFlowConstant.CLASS);
  528. ServiceFlow.HandleFile handleFile = new ServiceFlow.HandleFile();
  529. handleFile.setRouteCode(code);
  530. handleFile.setFileType(ServiceFlowConstant.JAVA);
  531. handleFile.setPackageName(packageName);
  532. handleFile.setClassName(javaName);
  533. handleFile.setFilePath(path);
  534. handleFile.setUsage(ServiceFlowConstant.FLOW_TYPE_ROUTE);
  535. ArrayList<ServiceFlow.HandleFile> handleFiles = new ArrayList<>();
  536. handleFiles.add(handleFile);
  537. serviceFlow.setHandleFiles(handleFiles);
  538. serviceFlowEventService.serviceFlowAdded(serviceFlow);
  539. SystemServiceFlowClass flowClass = new SystemServiceFlowClass();
  540. flowClass.setPackageName(packageName);
  541. flowClass.setClassName(javaName);
  542. flowClass.setClassPath(path);
  543. flowClass.setFlowId(systemServiceFlow.getId());
  544. flowClass.setType(ServiceFlowConstant.FLOW_TYPE_ROUTE);
  545. flowClass.setIsUpdate("1");
  546. flowClassDao.saveEntity(flowClass);
  547. return systemServiceFlow.getId();
  548. } else {
  549. System.out.println("生成route的java文件过程出错");
  550. return null;
  551. }
  552. }
  553. public Integer sendAddProcessor(String jobId, Integer flowId, Long timestamp, String newCron) throws Exception {
  554. List<SystemServiceFlowTemp> flowTempRouters = flowTempDao.getFlowTemps(flowId, ServiceFlowConstant.FLOW_TYPE_ROUTE);
  555. List<SystemServiceFlowTemp> flowTempProces = flowTempDao.getFlowTempByFlowId(flowId);
  556. SystemServiceFlow oldFlow = getFlowById(flowId);
  557. //route模板文件记录是否存在。不存在就返回。
  558. if (!flowTempRouters.isEmpty()) {
  559. //成功生成文件后,添加flow和flowclass记录
  560. SystemServiceFlow newFlow = new SystemServiceFlow();
  561. newFlow.setName(oldFlow.getName() + timestamp);
  562. newFlow.setCode(oldFlow.getCode() + timestamp);
  563. newFlow.setChart(oldFlow.getChart());
  564. newFlow.setValid(1);
  565. newFlow.setCreateDate(new Date());
  566. newFlow.setFileType(ServiceFlowConstant.CLASS);
  567. flowDao.saveEntity(newFlow);
  568. ArrayList<ServiceFlow.HandleFile> handleFiles = new ArrayList<>();
  569. //新增processor记录
  570. ServiceFlow serviceFlow = new ServiceFlow();
  571. serviceFlow.setRouteCode(newFlow.getCode());
  572. serviceFlow.setFlowType(ServiceFlowConstant.JAVA);
  573. for (SystemServiceFlowTemp process : flowTempProces) {
  574. StringBuilder proPath = new StringBuilder();
  575. if (process.getPackageName() != null) {
  576. //生成“/"分割的包名
  577. String packagePath[] = process.getPackageName().split("\\.");
  578. for (int i = 0; i < packagePath.length; i++) {
  579. proPath.append(packagePath[i]).append("/");
  580. }
  581. }
  582. SystemServiceFlowClass processClass = new SystemServiceFlowClass();
  583. processClass.setPackageName(process.getPackageName());
  584. processClass.setClassName(process.getClassName());
  585. processClass.setClassPath(processClass.getClassPath());
  586. processClass.setFlowId(newFlow.getId());
  587. processClass.setType(process.getType());
  588. processClass.setIsUpdate("1");
  589. //发送消息
  590. ServiceFlow.HandleFile handleFile = new ServiceFlow.HandleFile();
  591. handleFile.setRouteCode(newFlow.getCode());
  592. handleFile.setFileType(ServiceFlowConstant.JAVA);
  593. handleFile.setPackageName(processClass.getPackageName());
  594. handleFile.setUsage(processClass.getType());
  595. String newPath = null;
  596. if (ServiceFlowConstant.FLOW_TYPE_PROCESSOR.equals(process.getType())) {
  597. newPath = genProcessorJavaFile(jobId, newFlow.getCode(), process.getClassPath(), process.getClassName());
  598. handleFile.setClassName(processClass.getClassName());
  599. } else {
  600. newPath = genRouteJavaFile(jobId, newFlow.getCode(), process.getClassName(), process.getClassPath(), newCron);
  601. handleFile.setClassName(processClass.getClassName() + newFlow.getCode());
  602. }
  603. if (newPath != null) {
  604. handleFile.setFilePath(newPath);
  605. handleFiles.add(handleFile);
  606. processClass.setClassPath(newPath);
  607. flowClassDao.saveEntity(processClass);
  608. } else {
  609. System.out.println("生成processor的java文件过程出错");
  610. return null;
  611. }
  612. }
  613. serviceFlow.setHandleFiles(handleFiles);
  614. serviceFlowEventService.serviceFlowAdded(serviceFlow);
  615. return newFlow.getId();
  616. }
  617. return null;
  618. }
  619. /**
  620. * 服务启动/暂停
  621. *
  622. * @param flowId 流程ID
  623. * @param oper 服务操作;1:启动;0:暂停
  624. * @return
  625. * @throws Exception
  626. */
  627. public boolean serviceOpenOrPause(Integer flowId, Integer oper) throws Exception {
  628. SystemServiceFlow flow = flowDao.getEntity(SystemServiceFlow.class, flowId);
  629. if (flow != null) {
  630. List<SystemServiceFlowClass> flowClassList = flowClassDao.getFlowClassByFlowId(flowId);
  631. //route模板文件记录是否存在。不存在就返回。
  632. ArrayList<ServiceFlow.HandleFile> handleFiles = new ArrayList<>();
  633. //新增processor记录
  634. ServiceFlow serviceFlow = new ServiceFlow();
  635. serviceFlow.setRouteCode(flow.getCode());
  636. serviceFlow.setFlowType(ServiceFlowConstant.CLASS);
  637. for (SystemServiceFlowClass flowClass : flowClassList) {
  638. // String deName = DES.decrypt(flowClass.getClassPath(), DES.COMMON_PASSWORD);//吉阿米果的文件名
  639. //发送消息
  640. ServiceFlow.HandleFile handleFile = new ServiceFlow.HandleFile();
  641. handleFile.setRouteCode(flow.getCode());
  642. handleFile.setFileType(ServiceFlowConstant.CLASS);
  643. handleFile.setPackageName(flowClass.getPackageName());
  644. handleFile.setClassName(flowClass.getClassName());
  645. handleFile.setFilePath(flowClass.getClassPath());
  646. handleFile.setUsage(ServiceFlowConstant.FLOW_TYPE_PROCESSOR);
  647. handleFiles.add(handleFile);
  648. }
  649. serviceFlow.setHandleFiles(handleFiles);
  650. if (1 == oper) {
  651. serviceFlowEventService.serviceFlowStarted(serviceFlow);
  652. } else if (0 == oper) {
  653. serviceFlowEventService.serviceFlowStopped(serviceFlow);
  654. } else {
  655. return false;
  656. }
  657. return true;
  658. } else {
  659. return false;
  660. }
  661. }
  662. /**
  663. * 解析文件 包名和类名
  664. *
  665. * @param fileInput java文件流
  666. * @return
  667. */
  668. public Map<String, String> uploadAndParse(File file) {
  669. Map<String, String> result = null;
  670. String text = null;
  671. String packageName = null;
  672. String className = StringUtils.substringBefore(file.getName(), ".");
  673. if (file.getName().contains(".class")) {
  674. try {//class文件解析
  675. text = parseClassFile(file);
  676. packageName = StringUtils.substringBefore(StringUtils.substringAfter(text, "public class "), "." + className);//通过javap解析的结构不一样
  677. } catch (Exception e) {
  678. e.printStackTrace();
  679. }
  680. } else {//java文件解析
  681. text = FileUtil.readFileText(file);
  682. packageName = StringUtils.substringAfter(StringUtils.substringBefore(text, ";"), "package ");
  683. }
  684. result = new HashMap<>();
  685. result.put("package", packageName);
  686. result.put("class", className);
  687. return result;
  688. }
  689. /**
  690. * 解析class文件
  691. *
  692. * @param file
  693. * @return
  694. * @throws Exception
  695. */
  696. public String parseClassFile(File file) throws Exception {
  697. String path = file.getAbsolutePath();
  698. String command = "javap " + path;
  699. Process ps = Runtime.getRuntime().exec(command);
  700. String result = FileUtil.readFileText(ps.getInputStream());
  701. System.out.println(result);
  702. return result;
  703. }
  704. }