resin.java
来自「RESIN 3.2 最新源码」· Java 代码 · 共 1,949 行 · 第 1/3 页
JAVA
1,949 行
/* * Copyright (c) 1998-2008 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.server.resin;import com.caucho.amber.manager.PersistenceEnvironmentListener;import com.caucho.bam.*;import com.caucho.config.Config;import com.caucho.config.ConfigException;import com.caucho.config.SchemaBean;import com.caucho.config.lib.*;import com.caucho.config.program.*;import com.caucho.config.types.Bytes;import com.caucho.config.types.Period;import com.caucho.hemp.broker.HempBroker;import com.caucho.hemp.broker.HempBrokerManager;import com.caucho.jsp.cfg.JspPropertyGroup;import com.caucho.license.LicenseCheck;import com.caucho.lifecycle.Lifecycle;import com.caucho.lifecycle.LifecycleState;import com.caucho.loader.Environment;import com.caucho.loader.EnvironmentBean;import com.caucho.loader.DynamicClassLoader;import com.caucho.loader.EnvironmentClassLoader;import com.caucho.loader.EnvironmentLocal;import com.caucho.loader.EnvironmentProperties;import com.caucho.log.EnvironmentStream;import com.caucho.log.RotateStream;import com.caucho.management.server.ClusterMXBean;import com.caucho.management.server.ResinMXBean;import com.caucho.management.server.ThreadPoolMXBean;import com.caucho.naming.Jndi;import com.caucho.server.admin.TransactionManager;import com.caucho.server.admin.Management;import com.caucho.server.cluster.Cluster;import com.caucho.server.cluster.ClusterServer;import com.caucho.server.cluster.Server;import com.caucho.server.util.*;import com.caucho.server.webbeans.ResinWebBeansProducer;import com.caucho.util.Alarm;import com.caucho.util.CompileException;import com.caucho.util.L10N;import com.caucho.util.QDate;import com.caucho.util.RandomUtil;import com.caucho.vfs.Path;import com.caucho.vfs.QJniServerSocket;import com.caucho.vfs.QServerSocket;import com.caucho.vfs.Vfs;import com.caucho.vfs.WriteStream;import com.caucho.webbeans.manager.WebBeansAddLoaderListener;import com.caucho.webbeans.manager.WebBeansContainer;import javax.annotation.PostConstruct;import javax.management.ObjectName;import java.io.FileInputStream;import java.io.InputStream;import java.io.InterruptedIOException;import java.lang.reflect.*;import java.net.BindException;import java.net.InetAddress;import java.net.Socket;import java.net.SocketException;import java.net.SocketTimeoutException;import java.security.Provider;import java.security.Security;import java.util.*;import java.util.logging.Level;import java.util.logging.Logger;import javax.webbeans.Standard;/** * The Resin class represents the top-level container for Resin. * It exactly matches the <resin> tag in the resin.xml */public class Resin implements EnvironmentBean, SchemaBean{ private static Logger _log; private static L10N _L; private static final String OBJECT_NAME= "resin:type=Resin"; private static final EnvironmentLocal<Resin> _resinLocal = new EnvironmentLocal<Resin>(); private final EnvironmentLocal<String> _serverIdLocal = new EnvironmentLocal<String>("caucho.server-id"); private EnvironmentClassLoader _classLoader; private boolean _isGlobal; private String _serverId = ""; private DynamicServer _dynamicServer; private Path _resinHome; private Path _rootDirectory; private boolean _isGlobalSystemProperties; private long _minFreeMemory = 2 * 1024L * 1024L; private long _shutdownWaitMax = 60000L; private SecurityManager _securityManager; private HashMap<String,Object> _variableMap = new HashMap<String,Object>(); private ArrayList<ContainerProgram> _clusterDefaults = new ArrayList<ContainerProgram>(); private ArrayList<Cluster> _clusters = new ArrayList<Cluster>(); private Lifecycle _lifecycle; private Server _server; private long _initialStartTime; private long _startTime; private String _licenseErrorMessage; private String _configFile; private String _configServer; private Path _resinConf; private ClassLoader _systemClassLoader; private Thread _mainThread; private ArrayList<BoundPort> _boundPortList = new ArrayList<BoundPort>(); private Path _managementPath; protected Management _management; private HempBrokerManager _brokerManager; private ThreadPoolAdmin _threadPoolAdmin; private MemoryAdmin _memoryAdmin; private ObjectName _objectName; private ResinAdmin _resinAdmin; private InputStream _waitIn; private Socket _pingSocket; /** * Creates a new resin server. */ protected Resin(ClassLoader loader) { this(loader, null); } /** * Creates a new resin server. */ protected Resin(ClassLoader loader, String licenseErrorMessage) { _startTime = Alarm.getCurrentTime(); _licenseErrorMessage = licenseErrorMessage; // DynamicClassLoader.setJarCacheEnabled(true); Environment.init(); if (loader == null) loader = ClassLoader.getSystemClassLoader(); _isGlobal = (loader == ClassLoader.getSystemClassLoader()); if (loader instanceof EnvironmentClassLoader) _classLoader = (EnvironmentClassLoader) loader; else _classLoader = EnvironmentClassLoader.create(); Thread thread = Thread.currentThread(); ClassLoader oldLoader = thread.getContextClassLoader(); try { thread.setContextClassLoader(_classLoader); _resinLocal.set(this, _classLoader); _lifecycle = new Lifecycle(log(), "Resin[]"); String resinHome = System.getProperty("resin.home"); if (resinHome != null) setResinHome(Vfs.lookup(resinHome)); else setResinHome(Vfs.getPwd()); setRootDirectory(getResinHome()); // server.root backwards compat String serverRoot = System.getProperty("server.root"); if (serverRoot != null) setRootDirectory(Vfs.lookup(serverRoot)); // resin.root backwards compat serverRoot = System.getProperty("resin.root"); if (serverRoot != null) setRootDirectory(Vfs.lookup(serverRoot)); // default server id setServerId(""); // watchdog/0212 // else // setRootDirectory(Vfs.getPwd()); Environment.addChildLoaderListener(new WebBeansAddLoaderListener()); WebBeansContainer webBeans = WebBeansContainer.create(); webBeans.addSingleton(getResinHome(), "resinHome", Standard.class); webBeans.addSingleton(new Var(), "resin", Standard.class); webBeans.addSingleton(new Var(), "server", Standard.class); webBeans.addSingleton(new JavaVar(), "java", Standard.class); webBeans.addSingleton(System.getProperties(), "system", Standard.class); webBeans.addSingleton(System.getenv(), "getenv", Standard.class); _brokerManager = new HempBrokerManager(); _management = createManagement(); webBeans.addSingleton(new com.caucho.config.functions.FmtFunctions(), "fmt", Standard.class); ResinConfigLibrary.configure(webBeans); try { Method method = Jndi.class.getMethod("lookup", new Class[] { String.class }); webBeans.addSingleton(method, "jndi", Standard.class); webBeans.addSingleton(method, "jndi:lookup", Standard.class); } catch (Exception e) { throw ConfigException.create(e); } webBeans.addSingleton(new ResinWebBeansProducer()); webBeans.update(); _threadPoolAdmin = ThreadPoolAdmin.create(); _resinAdmin = new ResinAdmin(this); _threadPoolAdmin.register(); _memoryAdmin = MemoryAdmin.create(); } finally { thread.setContextClassLoader(oldLoader); } } /** * Creates a new Resin instance */ public static Resin create() { return create(Thread.currentThread().getContextClassLoader()); } /** * Creates a new Resin instance */ public static Resin create(ClassLoader loader) { String licenseErrorMessage = null; Resin resin = null; try { Class cl = Class.forName("com.caucho.server.resin.ProResin"); Constructor ctor = cl.getConstructor(new Class[] { ClassLoader.class }); resin = (Resin) ctor.newInstance(loader); } catch (ConfigException e) { log().log(Level.FINER, e.toString(), e); licenseErrorMessage = e.getMessage(); } catch (Throwable e) { log().log(Level.FINER, e.toString(), e); String msg = L().l(" Using Resin(R) Open Source under the GNU Public License (GPL).\n" + "\n" + " See http://www.caucho.com for information on Resin Professional,\n" + " including caching, clustering, JNI acceleration, and OpenSSL integration.\n"); licenseErrorMessage = msg; } if (resin == null) resin = new Resin(loader, licenseErrorMessage); _resinLocal.set(resin, loader); return resin; } /** * Returns the resin server. */ public static Resin getLocal() { return _resinLocal.get(); } /** * Returns the resin server. */ public static Resin getCurrent() { return getLocal(); } /** * Returns the classLoader */ public ClassLoader getClassLoader() { return _classLoader; } public ObjectName getObjectName() { return _objectName; } public ResinMXBean getAdmin() { return _resinAdmin; } /** * Returns the admin broker */ public BamBroker getAdminBroker() { return _management.getAdminBroker(); } public ThreadPoolMXBean getThreadPoolAdmin() { return _threadPoolAdmin; } protected String getLicenseMessage() { return null; } protected String getLicenseErrorMessage() { return _licenseErrorMessage; } /** * Sets the classLoader */ public void setEnvironmentClassLoader(EnvironmentClassLoader loader) { _classLoader = loader; } /** * Returns the relax schema. */ public String getSchema() { return "com/caucho/server/resin/resin.rnc"; } /** * Sets the server id. */ public void setServerId(String serverId) { WebBeansContainer.create().addSingletonByName(serverId, "serverId"); _serverId = serverId; _serverIdLocal.set(serverId); } /** * Returns the server id. */ public String getServerId() { return _serverId; } public static String getCurrentServerId() { Resin resin = getCurrent(); if (resin != null) return resin.getServerId(); else return ""; } /** * Sets the server id. */ public void addDynamicServer(String clusterId, String address, int port) { _dynamicServer = new DynamicServer(clusterId, address, port); String id = address + ":" + port; setServerId(id); } /** * Returns the server id. */ public String getDisplayServerId() { if ("".equals(_serverId)) return "default"; else return _serverId; } /** * Sets the config file. */ public void setConfigFile(String configFile) { _configFile = configFile; } /** * Sets resin.home */ public void setResinHome(Path home) { _resinHome = home; } /** * Returns resin.home. */ public Path getResinHome() { return _resinHome; } /** * Sets the root directory. */ public void setRootDirectory(Path root) { _rootDirectory = root; } /** * Gets the root directory. */ public Path getRootDirectory() { return _rootDirectory; } /** * Returns the admin directory */ public Path getAdminPath() { if (_management != null) return _management.getPath(); else return getRootDirectory().lookup("admin"); } /** * The configuration file used to start the server. */ public Path getResinConf() { return _resinConf; } protected String getResinName() { return "Resin"; } /** * Set true for Resin pro. */ public boolean isProfessional() { return false; } /** * Returns the cluster names. */ public ClusterMXBean []getClusters() { ClusterMXBean []clusters = new ClusterMXBean[_clusters.size()]; for (int i = 0; i < _clusters.size(); i++) clusters[i] = _clusters.get(i).getAdmin(); return clusters; } public void addClusterDefault(ContainerProgram program) { _clusterDefaults.add(program); } public Cluster createCluster() throws ConfigException { Cluster cluster = instantiateCluster(); for (int i = 0; i < _clusterDefaults.size(); i++) _clusterDefaults.get(i).configure(cluster); return cluster; } protected Cluster instantiateCluster() { return new Cluster(this); } public void addCluster(Cluster cluster) { _clusters.add(cluster); } public ArrayList<Cluster> getClusterList() { return _clusters; } /** * Set true if the server should enable environment-based * system properties. */ public void setEnvironmentSystemProperties(boolean isEnable) { EnvironmentProperties.enableEnvironmentSystemProperties(isEnable); } /** * Creates the compatibility server. */ public ServerCompatConfig createServer() { return new ServerCompatConfig(this); } /** * Configures the thread pool */ public ThreadPoolConfig createThreadPool() throws Exception { return new ThreadPoolConfig(); } /** * Sets the user name for setuid. */ public void setUserName(String userName) { } /** * Sets the group name for setuid. */ public void setGroupName(String groupName) { } /** * Sets the minimum free memory allowed. */ public void setMinFreeMemory(Bytes minFreeMemory) { _minFreeMemory = minFreeMemory.getBytes(); } /** * Gets the minimum free memory allowed. */ public long getMinFreeMemory() { return _minFreeMemory; } /** * Sets the shutdown time */ public void setShutdownWaitMax(Period shutdownWaitMax) { _shutdownWaitMax = shutdownWaitMax.getPeriod(); } /** * Gets the minimum free memory allowed. */ public long getShutdownWaitMax() { return _shutdownWaitMax; } /** * Set true if system properties are global. */ public void setGlobalSystemProperties(boolean isGlobal) { _isGlobalSystemProperties = isGlobal; } public SecurityManagerConfig createSecurityManager() { return new SecurityManagerConfig(); }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?