resin.java
来自「RESIN 3.2 最新源码」· Java 代码 · 共 1,949 行 · 第 1/3 页
JAVA
1,949 行
try { RandomUtil.addRandom(InetAddress.getLocalHost().toString()); } catch (Throwable e) { } // for systems with /dev/urandom, read more bits from it. try { InputStream is = new FileInputStream("/dev/urandom"); for (int i = 0; i < 16; i++) RandomUtil.addRandom(is.read()); is.close(); } catch (Throwable e) { } RandomUtil.addRandom(System.currentTimeMillis()); } /** * Thread to wait until Resin should be stopped. */ public void waitForExit() { int socketExceptionCount = 0; Integer memoryTest; Runtime runtime = Runtime.getRuntime(); /* * If the server has a parent process watching over us, close * gracefully when the parent dies. */ while (! isClosing()) { try { Thread.sleep(10); long minFreeMemory = getMinFreeMemory(); if (minFreeMemory <= 0) { // memory check disabled } else if (2 * minFreeMemory < getFreeMemory(runtime)) { // plenty of free memory } else { if (log().isLoggable(Level.FINER)) { log().finer(L().l("free memory {0} max:{1} total:{2} free:{3}", "" + getFreeMemory(runtime), "" + runtime.maxMemory(), "" + runtime.totalMemory(), "" + runtime.freeMemory())); } log().info(L().l("Forcing GC due to low memory. {0} free bytes.", getFreeMemory(runtime))); runtime.gc(); Thread.sleep(1000); runtime.gc(); if (getFreeMemory(runtime) < minFreeMemory) { log().severe(L().l("Restarting due to low free memory. {0} free bytes", getFreeMemory(runtime))); return; } } // second memory check memoryTest = new Integer(0); if (_waitIn != null) { int len; if ((len = _waitIn.read()) >= 0) { socketExceptionCount = 0; } else log().warning(L().l("Stopping due to watchdog or user.")); return; } else { synchronized (this) { wait(10000); } } } catch (SocketTimeoutException e) { socketExceptionCount = 0; } catch (InterruptedIOException e) { socketExceptionCount = 0; } catch (InterruptedException e) { socketExceptionCount = 0; } catch (SocketException e) { // The Solaris JVM will throw SocketException periodically // instead of interrupted exception, so those exceptions need to // be ignored. // However, the Windows JVMs will throw connection reset by peer // instead of returning an end of file in the read. So those // need to be trapped to close the socket. if (socketExceptionCount++ == 0) { log().log(Level.FINE, e.toString(), e); } else if (socketExceptionCount > 100) return; } catch (OutOfMemoryError e) { try { EnvironmentStream.getOriginalSystemErr().println("Resin halting due to out of memory"); } catch (Exception e1) { } finally { Runtime.getRuntime().halt(1); } } catch (Throwable e) { log().log(Level.WARNING, e.toString(), e); return; } } } public String toString() { return getClass().getSimpleName() + "[id=" + _serverId + "]"; } private static long getFreeMemory(Runtime runtime) { long maxMemory = runtime.maxMemory(); long totalMemory = runtime.totalMemory(); long freeMemory = runtime.freeMemory(); // Some JDKs (JRocket) return 0 for the maxMemory if (maxMemory < totalMemory) return freeMemory; else return maxMemory - totalMemory + freeMemory; } /** * Shuts the server down. */ public static void shutdown() { Resin resin = getLocal(); if (resin != null) { resin.destroy(); } } /** * The main start of the web server. * * <pre> * -conf resin.xml : alternate configuration file * -port port : set the server's portt * <pre> */ public static void main(String []argv) { try { EnvironmentClassLoader.initializeEnvironment(); validateEnvironment(); final Resin resin = Resin.create(); resin.parseCommandLine(argv); resin.initMain(); Server server = resin.getServer(); DestroyThread destroyThread = new DestroyThread(resin); destroyThread.start(); resin.waitForExit(); destroyThread.shutdown(); long stopTime = System.currentTimeMillis(); long endTime = stopTime + 15000L; if (server != null) endTime = stopTime + server.getShutdownWaitMax() ; while (System.currentTimeMillis() < endTime && ! resin.isClosed()) { try { Thread.interrupted(); Thread.sleep(100); } catch (Throwable e) { } } if (! resin.isClosed()) { EnvironmentStream.getOriginalSystemErr().println("Resin halting due to stalled shutdown"); Runtime.getRuntime().halt(1); } System.exit(0); } catch (Throwable e) { boolean isCompile = false; Throwable cause; for (cause = e; cause != null && cause.getCause() != null; cause = cause.getCause()) { if (cause instanceof CompileException) { isCompile = true; break; } } if (cause instanceof BindException) { System.err.println(e.getMessage()); log().severe(e.toString()); log().log(Level.FINE, e.toString(), e); System.exit(67); } else if (e instanceof CompileException) { System.err.println(e.getMessage()); log().log(Level.CONFIG, e.toString(), e); } else { e.printStackTrace(System.err); } } finally { System.exit(1); } } /** * Validates the environment. */ private static void validateEnvironment() throws ConfigException { String loggingManager = System.getProperty("java.util.logging.manager"); if (loggingManager == null || ! loggingManager.equals("com.caucho.log.LogManagerImpl")) { log().warning(L().l("The following system property must be set:\n -Djava.util.logging.manager=com.caucho.log.LogManagerImpl\nThe JDK 1.4 Logging manager must be set to Resin's log manager.")); } /* validatePackage("javax.servlet.Servlet", new String[] {"2.5", "1.5"}); validatePackage("javax.servlet.jsp.jstl.core.Config", new String[] {"1.1"}); validatePackage("javax.management.MBeanServer", new String[] { "1.2", "1.5" }); validatePackage("javax.resource.spi.ResourceAdapter", new String[] {"1.5", "1.4"}); */ } /** * Validates a package version. */ private static void validatePackage(String className, String []versions) throws ConfigException { Class cl = null; try { cl = Class.forName(className); } catch (Throwable e) { throw new ConfigException(L().l("class {0} is not loadable on startup. Resin requires {0} to be in the classpath on startup.", className), e); } Package pkg = cl.getPackage(); if (pkg == null) { log().warning(L().l("package for class {0} is missing. Resin requires class {0} in the classpath on startup.", className)); return; } else if (pkg.getSpecificationVersion() == null) { log().warning(L().l("{0} has no specification version. Resin {1} requires version {2}.", pkg, com.caucho.Version.VERSION, versions[0])); return; } for (int i = 0; i < versions.length; i++) { if (versions[i].compareTo(pkg.getSpecificationVersion()) <= 0) return; } log().warning(L().l("Specification version {0} of {1} is not compatible with Resin {2}. Resin {2} requires version {3}.", pkg.getSpecificationVersion(), pkg, com.caucho.Version.VERSION, versions[0])); } private static L10N L() { if (_L == null) _L = new L10N(Resin.class); return _L; } private static Logger log() { if (_log == null) _log = Logger.getLogger(Resin.class.getName()); return _log; } static class BoundPort { private QServerSocket _ss; private String _address; private int _port; BoundPort(QServerSocket ss, String address, int port) { if (ss == null) throw new NullPointerException(); _ss = ss; _address = address; _port = port; } public QServerSocket getServerSocket() { return _ss; } public int getPort() { return _port; } public String getAddress() { return _address; } } /** * EL variables */ public class Var { /** * Returns the resin.id */ public String getId() { return _serverId; } /** * Returns the local address * * @return IP address */ public String getAddress() { try { if (Alarm.isTest()) return "127.0.0.1"; else return InetAddress.getLocalHost().getHostAddress(); } catch (Exception e) { log().log(Level.FINE, e.toString(), e); return "localhost"; } } /** * Returns the resin config. */ public Path getConf() { if (Alarm.isTest()) return Vfs.lookup("file:/home/resin/conf/resin.xml"); else return getResinConf(); } /** * Returns the resin home. */ public Path getHome() { if (Alarm.isTest()) return Vfs.lookup("file:/home/resin"); else return Resin.this.getResinHome(); } /** * Returns the root directory. * * @return the root directory */ public Path getRoot() { if (Alarm.isTest()) return Vfs.lookup("file:/var/www"); else return Resin.this.getRootDirectory(); } public String getUserName() { return System.getProperty("user.name"); } /** * Returns the version * * @return version */ public String getVersion() { if (Alarm.isTest()) return "3.1.test"; else return com.caucho.Version.VERSION; } /** * Returns the version date * * @return version */ public String getVersionDate() { if (Alarm.isTest()) return "19980508T0251"; else return com.caucho.Version.VERSION_DATE; } /** * Returns the local hostname * * @return version */ public String getHostName() { try { if (Alarm.isTest()) return "localhost"; else return InetAddress.getLocalHost().getHostName(); } catch (Exception e) { log().log(Level.FINE, e.toString(), e); return "localhost"; } } /** * Returns the root directory. * * @return resin.home */ public Path getRootDir() { return getRoot(); } /** * Returns the root directory. * * @return resin.home */ public Path getRootDirectory() { return getRoot(); } /** * Returns true for Resin professional. */ public boolean isProfessional() { return Resin.this.isProfessional(); } /** * Returns the -server id */ public String getServerId() { return _serverId; } } /** * Java variables */ public class JavaVar { /** * Returns true for JDK 5 */ public boolean isJava5() { return true; } /** * Returns the JDK properties */ public Properties getProperties() { return System.getProperties(); } /** * Returns the user name */ public String getUserName() { return System.getProperty("user.name"); } /** * Returns the JDK version */ public String getVersion() { return System.getProperty("java.version"); } /** * Returns the JDK home */ public Path getHome() { return Vfs.lookup(System.getProperty("java.home")); } } class SecurityManagerConfig { private boolean _isEnable = true; SecurityManagerConfig() { if (_securityManager == null) _securityManager = new SecurityManager(); } public void setEnable(boolean enable) { _isEnable = enable; } public void setValue(boolean enable) { setEnable(enable); } public void setPolicyFile(Path path) throws ConfigException { if (! path.canRead()) throw new ConfigException(L().l("policy-file '{0}' must be readable.", path)); } @PostConstruct public void init() { if (_isEnable) System.setSecurityManager(_securityManager); } } static class DestroyThread extends Thread { private final Resin _resin; private boolean _isDestroy; DestroyThread(Resin resin) { _resin = resin; setName("resin-destroy"); setDaemon(true); } public void shutdown() { synchronized (this) { _isDestroy = true; notifyAll(); } } public void run() { synchronized (this) { while (! _isDestroy) { try { wait(); } catch (Exception e) { } } } EnvironmentStream.logStderr("closing server"); _resin.destroy(); } } static class DynamicServer { private final String _cluster; private final String _address; private final int _port; DynamicServer(String cluster, String address, int port) { _cluster = cluster; _address = address; _port = port; } String getCluster() { return _cluster; } String getAddress() { return _address; } int getPort() { return _port; } }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?