一、前言
通过定义ComponentBuilder组件构造器接口、XmlBuilder基于xml构造器实现类、GroovyBuilder构造实现类,再定义GroovyUtil工具类动态加载运行groovy脚本源文件,实现项目应用中动态灵活加载配置的使用场景。
二、代码说明
1. 动态组件构造器接口ComponentBuilder
import java.io.File;@b@@b@/**@b@ * 动态组件构造器 @b@ */@b@public interface ComponentBuilder {@b@@b@ /**@b@ * 要处理文件的类型@b@ * @return@b@ */@b@ String getType();@b@@b@ /**@b@ * 加载文件构造对象@b@ * @param f@b@ */@b@ void doBuild(File f);@b@}
2. 动态组件构造器实现类(GroovyBuilder、XmlBuilder)
import groovy.util.Node;@b@import groovy.util.XmlParser; @b@import java.io.File;@b@import java.util.HashMap;@b@import java.util.Map; @b@import org.apache.log4j.Logger; @b@import com.xwood.mobilegw.util.LoggerUtil;@b@@b@ @b@public class XmlBuilder implements ComponentBuilder {@b@@b@ private static Logger logger = Logger.getLogger(XmlBuilder.class);@b@@b@ /**@b@ * 缓存xml node@b@ */@b@ public static Map<String, Node> cachedXml = new HashMap<String, Node>();@b@@b@ private static final String SERVICE_CONFIG_XML = "serviceConfig.xml";@b@@b@ @Override@b@ public String getType() {@b@ // TODO Auto-generated method stub@b@ return "xml";@b@ }@b@@b@ @b@ @Override@b@ public void doBuild(File f) {@b@ // TODO Auto-generated method stub@b@ LoggerUtil.info(logger, "加载xml文件:" + f.getName());@b@ try {@b@ Node node = new XmlParser().parse(f);@b@ XmlBuilder.cachedXml.put(f.getName(), node);@b@ } catch (Exception e) {@b@ // TODO: handle exception@b@ LoggerUtil.error(logger, e, "加载xml文件发生异常!", "fileName:" + f.getName());@b@ }@b@ }@b@@b@}
import groovy.lang.GroovyClassLoader;@b@import java.io.File;@b@import java.util.HashMap;@b@import java.util.Map;@b@import org.apache.log4j.Logger;@b@import com.xwood.mobilegw.util.LoggerUtil;@b@@b@public class GroovyBuilder implements ComponentBuilder {@b@@b@ private static Logger logger = Logger.getLogger(GroovyBuilder.class);@b@@b@ private static GroovyClassLoader gcl = new GroovyClassLoader(@b@ GroovyBuilder.class.getClassLoader());@b@@b@ /**@b@ * 缓存class@b@ */@b@ @SuppressWarnings("rawtypes")@b@ public static Map<String, Class> cachedGroovyClass = new HashMap<String, Class>();@b@@b@ @Override@b@ public String getType() {@b@ // TODO Auto-generated method stub@b@ return "groovy";@b@ }@b@@b@ @Override@b@ public void doBuild(File f) {@b@ // TODO Auto-generated method stub@b@ LoggerUtil.info(logger, "加载groovy文件:" + f.getName());@b@ try {@b@ Class<?> groovyClass = gcl.parseClass(f);@b@ GroovyBuilder.cachedGroovyClass.put(f.getName(), groovyClass);@b@ //清空@b@ gcl.clearCache();@b@ } catch (Exception e) {@b@ // TODO: handle exception@b@ LoggerUtil.error(logger, e, "【加载groovy文件发生异常!", ";fileName:" + f.getName(),";Err:"+e.getMessage()+"】");@b@ }@b@ }@b@@b@}
3.Groovy调用公共方法
import groovy.lang.GroovyObject;@b@import org.apache.commons.logging.Log;@b@import org.apache.commons.logging.LogFactory;@b@import com.xwood.mobilegw.dynamic.builder.GroovyBuilder;@b@@b@ @b@public class GroovyUtil {@b@@b@ private static final Log logger = LogFactory.getLog(GroovyUtil.class); @b@ @b@ /**@b@ * @b@ * @param groovyPath@b@ * 文件路径@b@ * @param invokeMethod@b@ * 访问方法@b@ * @param params@b@ * 参数@b@ * @return@b@ */@b@ public static Object get(String groovyName, String invokeMethod, Object params) {@b@ Object obj = null;@b@ try {@b@@b@ Class<?> groovyClass = GroovyBuilder.cachedGroovyClass.get(groovyName);@b@ @b@ GroovyObject groovyObject = (GroovyObject) groovyClass.newInstance();@b@@b@ @b@ obj = groovyObject.invokeMethod(invokeMethod, params);@b@@b@ } catch (Exception e) {@b@ logger.error("调用Groovy出错", e);@b@ }@b@ return obj;@b@ }@b@@b@}