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 ObtainMetadata  extends MetadataSupportStrategy{
22  	
23  	private static MetadataSupportStrategy instance = new ObtainMetadata();
24  	
25  	private ObtainMetadata() {
26  		super();
27  	}
28  
29  	public Object getMetadata(String metadataType, String invokeMethod, BaseActionSupport action) {
30  		logger.info("Current Strategy:{}",this.getClass().getName());
31  		logger.info("get metadata type : " + metadataType + ", invoke : " + invokeMethod);
32  		JSONObject metadataObj;
33  
34  		//以下分2次分别依次获取:md元数据文件内容、数据
35  		boolean calledInvokedMethod = false;
36  		JSONObject returned = new JSONObject();
37  		//获取metadata需要先调用initXXXX方法,如果不存在initXXXX方法,调用请求XXXX方法
38  		if (!callInitInvokedMethod(invokeMethod, action)) {
39  			Method method = ReflectionUtils.findMethod(action.getClass(), invokeMethod);
40  			if (method != null) {
41  				try {
42  					ReflectionUtils.invokeMethod(method, action);
43  					calledInvokedMethod = true;
44  				} catch (Exception e) {
45  					e.printStackTrace();
46  				}
47  			}else{
48  				//方法不存在,默认作为调用成功
49  				calledInvokedMethod = true;
50  			}
51  		}
52  
53  		metadataObj = getMetadataFromResource(invokeMethod, action);
54  
55  		//失败直接返回到客户端
56  		if (metadataObj.containsKey("success") && metadataObj.getBoolean("success") == false) {
57  			returned = metadataObj;
58  		} else if (metadataObj.get("component").equals("iframe")) {//是iframe页面直接返回客户端
59  			returned.accumulate("metadata", metadataObj);
60  		} else {
61  			metadataObj = decorateMetadata(metadataObj, invokeMethod, action);
62  			returned.accumulate("metadata", metadataObj);
63  			//在获取metadata时即时返回data
64  			/**此处将calledInvokeMethod传入true,说明其已经调用过了,不在需要第二次调用 */
65  			decorateWithData(calledInvokedMethod, metadataObj, invokeMethod, action, returned);
66  		}
67  		return returned;
68  
69  	} 
70  
71  	public static MetadataSupportStrategy getInstance() {
72  		return instance;
73  	}
74  }