flowable-engine.6.3.1.jar
org.flowable.engine.ProcessEngines
public abstract class ProcessEngines {
// key:流程引擎名称,value: 流程引擎实例
protected static Map<String, ProcessEngine> processEngines = new HashMap<>();
// key:流程引擎名称,value: 流程引擎信息类实例对象
protected static Map<String, EngineInfo> processEngineInfosByName = new HashMap<>();
// key:构造流程引擎的资源名称,value: 流程引擎信息类实例对象
protected static Map<String, EngineInfo> processEngineInfosByResourceUrl = new HashMap<>();
// 存储流程引擎信息类实例对象列表
protected static List<EngineInfo> processEngineInfos = new ArrayList<>();
}
入口方法 获取默认的流程引擎
public static final String NAME_DEFAULT = "default";
public static ProcessEngine getDefaultProcessEngine() {
return getProcessEngine(NAME_DEFAULT);
}
先判断是否初始化,
protected static boolean isInitialized;
public static boolean isInitialized() {
return isInitialized;
}
public static ProcessEngine getProcessEngine(String processEngineName) {
if (!isInitialized()) {
init();
}
return processEngines.get(processEngineName);
}
初始化ProcessEngine对象
public static synchronized void init() {
if (!isInitialized()) {
if (processEngines == null) {
// Create new map to store process-engines if current map is null
processEngines = new HashMap<>();
}
ClassLoader classLoader = ReflectUtil.getClassLoader();
Enumeration<URL> resources = null;
try {
resources = classLoader.getResources("flowable.cfg.xml");
} catch (IOException e) {
throw new FlowableIllegalArgumentException("problem retrieving flowable.cfg.xml resources on the classpath: " + System.getProperty("java.class.path"), e);
}
// Remove duplicated configuration URL's using set. Some
// classloaders may return identical URL's twice, causing duplicate
// startups
Set<URL> configUrls = new HashSet<>();
while (resources.hasMoreElements()) {
configUrls.add(resources.nextElement());
}
for (Iterator<URL> iterator = configUrls.iterator(); iterator.hasNext();) {
URL resource = iterator.next();
LOGGER.info("Initializing process engine using configuration '{}'", resource.toString());
initProcessEngineFromResource(resource);
}
try {
resources = classLoader.getResources("flowable-context.xml");
} catch (IOException e) {
throw new FlowableIllegalArgumentException("problem retrieving flowable-context.xml resources on the classpath: " + System.getProperty("java.class.path"), e);
}
while (resources.hasMoreElements()) {
URL resource = resources.nextElement();
LOGGER.info("Initializing process engine using Spring configuration '{}'", resource.toString());
initProcessEngineFromSpringResource(resource);
}
setInitialized(true);
} else {
LOGGER.info("Process engines already initialized");
}
}
/**
从资源对象获取引擎信息对象
如果processEngineInfosByResourceUrl存在对象,先移除旧的
processEngineInfos.remove
processEngines.remove
processEngineInfosByName.remove
然后依次加入新的
*/
private static EngineInfo initProcessEngineFromResource(URL resourceUrl) {
EngineInfo processEngineInfo = processEngineInfosByResourceUrl.get(resourceUrl.toString());
// if there is an existing process engine info
if (processEngineInfo != null) {
// remove that process engine from the member fields
processEngineInfos.remove(processEngineInfo);
if (processEngineInfo.getException() == null) {
String processEngineName = processEngineInfo.getName();
processEngines.remove(processEngineName);
processEngineInfosByName.remove(processEngineName);
}
processEngineInfosByResourceUrl.remove(processEngineInfo.getResourceUrl());
}
String resourceUrlString = resourceUrl.toString();
try {
LOGGER.info("initializing process engine for resource {}", resourceUrl);
ProcessEngine processEngine = buildProcessEngine(resourceUrl);
String processEngineName = processEngine.getName();
LOGGER.info("initialised process engine {}", processEngineName);
processEngineInfo = new EngineInfo(processEngineName, resourceUrlString, null);
processEngines.put(processEngineName, processEngine);
processEngineInfosByName.put(processEngineName, processEngineInfo);
} catch (Throwable e) {
LOGGER.error("Exception while initializing process engine: {}", e.getMessage(), e);
processEngineInfo = new EngineInfo(null, resourceUrlString, ExceptionUtils.getStackTrace(e));
}
processEngineInfosByResourceUrl.put(resourceUrlString, processEngineInfo);
processEngineInfos.add(processEngineInfo);
return processEngineInfo;
}
// bean工厂读取URL,获取引擎配置对象ProcessEngineConfiguration
// 然后ProcessEngineConfiguration构建ProcessEngine返回
private static ProcessEngine buildProcessEngine(URL resource) {
InputStream inputStream = null;
try {
inputStream = resource.openStream();
ProcessEngineConfiguration processEngineConfiguration = ProcessEngineConfiguration.createProcessEngineConfigurationFromInputStream(inputStream);
return processEngineConfiguration.buildProcessEngine();
} catch (IOException e) {
throw new FlowableIllegalArgumentException("couldn't open resource stream: " + e.getMessage(), e);
} finally {
IoUtil.closeSilently(inputStream);
}
}
//bean工厂读取InputStream,获取名称为"processEngineConfiguration"的ProcessEngineConfiguration对象
public static ProcessEngineConfiguration createProcessEngineConfigurationFromInputStream(InputStream inputStream) {
return createProcessEngineConfigurationFromInputStream(inputStream, "processEngineConfiguration");
}
public static ProcessEngineConfiguration createProcessEngineConfigurationFromInputStream(InputStream inputStream, String beanName) {
return (ProcessEngineConfiguration) BeansConfigurationHelper.parseEngineConfigurationFromInputStream(inputStream, beanName);
}
protected static void initProcessEngineFromSpringResource(URL resource) {
try {
Class<?> springConfigurationHelperClass = ReflectUtil.loadClass("org.flowable.spring.SpringConfigurationHelper");
Method method = springConfigurationHelperClass.getDeclaredMethod("buildProcessEngine", new Class<?>[] { URL.class });
ProcessEngine processEngine = (ProcessEngine) method.invoke(null, new Object[] { resource });
String processEngineName = processEngine.getName();
EngineInfo processEngineInfo = new EngineInfo(processEngineName, resource.toString(), null);
processEngineInfosByName.put(processEngineName, processEngineInfo);
processEngineInfosByResourceUrl.put(resource.toString(), processEngineInfo);
} catch (Exception e) {
throw new FlowableException("couldn't initialize process engine from spring configuration resource " + resource.toString() + ": " + e.getMessage(), e);
}
}