server.java

来自「RESIN 3.2 最新源码」· Java 代码 · 共 1,876 行 · 第 1/3 页

JAVA
1,876
字号
  public void classLoaderDestroy(DynamicClassLoader loader)  {    /*    try {      Jmx.unregister("resin:name=default,type=Server");      Jmx.unregister("resin:type=ThreadPool");    } catch (Throwable e) {      log.log(Level.FINEST, e.toString(), e);    }    */  }  /**   * Initialization.   */  @PostConstruct  public void init()  {    _classLoader.init();    super.init();    // initialize git repository    if (_resin != null) {      Path root = _resin.getRootDirectory();      // QA      if (root instanceof MemoryPath)	root = Vfs.lookup("file:/tmp/caucho/qa");      _git = new GitRepository(root.lookup(".git"));      try {	_git.initDb();      } catch (Exception e) {	log.log(Level.WARNING, e.toString(), e);      }    }    // backwards compat    if (_resin != null && _resin.getManagementPath() != null)      createManagement().setManagementPath(_resin.getManagementPath());    if (_resin != null) {      createManagement().setCluster(getCluster());      createManagement().setServer(this);      createManagement().init();    }    if (_threadMax < _threadIdleMax)      throw new ConfigException(L.l("<thread-idle-max> ({0}) must be less than <thread-max> ({1})",				    _threadIdleMax, _threadMax));    if (_threadIdleMax < _threadIdleMin)      throw new ConfigException(L.l("<thread-idle-min> ({0}) must be less than <thread-idle-max> ({1})",				    _threadIdleMin, _threadIdleMax));    if (_threadMax < _threadExecutorTaskMax)      throw new ConfigException(L.l("<thread-executor-task-max> ({0}) must be less than <thread-max> ({1})",				    _threadExecutorTaskMax, _threadMax));        ThreadPool threadPool = ThreadPool.getThreadPool();        threadPool.setThreadMax(_threadMax);    threadPool.setThreadIdleMax(_threadIdleMax);    threadPool.setThreadIdleMin(_threadIdleMin);    threadPool.setExecutorTaskMax(_threadExecutorTaskMax);    if (_keepaliveSelectEnable) {      try {        Class cl = Class.forName("com.caucho.server.port.JniSelectManager");        Method method = cl.getMethod("create", new Class[0]);        initSelectManager((AbstractSelectManager) method.invoke(null, null));      } catch (ClassNotFoundException e) {        log.warning(L.l("'select-manager' requires Resin Professional.  See http://www.caucho.com for information and licensing."));      } catch (Throwable e) {        log.warning(L.l("Cannot enable select-manager {0}", e.toString()));        log.log(Level.FINER, e.toString());      }      if (getSelectManager() != null) {	if (_keepaliveSelectMax > 0)	  getSelectManager().setSelectMax(_keepaliveSelectMax);      }    }  }  /**   * Start the server.   */  public void start()  {    init();    Thread thread = Thread.currentThread();    ClassLoader oldLoader = thread.getContextClassLoader();    try {      thread.setContextClassLoader(_classLoader);      if (! _lifecycle.toStarting())        return;      _startTime = Alarm.getCurrentTime();      if (! Alarm.isTest()) {        log.info("");        log.info(System.getProperty("os.name")		 + " " + System.getProperty("os.version")		 + " " + System.getProperty("os.arch"));	log.info(System.getProperty("java.runtime.name")		 + " " + System.getProperty("java.runtime.version")		 + ", " + System.getProperty("file.encoding")		 + ", " + System.getProperty("user.language"));		         log.info(System.getProperty("java.vm.name")		 + " " + System.getProperty("java.vm.version")		 + ", " + System.getProperty("sun.arch.data.model")		 + ", " + System.getProperty("java.vm.info")		 + ", " + System.getProperty("java.vm.vendor"));	log.info("");        Resin resin = Resin.getLocal();        if (resin != null) {          log.info("resin.home = " + resin.getResinHome().getNativePath());          log.info("resin.root = " + resin.getRootDirectory().getNativePath());          log.info("resin.conf = " + resin.getResinConf());          log.info("");          log.info("server     = "		   + _clusterServer.getClusterPort().getAddress()		   + ":" + _clusterServer.getClusterPort().getPort()		   + " (" + getCluster().getId()		   + ":" + getServerId() + ")");        }        else {          log.info("resin.home = " + System.getProperty("resin.home"));        }	        log.info("user.name  = " + System.getProperty("user.name"));        log.info("");      }      _lifecycle.toStarting();      if (_resin != null && _resin.getManagement() != null)	_resin.getManagement().start(this);      AbstractSelectManager selectManager = getSelectManager();            if (! _keepaliveSelectEnable	  || selectManager == null	  || ! selectManager.start()) {	initSelectManager(null);      }      if (! _isBindPortsAtEnd) {        bindPorts();	startPorts();      }      _classLoader.start();      _hostContainer.start();            // initialize the environment admin      _classLoader.getAdmin();      getCluster().startPersistentStore();      // will only occur if bind-ports-at-end is true      if (_isBindPortsAtEnd) {        bindPorts();	startPorts();      }      getCluster().startRemote();      _alarm.queue(ALARM_INTERVAL);            _lifecycle.toActive();      // dynamic updates from the cluster start after we're capable of      // handling messages      getCluster().startClusterUpdate();    } catch (RuntimeException e) {      log.log(Level.WARNING, e.toString(), e);            _lifecycle.toError();            throw e;    } catch (Exception e) {      log.log(Level.WARNING, e.toString(), e);            _lifecycle.toError();            // if the server can't start, it needs to completely fail, especially      // for the watchdog      throw new RuntimeException(e);            // log.log(Level.WARNING, e.toString(), e);      // _configException = e;    } finally {      thread.setContextClassLoader(oldLoader);    }  }  /**   * Bind the ports.   */  public void bindPorts()    throws Exception  {    synchronized (this) {      if (_isStartedPorts)        return;      _isStartedPorts = true;    }    Thread thread = Thread.currentThread();    ClassLoader oldLoader = thread.getContextClassLoader();    try {      thread.setContextClassLoader(_classLoader);      ArrayList<Port> ports = _clusterServer.getPorts();      if (ports.size() > 0) {	log.info("");      	for (int i = 0; i < ports.size(); i++) {	  Port port = ports.get(i);	  port.setServer(this);	  port.bind();	}      	log.info("");      }    } finally {      thread.setContextClassLoader(oldLoader);    }  }  /**   * Start the ports.   */  public void startPorts()    throws Exception  {    Thread thread = Thread.currentThread();    ClassLoader oldLoader = thread.getContextClassLoader();    try {      thread.setContextClassLoader(_classLoader);      ArrayList<Port> ports = _clusterServer.getPorts();      for (int i = 0; i < ports.size(); i++) {        Port port = ports.get(i);	port.start();      }    } finally {      thread.setContextClassLoader(oldLoader);    }  }  /**   * Handles the alarm.   */  public void handleAlarm(Alarm alarm)  {    if (! _lifecycle.isActive())      return;    try {      long now = Alarm.getCurrentTime();      if (isModified()) {        // XXX: message slightly wrong        log.warning("Resin restarting due to configuration change");        _clusterServer.getCluster().getResin().destroy();        return;      }      try {	ArrayList<Port> ports = _clusterServer.getPorts();	        for (int i = 0; i < ports.size(); i++) {          Port port = ports.get(i);          if (port.isClosed()) {            log.severe("Resin restarting due to closed port: " + port);            // destroy();            //_controller.restart();          }        }      } catch (Throwable e) {        log.log(Level.WARNING, e.toString(), e);        // destroy();        //_controller.restart();        return;      }    } finally {      alarm.queue(ALARM_INTERVAL);    }  }  /**   * Returns true if the server has been modified and needs restarting.   */  public boolean isModified()  {    boolean isModified = _classLoader.isModified();    if (isModified)      log.fine("server is modified");    return isModified;  }  /**   * Returns true if the server has been modified and needs restarting.   */  public boolean isModifiedNow()  {    boolean isModified = _classLoader.isModifiedNow();    if (isModified)      log.fine("server is modified");    return isModified;  }  /**   * Returns true if the server is stopped.   */  public boolean isStopping()  {    return _lifecycle.isStopping();  }  /**   * Returns true if the server is stopped.   */  public boolean isStopped()  {    return _lifecycle.isStopped();  }  /**   * Returns true if the server is closed.   */  public boolean isDestroyed()  {    return _lifecycle.isDestroyed();  }  /**   * Returns true if the server is closed.   */  public boolean isDestroying()  {    return _lifecycle.isDestroying();  }  /**   * Returns true if the server is currently active and accepting requests   */  public boolean isActive()  {    return _lifecycle.isActive();  }  /**   * Clears the catch by matching the invocation.   */  public void clearCacheByPattern(String hostPattern, String uriPattern)  {    final Matcher hostMatcher;    if (hostPattern != null)      hostMatcher = Pattern.compile(hostPattern).matcher("");    else      hostMatcher = null;    final Matcher uriMatcher;    if (uriPattern != null)      uriMatcher = Pattern.compile(uriPattern).matcher("");    else      uriMatcher = null;    InvocationMatcher matcher = new InvocationMatcher() {        public boolean isMatch(Invocation invocation)        {          if (hostMatcher != null) {            hostMatcher.reset(invocation.getHost());            if (! hostMatcher.find()) {              return false;            }          }          if (uriMatcher != null) {            uriMatcher.reset(invocation.getURI());            if (! uriMatcher.find()) {              return false;            }          }          return true;        }      };    invalidateMatchingInvocations(matcher);  }  /**   * Clears the proxy cache.   */  public void clearCache()  {    // skip the clear on restart    if (isStopping())      return;    if (log.isLoggable(Level.FINEST))      log.finest("ServletServer clearCache");    // the invocation cache must be cleared first because the old    // filter chain entries must not point to the cache's    // soon-to-be-invalid entries    super.clearCache();    if (_cache != null)      _cache.clear();  }  /**   * Returns the proxy cache hit count.   */  public long getProxyCacheHitCount()  {    if (_cache != null)      return _cache.getHitCount();    else      return 0;  }  /**   * Returns the proxy cache miss count.   */  public long getProxyCacheMissCount()  {    if (_cache != null)      return _cache.getMissCount();    else      return 0;  }  /**   * Returns any HMTP stream   */  public BamStream getHmtpStream()  {    return null;  }    public void addDynamicServer(String clusterId,			       String serverId,			       String address,			       int port)  {  }    public void removeDynamicServer(String clusterId,				  String address,				  int port)  {  }  /**   * Closes the server.   */  public void stop()  {    Thread thread = Thread.currentThread();    ClassLoader oldLoader = thread.getContextClassLoader();    try {      thread.setContextClassLoader(_classLoader);      if (! _lifecycle.toStopping())        return;      Alarm alarm = _alarm;      _alarm = null;      if (alarm != null)        alarm.dequeue();      if (getSelectManager() != null)        getSelectManager().stop();      ArrayList<Port> ports = _clusterServer.getPorts();      for (int i = 0; i < ports.size(); i++) {        Port port = ports.get(i);        try {          port.close();        } catch (Throwable e) {          log.log(Level.WARNING, e.toString(), e);        }      }      try {        ThreadPool.getThreadPool().interrupt();      } catch (Throwable e) {        log.log(Level.WARNING, e.toString(), e);      }      try {        Thread.yield();      } catch (Throwable e) {      }      try {	if (_hostContainer != null)	  _hostContainer.stop();      } catch (Throwable e) {        log.log(Level.WARNING, e.toString(), e);      }      try {        _classLoader.stop();      } catch (Throwable e) {        log.log(Level.WARNING, e.toString(), e);      }      _lifecycle.toStop();    } finally {      thread.setContextClassLoader(oldLoader);            super.stop();    }  }  /**   * Closes the server.   */  public void destroy()  {    stop();    if (! _lifecycle.toDestroy())      return;    Thread thread = Thread.currentThread();    ClassLoader oldLoader = thread.getContextClassLoader();    try {      thread.setContextClassLoader(_classLoader);      try {	Management management = _management;	_management = null;	if (management != null)	  management.destroy();      } catch (Throwable e) {        log.log(Level.WARNING, e.toString(), e);      }      try {        _hostContainer.destroy();      } catch (Throwable e) {        log.log(Level.WARNING, e.toString(), e);      }      super.destroy();      log.fine(this + " destroyed");      _classLoader.destroy();      _hostContainer = null;      _accessLog = null;      _cache = null;    } finally {      DynamicClassLoader.setOldLoader(thread, oldLoader);      Resin resin = _resin;      if (resin != null)        resin.destroy();    }  }  public String toString()  {    return (getClass().getSimpleName() + "[id=" + getServerId()	    + ",cluster=" + _clusterServer.getCluster().getId() + "]");  }  public static class SelectManagerCompat {    private boolean _isEnable = true;    public void setEnable(boolean isEnable)    {      _isEnable = isEnable;    }    public boolean isEnable()    {      return _isEnable;    }  }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?