View Javadoc
1   /**
2    * Copyright By Grandsoft Company Limited.  
3    * 2013-8-10 上午09:03:57
4    */
5   package gboat2.base.core.web.md;
6   
7   import gboat2.base.bridge.debug.DefaultDebugHook;
8   import gboat2.base.core.web.BaseActionSupport;
9   
10  import java.net.URL;
11  import java.util.List;
12  
13  import net.sf.json.JSONObject;
14  
15  import org.apache.commons.lang3.StringUtils;
16  import org.osgi.framework.Bundle;
17  import org.osgi.framework.FrameworkUtil;
18  import org.slf4j.Logger;
19  import org.slf4j.LoggerFactory;
20  
21  /**
22   * 
23   * 元数据处理基类
24   * @author lysming
25   * @since jdk1.6
26   * @date 2013-8-10
27   *  
28   */
29  
30  public class MetadataSupport {
31  	
32  	private static Logger logger = LoggerFactory.getLogger(MetadataSupport.class);
33  	
34  	private static URL getFileURL(String invokeMethod, BaseActionSupport action) {
35  		Bundle bundle = FrameworkUtil.getBundle(action.getClass());
36  		List<IFileFilter> filters = MetadataHandlerChain.getFileFilters();
37  		String filePath;
38  		//由各IFileFilter去检查各文件规则的文件是否存在,优先级依据MetadataHandlerChain中的初始化顺序
39  		URL url = null;
40  		for (IFileFilter handler : filters) {
41  			filePath = handler.getFile(invokeMethod, action, bundle);
42  			if (!StringUtils.isEmpty(filePath) && (url=DefaultDebugHook.getInstance().findResource(bundle, filePath)) != null) {
43  				logger.debug("find file with FileFilter[" + handler.getClass().getSimpleName() + "] : " + filePath);
44  				return url;
45  			}
46  		}
47  		return null;
48  	}
49  
50  	public static JSONObject parseMetadata(String invokeMethod, BaseActionSupport action) {
51  		Bundle bundle = FrameworkUtil.getBundle(action.getClass());
52  		URL url = getFileURL(invokeMethod, action);
53  		MetadataHandlerChain chain = MetadataHandlerChain.getHandlerChain(action, invokeMethod);
54  		if (url != null) {
55  			return parseMetadata(url, bundle, chain);
56  		}
57  		
58  		return null;
59  	}
60  	
61  	public static JSONObject parseMetadata(URL url, Bundle bundle, MetadataHandlerChain chain) {
62  		JSONObject object = null;
63  		if (url != null) {
64  			object = chain.getHandler().parseMetadata(object, url, bundle, chain);
65  		}
66  		
67  		return object;
68  	}
69  }