1.概述
本文主要分析下jetty的start.jar中的jar包加载顺序和一些简单逻辑,由于没有下到start.jar包的源码,用反编译的方式搞的,代码可能有些地方不一定准确。
2.分析
1.main方法
main方法非常简单,基本就这一句:
1 |
Main main = new Main().parseCommandLine(args); |
2. public void parseCommandLine(String[] args)
该函数主要完成解析命令行参数的功能:
1.首先遍历所有的传入参数,并把他们加入到arguments这个ArrayList中。如果指定了–ini=,则吧ini指定文件中的所有参数也加入到
arguments,如果是–config=,则设置 this._startConfig,最后如果传入参数中没有 –ini=或者–ini,就是用默认的ini,并把参数加入到arguments。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
List arguments = new ArrayList(); for (String arg1 : args) { if ((arg1.startsWith("--ini=")) || (arg1.equals("--ini"))) { arguments.addAll(loadStartIni(arg1.substring(6))); } else if (arg1.startsWith("--config=")) { this._startConfig = arg1.substring(9); } else { arguments.add(arg1); } } if (!(ini)){ arguments.addAll(0, loadStartIni(null));} |
2.遍历arguments并一一解析处理,主要处理的参数有这些:
一些常见参数,类似–help,–stop等的处理
- -daemon的处理,控制启动日志打印
- -D…的参数,设置到System.properties中
- - 的参数,设置为jvm参数
- 类似a=b的参数,设置到this._config中
3.调用start(xmls);
public void start(List xmls) 主要完成这几件事情:
1.startMonitor();
2.List configuredXmls = loadConfig(xmls);
3.configuredXmls = resolveXmlConfigs(configuredXmls);//主要是确定所有xml的位置,包括文件夹转化为一个个的xml路径
4.mainClass = System.getProperty(“main.class”); invokeMain(cl, classname, configuredXmls);
其中loadConfig(xmls);和invokeMain(cl, classname, configuredXmls);是主要逻辑,loadConfig(xmls);主要是处理定义好的classpath和xml文件的处理顺序等,逻辑委托给org.eclipse.jetty.start.Config类来实现,部分代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
private InputStream getConfigStream() throws FileNotFoundException { String config = this._startConfig; if ((config == null) || (config.length() == 0)) { config = System.getProperty("START", "org/eclipse/jetty/start/start.config"); } Config.debug("config=" + config); InputStream cfgstream = super.getClass().getClassLoader().getResourceAsStream(config); if (cfgstream == null) { cfgstream = new FileInputStream(config); } return cfgstream; } |
可以看到,如果我们自定义了start文件,就使用自定义的,否则使用start.jar中自带的,这个文件的主要功能是什么的,文档说:This file controls what file are to be put on classpath or command line.截取一小段示例一下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# add a property defined classpath ${path}.path property path # add a property defined library directory ${lib}/** exists ${lib} # Try different settings of jetty.home until the start.jar is found. jetty.home=. ! exists $(jetty.home)/start.jar jetty.home=.. ! exists $(jetty.home)/start.jar jetty.home=jetty-distribution/src/main/resources ! exists $(jetty.home)/start.jar jetty.home=../jetty-distribution/src/main/resources ! exists $(jetty.home)/start.jar jetty.home=. ! exists $(jetty.home)/start.jar jetty.home/=$(jetty.home) exists $(jetty.home)/start.jar # The main class to run org.eclipse.jetty.xml.XmlConfiguration.class ${start.class}.class property start.class # The default configuration files $(jetty.home)/etc/jetty.xml nargs == 0 ./jetty-server/src/main/config/etc/jetty.xml nargs == 0 AND ! exists $(jetty.home)/etc/jetty.xml # Default OPTIONS if not specified on the command line OPTIONS~=default,* ! property OPTIONS # Add a resources directory if it is there [All,resources,default] $(jetty.home)/resources/ # Add jetty modules [*] $(jetty.home)/lib/jetty-util-$(version).jar ! available org.eclipse.jetty.util.StringUtil $(jetty.home)/lib/jetty-io-$(version).jar |
可以看到它主要定义了一些classpath的路径等信息,解析过程比较复杂,在此不详述,有兴趣可以自己了解。这里举一个例子,比如我们在启动参数中定义了-lib=D:/mylib/
这个classpath就会被加入到java.class.path中,甚至比jetty的server等类还靠前,这样我们就可以覆盖或者替换jetty的类,达到一些特殊的目的。
完成一系列的预处理之后,就该真正的启动server了
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
String mainClass = System.getProperty("jetty.server"); if (mainClass != null) { classname = mainClass; } mainClass = System.getProperty("main.class"); if (mainClass != null) { classname = mainClass; } Config.debug("main.class=" + classname); invokeMain(cl, classname, configuredXmls); |
invokeMain(cl, classname, configuredXmls);这个方法就是关键的启动方法了,这里默认的main.class=org.eclipse.jetty.xml.XmlConfiguration,其主要逻辑如是:
先初始化properties文件或者解析xml文件,在解析的过程中可能会通过反射机制初始化一些类。包括server类等;然后对于生成的类,如果是继承了lifecycle的,全部启动,从而完成容器的启动,主要代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 |
// For all arguments, load properties or parse XMLs XmlConfiguration last = null; Object[] obj = new Object[args.length]; for ( int i = 0; i 0 ) configuration.setProperties( properties ); obj[i] = configuration.configure(); last = configuration; } } // For all objects created by XmlConfigurations, start them if they are lifecycles. for ( int i = 0; i |
通过debug级别的log可以看到,最先解析的是parsing: sid=file:/D:/learn/jetty7/etc/jetty.xml,其实现的逻辑和以下这段代码类似:
1 2 3 4 5 |
Server server = new Server(8080); WebAppContext webapp = new WebAppContext(); webapp.setContextPath("/"); server.setHandler(webapp); server.start(); |
然后还有自定义的一些,比如jetty-deploy.xml等,整个初始化的过程就完成了。其实这里jetty也实现了一个简单的依赖注入的功能。
4. 总结:
start.jar 主要干了这几件事情:
1.解析命令行参数。
2.定位资源,根据参数和配置文件决定classpath和xmls的位置等。
3.反射调用启动server,并根据xml配置实现相关的依赖注入的功能