天天看點

snakerflow 流程引擎,并發bug修複

使用中發現的問題。

/* Copyright 2013-2015 www.snakerflow.com.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.snaker.engine.parser;

import java.util.List;

import org.snaker.engine.helper.XmlHelper;
import org.snaker.engine.model.NodeModel;
import org.snaker.engine.model.TransitionModel;
import org.w3c.dom.Element;

/**
 * 修複節點parse全局變量,并發引起的bug
 * 抽象dom節點解析類 完成通用的屬性、變遷解析
 * 
 * @author yuqs
 * @since 1.0
 */
public abstract class AbstractNodeParser implements NodeParser {

	/**
	 * 實作NodeParser接口的parse函數
	 * 由子類産生各自的模型對象,設定常用的名稱屬性,并且解析子節點transition,構造TransitionModel模型對象
	 */
	public NodeModel parse(Element element) {
		/**
                 * 修複并發下bug
		 * NodeModel,每次傳回新執行個體
		 */
		NodeModel model = newModel();
		model.setName(element.getAttribute(ATTR_NAME));
		model.setDisplayName(element.getAttribute(ATTR_DISPLAYNAME));
		model.setLayout(element.getAttribute(ATTR_LAYOUT));
		model.setPreInterceptors(element.getAttribute(ATTR_PREINTERCEPTORS));
		model.setPostInterceptors(element.getAttribute(ATTR_POSTINTERCEPTORS));

		List<Element> transitions = XmlHelper.elements(element, NODE_TRANSITION);
		for (Element te : transitions) {
			TransitionModel transition = new TransitionModel();
			transition.setName(te.getAttribute(ATTR_NAME));
			transition.setDisplayName(te.getAttribute(ATTR_DISPLAYNAME));
			transition.setTo(te.getAttribute(ATTR_TO));
			transition.setExpr(te.getAttribute(ATTR_EXPR));
			transition.setG(te.getAttribute(ATTR_G));
			transition.setOffset(te.getAttribute(ATTR_OFFSET));
			transition.setSource(model);
			model.getOutputs().add(transition);
		}

		parseNode(model, element);
		return model;
	}

	/**
	 * 子類可覆寫此方法,完成特定的解析
	 * 
	 * @param model
	 * @param element
	 */
	protected void parseNode(NodeModel model, Element element) {

	}

	/**
	 * 抽象方法,由子類産生各自的模型對象
	 * 
	 * @return
	 */
	protected abstract NodeModel newModel();

	/**
	 * 傳回模型對象
	 */
	public NodeModel getModel() {
		return null;
	}
}