View Javadoc
1   /**
2    * Copyright By Grandsoft Company Limited.  
3    * 2012-2-16 下午03:34:12
4    */
5   package gboat2.base.core.web;
6   
7   import java.lang.reflect.Method;
8   
9   import net.sf.json.JSONObject;
10  
11  import org.springframework.util.ReflectionUtils;
12  
13  /**
14   * 
15   * 获取数据
16   * @author zhangxj-a
17   * @since jdk1.6
18   * @date 2013-5-20
19   *  
20   */
21  public  class ObtainData  extends MetadataSupportStrategy{
22  
23  	private static MetadataSupportStrategy instance = new ObtainData();
24  
25  	private  ObtainData() {
26  		super();
27  	}
28  
29  	public  Object getMetadata(String metadataType, String invokeMethod, BaseActionSupport action) {
30  		logger.info("执行 [{}] 方法,数据类型 [{}],使用的策略类 [{}]", invokeMethod, metadataType, getClass().getName());
31  		JSONObject metadataObj;
32  
33  		// 以下分 2 次分别依次获取:md 元数据文件内容、数据
34  		boolean calledInvokedMethod = false;
35  		JSONObject returned = new JSONObject();
36  		
37  		if (!callInitInvokedMethod(invokeMethod, action)) {
38  			Method method = ReflectionUtils.findMethod(action.getClass(), invokeMethod);
39  			if (method != null) {
40  				try {
41  					ReflectionUtils.invokeMethod(method, action);
42  					calledInvokedMethod = true;
43  				} catch (Exception e) {
44  					e.printStackTrace();
45  				}
46  			}else{
47  				//方法不存在,默认作为调用成功
48  				calledInvokedMethod = true;
49  			}
50  		}
51  		
52  		try {
53  			metadataObj = getMetadataFromResource(invokeMethod, action);
54  		} catch (MetadataException e) {
55  			metadataObj = new JSONObject();
56  		}
57  		decorateWithData(calledInvokedMethod, metadataObj, invokeMethod, action, returned);
58  
59  		if (returned != null) {
60  			logger.debug(returned.toString());
61  		}
62  		return returned;
63  	}
64  
65  	/**
66  	 * @return 获取单例
67  	 */
68  	public static  MetadataSupportStrategy getInstance() {
69  		return instance;
70  	}
71  
72  }