flowable引擎类源码分析

 

首先我们看ProcessEngines的四个集合

flowable引擎类源码分析_第1张图片

四个的存的值:

flowable引擎类源码分析_第2张图片

下面看下具体流程:

第一步,执行getDefaultProcessEngine方法:

public static final String NAME_DEFAULT = "default";

public static ProcessEngine getDefaultProcessEngine() {
        return getProcessEngine(NAME_DEFAULT);
}

第二部,看是否已经被初始化,如果没有初始化执行初始化方法,若已被初始化则从定义的

protected static Map processEngines = new HashMap<>()中获取:
public static ProcessEngine getProcessEngine(String processEngineName) {
        if (!isInitialized()) {
            init();
        }
        return processEngines.get(processEngineName);
}

第三部执行init()方法:

 public static synchronized void init() {
        if (!isInitialized()) {
            if (processEngines == null) {
                // 如果为空直接new一个
                processEngines = new HashMap<>();
            }
            ClassLoader classLoader = ReflectUtil.getClassLoader();
            Enumeration 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 configUrls = new HashSet<>();
            while (resources.hasMoreElements()) {
                configUrls.add(resources.nextElement());
            }
            for (Iterator iterator = configUrls.iterator(); iterator.hasNext();) {
                URL resource = iterator.next();
                LOGGER.info("Initializing process engine using configuration '{}'", resource);
                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);
                initProcessEngineFromSpringResource(resource);
            }

            setInitialized(true);
        } else {
            LOGGER.info("Process engines already initialized");
        }
}

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 + ": " + e.getMessage(), e);
        }
}


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);
        }
}

第四部,通过ProcessEngineConfiguration创建ProcessEngine

//"processEngineConfiguration"是从flowable.cfg.xml中获取
public static ProcessEngineConfiguration createProcessEngineConfigurationFromInputStream(InputStream inputStream) {
        return createProcessEngineConfigurationFromInputStream(inputStream, "processEngineConfiguration");
}
//通过BeansConfigurationHelper助手类
public static ProcessEngineConfiguration createProcessEngineConfigurationFromInputStream(InputStream inputStream, String beanName) {
        return (ProcessEngineConfiguration) BeansConfigurationHelper.parseEngineConfigurationFromInputStream(inputStream, beanName);
    }
//为spring-core中的类实例化bean
public class BeansConfigurationHelper {

    public static AbstractEngineConfiguration parseEngineConfiguration(Resource springResource, String beanName) {
        DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
        XmlBeanDefinitionReader xmlBeanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);
        xmlBeanDefinitionReader.setValidationMode(XmlBeanDefinitionReader.VALIDATION_XSD);
        xmlBeanDefinitionReader.loadBeanDefinitions(springResource);
        AbstractEngineConfiguration engineConfiguration = (AbstractEngineConfiguration) beanFactory.getBean(beanName);
        engineConfiguration.setBeans(new SpringBeanFactoryProxyMap(beanFactory));
        return engineConfiguration;
    }

    public static AbstractEngineConfiguration parseEngineConfigurationFromInputStream(InputStream inputStream, String beanName) {
        Resource springResource = new InputStreamResource(inputStream);
        return parseEngineConfiguration(springResource, beanName);
    }

    public static AbstractEngineConfiguration parseEngineConfigurationFromResource(String resource, String beanName) {
        Resource springResource = new ClassPathResource(resource);
        return parseEngineConfiguration(springResource, beanName);
    }

}
引擎配饰类加载的bean


        
        
        
        
//SpringBeanFactoryProxyMap 通过这个累获取bean
public class SpringBeanFactoryProxyMap implements Map {

    protected BeanFactory beanFactory;

    public SpringBeanFactoryProxyMap(BeanFactory beanFactory) {
        this.beanFactory = beanFactory;
    }

    @Override
    public Object get(Object key) {
        if ((key == null) || !String.class.isAssignableFrom(key.getClass())) {
            return null;
        }
        return beanFactory.getBean((String) key);
    }

    @Override
    public boolean containsKey(Object key) {
        if ((key == null) || !String.class.isAssignableFrom(key.getClass())) {
            return false;
        }
        return beanFactory.containsBean((String) key);
    }

    @Override
    public Set keySet() {
        throw new FlowableException("unsupported operation on configuration beans");
        // List beanNames =
        // Arrays.asList(beanFactory.getBeanDefinitionNames());
        // return new HashSet(beanNames);
    }

    @Override
    public void clear() {
        throw new FlowableException("can't clear configuration beans");
    }

    @Override
    public boolean containsValue(Object value) {
        throw new FlowableException("can't search values in configuration beans");
    }

    @Override
    public Set> entrySet() {
        throw new FlowableException("unsupported operation on configuration beans");
    }

    @Override
    public boolean isEmpty() {
        throw new FlowableException("unsupported operation on configuration beans");
    }

    @Override
    public Object put(Object key, Object value) {
        throw new FlowableException("unsupported operation on configuration beans");
    }

    @Override
    public void putAll(Map m) {
        throw new FlowableException("unsupported operation on configuration beans");
    }

    @Override
    public Object remove(Object key) {
        throw new FlowableException("unsupported operation on configuration beans");
    }

    @Override
    public int size() {
        throw new FlowableException("unsupported operation on configuration beans");
    }

    @Override
    public Collection values() {
        throw new FlowableException("unsupported operation on configuration beans");
    }
} 
  

 

你可能感兴趣的:(flowable,工作流,flowable)