zldeng1984 发表于 2013-2-3 11:24:09

jetty源码分析4-start.jar

本文主要分析下jetty的start.jar中的jar包加载顺序和一些简单逻辑,由于没有下到start.jar包的源码,用反编译的方式搞的,代码可能有些地方不一定准确。
 
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.jarjetty.home/=$(jetty.home)                        exists $(jetty.home)/start.jar# The main class to runorg.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 lineOPTIONS~=default,*                               ! property OPTIONS# Add a resources directory if it is there$(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;                  for ( int i = 0; i < args.length; i++ )                  {                        if ( args.toLowerCase().endsWith( ".properties" ) )                        {                            properties.load( Resource.newResource( args ).getInputStream() );                        }                        else                        {                            XmlConfiguration configuration =                              new XmlConfiguration( Resource.newResource( args ).getURL() );                            if ( last != null )                              configuration.getIdMap().putAll( last.getIdMap() );                            if ( properties.size() > 0 )                              configuration.setProperties( properties );                            obj = 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 instanceof LifeCycle )                        {                            LifeCycle lc = (LifeCycle) obj;                            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配置实现相关的依赖注入的功能
 
 
 
页: [1]
查看完整版本: jetty源码分析4-start.jar