在上一节中我们分析了jbpmConfiguration,这里我们再来看另外一个重要的类;JbpmContext:
我们首先看下JbpmContext是如何生成的:
接下来,我们看一下JbpmConfiguration的
对象工厂是如何利用ObjectInfo生成object的;
我们看下ContextBeanInfo是如何生成对象的;
一次完美的策略模式的应用;
JbpmContextInfo的xml格式如下:
我们首先看下JbpmContext是如何生成的:
- public JbpmContext createJbpmContext() {
- //如果是空的话,可以生成一个默认的JbpmContext
- return createJbpmContext(JbpmContext.DEFAULT_JBPM_CONTEXT_NAME);
- }
- public JbpmContext createJbpmContext(String name) {
- JbpmContext jbpmContext = (JbpmContext) objectFactory.createObject(name);
- jbpmContext.jbpmConfiguration = this;
- jbpmContextCreated(jbpmContext);
- return jbpmContext;
- }
- public synchronized Object createObject(String name) {
- ObjectInfo objectInfo = (ObjectInfo)namedObjectInfos.get(name);
- if (objectInfo==null) {
- throw new ConfigurationException("name '"+name+"' is not defined in the configuration. configured names: "+namedObjectInfos.keySet());
- }
- //会将这个对象生成的任务委托给ObjectInfo
- return createObject(objectInfo);
- }
- public Object createObject(ObjectInfo objectInfo) {
- //并且提供相应的管理;
- clearRegistry();
- return getObject(objectInfo);
- }
- Object getObject(ObjectInfo objectInfo) {
- Object object = null;
- Object registryKey = getRegistryKey(objectInfo);
- if (isInRegistry(registryKey)) {
- object = findInRegistry(registryKey);
- } else {
- if (registryKey!=null) {
- if (objectsUnderConstruction.contains(registryKey)) {
- throw new JbpmException("circular object dependency on bean '"+registryKey+"'");
- }
- objectsUnderConstruction.add(registryKey);
- try {
- object = objectInfo.createObject(this);
- } finally {
- objectsUnderConstruction.remove(registryKey);
- }
- putInRegistry(objectInfo, object, registryKey);
- } else {
- //真正有能力生成对象的是ObjectInfo;
- object = objectInfo.createObject(this);
- }
- }
- return object;
- }
- public synchronized Object createObject(ObjectFactoryImpl objectFactory) {
- if (serviceFactories==null) {
- serviceFactories = new HashMap();
- //这个contextInfo把context元素下面的子元素存储到了一个
- //名字和工厂类名为键值的hashMap中;
- Iterator iter = serviceFactoryObjectInfos.entrySet().iterator();
- while (iter.hasNext()) {
- Map.Entry entry = (Map.Entry) iter.next();
- String serviceName = (String) entry.getKey();
- //根据工厂类名生成工厂;
- ObjectInfo serviceFactoryObjectInfo = (ObjectInfo) entry.getValue();
- serviceFactories.put(serviceName, serviceFactoryObjectInfo.createObject(objectFactory));
- }
- }
- if ( (saveOperations==null)
- && (saveOperationObjectInfos!=null)
- ) {
- saveOperations = new ArrayList();
- for (int i=0; i<saveOperationObjectInfos.length; i++) {
- Object saveOperation = saveOperationObjectInfos[i].createObject(objectFactory);
- saveOperations.add(saveOperation);
- }
- }
- //根据服务工厂存储到services中,等待以后上下文能够利用其生成服务;
- Services services = new Services(serviceFactories, serviceNames, saveOperations);
- if (log.isDebugEnabled()) log.debug("creating jbpm context with service factories '"+serviceFactories.keySet()+"'");
- return new JbpmContext(services, objectFactory);
- }
一次完美的策略模式的应用;
JbpmContextInfo的xml格式如下:
- <jbpm-context>
- <service name="persistence" factory="org.jbpm.persistence.db.DbPersistenceServiceFactory" />
- <service name="tx" factory="org.jbpm.tx.TxServiceFactory" />
- <service name="message" factory="org.jbpm.msg.db.DbMessageServiceFactory" />
- <service name="scheduler" factory="org.jbpm.scheduler.db.DbSchedulerServiceFactory" />
- <service name="logging" factory="org.jbpm.logging.db.DbLoggingServiceFactory" />
- <service name="authentication" factory="org.jbpm.security.authentication.DefaultAuthenticationServiceFactory" />
- </jbpm-context>