001package gu.sql2java.generator; 002 003import org.apache.commons.cli.CommandLine; 004import org.apache.commons.cli.Option; 005import org.apache.commons.cli.Options; 006import org.apache.commons.cli.ParseException; 007import org.slf4j.Logger; 008import org.slf4j.LoggerFactory; 009 010import com.google.common.base.Function; 011import com.google.common.collect.Lists; 012 013import net.gdface.cli.BaseAppConfig; 014import net.gdface.utils.FaceUtilits; 015 016import java.io.File; 017import java.net.MalformedURLException; 018import java.net.URL; 019import java.net.URLClassLoader; 020import java.util.List; 021/** 022 * 终端命令行配置参数 023 * @author guyadong 024 * 025 */ 026public class GeneratorConfig extends BaseAppConfig implements GeneratorConstants { 027 private static final Logger logger = LoggerFactory.getLogger(GeneratorConfig.class); 028 029 static final GeneratorConfig CONFIG = new GeneratorConfig(); 030 031 private File propFile; 032 private String classPath; 033 private boolean trace; 034 035 private URLClassLoader classLoader; 036 public GeneratorConfig() { 037 038 options.addOption(Option.builder(PROPFILE_OPTION).longOpt(PROPFILE_OPTION_LONG) 039 .desc(PROPFILE_OPTION_DESC).numberOfArgs(1).type(File.class).required(true).build()); 040 041 options.addOption(Option.builder(CLASSPATH_OPTION).longOpt(CLASSPATH_OPTION_LONG) 042 .desc(CLASSPATH_OPTION_DESC).numberOfArgs(1).build()); 043 044 options.addOption(Option.builder(TRACE_OPTION).longOpt(TRACE_OPTION_LONG) 045 .desc(TRACE_OPTION_DESC ).hasArg(false).build()); 046 047 defaultValue.setProperty(PROPFILE_OPTION_LONG, null); 048 defaultValue.setProperty(CLASSPATH_OPTION_LONG, ""); 049 050 } 051 @Override 052 public void loadConfig(Options options, CommandLine cmd) throws ParseException { 053 super.loadConfig(options, cmd); 054 this.propFile = getProperty(PROPFILE_OPTION_LONG); 055 this.classPath = getProperty(CLASSPATH_OPTION_LONG); 056 this.trace = getProperty(TRACE_OPTION_LONG); 057 058 } 059 /** 060 * @return 发生异常时是否输出详细堆栈信息 061 */ 062 public boolean isTrace() { 063 return trace; 064 } 065 @Override 066 protected String getAppName() { 067 return Generator.class.getSimpleName(); 068 } 069 @Override 070 protected String getHeader() { 071 return "Sql2java (java)代码生成器"; 072 } 073 074 /** 075 * @return propfile 076 */ 077 public File getPropFile() { 078 return propFile; 079 } 080 081 /** 082 * 根据classPath参数创建自定义{@link URLClassLoader}对象 083 * @return {@link URLClassLoader}对象,classPath为空则返回{@code null} 084 */ 085 public synchronized ClassLoader getClassloader(){ 086 if(null == classLoader){ 087 List<String> paths = FaceUtilits.elementsOf(classPath); 088 if(!paths.isEmpty()){ 089 List<URL> urls = Lists.transform(paths, new Function<String, URL>() { 090 091 @Override 092 public URL apply(String input) { 093 try { 094 return new File(input).getAbsoluteFile().toURI().toURL(); 095 } catch (MalformedURLException e) { 096 throw new RuntimeException(e); 097 } 098 } 099 }); 100 logger.info("classpath: {}",urls); 101 classLoader = new URLClassLoader(urls.toArray(new URL[urls.size()])); 102 } 103 } 104 return classLoader; 105 } 106}