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配置实现相关的依赖注入的功能
运维网声明
1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网 享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com