jetty的start.jar
来源:互联网 发布:阿里云监控怎么用 编辑:程序博客网 时间:2024/09/21 09:20
1.main方法
main方法非常简单,基本就这一句:
- Main main = new Main().parseCommandLine(args);
2. public void parseCommandLine(String[] args)
该函数主要完成解析命令行参数的功能:
1.首先遍历所有的传入参数,并把他们加入到arguments这个ArrayList<String>中。如果指定了--ini=,则吧ini指定文件中的所有参数也加入到
arguments,如果是--config=,则设置 this._startConfig,最后如果传入参数中没有 --ini=或者--ini,就是用默认的ini,并把参数加入到arguments。
- 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);
3. public void start(List<String> 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类来实现,部分代码如下:
- 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.截取一小段示例一下:
- # 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了
- 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的,全部启动,从而完成容器的启动,主要代码如下:
- // For all arguments, load properties or parse XMLs
- XmlConfiguration last = null;
- Object[] obj = new Object[args.length];
- for ( int i = 0; i < args.length; i++ )
- {
- if ( args[i].toLowerCase().endsWith( ".properties" ) )
- {
- properties.load( Resource.newResource( args[i] ).getInputStream() );
- }
- else
- {
- XmlConfiguration configuration =
- new XmlConfiguration( Resource.newResource( args[i] ).getURL() );
- if ( last != null )
- configuration.getIdMap().putAll( last.getIdMap() );
- if ( properties.size() > 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 < args.length; i++ )
- {
- if ( obj[i] instanceof LifeCycle )
- {
- LifeCycle lc = (LifeCycle) obj[i];
- if ( !lc.isRunning() )
- lc.start();
- }
- }
通过debug级别的log可以看到,最先解析的是parsing: sid=file:/D:/learn/jetty7/etc/jetty.xml,其实现的逻辑和以下这段代码类似:
- 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配置实现相关的依赖注入的功能
- jetty的start.jar
- jetty的start.jar
- Jetty学习 【八】 使用start.jar启动webapp
- Jetty中添加额外的jar包
- jetty-start(1)
- jetty-start(2)
- Jetty Quick Start
- jetty start 启动的项目实例及调试操作
- jetty学习1 quick start jetty
- Jetty 配置文件语法 Start.config
- jetty.start.config中英文对照
- jetty命令行方式启动jetty-runner.jar 容器
- jar包冲突,jetty跑起来的项目在tomcat跑不起来
- jdk、IDE以及jetty容器加载不同jar中相同类的区别
- jetty-runner-*.jar 运行war小例子
- maven启动jetty 加载jar包
- Jetty学习【一】 Jetty的嵌入式使用
- [深入浅出Jetty 04]Jetty的启动方式
- 【元数据分布算法研究笔记】-HAP
- 谷歌眼镜GDK开发指南之Immersions
- jetty学习3-handlers
- 寒假打算
- 广州传智播客为改变中国IT教育之.Net培训
- jetty的start.jar
- 关于AD和msps
- 从月薪3500到700万——一个大学生的成长经历
- 广州学安卓哪里好?
- jetty迁移的classload一例
- 第五章 二
- 高手速成android开源项目【blog篇】
- qemu1.7 kvm相关新特性列表
- 谷歌眼镜GDK开发指南之活动卡片中显示菜单