Package com.caucho.boot

Source Code of com.caucho.boot.ResinBoot

/*
* Copyright (c) 1998-2011 Caucho Technology -- all rights reserved
*
* This file is part of Resin(R) Open Source
*
* Each copy or derived work must preserve the copyright notice and this
* notice unmodified.
*
* Resin Open Source is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* Resin Open Source is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
* of NON-INFRINGEMENT.  See the GNU General Public License for more
* details.
*
* You should have received a copy of the GNU General Public License
* along with Resin Open Source; if not, write to the
*
*   Free Software Foundation, Inc.
*   59 Temple Place, Suite 330
*   Boston, MA 02111-1307  USA
*
* @author Scott Ferguson
*/

package com.caucho.boot;

import java.util.HashMap;
import java.util.logging.ConsoleHandler;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.Logger;

import com.caucho.VersionFactory;
import com.caucho.boot.WatchdogArgs.StartMode;
import com.caucho.config.Config;
import com.caucho.config.ConfigException;
import com.caucho.config.inject.InjectManager;
import com.caucho.config.lib.ResinConfigLibrary;
import com.caucho.env.service.ResinSystem;
import com.caucho.env.shutdown.ExitCode;
import com.caucho.loader.DynamicClassLoader;
import com.caucho.loader.Environment;
import com.caucho.loader.LibraryLoader;
import com.caucho.server.resin.ResinELContext;
import com.caucho.util.L10N;
import com.caucho.vfs.Path;
import com.caucho.vfs.Vfs;

/**
* ResinBoot is the main bootstrap class for Resin.  It parses the
* resin.xml and looks for the <server> block matching the -server
* argument.
*
* <h3>Start Modes:</h3>
*
* The start modes are STATUS, DIRECT, START, STOP, KILL, RESTART, SHUTDOWN.
*
* <ul>
* <li>DIRECT starts a <server> from the command line
* <li>START starts a <server> with a Watchdog in the background
* <li>STOP stop the <server> Resin in the background
* </ul>
*/
public class ResinBoot {
  private static L10N _L;
  private static Logger _log;
 
  private static HashMap<StartMode,BootCommand> _commandMap
    = new HashMap<StartMode,BootCommand>();

  private WatchdogArgs _args;

  private WatchdogClient _client;
  private ResinGUI _ui;

  ResinBoot(String []argv)
    throws Exception
  {
    _args = new WatchdogArgs(argv);

    Path resinHome = _args.getResinHome();

    ClassLoader loader = ProLoader.create(resinHome, _args.is64Bit());

    if (loader != null) {
      System.setProperty("resin.home", resinHome.getNativePath());

      Thread.currentThread().setContextClassLoader(loader);

      Environment.init();

      Vfs.initJNI();

      resinHome = Vfs.lookup(resinHome.getFullPath());

      _args.setResinHome(resinHome);
    }
    else {
      Environment.init();
    }
   
    String jvmVersion = System.getProperty("java.runtime.version");
   
    if ("1.6".compareTo(jvmVersion) > 0) {
      throw new ConfigException(L().l("Resin requires Java 1.6 or later but was started with {0}",
                                      jvmVersion));
    }

    // required for license check
    System.setProperty("resin.home", resinHome.getNativePath());

    // watchdog/0210
    // Vfs.setPwd(_rootDirectory);

    if (! _args.getResinConf().canRead()) {
      throw new ConfigException(L().l("Resin/{0} can't open configuration file '{1}'",
                                      VersionFactory.getVersion(),
                                      _args.getResinConf().getNativePath()));
    }
   
    Path rootDirectory = _args.getRootDirectory();
    Path dataDirectory = rootDirectory.lookup("watchdog-data");

    ResinSystem system = new ResinSystem("watchdog",
                                         rootDirectory,
                                         dataDirectory);

    Thread thread = Thread.currentThread();
    thread.setContextClassLoader(system.getClassLoader());
   
    LibraryLoader libLoader = new LibraryLoader();
    libLoader.setPath(rootDirectory.lookup("lib"));
    libLoader.init();

    Config config = new Config();
    BootResinConfig bootManager = new BootResinConfig(system, _args);

    ResinELContext elContext = _args.getELContext();

    /**
     * XXX: the following setVar calls should not be necessary, but the
     * EL.setEnviornment() call above is not effective:
     */
    InjectManager beanManager = InjectManager.create();

    Config.setProperty("resinHome", elContext.getResinHome());
    Config.setProperty("java", elContext.getJavaVar());
    Config.setProperty("resin", elContext.getResinVar());
    Config.setProperty("server", elContext.getServerVar());
    Config.setProperty("system", System.getProperties());
    Config.setProperty("getenv", System.getenv());

    ResinConfigLibrary.configure(beanManager);

    config.configure(bootManager, _args.getResinConf(),
                     "com/caucho/server/resin/resin.rnc");

    if (_args.isDynamicServer()) {
      _client = bootManager.addDynamicClient(_args);
    }
    else {
      _client = bootManager.findClient(_args.getServerId());
    }
   
    if (_client == null && _args.isShutdown()) {
      _client = bootManager.findShutdownClient();
    }
   
    if (_client == null && ! (_args.isStart() || _args.isConsole())) {
      _client = bootManager.findShutdownClient();
    }

    if (_client == null) {
      throw new ConfigException(L().l("Resin/{0}: -server '{1}' does not match any defined <server>\nin {2}.",
                                      VersionFactory.getVersion(), _args.getServerId(), _args.getResinConf()));
    }
  }

  boolean start()
    throws Exception
  {
    if (_args.isStatus()) {
      try {
        String status = _client.statusWatchdog();

        System.out.println(L().l("Resin/{0} status for watchdog at {1}:{2}",
                                 VersionFactory.getVersion(),
                                 _client.getWatchdogAddress(),
                                 _client.getWatchdogPort()));
        System.out.println(status);
      } catch (Exception e) {
        System.out.println(L().l("Resin/{0} can't retrieve status of -server '{1}' for watchdog at {2}:{3}.\n{4}",
                                 VersionFactory.getVersion(), _client.getId(),
                                 _client.getWatchdogAddress(),
                                 _client.getWatchdogPort(),
                                 e.toString()));

        log().log(Level.FINE, e.toString(), e);

        System.exit(1);
      }

      return false;
    }
    else if (_args.isStart()) {
      try {
        _client.startWatchdog(_args.getArgv());

        System.out.println(L().l("Resin/{0} started -server '{1}' for watchdog at {2}:{3}",
                                 VersionFactory.getVersion(), _client.getId(),
                                 _client.getWatchdogAddress(),
                                 _client.getWatchdogPort()));
      } catch (Exception e) {
        String eMsg;

        if (e instanceof ConfigException)
          eMsg = e.getMessage();
        else
          eMsg = e.toString();

        System.out.println(L().l("Resin/{0} can't start -server '{1}' for watchdog at {2}:{3}.\n  {4}",
                                 VersionFactory.getVersion(), _client.getId(),
                                 _client.getWatchdogAddress(),
                                 _client.getWatchdogPort(),
                                 eMsg));

        log().log(Level.FINE, e.toString(), e);

        System.exit(1);
      }

      return false;
    }
    else if (_args.isStop()) {
      try {
        _client.stopWatchdog();

        System.out.println(L().l("Resin/{0} stopped -server '{1}' for watchdog at {2}:{3}",
                                 VersionFactory.getVersion(), _client.getId(),
                                 _client.getWatchdogAddress(),
                                 _client.getWatchdogPort()));
      } catch (Exception e) {
        System.out.println(L().l("Resin/{0} can't stop -server '{1}' for watchdog at {2}:{3}.\n{4}",
                                 VersionFactory.getVersion(), _client.getId(),
                                 _client.getWatchdogAddress(),
                                 _client.getWatchdogPort(),
                                 e.toString()));

        log().log(Level.FINE, e.toString(), e);

        System.exit(1);
      }

      return false;
    }
    else if (_args.isKill()) {
      try {
        _client.killWatchdog();

        System.out.println(L().l("Resin/{0} killed -server '{1}' for watchdog at {2}:{3}",
                                 VersionFactory.getVersion(), _client.getId(),
                                 _client.getWatchdogAddress(),
                                 _client.getWatchdogPort()));
      } catch (Exception e) {
        System.out.println(L().l("Resin/{0} can't kill -server '{1}' for watchdog at {2}:{3}.\n{4}",
                                 VersionFactory.getVersion(), _client.getId(),
                                 _client.getWatchdogAddress(),
                                 _client.getWatchdogPort(),
                                 e.toString()));

        log().log(Level.FINE, e.toString(), e);

        System.exit(1);
      }

      return false;
    }
    else if (_args.isRestart()) {
      try {
        _client.restartWatchdog(_args.getArgv());

        System.out.println(L().l("Resin/{0} restarted -server '{1}' for watchdog at {2}:{3}",
                                 VersionFactory.getVersion(), _client.getId(),
                                 _client.getWatchdogAddress(),
                                 _client.getWatchdogPort()));
      } catch (Exception e) {
        System.out.println(L().l("Resin/{0} can't restart -server '{1}'.\n{2}",
                                 VersionFactory.getVersion(), _client.getId(),
                                 e.toString()));

        log().log(Level.FINE, e.toString(), e);

        System.exit(1);
      }

      return false;
    }
    else if (_args.isShutdown()) {
      try {
        _client.shutdown();

        System.out.println(L().l("Resin/{0} shutdown watchdog at {1}:{2}",
                                 VersionFactory.getVersion(),
                                 _client.getWatchdogAddress(),
                                 _client.getWatchdogPort()));
      } catch (Exception e) {
        System.out.println(L().l("Resin/{0} can't shutdown watchdog at {1}:{2}.\n{3}",
                                 VersionFactory.getVersion(),
                                 _client.getWatchdogAddress(),
                                 _client.getWatchdogPort(),
                                 e.toString()));

        log().log(Level.FINE, e.toString(), e);

        System.exit(1);
      }

      return false;
    }
    else if (_args.isConsole()) {
      return _client.startConsole() != 0;
    }
    else if (_args.isWatchdogConsole()) {
      WatchdogManager.main(_args.getRawArgv());
    }
    else if (_args.isGui()) {
      if (_ui != null && _ui.isVisible())
        return true;
      else if (_ui != null)
        return false;

      _ui = new ResinGUI(this, _client);
      _ui.setVisible(true);

      return true;
    }
   
    BootCommand command = _commandMap.get(_args.getStartMode());
   
    if (command != null && _args.isHelp()) {
      command.usage();
     
      return false;
    } else if (command != null) {
      command.doCommand(_args, _client);

      return false;
    }
   
    throw new IllegalStateException(L().l("Unknown start mode"));
  }

  /**
   * The main start of the web server.
   *
   * <pre>
   * -conf resin.xml  : alternate configuration file
   * -server web-a    : &lt;server> to start
   * <pre>
   */
  public static void main(String []argv)
  {
    if (System.getProperty("log.level") != null) {
      Logger.getLogger("").setLevel(Level.FINER);
    }
    else {
      for (Handler handler : Logger.getLogger("").getHandlers()) {
        if (handler instanceof ConsoleHandler) {
          handler.setLevel(Level.FINER);
          Logger.getLogger("").removeHandler(handler);
        }
      }
    }

    try {
      ResinBoot boot = new ResinBoot(argv);

      while (boot.start()) {
        try {
          synchronized (boot) {
            boot.wait(5000);
          }
        } catch (Exception e) {
        }
      }
     
      System.exit(ExitCode.OK.ordinal());
    } catch (ConfigException e) {
      System.out.println(e.getMessage());

      System.exit(ExitCode.BAD_CONFIG.ordinal());
    } catch (Exception e) {
      e.printStackTrace();

      System.exit(ExitCode.UNKNOWN.ordinal());
    }
  }

  private static L10N L()
  {
    if (_L == null)
      _L = new L10N(ResinBoot.class);

    return _L;
  }

  private static Logger log()
  {
    if (_log == null)
      _log = Logger.getLogger(ResinBoot.class.getName());

    return _log;
  }
 
  static {
    _commandMap.put(StartMode.DEPLOY_COPY, new DeployCopyCommand());
    _commandMap.put(StartMode.DEPLOY, new DeployCommand());
    _commandMap.put(StartMode.DEPLOY_LIST, new DeployListCommand());
    _commandMap.put(StartMode.DEPLOY_RESTART, new DeployRestartCommand());
    _commandMap.put(StartMode.THREAD_DUMP, new DeployStartCommand());
    _commandMap.put(StartMode.DEPLOY_START, new DeployStartCommand());
    _commandMap.put(StartMode.DEPLOY_STOP, new DeployStopCommand());
    _commandMap.put(StartMode.DISABLE, new DisableCommand());
    _commandMap.put(StartMode.ENABLE, new EnableCommand());
    _commandMap.put(StartMode.HEAP_DUMP, new HeapDumpCommand());
    _commandMap.put(StartMode.JMX_CALL, new JmxCallCommand());
    _commandMap.put(StartMode.JMX_LIST, new JmxListCommand());
    _commandMap.put(StartMode.JMX_SET, new JmxSetCommand());
    _commandMap.put(StartMode.JSPC, new JspcCommand());
    _commandMap.put(StartMode.LIST_RESTARTS, new ListRestartsCommand());
    _commandMap.put(StartMode.LOG_LEVEL, new LogLevelCommand());
    _commandMap.put(StartMode.PROFILE, new ProfileCommand());
    _commandMap.put(StartMode.THREAD_DUMP, new ThreadDumpCommand());

    _commandMap.put(StartMode.UNDEPLOY, new UnDeployCommand());

    _commandMap.put(StartMode.USER_ADD, new AddUserCommand());
    _commandMap.put(StartMode.USER_LIST, new ListUsersCommand());
    _commandMap.put(StartMode.USER_REMOVE, new RemoveUserCommand());
  }
}
TOP

Related Classes of com.caucho.boot.ResinBoot

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.