Xml Code
- <GlobalNamingResources>
- <ResourceName = "UserDatabase" auth = "Container"
- Type = "org. apache. catalina. UserDatabase"
- Description = "User database that can be updated and saved"
- Factory = "org. apache. catalina. users. MemoryUserDatabaseFactory"
- Path Name = "conf/tomcat-users.xml"/>
- <ResourceName = "jdbc/wroxTC6" auth = "Container"
- Type = "javax. SQL. DataSource" maxActive = "20" maxIdle = "30"
- MaxWait = "10000" username = "root" password = "1234"
- DriverClassName = "com. mysql. jdbc. Driver"
- Url = "jdbc: mysql: // localhost: 3306/test? AutoReconnect = true"/>
- </GlobalNamingResources>
<GlobalNamingResources> <Resource name="UserDatabase" auth="Container" type="org.apache.catalina.UserDatabase" description="User database that can be updated and saved" factory="org.apache.catalina.users.MemoryUserDatabaseFactory" pathname="conf/tomcat-users.xml" /> <Resource name="jdbc/wroxTC6" auth="Container"type="javax.sql.DataSource" maxActive="20" maxIdle="30"maxWait="10000" username="root" password="1234"driverClassName="com.mysql.jdbc.Driver"url="jdbc:mysql://localhost:3306/test?autoReconnect=true" /> </GlobalNamingResources>
In this configuration, two resources are specified: UserDatabase and jdbc/wroxTC6. This article mainly describes how tomcat finds these resources through JNDI.
1. JNDI basics and JNDI in Tomcat
The full name of JNDI is Java Naming and Directory Interface. The overall architecture of JNDI includes the jndi api and jndi spi. The former allows access to various Naming Services and Directory Services.
Service Providers uses the jndi spi to write these services. Therefore, these services can be flexibly pluggable by Java applications.
In JNDI, a service object is identified by name. The association between name and object is called a binding, and the set of binding is called context.
Javax. naming. Context is the Context interface of Naming Services; javax. naming. directory. DirContext is the Context interface of Directory Services.
In tomcat, The NamingContext class implements the interface Context. Multiple bindings exist in this class, and each binding is a NamingEntry instance. NamingEntry can be divided into four types: ENTRY, LINK_REF, REFERENCE, and CONTEXT.
In the configuration file service. xml in tomcat, you can configure a global Naming Resource for the tomcat server, or configure a local Naming Resource for a web application.
2. Search for bound Resources
The responsibility chain mode is used when tomcat is started. When StandardServer is started, the related listener is called. In the listener NamingContextListener, set Context for global Naming Resources, bind Resources, and search for Resources. When StandardContext is started, the listener NamingContextListener is responsible for completing local Naming Resources operations. This article describes how to process global Naming Resources.
(1) first, create an instance of the NamingContext class: Root Context (name :"/").
(2) retrieve the resource object from NamingResources, form a Reference, and bind it to the root Context.
When loading the configuration file, the Digester in tomcat has already formed an instance of NamingResources class, that is, the configuration file information has formed the corresponding NamingResources object. Retrieve resources from the NamingResources instance and create a ResourceRef instance.
ResourceRef inherits java. naming. Reference. This class is not a specific Resource and is not added to the Context as an object in the binding. However, with this object, you can find the Service to be bound. For example, in the ResourceRef instance formed by UserDatabase, The className of the specified Reference is org. apache. catalina. userDatabase; the factory attribute of the configuration file corresponds to a StringRefAddr object and is added to the Reference object.
This Reference object is a NamingEntry object of the REFERENCE type, and is added to the root Context with the name attribute of the resource as the name. For example, add the NamingEntry object of REFERENCE type corresponding to the Resource to the root Context with UserDatabase as the name.
(3) Search for resources from the root Context and use Reference to find resources.
The NamingContext. lookup method is called when the resource name is used to find the resource. In this method, the JNDI method NamingManager. getObjectInstance is used to call the Service provided by the Service Provider.
In method NamingManager. in getObjectInstance, if the passed Reference is not empty, call getFactoryClassName of the Reference to obtain the specific factory class (the factory class is implemented by the Service Provider, so the specific resources can be found. In Tomcat, ResourceRef overwrites the getFactoryClassName method and obtains the ResourceFactory instance)
Call ResourceFactory. getObjectInstance to obtain the org. apache. catalina. users. MemoryUserDatabaseFactory instance from RefAddr of the Reference, and obtain the specific resource MemoryUserDatabase object through MemoryUserDatabaseFactory. getObjectInstance.
(4) Assign the real object to the object attribute of NamingEntry, and change the NamingEntry type to ENTRY.
At this time, there is a binding to the actual resource in the root Context, and the name is the name of the resource.
3. Create a sub-Context
If the resource name contains "/", you must create a sub-Context before adding the NamingEntry object of the REFERENCE type to the Context. For example, to create a resource "jdbc/wroxTC6", you must create a sub-Context.
(1) create a sub-Context
Create a sub-Context and add the Sub-Context to the root CONTEXT with the NamingEntry of the Context type.
(2) retrieve the resource object from NamingResources, form a Reference, and bind it to the sub-Context.
When the bind method of the root Context object is called, bind the resource to the sub-Context and bind the Reference to the sub-Context. For example, bind the resource "wroxTC6" to a sub-Context named "jdbc.
(3) Search for resources and bind the actual resources to the sub-Context.
When searching resources and finding real resources, the principle is the same as 2, except that the sub-Context is first found and then performed.