JBPM源码分析(二)-----JbpmContext

在上一节中我们分析了jbpmConfiguration,这里我们再来看另外一个重要的类;JbpmContext:

我们首先看下JbpmContext是如何生成的:


  1.  public JbpmContext createJbpmContext() {
  2.     //如果是空的话,可以生成一个默认的JbpmContext
  3.     return createJbpmContext(JbpmContext.DEFAULT_JBPM_CONTEXT_NAME);
  4.   }
  5.   public JbpmContext createJbpmContext(String name) {
  6.     JbpmContext jbpmContext = (JbpmContext) objectFactory.createObject(name);
  7.     jbpmContext.jbpmConfiguration = this;
  8.     jbpmContextCreated(jbpmContext);
  9.     return jbpmContext;
  10.   }
接下来,我们看一下JbpmConfiguration的 对象工厂是如何利用ObjectInfo生成object的;

  1.   public synchronized Object createObject(String name) {
  2.     ObjectInfo objectInfo = (ObjectInfo)namedObjectInfos.get(name);
  3.     if (objectInfo==null) {
  4.       throw new ConfigurationException("name '"+name+"' is not defined in the configuration. configured names: "+namedObjectInfos.keySet());
  5.     }
  6.     //会将这个对象生成的任务委托给ObjectInfo
  7.     return createObject(objectInfo);
  8.   }
  1.   public Object createObject(ObjectInfo objectInfo) {
  2.     //并且提供相应的管理;
  3.     clearRegistry();
  4.     return getObject(objectInfo);
  5.   }
  6.   
  1.   Object getObject(ObjectInfo objectInfo) {
  2.     Object object = null;
  3.     Object registryKey = getRegistryKey(objectInfo);
  4.     if (isInRegistry(registryKey)) {
  5.       object = findInRegistry(registryKey);
  6.       
  7.     } else {
  8.       if (registryKey!=null) {
  9.         if (objectsUnderConstruction.contains(registryKey)) {
  10.           throw new JbpmException("circular object dependency on bean '"+registryKey+"'");
  11.         }
  12.         objectsUnderConstruction.add(registryKey);
  13.         try {
  14.           object = objectInfo.createObject(this);
  15.         } finally {
  16.           objectsUnderConstruction.remove(registryKey);
  17.         }
  18.       
  19.         putInRegistry(objectInfo, object, registryKey);
  20.       
  21.       } else {
  22.         //真正有能力生成对象的是ObjectInfo;
  23.         object = objectInfo.createObject(this);
  24.       }
  25.     }
  26.     return object;
  27.   }
我们看下ContextBeanInfo是如何生成对象的;

  1. public synchronized Object createObject(ObjectFactoryImpl objectFactory) {
  2.     if (serviceFactories==null) {
  3.       serviceFactories = new HashMap();
  4.       //这个contextInfo把context元素下面的子元素存储到了一个
  5.       //名字和工厂类名为键值的hashMap中;
  6.       Iterator iter = serviceFactoryObjectInfos.entrySet().iterator();
  7.       while (iter.hasNext()) {
  8.         Map.Entry entry = (Map.Entry) iter.next();
  9.         String serviceName = (String) entry.getKey();
  10.         //根据工厂类名生成工厂;
  11.         ObjectInfo serviceFactoryObjectInfo = (ObjectInfo) entry.getValue();
  12.         serviceFactories.put(serviceName, serviceFactoryObjectInfo.createObject(objectFactory));
  13.       }
  14.     }
  15.     if ( (saveOperations==null)
  16.          && (saveOperationObjectInfos!=null)
  17.        ) {
  18.       saveOperations = new ArrayList();
  19.       for (int i=0; i<saveOperationObjectInfos.length; i++) {
  20.         Object saveOperation = saveOperationObjectInfos[i].createObject(objectFactory);
  21.         saveOperations.add(saveOperation);
  22.       }
  23.     }
  24.     //根据服务工厂存储到services中,等待以后上下文能够利用其生成服务;
  25.     Services services = new Services(serviceFactories, serviceNames, saveOperations);
  26.     
  27.     if (log.isDebugEnabled()) log.debug("creating jbpm context with service factories '"+serviceFactories.keySet()+"'");
  28.     return new JbpmContext(services, objectFactory);
  29.   }


一次完美的策略模式的应用;
JbpmContextInfo的xml格式如下:

  1.   <jbpm-context>
  2.     <service name="persistence" factory="org.jbpm.persistence.db.DbPersistenceServiceFactory" />
  3.     <service name="tx" factory="org.jbpm.tx.TxServiceFactory" />
  4.     <service name="message" factory="org.jbpm.msg.db.DbMessageServiceFactory" />
  5.     <service name="scheduler" factory="org.jbpm.scheduler.db.DbSchedulerServiceFactory" />
  6.     <service name="logging" factory="org.jbpm.logging.db.DbLoggingServiceFactory" />
  7.     <service name="authentication" factory="org.jbpm.security.authentication.DefaultAuthenticationServiceFactory" />
  8.   </jbpm-context>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值