一个模仿Struct的Action类

原创
2012/04/24 16:25
阅读数 450
我在项目中也写了一个模仿Struct的Action类,目前还不成熟,先拿出来讨论讨论。
mfaction.xml 
<?xml version="1.0" encoding="GBK"?> 
<thinkwide>
	<include file="user.xml"/> 

	<constant name="MFSys.Debug" value="true"/>
	<constant name="MFSys.encoding" value="GB2312"/>

	<package name="test" namespace="test/">    
    		<action name="user" class="weaver.test.testuser" method="test">    
    			<result name="success">testExcel.jsp</result> 
			<result name="test">/login.jsp</result>   
   		 </action>  
	</package>   
</thinkwide>

/****************************************************************
 * <strong>MFActionManager动作管理类</strong>
 * 
 * <p>
 * MFActionManager动作管理类
 * </p>
 * 
 * @author  AndyZhang 2011
 * @version 0.1 
 * ToDo:2011-12-29
 ***************************************************************/

package weaver.action;

import java.io.File;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import weaver.core.MFPath;
import weaver.core.MFString;
import weaver.debug.MFDebug;
import weaver.ormapping.MFDbTypeTransfer;
import weaver.ormapping.MFReflect;

public class MFActionManager {
	
	private List<String> includeFiles;
	private Map<String,String> configs;
	private MFActionItems items;
	private HttpServletRequest request;
	private HttpServletResponse response;
	private static String ext=".tw";
	private static String execute="execute";
	
	public MFActionManager()
	{
		includeFiles=new ArrayList<String>();
		configs=new HashMap<String, String>();
		//loadMain();
		items=new MFActionItems();
	}
	
	public MFActionManager(HttpServletRequest request,HttpServletResponse response)
	{
		this();
		updateState(request,response);	
	}
	
	public void updateState(HttpServletRequest request,HttpServletResponse response)
	{
		this.request=request;
		this.response=response;
	}
	
	public boolean prepareProperty(Class<?> classType,Object obj) throws Throwable
	{
		if (obj instanceof IMFRequestAware)
		{
			((IMFRequestAware) obj).setRequest(request);
			((IMFRequestAware) obj).setResponse(response);
		}
		
		Enumeration params=request.getParameterNames();
		while(params.hasMoreElements()){
	  	    String name=(String)params.nextElement();
	  	    String value=request.getParameter(name);
	  	    
	  	    if (MFString.NotNullOrEmpty(name) && MFReflect.IsHaveProperty(obj,name))
	  	    {
	  	    	Class<?> type=MFReflect.GetPropertyType(obj, name);
	  	    	Object typedValue = MFDbTypeTransfer.toJavaType(type, value);
	  	   		MFReflect.SetProperty(obj, name, typedValue);
	  	   		
	  	    }
	  	}
		return true;
	}
	
	public boolean runAction(MFActionItem item) throws Throwable
	{
		if (item!=null)
		{
			Class<?> c=Class.forName(item.getClassName());
			
			Object obj=c.newInstance();
			
			prepareProperty(c,obj);
			
			String methodName =MFString.NullToString(item.getMethod(),execute);
			Method method=c.getDeclaredMethod(methodName, null);
			
			Object value=method.invoke(obj, null);
			if (value!=null)
			{
				String url=item.getResults().get(value);
				if (MFString.NotNullOrEmpty(url))
				{
					//String path=MFString.NullToString(item.getNamespace())+url;
					response.sendRedirect(url);
				}				
			}
			
		}
		return false;
	}
	
	public boolean runAction(String url) throws Throwable
	{
		MFActionItem item=findAction(url);
		return runAction(item);
	}
	
	public String CheckPath(String url)
	{
		if (MFString.NotNullOrEmpty(url)  && !url.startsWith(MFPath.useparator)) 
			url=MFPath.useparator+url;
		return url;
	}
	public MFActionItem findAction(String url)
	{
		for(MFActionItem item:items)
		{
			String Str=MFString.NullToString(item.getNamespace())+item.getActionName()+ext;
			Str=CheckPath(Str);

			if (MFString.ComparerNoCase(url, Str)) 
			{
				MFDebug.println("find");
				return item;
			}
			
		}
		
		return null;
	}
	
	public String getAttrValue(Element item,String name)
	{
		Attribute attr=item.attribute(name);
		if (attr!=null)
		{
			String text=attr.getText();
			return text;
		}
		return null;	
	}
	
	public boolean loadConfig(String filePath) throws DocumentException
	{
		File file = new File(filePath);
        
        SAXReader reader = new SAXReader();
		Document doc=reader.read(file);
		Element root = doc.getRootElement(); 
		
		//load include file
		for (Iterator<?> i = root.elementIterator("include"); i.hasNext();) 
		{
			Element item = (Element) i.next();
			String value=getAttrValue(item,"file");
			if (MFString.NotNullOrEmpty(value)) 
				includeFiles.add(value);	
		}
		
		//load constant
		for (Iterator<?> i = root.elementIterator("constant"); i.hasNext();) 
		{
			Element item = (Element) i.next();
			
			String conName=getAttrValue(item,"name");
			String conValue=getAttrValue(item,"value");
			if (MFString.NotNullOrEmpty(conName)&&MFString.NotNullOrEmpty(conValue) ) 
			{
				configs.put(conName, conValue);
			}		
		}
		
		//load action
		for (Iterator<?> i = root.elementIterator("package"); i.hasNext();) 
		{
			Element packageItem = (Element) i.next();
			String packageName=getAttrValue(packageItem,"name");
			String namespace=getAttrValue(packageItem,"namespace");
			
			for (Iterator<?> j = packageItem.elementIterator("action"); j.hasNext();) 
			{
				MFActionItem action=new MFActionItem();
				Element actionItem = (Element) j.next();
				
				String actionName=getAttrValue(actionItem,"name");
				String className=getAttrValue(actionItem,"class");
				String method=getAttrValue(actionItem,"method");
				
				action.setPackageName(packageName);
				action.setNamespace(namespace);
				action.setActionName(actionName);
				action.setActionName(actionName);
				action.setClassName(className);
				action.setMethod(method);
				
				for (Iterator<?> k = actionItem.elementIterator("result"); k.hasNext();) 
				{
					Element resultItem = (Element) k.next();
					
					String resultName=getAttrValue(resultItem,"name");
					String url=(String)resultItem.getData();
					action.getResults().put(resultName, url);
				}
				
				items.add(action);
			}
		}
		
		return true;
	}

	public void setIncludeFiles(List<String> includeFiles) {
		this.includeFiles = includeFiles;
	}

	public List<String> getIncludeFiles() {
		return includeFiles;
	}
}


展开阅读全文
打赏
0
2 收藏
分享
加载中
没人评论?支持一下
2013/04/15 17:57
回复
举报
更多评论
打赏
1 评论
2 收藏
0
分享
OSCHINA
登录后可查看更多优质内容
返回顶部
顶部