jboss之启动加载过程详解(三)

来源:互联网 发布:智能排班软件 编辑:程序博客网 时间:2024/05/20 05:06

jboss启动的main函数源码:

private Properties props = new Properties(System.getProperties());  public void boot(String[] args)    throws Exception  {    processCommandLine(args);    String homeDir = this.props.getProperty("jboss.home.dir");    if (homeDir == null)    {      String path = Main.class.getProtectionDomain().getCodeSource().getLocation().getFile();      path = URLDecoder.decode(path, "UTF-8");      File runJar = new File(path);      File homeFile = runJar.getParentFile().getParentFile();      homeDir = homeFile.getCanonicalPath();    }    this.props.setProperty("jboss.home.dir", homeDir);    String homeURL = this.props.getProperty("jboss.home.url");    if (homeURL == null)    {      File file = new File(homeDir);      homeURL = file.toURL().toString();      this.props.setProperty("jboss.home.url", homeURL);    }    ServerLoader loader = new ServerLoader(this.props);    if (this.bootURL != null)    {      if (this.bootURL.getProtocol().equals("file"))      {        File dir = new File(this.bootURL.getFile());        if (dir.exists())        {          loader.addURL(dir.toURL());          File[] jars = dir.listFiles(new JarFilter());          for (int j = 0; (jars != null) && (j < jars.length); j++)          {            loader.addURL(jars[j].getCanonicalFile().toURL());          }        }      }      else      {        loader.addURL(this.bootURL);      }    }    for (int i = 0; i < this.bootLibraries.size(); i++)    {      loader.addLibrary((String)this.bootLibraries.get(i));    }    loader.addEndorsedJars();    loader.addLibraries(this.jmxLibs);    loader.addLibrary(this.concurrentLib);    for (int i = 0; i < this.extraLibraries.size(); i++)    {      loader.addLibrary((String)this.extraLibraries.get(i));    }    for (int i = 0; i < this.extraClasspath.size(); i++)    {      loader.addURL((URL)this.extraClasspath.get(i));    }    ClassLoader parentCL = Thread.currentThread().getContextClassLoader();    Server server = loader.load(parentCL);    server.init(this.props);    ServerConfig config = server.getConfig();    config.setExitOnShutdown(true);    server.start();  }  private URL makeURL(String urlspec) throws MalformedURLException  {    urlspec = urlspec.trim();    try    {      URL url = new URL(urlspec);      if (url.getProtocol().equals("file"))      {        File file = new File(url.getFile()).getCanonicalFile();        url = file.toURL();      }    }    catch (Exception e)    {      try      {        File file = new File(urlspec).getCanonicalFile();        url = file.toURL();      }      catch (Exception n)      {        URL url;        throw new MalformedURLException(n.toString());      }    }    URL url;    return url;  }  private void processCommandLine(String[] args)    throws Exception  {    String programName = System.getProperty("program.name", "jboss");    String sopts = "-:hD:d:p:n:c:Vj::B:L:C:P:b:g:u:";    LongOpt[] lopts = { new LongOpt("help", 0, null, 104), new LongOpt("bootdir", 1, null, 100), new LongOpt("patchdir", 1, null, 112), new LongOpt("netboot", 1, null, 110), new LongOpt("configuration", 1, null, 99), new LongOpt("version", 0, null, 86), new LongOpt("jaxp", 1, null, 106), new LongOpt("bootlib", 1, null, 66), new LongOpt("library", 1, null, 76), new LongOpt("classpath", 1, null, 67), new LongOpt("properties", 1, null, 80), new LongOpt("host", 1, null, 98), new LongOpt("partition", 1, null, 103), new LongOpt("udp", 1, null, 117) };    Getopt getopt = new Getopt(programName, args, sopts, lopts);    this.props.setProperty("jboss.bind.address", "0.0.0.0");    System.setProperty("jboss.bind.address", "0.0.0.0");    int code;    while ((code = getopt.getopt()) != -1)    {      switch (code)      {      case 58:      case 63:        System.exit(1);        break;      case 1:        System.err.println(programName + ": unused non-option argument: " + getopt.getOptarg());        break;      case 104:        System.out.println("usage: " + programName + " [options]");        System.out.println();        System.out.println("options:");        System.out.println("    -h, --help                    Show this help message");        System.out.println("    -V, --version                 Show version information");        System.out.println("    --                            Stop processing options");        System.out.println("    -D<name>[=<value>]            Set a system property");        System.out.println("    -d, --bootdir=<dir>           Set the boot patch directory; Must be absolute or url");        System.out.println("    -p, --patchdir=<dir>          Set the patch directory; Must be absolute or url");        System.out.println("    -n, --netboot=<url>           Boot from net with the given url as base");        System.out.println("    -c, --configuration=<name>    Set the server configuration name");        System.out.println("    -B, --bootlib=<filename>      Add an extra library to the front bootclasspath");        System.out.println("    -L, --library=<filename>      Add an extra library to the loaders classpath");        System.out.println("    -C, --classpath=<url>         Add an extra url to the loaders classpath");        System.out.println("    -P, --properties=<url>        Load system properties from the given url");        System.out.println("    -b, --host=<host or ip>       Bind address for all JBoss services");        System.out.println("    -g, --partition=<name>        HA Partition name (default=DefaultDomain)");        System.out.println("    -u, --udp=<ip>                UDP multicast address");        System.out.println();        System.exit(0);        break;      case 68:        String arg = getopt.getOptarg();        int i = arg.indexOf("=");        String value;        String name;        String value;        if (i == -1)        {          String name = arg;          value = "true";        }        else        {          name = arg.substring(0, i);          value = arg.substring(i + 1, arg.length());        }        System.setProperty(name, value);        break;      case 100:        this.bootURL = makeURL(getopt.getOptarg());        break;      case 112:        URL patchURL = makeURL(getopt.getOptarg());        this.props.put("jboss.patch.url", patchURL.toString());        break;      case 110:        String arg = getopt.getOptarg();        if (!arg.endsWith("/")) arg = arg + "/";        this.props.put("jboss.home.url", new URL(arg).toString());        break;      case 99:        String arg = getopt.getOptarg();        this.props.put("jboss.server.name", arg);        break;      case 86:        Package jbossPackage = Package.getPackage("org.jboss");        System.out.println("JBoss " + jbossPackage.getImplementationVersion());        System.out.println();        System.out.println("Distributable under LGPL license.");        System.out.println("See terms of license at gnu.org.");        System.out.println();        System.exit(0);        break;      case 106:        System.err.println(programName + ": option '-j, --jaxp' no longer supported");        System.exit(1);        break;      case 66:        String arg = getopt.getOptarg();        this.bootLibraries.add(arg);        break;      case 76:        String arg = getopt.getOptarg();        this.extraLibraries.add(arg);        break;      case 67:        URL url = makeURL(getopt.getOptarg());        this.extraClasspath.add(url);        break;      case 80:        URL url = makeURL(getopt.getOptarg());        Properties props = System.getProperties();        props.load(url.openConnection().getInputStream());        break;      case 98:        String arg = getopt.getOptarg();        this.props.put("jboss.bind.address", arg);        System.setProperty("jboss.bind.address", arg);        System.setProperty("bind.address", arg);        String rmiHost = System.getProperty("java.rmi.server.hostname");        if (rmiHost != null)          continue;        rmiHost = ServerConfigUtil.fixRemoteAddress(arg);        System.setProperty("java.rmi.server.hostname", rmiHost); break;      case 103:        String arg = getopt.getOptarg();        this.props.put("jboss.partition.name", arg);        System.setProperty("jboss.partition.name", arg);        break;      case 117:        String arg = getopt.getOptarg();        this.props.put("jboss.partition.udpGroup", arg);        System.setProperty("jboss.partition.udpGroup", arg);        break;      default:        throw new Error("unhandled option code: " + code);      }    }  }  public static void main(String[] args)    throws Exception  {    Runnable worker = new Runnable(args) {      private final String[] val$args;      public void run() {        try { Main main = new Main();          main.boot(this.val$args);        }        catch (Exception e)        {          System.err.println("Failed to boot JBoss:");          e.printStackTrace();        }      }    };    ThreadGroup threads = new ThreadGroup("jboss");    new Thread(threads, worker, "main").start();  }  public static void systemExit(String[] argv)  {    System.exit(0);  }  static class JarFilter implements FilenameFilter  {    public boolean accept(File dir, String name)    {      return name.endsWith(".jar");    }  }}


0 0
原创粉丝点击