Skip to content

Commit 7a6afe1

Browse files
author
yusu
committed
optimize and prepare to add starter
1 parent 2a135e4 commit 7a6afe1

File tree

11 files changed

+158
-147
lines changed

11 files changed

+158
-147
lines changed

src/main/java/com/alibaba/compileflow/engine/ProcessEngineFactory.java

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,35 +20,29 @@
2020
import com.alibaba.compileflow.engine.process.impl.BpmnStatelessProcessEngineImpl;
2121
import com.alibaba.compileflow.engine.process.impl.TbbpmStatelessProcessEngineImpl;
2222

23-
import java.util.HashMap;
2423
import java.util.Map;
24+
import java.util.concurrent.ConcurrentHashMap;
2525

2626
/**
2727
* @author wuxiang
2828
* @author yusu
2929
*/
3030
public class ProcessEngineFactory {
3131

32-
private static final Map<FlowModelType, ProcessEngine> STATELESS_PROCESS_ENGINES = new HashMap<>();
32+
private static final Map<FlowModelType, ProcessEngine> STATELESS_PROCESS_ENGINES = new ConcurrentHashMap<>();
3333

3434
public static ProcessEngine getProcessEngine() {
3535
return getStatelessProcessEngine(FlowModelType.TBBPM);
3636
}
3737

3838
public static ProcessEngine getStatelessProcessEngine(FlowModelType flowModelType) {
39-
40-
if (STATELESS_PROCESS_ENGINES.get(flowModelType) == null) {
41-
synchronized (STATELESS_PROCESS_ENGINES) {
42-
if (STATELESS_PROCESS_ENGINES.get(flowModelType) == null) {
43-
if (FlowModelType.BPMN.equals(flowModelType)) {
44-
STATELESS_PROCESS_ENGINES.put(flowModelType, new BpmnStatelessProcessEngineImpl());
45-
} else {
46-
STATELESS_PROCESS_ENGINES.put(flowModelType, new TbbpmStatelessProcessEngineImpl());
47-
}
48-
}
39+
return STATELESS_PROCESS_ENGINES.computeIfAbsent(flowModelType, flowModelType1 -> {
40+
if (FlowModelType.BPMN.equals(flowModelType)) {
41+
return new BpmnStatelessProcessEngineImpl();
42+
} else {
43+
return new TbbpmStatelessProcessEngineImpl();
4944
}
50-
}
51-
return STATELESS_PROCESS_ENGINES.get(flowModelType);
45+
});
5246
}
5347

5448
}

src/main/java/com/alibaba/compileflow/engine/common/utils/DataType.java

Lines changed: 97 additions & 79 deletions
Large diffs are not rendered by default.

src/main/java/com/alibaba/compileflow/engine/process/impl/AbstractProcessEngine.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,14 @@ public abstract class AbstractProcessEngine<T extends FlowModel<? extends Transi
4848

4949
@Override
5050
public void preCompile(String... codes) {
51-
if (ArrayUtils.isNotEmpty(codes)) {
52-
for (String code : codes) {
53-
AbstractProcessRuntime runtime = getProcessRuntime(code);
54-
runtime.compile();
55-
}
56-
} else {
51+
if (ArrayUtils.isEmpty(codes)) {
5752
throw new CompileFlowException("No process to compile");
5853
}
54+
55+
for (String code : codes) {
56+
AbstractProcessRuntime runtime = getProcessRuntime(code);
57+
runtime.compile();
58+
}
5959
}
6060

6161
@Override
@@ -70,7 +70,7 @@ protected <R extends AbstractProcessRuntime> R getProcessRuntime(String code) {
7070
String cacheKey = getCacheKey(code);
7171
AbstractProcessRuntime runtime = runtimeCache.computeIfAbsent(cacheKey, c ->
7272
getCompiledRuntime(code));
73-
return (R)runtime;
73+
return (R) runtime;
7474
}
7575

7676
private AbstractProcessRuntime getCompiledRuntime(String code) {
@@ -89,10 +89,9 @@ private AbstractProcessRuntime getRuntimeFromSource(String code) {
8989
@Override
9090
@SuppressWarnings("unchecked")
9191
public T load(String code) {
92-
FlowModelType flowModelType = getFlowModelType();
93-
FlowStreamSource flowStreamSource = loadFlowModel(code, flowModelType);
92+
FlowStreamSource flowStreamSource = loadFlowSource(code);
9493

95-
T flowModel = (T)getFlowModelConverter().convertToModel(flowStreamSource);
94+
T flowModel = (T) getFlowModelConverter().convertToModel(flowStreamSource);
9695
if (flowModel == null) {
9796
throw new RuntimeException("No valid flow model found, code is " + code);
9897
}
@@ -116,13 +115,14 @@ public String getTestCode(String code) {
116115
return runtime.generateTestCode();
117116
}
118117

119-
private FlowStreamSource loadFlowModel(String code, FlowModelType flowModelType) {
120-
String filePath = convertPackagePath(code, flowModelType);
118+
private FlowStreamSource loadFlowSource(String code) {
119+
String filePath = convertToFilePath(code);
121120
return ResourceFlowStreamSource.of(filePath);
122121
}
123122

124-
private String convertPackagePath(String code, FlowModelType flowModelType) {
123+
private String convertToFilePath(String code) {
125124
String path = code.replace(".", "/");
125+
FlowModelType flowModelType = getFlowModelType();
126126
return path + getBpmFileSuffix(flowModelType);
127127
}
128128

@@ -157,7 +157,7 @@ private void checkContinuous(TransitionNode node, List<TransitionNode> visitedNo
157157

158158
@SuppressWarnings("unchecked")
159159
private void checkCycle(T flowModel) {
160-
DirectedGraph directedGraph = new DirectedGraph();
160+
DirectedGraph<TransitionNode> directedGraph = new DirectedGraph<>();
161161
for (TransitionNode node : flowModel.getAllNodes()) {
162162
List<TransitionNode> outgoingNodes = node.getOutgoingNodes();
163163
if (CollectionUtils.isNotEmpty(outgoingNodes)) {

src/main/java/com/alibaba/compileflow/engine/process/preruntime/compiler/impl/CompilerImpl.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818

1919
import com.alibaba.compileflow.engine.common.CompileFlowException;
2020
import com.alibaba.compileflow.engine.common.utils.FileUtils;
21-
import com.alibaba.compileflow.engine.process.preruntime.compiler.*;
2221
import com.alibaba.compileflow.engine.process.preruntime.compiler.Compiler;
22+
import com.alibaba.compileflow.engine.process.preruntime.compiler.*;
2323
import com.alibaba.compileflow.engine.process.preruntime.compiler.impl.support.EcJavaCompiler;
2424

2525
import java.io.File;
@@ -31,7 +31,7 @@
3131
*/
3232
public class CompilerImpl implements Compiler {
3333

34-
private static JavaCompiler javaCompiler = new EcJavaCompiler();
34+
private static final JavaCompiler JAVA_COMPILER = new EcJavaCompiler();
3535

3636
@Override
3737
public Class<?> compileJavaCode(String fullClassName, String sourceCode) {
@@ -49,7 +49,7 @@ public Class<?> compileJavaCode(String fullClassName, String sourceCode) {
4949
File javaSourceFile = writeJavaFile(dirFile, fullClassName, sourceCode);
5050
JavaSource javaSource = JavaSource.of(javaSourceFile, sourceCode, fullClassName);
5151

52-
javaCompiler.compile(javaSource, new File(dirPath), new CompileOption());
52+
JAVA_COMPILER.compile(javaSource, new File(dirPath), new CompileOption());
5353

5454
File classFile = new File(dirFile, fullClassName.replace('.', File.separatorChar) + ".class");
5555
byte[] classBytes = FileUtils.readFileToByteArray(classFile);

src/main/java/com/alibaba/compileflow/engine/process/preruntime/compiler/impl/support/EcJavaCompiler.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@
2121
import com.alibaba.compileflow.engine.process.preruntime.compiler.JavaCompiler;
2222
import com.alibaba.compileflow.engine.process.preruntime.compiler.JavaSource;
2323
import org.eclipse.jdt.core.compiler.IProblem;
24-
import org.eclipse.jdt.internal.compiler.*;
2524
import org.eclipse.jdt.internal.compiler.Compiler;
25+
import org.eclipse.jdt.internal.compiler.*;
2626
import org.eclipse.jdt.internal.compiler.classfmt.ClassFileReader;
2727
import org.eclipse.jdt.internal.compiler.env.ICompilationUnit;
2828
import org.eclipse.jdt.internal.compiler.env.INameEnvironment;
@@ -248,9 +248,9 @@ private String getErrorMsg(File javaFile, String className, Collection<IProblem>
248248
}
249249

250250
static class CompilationUnit implements ICompilationUnit {
251-
private String className;
252-
private String sourceFile;
253-
private String encode;
251+
private final String className;
252+
private final String sourceFile;
253+
private final String encode;
254254

255255
CompilationUnit(String sourceFile, String className, String encode) {
256256
this.className = className;

src/main/java/com/alibaba/compileflow/engine/process/preruntime/converter/impl/writer/support/tbbpm/AbstractTbbpmActionNodeWriter.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ protected void writeAction(IAction action, XMLStreamWriter xsw) throws Exception
4848
xsw.writeEndElement();
4949
}
5050

51+
@SuppressWarnings("unchecked")
5152
private void writeActionHandle(IActionHandle actionHandle, XMLStreamWriter xsw) throws Exception {
5253
if (actionHandle == null) {
5354
return;

src/main/java/com/alibaba/compileflow/engine/process/preruntime/generator/bean/SpringApplicationContextProvider.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,11 @@
2525
*/
2626
public class SpringApplicationContextProvider implements ApplicationContextAware {
2727

28-
private static ApplicationContext context;
29-
30-
public static ApplicationContext getContext() {
31-
return context;
32-
}
28+
public static ApplicationContext applicationContext;
3329

3430
@Override
3531
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
36-
context = applicationContext;
32+
SpringApplicationContextProvider.applicationContext = applicationContext;
3733
}
3834

3935
}

src/main/java/com/alibaba/compileflow/engine/process/preruntime/generator/provider/AbstractNodeGeneratorProvider.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
public abstract class AbstractNodeGeneratorProvider implements NodeGeneratorProvider {
3333

3434
protected AbstractProcessRuntime runtime;
35-
private Map<String, Generator> generatorMap = new HashMap<>();
35+
private final Map<String, Generator> generatorMap = new HashMap<>();
3636

3737
public AbstractNodeGeneratorProvider(AbstractProcessRuntime runtime) {
3838
this.runtime = runtime;

src/main/java/com/alibaba/compileflow/engine/process/preruntime/validator/factory/ModelValidatorFactory.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@
2424

2525
public class ModelValidatorFactory {
2626

27-
private static FlowModelValidator bpmnModelValidator = new BpmnModelValidator();
27+
private static final FlowModelValidator bpmnModelValidator = new BpmnModelValidator();
2828

29-
private static FlowModelValidator tbbpmModelValidator = new TbbpmModelValidator();
29+
private static final FlowModelValidator tbbpmModelValidator = new TbbpmModelValidator();
3030

3131
public static FlowModelValidator getFlowModelValidator(FlowModelType flowModelType) {
3232

src/main/java/com/alibaba/compileflow/engine/runtime/impl/AbstractProcessRuntime.java

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -71,21 +71,21 @@
7171
public abstract class AbstractProcessRuntime<T extends FlowModel> implements ProcessRuntime {
7272

7373
private static final Compiler COMPILER = new CompilerImpl();
74-
private static AtomicBoolean inited = new AtomicBoolean(false);
74+
private static final AtomicBoolean inited = new AtomicBoolean(false);
7575
protected final Map<String, List<TransitionNode>> followingGraph = new HashMap<>();
7676
protected final Map<String, List<TransitionNode>> branchGraph = new HashMap<>();
7777
private final Map<String, String> javaCodeCache = new ConcurrentHashMap<>();
78-
private final Map<String, Class> compiledClassCache = new ConcurrentHashMap<>();
78+
private final Map<String, Class<?>> compiledClassCache = new ConcurrentHashMap<>();
7979
protected T flowModel;
8080
protected ClassTarget classTarget;
8181
protected NodeGeneratorProvider nodeGeneratorProvider;
8282
protected String code;
83-
private String id;
84-
private String name;
83+
private final String id;
84+
private final String name;
8585
private List<IVar> vars;
86-
private List<IVar> paramVars;
87-
private List<IVar> returnVars;
88-
private List<IVar> innerVars;
86+
private final List<IVar> paramVars;
87+
private final List<IVar> returnVars;
88+
private final List<IVar> innerVars;
8989

9090
@SuppressWarnings("unchecked")
9191
public AbstractProcessRuntime(T flowModel) {
@@ -118,7 +118,7 @@ public Map<String, List<TransitionNode>> getBranchGraph() {
118118
}
119119

120120
public <P extends NodeGeneratorProvider> P getNodeGeneratorProvider() {
121-
return (P)nodeGeneratorProvider;
121+
return (P) nodeGeneratorProvider;
122122
}
123123

124124
public void compile() {
@@ -157,13 +157,15 @@ public String generateTestCode() {
157157
method.addAnnotation("@Test");
158158
method.addException(ClassWrapper.of(Exception.class));
159159
method.addBodyLine("String code = \"" + code + "\";");
160-
String engineCode = isStateless() && isBpmn20() ? "StatelessProcessEngine engine = "
161-
+ "ProcessEngineFactory.getStatelessProcessEngine(FlowModelType.BPMN);"
162-
: !isStateless() && !isBpmn20() ? "StatelessProcessEngine engine = "
163-
+ "ProcessEngineFactory.getStatefulProcessEngine();"
164-
: !isStateless() && isBpmn20() ? "StatelessProcessEngine engine = "
165-
+ "ProcessEngineFactory.getStatefulProcessEngine(FlowModelType.BPMN);"
166-
: "StatelessProcessEngine engine = ProcessEngineFactory.getProcessEngine();";
160+
// String engineCode = isStateless() && isBpmn20() ? "StatelessProcessEngine engine = "
161+
// + "ProcessEngineFactory.getStatelessProcessEngine(FlowModelType.BPMN);"
162+
// : !isStateless() && !isBpmn20() ? "StatelessProcessEngine engine = "
163+
// + "ProcessEngineFactory.getStatefulProcessEngine();"
164+
// : !isStateless() && isBpmn20() ? "StatelessProcessEngine engine = "
165+
// + "ProcessEngineFactory.getStatefulProcessEngine(FlowModelType.BPMN);"
166+
// : "StatelessProcessEngine engine = ProcessEngineFactory.getProcessEngine();";
167+
String engineCode = isBpmn20() ? "ProcessEngine<BpmnModel> engine = ProcessEngineFactory.getProcessEngine(FlowModelType.BPMN);"
168+
: "ProcessEngine<TbbpmModel> engine = ProcessEngineFactory.getProcessEngine();";
167169
method.addBodyLine(engineCode);
168170
method.addBodyLine("System.out.println(engine.getJavaCode(code));");
169171
method.addBodyLine("Map<String, Object> context = new HashMap<String, Object>();");
@@ -229,7 +231,7 @@ private <T extends ProcessInstance> T getProcessInstance() {
229231
throw new CompileFlowException("Failed to get compile class, code is " + code);
230232
}
231233
try {
232-
return (T)ClassUtils.newInstance(clazz);
234+
return (T) ClassUtils.newInstance(clazz);
233235
} catch (Exception e) {
234236
throw new CompileFlowException("Failed to get process instance, code is " + code, e);
235237
}
@@ -297,8 +299,8 @@ private void addVars(List<IVar> vars) {
297299
for (IVar var : vars) {
298300
ClassWrapper rvType = ClassWrapper.of(var.getDataType());
299301
classTarget.addImportedType(rvType);
300-
String nullValue = DataType.getDefaultValueString(
301-
DataType.getJavaClass(var.getDataType()), var.getDefaultValue());
302+
String nullValue = DataType.getDefaultValueString(DataType.getJavaClass(var.getDataType()),
303+
var.getDefaultValue());
302304
classTarget.addField(rvType, var.getName(), nullValue);
303305
}
304306
}
@@ -337,7 +339,7 @@ protected List<ValidateMessage> validateFlowModel() {
337339
}
338340

339341
private void initBeanProvider() {
340-
ApplicationContext contex = SpringApplicationContextProvider.getContext();
342+
ApplicationContext contex = SpringApplicationContextProvider.applicationContext;
341343
SpringBeanHolder beanHolder = SpringBeanHolder.of(contex);
342344
BeanProvider.registerBeanHolder(beanHolder);
343345
}
@@ -416,15 +418,15 @@ private void addJavaTypes() {
416418
}
417419

418420
private void addExtImportedTypes() {
419-
List<Class> extImportedTypes = getExtImportedTypes();
421+
List<Class<?>> extImportedTypes = getExtImportedTypes();
420422
if (CollectionUtils.isNotEmpty(extImportedTypes)) {
421-
for (Class extImportedType : extImportedTypes) {
423+
for (Class<?> extImportedType : extImportedTypes) {
422424
classTarget.addImportedType(ClassWrapper.of(extImportedType));
423425
}
424426
}
425427
}
426428

427-
protected abstract List<Class> getExtImportedTypes();
429+
protected abstract List<Class<?>> getExtImportedTypes();
428430

429431
private void addImportedTypes() {
430432
classTarget.addImportedType(ClassWrapper.of(Map.class));
@@ -434,4 +436,4 @@ private void addImportedTypes() {
434436
classTarget.addImportedType(ClassWrapper.of(BeanProvider.class));
435437
}
436438

437-
}
439+
}

0 commit comments

Comments
 (0)