⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 flow4jactionservlet.java

📁 一个java写的business process management系统
💻 JAVA
字号:
/*
 * Copyright (c) 2003-2004, Alexander Greif
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the Flow4J-Eclipse project nor the names of its
 *       contributors may be used to endorse or promote products derived from
 *       this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
 * THE POSSIBILITY OF SUCH DAMAGE.
 */

package net.orthanc.flow4j.runtime.connectors.struts;

import java.util.Iterator;

import javax.servlet.ServletException;
import javax.servlet.UnavailableException;

import net.orthanc.flow4j.runtime.Flow4JRuntimeConsts;
import net.orthanc.flow4j.runtime.FlowManager;
import net.orthanc.flow4j.runtime.IFlow;
import net.orthanc.flow4j.runtime.IFlowRepository;
import net.orthanc.flow4j.runtime.StartFlowlet;

import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionServlet;
import org.apache.struts.config.ActionConfig;
import org.apache.struts.config.FormBeanConfig;
import org.apache.struts.config.ModuleConfig;

/**
 * Action servlet of the Flow4J framework that registers
 * flows with the jakarta Struts modules, so that Struts can
 * additionally trigger flows beside of action classes.<br/>
 * For example the URL<br/>
 * <code>http://server/myapp/Cart-Show.do?...</code><br/>
 * fills the form beans and then starts the flow "Cart" at its
 * entry point "Show". After flow execution all dictionary
 * contents are filled in the request scope.
 * @author greifa
 */
public class Flow4JActionServlet extends ActionServlet {

	public final static String INIT_PARAM_FLOW_REPOSITORY_CLASS =
		"flow-repository-class";

	/** Registeres the flows with the <code>FlowManager</code>.
	 * @see org.apache.struts.action.ActionServlet#initOther()
	 */
	protected void initOther() throws ServletException {
		super.initOther();

		try {
			//	register flows ...
			String flowRepositoryClassName =
				getInitParameter(INIT_PARAM_FLOW_REPOSITORY_CLASS);
			//	create a new one
			if (log.isInfoEnabled())
				log.info("register FlowRepository: " + flowRepositoryClassName);

			if (flowRepositoryClassName != null) {
				IFlowRepository flowRepository =
					(IFlowRepository) Class
						.forName(flowRepositoryClassName)
						.newInstance();
				//	.... at the FlowManager
				FlowManager.registerFlows(flowRepository);
			}
		} catch (InstantiationException e) {
			e.printStackTrace();
			throw new ServletException(e);
		} catch (IllegalAccessException e) {
			e.printStackTrace();
			throw new ServletException(e);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
			throw new ServletException(e);
		} catch (Throwable e) {
			e.printStackTrace();
			throw new ServletException(e);
		}

	}

	/** Generates <code>ActionConfig</code> and <code>FormBeanConfig</code> Objects
	 *  of very start flowet of every flow for all modules.
	 * @see org.apache.struts.action.ActionServlet#initModuleConfig(java.lang.String, java.lang.String)
	 */
	protected ModuleConfig initModuleConfig(String prefix, String paths)
		throws ServletException {
		ModuleConfig moduleConfig = super.initModuleConfig(prefix, paths);
		try {
			//	register flows ...
			String flowRepositoryClassName =
				getInitParameter(INIT_PARAM_FLOW_REPOSITORY_CLASS);
			if (flowRepositoryClassName != null) {
				IFlowRepository flowRepository =
					(IFlowRepository) Class
						.forName(flowRepositoryClassName)
						.newInstance();

				//	... at Struts ModuleConfig
				if (flowRepository.getFlowClasses() == null
					|| flowRepository.getFlowClasses().isEmpty())
					return moduleConfig;

				//	iterate over all registered flows
				for (Iterator iter = flowRepository.getFlowClasses().iterator();
					iter.hasNext();
					) {
					IFlow flow = (IFlow) ((Class) iter.next()).newInstance();
					String flowName = flow.getName();

					//	iterate over start flowlets
					for (int i = 0; i < flow.getStartFlowlets().length; i++) {
						StartFlowlet startFlowlet = flow.getStartFlowlets()[i];
						registerAtStruts(flowName, startFlowlet, moduleConfig);
					}
				}
			}
		} catch (InstantiationException e) {
			log.error("Error: ", e);
			throw new ServletException(e);
		} catch (IllegalAccessException e) {
			log.error("Error: ", e);
			throw new ServletException(e);
		} catch (ClassNotFoundException e) {
			log.error("Error: ", e);
			throw new ServletException(e);
		} catch (Throwable e) {
			log.error("Error: ", e);
			throw new ServletException(e);
		}

		return moduleConfig;
	}

	/**
	 * Registers the Action and if possible the Form Bean too at Struts.
	 * @param flowName
	 * @param startFlowlet
	 * @param moduleConfig
	 */
	private void registerAtStruts(
		String flowName,
		StartFlowlet startFlowlet,
		ModuleConfig moduleConfig)
		throws ServletException {

		String formBeanType =
			startFlowlet.getProperty(Flow4JStrutsConsts.PROP_FORM_BEAN_TYPE);
		String formBeanName =
			startFlowlet.getProperty(Flow4JStrutsConsts.PROP_FORM_BEAN_NAME);

		String flowStart =
			flowName
				+ Flow4JRuntimeConsts.FLOW_START_DELIMITER
				+ startFlowlet.getName();

		//	register form bean
		FormBeanConfig formBeanConfig =
			registerFormBean(
				flowStart,
				formBeanType,
				formBeanName,
				moduleConfig);

		//	register action
		registerAction(formBeanConfig, flowStart, startFlowlet, moduleConfig);

	}

	/**
	 * create and register form bean if it is not in the struts-config.xml
	 * @param formBeanType the type of the form bean provided in the Start Flowlet
	 * @param formBeanName the name of the form bean provided in the Start Flowlet
	 * @param moduleConfig
	 * @return
	 */
	private FormBeanConfig registerFormBean(
		String flowStart,
		String formBeanType,
		String formBeanName,
		ModuleConfig moduleConfig)
		throws ServletException {
		boolean nameWasNull = formBeanName == null;
		//	create new name if not provided in Start Flowlet
		if (formBeanName == null)
			formBeanName = flowStart + Flow4JStrutsConsts.SUFFIX_FORMBEAN_NAME;
		// check declaration in struts-config
		FormBeanConfig formBeanConfig =
			moduleConfig.findFormBeanConfig(formBeanName);

		if (formBeanType != null
			&& formBeanConfig != null
			&& formBeanConfig.getType() != null
			&& (!formBeanType.trim().equals(formBeanConfig.getType().trim()))) {
			throw new UnavailableException(
				"Form Bean with name \""
					+ formBeanName
					+ "\" in flow \""
					+ flowStart
					+ "\" is declared with a different type than in the struts-config file.");
		}

		if (formBeanType != null
			&& formBeanConfig != null
			&& (formBeanConfig.getType() == null
				|| (formBeanConfig.getType().trim().length() == 0)))
			formBeanConfig.setType(formBeanType);

		if (formBeanConfig != null)
			//	return the already registered one
			return formBeanConfig;

		if (nameWasNull && formBeanType == null)
			return null;

		if (!nameWasNull && formBeanType == null) {
			throw new UnavailableException(
				"Form Bean type not declared for name: \""
					+ formBeanName
					+ "\" in flow \""
					+ flowStart
					+ "\"");
		}

		//	create a new one
		if (log.isInfoEnabled())
			log.info(
				"register new form bean. name: "
					+ formBeanName
					+ "   type: "
					+ formBeanType);
		formBeanConfig = new FormBeanConfig();
		formBeanConfig.setModuleConfig(moduleConfig);
		formBeanConfig.setName(formBeanName);
		formBeanConfig.setType(formBeanType);
		moduleConfig.addFormBeanConfig(formBeanConfig);
		formBeanConfig.freeze();

		if (log.isInfoEnabled())
			log.info(
				"registered struts form bean for flow \""
					+ flowStart
					+ "\"    with name \""
					+ formBeanName
					+ "\"");

		return formBeanConfig;
	}

	/**
	 * 
	 * @param formBeanConfig
	 * @param flowName
	 * @param startFlowlet
	 * @param moduleConfig
	 */
	private void registerAction(
		FormBeanConfig formBeanConfig,
		String flowStart,
		StartFlowlet startFlowlet,
		ModuleConfig moduleConfig)
		throws ServletException {

		String actionPath = "/" + flowStart;
		//	get ActionConfig from struts-config.xml
		ActionConfig actionConfig = moduleConfig.findActionConfig(actionPath);

		String input =
			startFlowlet.getProperty(Flow4JStrutsConsts.PROP_ACTION_INPUT);

		if (input != null
			&& actionConfig != null
			&& actionConfig.getInput() != null
			&& (!input.trim().equals(actionConfig.getInput().trim()))) {
			throw new UnavailableException(
				"Action with path \""
					+ actionPath
					+ "\" in flow \""
					+ flowStart
					+ "\" is declared with a different input than in the struts-config file.");
		}

		if (actionConfig == null) {
			//	not registered in struts-config
			if (log.isInfoEnabled())
				log.info(
					"register new action. path: "
						+ actionPath
						+ "   type: "
						+ Flow4JAction.class.getName()
						+ "   input: "
						+ input);
			actionConfig = new ActionMapping();
			actionConfig.setModuleConfig(moduleConfig);
			actionConfig.setPath(actionPath);
			//	Fully qualified Java class name of the Action subclass [org.apache.struts.action.Action]
			//	that will process requests for this action mapping.
			actionConfig.setType(Flow4JAction.class.getName());
			moduleConfig.addActionConfig(actionConfig);

			if (log.isInfoEnabled())
				log.info(
					"registered struts action for flow \""
						+ flowStart
						+ "\"    with path \""
						+ actionPath
						+ "\"");
		}
		//	sets he name if not provided in the struts-config
		if (formBeanConfig != null
			&& (actionConfig.getName() == null
				|| actionConfig.getName().trim().length() == 0))
			actionConfig.setName(formBeanConfig.getName());

		//	try to set the input, if it's not specified in the struts-config
		if (actionConfig.getInput() == null
			|| actionConfig.getInput().trim().length() == 0) {
			actionConfig.setInput(input);
		}

		//	try to set the type, if it's not specified in the struts-config
		if (actionConfig.getType() == null
			|| actionConfig.getType().trim().length() == 0) {
			actionConfig.setType(Flow4JAction.class.getName());
		}
		actionConfig.freeze();
	}

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -