Commit e66c14be by zhouhaibao

[+]sop增加java脚本执行器

1 parent 8ec690e9
......@@ -2,6 +2,8 @@ package cn.com.glsx.process.common.constant;
import com.glsx.plat.core.constant.BasicConstants;
import java.nio.charset.Charset;
/**
* 〈一句话功能简述〉<br>
* 公共常量类
......@@ -13,4 +15,6 @@ public final class Constants extends BasicConstants {
public final static String SERVER_NAME = "glsx_rest_admin";
public static final Charset CHARSET = Charset.forName("utf-8");
}
package cn.com.glsx.process.modules.controller;
import com.alibaba.fastjson.JSON;
public class Controller {
public JSON execute(JSON param) {
System.out.println("测试java");
return param;
}
}
package cn.com.glsx.process.modules.service;
import cn.com.glsx.process.modules.entity.SopTplLink;
import cn.com.glsx.process.modules.mapper.SopTplLinkMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class SopTplLinkService {
@Autowired
private SopTplLinkMapper sopTplLinkMapper;
/**
* 根据id查询
*
* @param linkId
* @return
*/
private SopTplLink findById(Integer linkId) {
return this.sopTplLinkMapper.selectByPrimaryKey(linkId);
}
/**
* 保存
*
* @param sopTplLink
* @return
*/
public int insert(SopTplLink sopTplLink) {
return this.sopTplLinkMapper.insertUseGeneratedKeys(sopTplLink);
}
/**
* 更新
*
* @param sopTplLink
* @return
*/
public boolean update(SopTplLink sopTplLink) {
int i = this.sopTplLinkMapper.updateByPrimaryKeySelective(sopTplLink);
if (i > 0) {
return true;
}
return false;
}
/**
* 根据id删除
*
* @param id
* @return
*/
public boolean delete(Integer id) {
int i = this.sopTplLinkMapper.deleteByPrimaryKey(id);
if (i > 0) {
return true;
}
return false;
}
}
package cn.com.glsx.process.modules.service;
import cn.com.glsx.process.modules.entity.SopTplLink;
import cn.com.glsx.process.modules.entity.SopTplNode;
import cn.com.glsx.process.modules.mapper.SopTplNodeMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class SopTplNodeService {
@Autowired
private SopTplNodeMapper sopTplNodeMapper;
/**
* 根据id查询
*
* @param nodeId
* @return
*/
private SopTplNode findById(Integer nodeId) {
return sopTplNodeMapper.selectByPrimaryKey(nodeId);
}
/**
* 保存
*
* @param sopTplNode
* @return
*/
public int insert(SopTplNode sopTplNode) {
return sopTplNodeMapper.insertUseGeneratedKeys(sopTplNode);
}
/**
* 更新
*
* @param sopTplNode
* @return
*/
public boolean update(SopTplNode sopTplNode) {
int i = this.sopTplNodeMapper.updateByPrimaryKeySelective(sopTplNode);
if (i > 0) {
return true;
}
return false;
}
/**
* 根据id删除
*
* @param nodeId
* @return
*/
public boolean delete(Integer nodeId) {
int i = this.sopTplNodeMapper.deleteByPrimaryKey(nodeId);
if (i > 0) {
return true;
}
return false;
}
}
/*
* @Copyright (c) 2018 缪聪(mcg-helper@qq.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cn.com.glsx.process.plugin.java;
import javax.tools.SimpleJavaFileObject;
import java.net.URI;
public class CharSequenceJavaFileObject extends SimpleJavaFileObject {
private CharSequence content;
public CharSequenceJavaFileObject(String className,
CharSequence content) {
super(URI.create("string:///" + className.replace('.', '/')
+ Kind.SOURCE.extension), Kind.SOURCE);
this.content = content;
}
@Override
public CharSequence getCharContent(
boolean ignoreEncodingErrors) {
return content;
}
}
/*
* @Copyright (c) 2018 缪聪(mcg-helper@qq.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cn.com.glsx.process.plugin.java;
import javax.tools.FileObject;
import javax.tools.ForwardingJavaFileManager;
import javax.tools.JavaFileObject;
import javax.tools.StandardJavaFileManager;
import java.io.IOException;
public class ClassFileManager extends ForwardingJavaFileManager {
public JavaClassObject getJavaClassObject() {
return jclassObject;
}
private JavaClassObject jclassObject;
public ClassFileManager(StandardJavaFileManager
standardManager) {
super(standardManager);
}
@Override
public JavaFileObject getJavaFileForOutput(Location location,
String className, JavaFileObject.Kind kind, FileObject sibling)
throws IOException {
jclassObject = new JavaClassObject(className, kind);
return jclassObject;
}
}
/*
* @Copyright (c) 2018 缪聪(mcg-helper@qq.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cn.com.glsx.process.plugin.java;
import java.net.URL;
import java.net.URLClassLoader;
public class DynamicClassLoader extends URLClassLoader {
public DynamicClassLoader(ClassLoader parent) {
super(new URL[0], parent);
}
public Class<?> findClassByClassName(String className) throws ClassNotFoundException {
return this.findClass(className);
}
public Class<?> loadClass(String fullName, JavaClassObject jco) {
byte[] classData = jco.getBytes();
return this.defineClass(fullName, classData, 0, classData.length);
}
}
/*
* @Copyright (c) 2018 缪聪(mcg-helper@qq.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cn.com.glsx.process.plugin.java;
import cn.com.glsx.process.common.constant.Constants;
import javax.tools.*;
import java.io.File;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;
import java.util.ArrayList;
import java.util.List;
public class DynamicEngine {
private static DynamicEngine ourInstance = new DynamicEngine();
public static DynamicEngine getInstance() {
return ourInstance;
}
private URLClassLoader parentClassLoader;
private String classpath;
private DynamicEngine() {
this.parentClassLoader = (URLClassLoader) this.getClass().getClassLoader();
this.buildClassPath();
}
private void buildClassPath() {
this.classpath = null;
StringBuilder sb = new StringBuilder();
for (URL url : this.parentClassLoader.getURLs()) {
String p = url.getFile();
sb.append(p).append(File.pathSeparator);
}
this.classpath = sb.toString();
}
/**
* 调用类方法
* @param cls 类
* @param methodName 方法名
* @param paramsCls 方法参数类型
* @param params 方法参数
* @return
* @throws SecurityException
* @throws NoSuchMethodException
* @throws IllegalAccessException
* @throws InstantiationException
* @throws InvocationTargetException
* @throws IllegalArgumentException
*/
public Object invoke(Class<?> cls, String methodName, Class<?>[] paramsCls, Object[] params) throws NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException{
Object result = null;
Method method = cls.getDeclaredMethod(methodName, paramsCls);
Object obj = cls.newInstance();
result = method.invoke(obj, params);
return result;
}
public Object execute(String fullClassName, String javaCode, String method, Class<?>[] paramsCls,Object[] params) throws Exception {
Object result = null;
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
DiagnosticCollector<JavaFileObject> diagnostics = new DiagnosticCollector<JavaFileObject>();
ClassFileManager fileManager = new ClassFileManager(compiler.getStandardFileManager(diagnostics, null, null));
List<JavaFileObject> jfiles = new ArrayList<JavaFileObject>();
jfiles.add(new CharSequenceJavaFileObject(fullClassName, javaCode));
List<String> options = new ArrayList<String>();
options.add("-encoding");
options.add(Constants.CHARSET.toString());
options.add("-classpath");
options.add(this.classpath);
JavaCompiler.CompilationTask task = compiler.getTask(null, fileManager, diagnostics, options, null, jfiles);
boolean success = task.call();
if (success) {
JavaClassObject jco = fileManager.getJavaClassObject();
DynamicClassLoader dynamicClassLoader = new DynamicClassLoader(this.parentClassLoader);
Class<?> clazz = dynamicClassLoader.loadClass(fullClassName,jco);
result = invoke(clazz, method, paramsCls, params);
// instance = clazz.newInstance();
dynamicClassLoader.close();
} else {
StringBuilder error = new StringBuilder();
for (Diagnostic<?> diagnostic : diagnostics.getDiagnostics()) {
error.append(compilePrint(diagnostic));
}
throw new Exception(error.toString());
}
return result;
}
private String compilePrint(Diagnostic<?> diagnostic) {
StringBuilder res = new StringBuilder();
res.append("Code:[" + diagnostic.getCode() + "]\n");
res.append("Kind:[" + diagnostic.getKind() + "]\n");
res.append("Position:[" + diagnostic.getPosition() + "]\n");
res.append("Start Position:[" + diagnostic.getStartPosition() + "]\n");
res.append("End Position:[" + diagnostic.getEndPosition() + "]\n");
res.append("Source:[" + diagnostic.getSource() + "]\n");
res.append("Message:[" + diagnostic.getMessage(null) + "]\n");
res.append("LineNumber:[" + diagnostic.getLineNumber() + "]\n");
res.append("ColumnNumber:[" + diagnostic.getColumnNumber() + "]\n");
return res.toString();
}
}
/*
* @Copyright (c) 2018 缪聪(mcg-helper@qq.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package cn.com.glsx.process.plugin.java;
import javax.tools.SimpleJavaFileObject;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URI;
public class JavaClassObject extends SimpleJavaFileObject {
protected final ByteArrayOutputStream bos = new ByteArrayOutputStream();
public JavaClassObject(String name, Kind kind) {
super(URI.create("string:///" + name.replace('.', '/') + kind.extension), kind);
}
public byte[] getBytes() {
return bos.toByteArray();
}
@Override
public OutputStream openOutputStream() throws IOException {
return bos;
}
}
......@@ -6,6 +6,9 @@ import cn.com.glsx.process.modules.service.SopDataSourceService;
import cn.com.glsx.process.modules.service.UserDemoService;
import cn.com.glsx.process.plugin.dbconn.FlowDataAdapterImpl;
import cn.com.glsx.process.plugin.dbconn.McgBizAdapter;
import cn.com.glsx.process.plugin.java.DynamicEngine;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
......@@ -34,8 +37,33 @@ public class TestApplication {
SopDataSource byId = sopDataSourceService.getById(1);
McgBizAdapter mcgBizAdapter = new FlowDataAdapterImpl(byId);
List<Map<String, Object>> maps = mcgBizAdapter.tableQuery("SELECT * FROM sop_data_source");
List<Map<String, Object>> maps = mcgBizAdapter.tableQuery("SELECT * FROM aa_bb");
System.out.println(maps);
}
@Test
public void testupdate() throws SQLException {
SopDataSource byId = sopDataSourceService.getById(1);
McgBizAdapter mcgBizAdapter = new FlowDataAdapterImpl(byId);
mcgBizAdapter.executeUpdate("SELECT * FROM aa_bb");
}
@Test
public void testjava() throws Exception {
DynamicEngine de = DynamicEngine.getInstance();
String source = "import com.alibaba.fastjson.JSON;\n" +
"\n" +
"public class Controller {\n" +
"\n" +
" public JSON execute(JSON param) {\n" +
" System.out.println(\"测试java\");\n" +
" return param;\n" +
" }\n" +
"}\n";
JSONObject jsonObject=new JSONObject();
jsonObject.put("id",22);
Object instance = de.execute("Controller", source, "execute", new Class[]{JSON.class}, new Object[]{jsonObject});
System.out.println(JSON.toJSONString(instance));
}
}
\ No newline at end of file
package cn.com.glsx.process.modules.converter;
import cn.com.glsx.process.modules.entity.SopInst;
import cn.com.glsx.process.modules.entity.SopTpl;
import cn.com.glsx.process.modules.model.SopTplDTO;
import javax.annotation.Generated;
@Generated(
value = "org.mapstruct.ap.MappingProcessor",
date = "2023-09-11T15:01:39+0800",
comments = "version: 1.5.2.Final, compiler: javac, environment: Java 1.8.0_261 (Oracle Corporation)"
)
public class SopTplConverterImpl implements SopTplConverter {
@Override
public SopTplDTO do2dto(SopTpl sopTpl) {
if ( sopTpl == null ) {
return null;
}
SopTplDTO sopTplDTO = new SopTplDTO();
sopTplDTO.setTplId( sopTpl.getTplId() );
sopTplDTO.setTplName( sopTpl.getTplName() );
sopTplDTO.setTplCron( sopTpl.getTplCron() );
sopTplDTO.setTplRemark( sopTpl.getTplRemark() );
sopTplDTO.setTplStatus( sopTpl.getTplStatus() );
sopTplDTO.setTplParamDef( sopTpl.getTplParamDef() );
sopTplDTO.setTplResultDef( sopTpl.getTplResultDef() );
sopTplDTO.setTplSingleton( sopTpl.getTplSingleton() );
sopTplDTO.setCreateTime( sopTpl.getCreateTime() );
sopTplDTO.setUpdateTime( sopTpl.getUpdateTime() );
return sopTplDTO;
}
@Override
public SopTpl dto2do(SopTplDTO sopTplDTO) {
if ( sopTplDTO == null ) {
return null;
}
SopTpl sopTpl = new SopTpl();
sopTpl.setTplId( sopTplDTO.getTplId() );
sopTpl.setTplName( sopTplDTO.getTplName() );
sopTpl.setTplCron( sopTplDTO.getTplCron() );
sopTpl.setTplRemark( sopTplDTO.getTplRemark() );
sopTpl.setTplStatus( sopTplDTO.getTplStatus() );
sopTpl.setTplParamDef( sopTplDTO.getTplParamDef() );
sopTpl.setTplResultDef( sopTplDTO.getTplResultDef() );
sopTpl.setTplSingleton( sopTplDTO.getTplSingleton() );
sopTpl.setCreateTime( sopTplDTO.getCreateTime() );
sopTpl.setUpdateTime( sopTplDTO.getUpdateTime() );
return sopTpl;
}
@Override
public SopInst do2inst(SopTpl sopTpl) {
if ( sopTpl == null ) {
return null;
}
SopInst sopInst = new SopInst();
sopInst.setInstName( sopTpl.getTplName() );
sopInst.setInstCron( sopTpl.getTplCron() );
sopInst.setInstRemark( sopTpl.getTplRemark() );
sopInst.setInstParam( sopTpl.getTplParamDef() );
sopInst.setCreateTime( sopTpl.getCreateTime() );
sopInst.setUpdateTime( sopTpl.getUpdateTime() );
return sopInst;
}
}
package cn.com.glsx.process.modules.converter;
import cn.com.glsx.process.modules.entity.SopInstLink;
import cn.com.glsx.process.modules.entity.SopTplLink;
import javax.annotation.Generated;
@Generated(
value = "org.mapstruct.ap.MappingProcessor",
date = "2023-09-11T15:01:39+0800",
comments = "version: 1.5.2.Final, compiler: javac, environment: Java 1.8.0_261 (Oracle Corporation)"
)
public class SopTplLinKConverterImpl implements SopTplLinKConverter {
@Override
public SopInstLink tpl2inst(SopTplLink link) {
if ( link == null ) {
return null;
}
SopInstLink sopInstLink = new SopInstLink();
sopInstLink.setSourceId( link.getSourceId() );
sopInstLink.setTargetId( link.getTargetId() );
return sopInstLink;
}
}
package cn.com.glsx.process.modules.converter;
import cn.com.glsx.process.modules.entity.SopInstNode;
import cn.com.glsx.process.modules.entity.SopTplNode;
import javax.annotation.Generated;
@Generated(
value = "org.mapstruct.ap.MappingProcessor",
date = "2023-09-11T15:01:39+0800",
comments = "version: 1.5.2.Final, compiler: javac, environment: Java 1.8.0_261 (Oracle Corporation)"
)
public class SopTplNodeConverterImpl implements SopTplNodeConverter {
@Override
public SopInstNode tpl2inst(SopTplNode node) {
if ( node == null ) {
return null;
}
SopInstNode sopInstNode = new SopInstNode();
sopInstNode.setNodeParam( node.getNodeParamDef() );
sopInstNode.setNodeContent( node.getNodeContentDef() );
sopInstNode.setNodeId( node.getNodeId() );
sopInstNode.setNodeName( node.getNodeName() );
sopInstNode.setNodeType( node.getNodeType() );
sopInstNode.setNodeLabel( node.getNodeLabel() );
sopInstNode.setNodeKey( node.getNodeKey() );
sopInstNode.setNodeShow( node.getNodeShow() );
sopInstNode.setNodeVisible( node.getNodeVisible() );
sopInstNode.setNodeCron( node.getNodeCron() );
sopInstNode.setNodeExpireCron( node.getNodeExpireCron() );
return sopInstNode;
}
}
package cn.com.glsx.process.modules.converter;
import cn.com.glsx.process.modules.entity.User;
import cn.com.glsx.process.modules.model.UserBO;
import cn.com.glsx.process.modules.model.UserDTO;
import javax.annotation.Generated;
@Generated(
value = "org.mapstruct.ap.MappingProcessor",
date = "2023-09-11T15:01:39+0800",
comments = "version: 1.5.2.Final, compiler: javac, environment: Java 1.8.0_261 (Oracle Corporation)"
)
public class UserConverterImpl implements UserConverter {
@Override
public UserDTO do2dto(User user) {
if ( user == null ) {
return null;
}
UserDTO userDTO = new UserDTO();
userDTO.setId( user.getId() );
userDTO.setAccount( user.getAccount() );
userDTO.setUsername( user.getUsername() );
userDTO.setRealname( user.getRealname() );
userDTO.setPassword( user.getPassword() );
userDTO.setPhone( user.getPhone() );
return userDTO;
}
@Override
public User dto2do(UserDTO userDTO) {
if ( userDTO == null ) {
return null;
}
User user = new User();
user.setId( userDTO.getId() );
user.setAccount( userDTO.getAccount() );
user.setPassword( userDTO.getPassword() );
user.setUsername( userDTO.getUsername() );
user.setRealname( userDTO.getRealname() );
user.setPhone( userDTO.getPhone() );
return user;
}
@Override
public UserBO do2bo(User user) {
if ( user == null ) {
return null;
}
UserBO userBO = new UserBO();
userBO.setRealName( user.getRealname() );
userBO.setId( user.getId() );
userBO.setAccount( user.getAccount() );
userBO.setPassword( user.getPassword() );
userBO.setUsername( user.getUsername() );
userBO.setPhone( user.getPhone() );
userBO.setAvatar( user.getAvatar() );
return userBO;
}
@Override
public User bo2do(UserBO userBO) {
if ( userBO == null ) {
return null;
}
User user = new User();
user.setRealname( userBO.getRealName() );
user.setId( userBO.getId() );
user.setAccount( userBO.getAccount() );
user.setPassword( userBO.getPassword() );
user.setUsername( userBO.getUsername() );
user.setPhone( userBO.getPhone() );
user.setAvatar( userBO.getAvatar() );
return user;
}
}
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!