In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Editor to share with you how to load Tomcat9 server.xml, I believe that most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to understand it!
1.Tomcat starts org.apache.catalina.startup.Bootstrap.main (String args []) public static void main (String args []) {synchronized (daemonLock) {if (daemon = = null) {Bootstrap bootstrap = new Bootstrap (); try {bootstrap.init () / / initialize classloader} catch (Throwable t) {handleThrowable (t); t.printStackTrace (); return;} daemon = bootstrap;} else {Thread.currentThread () .setContextClassLoader (daemon.catalinaLoader) }} / / process try {String command = "start"; if (args.length > 0) {command = args [args.length-1] according to the different instructions passed in } if (command.equals ("startd")) {args [args.length-1] = "start"; daemon.load (args); daemon.start ();} else if (command.equals ("stopd")) {args [args.length-1] = "stop"; daemon.stop () } else if (command.equals ("start")) {daemon.setAwait (true); daemon.load (args); daemon.start (); if (null = = daemon.getServer ()) {System.exit (1) }} else if (command.equals ("stop")) {daemon.stopServer (args);} else if (command.equals ("configtest")) {daemon.load (args); if (null = = daemon.getServer ()) {System.exit (1) } System.exit (0);} else {log.warn ("Bootstrap: command\"+ command +"\ "does not exist.") }} catch (Throwable t) {if (t instanceof InvocationTargetException & & t.getCause ()! = null) {t = t.getCause ();} handleThrowable (t); t.printStackTrace (); System.exit (1);}}
There are two main parts of logic in the main method:
Call bootstrap.init () for initialization
According to the different instructions passed in for corresponding processing, this paper mainly analyzes the start designation, that is, service startup. Starting the service start mainly calls the org.apache.catalina.startup.Catalina.load () and start () methods
Org.apache.catalina.startup.Bootstrap.init () public void init () throws Exception {initClassLoaders (); / / initialize class loading Thread.currentThread () .setContextClassLoader (catalinaLoader); / / set the current thread's class loader to catalinaLoader SecurityClassLoad.securityClassLoad (catalinaLoader) / / enable the processing of java security management / / instantiate org.apache.catalina.startup.Catalina by reflection and set the parent class loader to sharedLoader if (log.isDebugEnabled ()) log.debug ("Loading startup class"); Class startupClass = catalinaLoader.loadClass ("org.apache.catalina.startup.Catalina"); Object startupInstance = startupClass.getConstructor (). NewInstance () If (log.isDebugEnabled ()) log.debug ("Setting startup class properties"); String methodName = "setParentClassLoader"; Class paramTypes [] = new Class [1]; paramTypes [0] = Class.forName ("java.lang.ClassLoader"); Object paramValues [] = new Object [1]; paramValues [0] = sharedLoader; Method method = startupInstance.getClass (). GetMethod (methodName, paramTypes) Method.invoke (startupInstance, paramValues); catalinaDaemon = startupInstance;}
Initialize the class loader and create commonLoader, catalinaLoader and sharedLoader. For more information, please refer to the previous article, "Tomcat9 Source Code Analysis-Class loading system".
Enable processing of java security management
Instantiate org.apache.catalina.startup.Catalina by reflection and set the parent class loader to sharedLoader
Org.apache.catalina.security.SecurityClassLoadpublic final class SecurityClassLoad {public static void securityClassLoad (ClassLoader loader) throws Exception {securityClassLoad (loader, true);} static void securityClassLoad (ClassLoader loader, boolean requireSecurityManager) throws Exception {if (requireSecurityManager & & System.getSecurityManager () = = null) {return;} loadCorePackage (loader); loadCoyotePackage (loader); loadLoaderPackage (loader); loadRealmPackage (loader); loadServletsPackage (loader) LoadSessionPackage (loader); loadUtilPackage (loader); loadJavaxPackage (loader); loadConnectorPackage (loader); loadTomcatPackage (loader);}
When using Java SecurityManager at that time, some necessary java classes would be loaded in advance to avoid triggering permission exception AccessControlException.
2.server.xml parsing framework 2.1 SAX
SAX is used in Tomcat to parse server.xml files. The SAX parsing method parses the XML document line by line, triggers the parsing processor when it encounters a tag, and parses the XML by event handling. Its advantage is that it does not need to load the complete XML document into memory, and can parse the document at the same time, saving memory. It is suitable for parsing super-large XML. The main methods are:
StartDocument (): called at the beginning of document parsing, and this method is called only once
StartElement (String uri, String localName, String qName, Attributes attributes): called when tag parsing starts
EndElement (String uri, String localName, String qName): called after tag (node) parsing
EndDocument (): called after document parsing, this method will only be called once
2.2 Rule Rules
Tomcat abstracts the parsing of server.xml into rules, uses the reference transmission of Java, and parses xml through the void method with side effects. The order of rule invocation is consistent with the order of xml parsing, that is, the start method is positive and the end method is reverse.
The rule contains the following methods:
Begin:Degister.startElement method call
Called in the body and end:Degister.endElement methods, first call body, then call end
Called in the finish:Degister.endDocument method
Common types of rules in Tomcat:
ObjectCreateRule creates an object instance corresponding to class and puts it in the stack member property of Designer
SetPropertiesRule gets the element at the top of the stack and assigns the attribute of the xml element to the object instance
SetNextRule calls the instance object of the parent node, takes the current object as a parameter, and reflects to call a method.
ListenerCreateRule when the optional attribute of the Listener tag is true, when an instance is created exception, the OptionalListener instance is forcibly added
ConnectorCreateRule creates Connector instance
The SetAllPropertiesRule body function is consistent with SetPropertiesRule, and this Rule can exclude some attribute settings.
AddPortOffsetRule Set portOffset on all the connectors based on portOffset in the Server
CertificateCreateRule instantiates SSLHostConfigCertificate
3.server.xml parses the source code parsing org.apache.catalina.startup.Catalina.load () public void load () {if (loaded) {return;} loaded = true; long T1 = System.nanoTime (); initDirs (); / / Before digester-it may be needed initNaming () / / read conf/server.xml ConfigFileLoader.setSource (new CatalinaBaseConfigurationSource (Bootstrap.getCatalinaBaseFile (), getConfigFile (); File file = configFile (); / / create xml parsing Digester Digester digester = createStartDigester (); try (ConfigurationSource.Resource resource = ConfigFileLoader.getSource (). GetServerXml ()) {InputStream inputStream = resource.getInputStream () InputSource inputSource = new InputSource (resource.getURI (). ToURL (). ToString ()); inputSource.setByteStream (inputStream); digester.push (this); digester.parse (inputSource); / / parse xml} catch (Exception e) {log.warn (sm.getString ("catalina.configFail", file.getAbsolutePath ()), e) If (file.exists () & &! file.canRead ()) {log.warn (sm.getString ("catalina.incorrectPermissions"));} return;} / / sets the property of server getServer (). SetCatalina (this); getServer (). SetCatalinaHome (Bootstrap.getCatalinaHomeFile ()); getServer (). SetCatalinaBase (Bootstrap.getCatalinaBaseFile ()) / / Stream redirection initStreams (); / / initialize server try {getServer () .init ();} catch (LifecycleException e) {if (Boolean.getBoolean ("org.apache.catalina.startup.EXIT_ON_INIT_FAILURE")) {throw new java.lang.Error (e) } else {log.error (sm.getString ("catalina.initError"), e);}} long T2 = System.nanoTime (); if (log.isInfoEnabled ()) {log.info (sm.getString ("catalina.init", Long.valueOf ((T2-T1) / 1000000));}}
Start instruction logic in Bootstrap, calling Catalina.load () through reflection
Catalina.load () reads conf/server.xml and creates a Digester that parses xml
Start initializing server
Org.apache.catalina.startup.Catalina.createStartDigester () protected Digester createStartDigester () {long t1=System.currentTimeMillis (); / / Initialize the digester Digester digester = new Digester (); digester.setValidating (false); digester.setRulesValidation (true); Map
Its structure is shown in the following figure:
Server stands for server, and there is only one Server for a Tomcat
Service represents services: a Server can provide multiple services
Connector connector: one of the core components of service services, mainly linking client requests
Container container: one of the core components of service services, which mainly executes business logic, here Engine, Host, Context by hierarchy
Wrapper: definition of the corresponding Servlet
These are all the contents of the article "how Tomcat9 loads server.xml". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.