Java tutorial
/** * Copyright 1996-2014 FoxBPM ORG. * * 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. * * @author yangguangftlp */ package org.foxbpm.engine.impl.util; import java.io.InputStream; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import org.dom4j.Attribute; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; import org.dom4j.io.SAXReader; import org.foxbpm.engine.config.FoxBPMConfig; import org.foxbpm.engine.config.ProcessEngineConfigurator; import org.foxbpm.engine.impl.datavariable.DataObjectDefinitionImpl; import org.foxbpm.engine.impl.event.EventListenerImpl; import org.foxbpm.engine.impl.task.CommandParam; import org.foxbpm.engine.impl.task.TaskCommandDefinitionImpl; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * foxbpmcfg??? * * @author yangguangftlp * @date 20141028 */ @SuppressWarnings("unchecked") public class FoxBPMCfgParseUtil { protected static final Logger LOGGER = LoggerFactory.getLogger(XMLToObject.class); private static FoxBPMCfgParseUtil instance; public static final String GENERAL_M_PREFIX = "set"; public static final String BOOL_PREFIX = "is"; public static final String ELEMENT_FOXBPMCONFIG = "FoxBPMConfig"; public static final String ELEMENT_TASKCOMMANDS = "taskCommands"; public static final String ELEMENT_TASKCOMMANDDEFINITION = "taskCommandDefinition"; public static final String ELEMENT_COMMANDPARAM = "commandParam"; public static final String ELEMENT_EVENTLISTENERS = "eventListeners"; public static final String ELEMENT_EVENTLISTENER = "eventListener"; public static final String ELEMENT_BIZDATAOBJECTS = "bizDataObjects"; public static final String ELEMENT_DATAOBJECTBEHAVIOR = "dataObjectBehavior"; public static final String ELEMENT_PLUGIN = "plugin"; public static final String ELEMENT_PLUGINS = "plugins"; private FoxBPMCfgParseUtil() { } /** * ? * * @return */ public static FoxBPMCfgParseUtil getInstance() { if (null == instance) { synchronized (XMLToObject.class) { if (null == instance) { instance = new FoxBPMCfgParseUtil(); } } } return instance; } /** * ?foxbpm.cfg? * * @param in * ? * @return ???FoxBPMConfig */ public FoxBPMConfig parsecfg(InputStream in) { if (in == null) { return null; } try { SAXReader reader = new SAXReader(); Document document; document = reader.read(in); Element rootElem = document.getRootElement(); if (ELEMENT_FOXBPMCONFIG.equals(rootElem.getName())) { return parseElement(rootElem); } } catch (DocumentException e) { throw ExceptionUtil.getException("00006001", e); } return null; } private FoxBPMConfig parseElement(Element element) { Element childElem = null; String nodeName = null; FoxBPMConfig foxBPMConfig = new FoxBPMConfig(); for (Iterator<Element> iterator = element.elementIterator(); iterator.hasNext();) { childElem = iterator.next(); nodeName = childElem.getName(); if (ELEMENT_TASKCOMMANDS.equals(nodeName)) { foxBPMConfig.setTaskCommandDefinitions(parseTaskCommands(childElem)); } else if (ELEMENT_EVENTLISTENERS.equals(nodeName)) { foxBPMConfig.setEventListeners(parseEventListeners(childElem)); } else if (ELEMENT_BIZDATAOBJECTS.equals(nodeName)) { foxBPMConfig.setDataObjectDefinitions(parseBizDataObject(childElem)); } else if (ELEMENT_PLUGINS.equals(nodeName)) { foxBPMConfig.setConfigurators(parsePlugins(childElem)); } } return foxBPMConfig; } private List<ProcessEngineConfigurator> parsePlugins(Element element) { List<ProcessEngineConfigurator> configurators = null; Element childElem = null; String nodeName = null; String className = null; ProcessEngineConfigurator configurator = null; for (Iterator<Element> iterator = element.elementIterator(); iterator.hasNext();) { childElem = iterator.next(); nodeName = childElem.getName(); if (ELEMENT_PLUGIN.equals(nodeName)) { String id = childElem.attributeValue("id"); className = childElem.attributeValue("configurator"); if (StringUtil.isEmpty(className)) { LOGGER.error("plugin{}?configurator,?", id); continue; } try { configurator = (ProcessEngineConfigurator) ReflectUtil.instantiate(className); } catch (Exception ex) { LOGGER.error("plugin:" + id + ":" + className + "???", ex); continue; } if (configurator != null) { if (configurators == null) { configurators = new ArrayList<ProcessEngineConfigurator>(); } configurators.add(configurator); } } } return configurators; } /** * ? * * @param element * * @return * @throws InvocationTargetException * @throws IllegalAccessException * @throws IllegalArgumentException */ private List<TaskCommandDefinitionImpl> parseTaskCommands(Element element) { Element childElem = null; String nodeName = null; List<TaskCommandDefinitionImpl> taskCommandDefs = null; TaskCommandDefinitionImpl taskCommandDef = null; List<CommandParam> commandParams = null; for (Iterator<Element> iterator = element.elementIterator(); iterator.hasNext();) { childElem = iterator.next(); nodeName = childElem.getName(); if (ELEMENT_TASKCOMMANDDEFINITION.equals(nodeName)) { taskCommandDef = new TaskCommandDefinitionImpl(); // ? doAttributes(childElem, taskCommandDef); // ? commandParams = parseCommandParams(childElem); if (null != commandParams) { taskCommandDef.setCommandParam(commandParams); } if (null == taskCommandDefs) { taskCommandDefs = new ArrayList<TaskCommandDefinitionImpl>(); } taskCommandDefs.add(taskCommandDef); } } return taskCommandDefs; } private List<CommandParam> parseCommandParams(Element element) { Element childElem = null; String nodeName = null; List<CommandParam> commandParams = null; CommandParam commandParam = null; for (Iterator<Element> iterator = element.elementIterator(); iterator.hasNext();) { // ? childElem = iterator.next(); nodeName = childElem.getName(); if (ELEMENT_COMMANDPARAM.equals(nodeName)) { commandParam = new CommandParam(); doAttributes(childElem, commandParam); if (null == commandParams) { commandParams = new ArrayList<CommandParam>(); } commandParams.add(commandParam); } } return commandParams; } private List<EventListenerImpl> parseEventListeners(Element element) { Element childElem = null; String nodeName = null; List<EventListenerImpl> eventListeners = null; EventListenerImpl eventListener = null; for (Iterator<Element> iterator = element.elementIterator(); iterator.hasNext();) { childElem = iterator.next(); nodeName = childElem.getName(); if (ELEMENT_EVENTLISTENER.equals(nodeName)) { eventListener = new EventListenerImpl(); // ? doAttributes(childElem, eventListener); if (null == eventListeners) { eventListeners = new ArrayList<EventListenerImpl>(); } eventListeners.add(eventListener); } } return eventListeners; } private List<DataObjectDefinitionImpl> parseBizDataObject(Element element) { Element childElem = null; String nodeName = null; List<DataObjectDefinitionImpl> dataObjectDefinitions = null; DataObjectDefinitionImpl dataObjectDefinition = null; for (Iterator<Element> iterator = element.elementIterator(); iterator.hasNext();) { childElem = iterator.next(); nodeName = childElem.getName(); if (ELEMENT_DATAOBJECTBEHAVIOR.equals(nodeName)) { dataObjectDefinition = new DataObjectDefinitionImpl(); // ? doAttributes(childElem, dataObjectDefinition); if (null == dataObjectDefinitions) { dataObjectDefinitions = new ArrayList<DataObjectDefinitionImpl>(); } dataObjectDefinitions.add(dataObjectDefinition); } } return dataObjectDefinitions; } private void doAttributes(Element element, Object paramObj) { // ?element Method method = null; Attribute attribute = null; Map<String, Method> methodMap = getSetMethods(GENERAL_M_PREFIX, paramObj); for (int i = 0, length = element.attributeCount(); i < length; i++) { attribute = element.attribute(i); method = methodMap.get(generateMethodName(GENERAL_M_PREFIX, attribute.getName())); if (null != method) { doAttributeValue(paramObj, method, attribute.getValue(), method.getParameterTypes()[0]); } } } private Map<String, Method> getSetMethods(String prefix, Object obj) { Map<String, Method> methodMap = new HashMap<String, Method>(); Method[] methods = obj.getClass().getMethods(); if (null != methods) { int length = methods.length; Method method = null; for (int i = 0; i < length; i++) { method = methods[i]; if (method.getParameterTypes().length == 1 && method.getName().startsWith(prefix)) { methodMap.put(method.getName(), method); } } } return methodMap; } /** * ???? * * @param prefix * get * @param name * ?? * @return ?? */ private String generateMethodName(String prefix, String name) { StringBuffer sbuffer = new StringBuffer(); sbuffer.append(name.substring(name.indexOf(':') + 1)); // ?boolean?? if (sbuffer.toString().startsWith(BOOL_PREFIX)) { sbuffer.delete(0, 2); } // ?a:b a: return new StringBuffer(prefix).append(Character.toUpperCase(sbuffer.charAt(0))) .append(sbuffer.substring(1)).toString(); } private void doAttributeValue(Object pObj, Method method, String value, Class<?> pType) { try { if (String.class == pType) { method.invoke(pObj, value); } else if (boolean.class == pType) { method.invoke(pObj, Boolean.valueOf(value)); } else if (int.class == pType) { method.invoke(pObj, Integer.valueOf(value)); } else { // ?? LOGGER.warn("??:" + pType); } } catch (IllegalArgumentException e) { throw ExceptionUtil.getException("00006002", e, method.toString(), pObj.toString(), value, pType.getName()); } catch (IllegalAccessException e) { throw ExceptionUtil.getException("00006003", e, method.toString(), pObj.toString(), value, pType.getName()); } catch (InvocationTargetException e) { throw ExceptionUtil.getException("00006004", e, method.toString(), pObj.toString(), value, pType.getName()); } } }