Custom application project jar package loading path for tomcat source research

When tomcat starts the application project, it will load the jar package in the application project, and in general, these jar packages are placed in the WEB-INF/lib path by default, so can we specify an additional path to load the application project. What about the jar package of the

We need to configure in Context.xml
<Context docBase="\webapps\mydocbase">
<Loader className="org.apache.catalina.loader.VirtualWebappLoader"
virtualClasspath="/dir/classes;/somedir/somejar.jar;
/somedir/*.jar"/>
</Context>

 And how Context.xml is stored in META-INF/context.xml of the application project is actually to configure a Loader node in the Context node

The className in the loader node is the class that parses and maps the Loader, and the virtualclasspath attribute is the path where the jar package is stored

The jar package loaded here will be the same as the jar under the default path WEB-INF/lib

see below

public class VirtualWebappLoader extends WebappLoader {

    private static final org.apache.juli.logging.Log log=
        org.apache.juli.logging.LogFactory.getLog( VirtualWebappLoader.class );

    /**
     * <code>;</code> separated list of additional path elements.
     */
    private String virtualClasspath = "";

    /**
     * Construct a new WebappLoader with no defined parent class loader (so that
     * the actual parent will be the system class loader).
     */
    public VirtualWebappLoader() {
        super();
    }

    /**
     * Construct a new WebappLoader with the specified class loader to be
     * defined as the parent of the ClassLoader we ultimately create.
     *
     * @param parent The parent class loader
     */
    public VirtualWebappLoader(ClassLoader parent) {
        super(parent);
    }

    /**
     * <code>virtualClasspath</code> attribute that will be automatically set
     * from the <code>Context</code> <code>virtualClasspath</code> attribute
     * from the context xml file.
     * @param path <code>;</code> separated list of path elements.
     */
    public void setVirtualClasspath(String path) {
        virtualClasspath = path;
    }

    /**
     * @return Returns searchVirtualFirst.
     */
    public boolean getSearchVirtualFirst() {
        return getSearchExternalFirst();
    }

    /**
     * @param searchVirtualFirst Whether the virtual class path should be searched before the webapp
     */
    public void setSearchVirtualFirst(boolean searchVirtualFirst) {
        setSearchExternalFirst(searchVirtualFirst);
    }

    /**
     * Implement the requirements
     * of {@link org.apache.catalina.util.LifecycleBase#startInternal()}.
     *
     * @exception LifecycleException if this component detects a fatal error
     *  that prevents this component from being used
     */
    @Override
    protected void startInternal() throws LifecycleException {

        // just add any jar/directory set in virtual classpath to the
        // repositories list before calling start on the standard WebappLoader
        StringTokenizer tkn = new StringTokenizer(virtualClasspath, ";");
        Set<String> set = new LinkedHashSet<String>();
        while (tkn.hasMoreTokens()) {
            String token = tkn.nextToken().trim();

            if (token.isEmpty()) {
                continue;
            }

            if (log.isDebugEnabled())
                log.debug(sm.getString("virtualWebappLoader.token", token));

            if (token.endsWith("*.jar")) {
                // glob
                token = token.substring(0, token.length() - "*.jar".length());

                File directory = new File(token);
                if (!directory.isDirectory()) {
                    if (log.isDebugEnabled()) {
                        log.debug(sm.getString(
                                "virtualWebappLoader.token.notDirectory",
                                directory.getAbsolutePath()));
                    }
                    continue;
                }
                if (log.isDebugEnabled()) {
                    log.debug(sm.getString(
                            "virtualWebappLoader.token.glob.dir",
                            directory.getAbsolutePath()));
                }
                String filenames[] = directory.list();
                Arrays.sort(filenames);
                for (int j = 0; j < filenames.length; j++) {
                    String filename = filenames[j].toLowerCase(Locale.ENGLISH);
                    if (!filename.endsWith(".jar"))
                        continue;
                    File file = new File(directory, filenames[j]);
                    if (!file.isFile()) {
                        if (log.isDebugEnabled()) {
                            log.debug(sm.getString(
                                    "virtualWebappLoader.token.notFile",
                                    file.getAbsolutePath()));
                        }
                        continue;
                    }
                    if (log.isDebugEnabled()) {
                        log.debug(sm.getString(
                                "virtualWebappLoader.token.file",
                                file.getAbsolutePath()));
                    }
                    set.add(file.toURI().toString());
                }
            } else {
                // single file or directory
                File file = new File(token);
                if (!file.exists()) {
                    if (log.isDebugEnabled()) {
                        log.debug(sm.getString(
                                "virtualWebappLoader.token.notExists",
                                file.getAbsolutePath()));
                    }
                    continue;
                }
                if (log.isDebugEnabled()) {
                    log.debug(sm.getString(
                            "virtualWebappLoader.token.file",
                            file.getAbsolutePath()));
                }
                set.add(file.toURI().toString());
            }
        }

        for (String repository: set) {
            addRepository(repository);
        }

        super.startInternal();
    }

}

 From this class, we can know that he actually inherits the WebappLoader class, and this WebappLoader class is actually the class that loads the jar class and other files in the entire tomcat.

And his startInternal method happens to indicate that he is there, and will help me handle the loading of additional jars during the startup process

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326435637&siteId=291194637