Search in sources :

Example 11 with StandardContext

use of org.apache.catalina.core.StandardContext in project tomcat by apache.

the class HostConfig method checkResources.

/**
     * Check resources for redeployment and reloading.
     *
     * @param app   The web application to check
     * @param skipFileModificationResolutionCheck
     *              When checking files for modification should the check that
     *              requires that any file modification must have occurred at
     *              least as long ago as the resolution of the file time stamp
     *              be skipped
     */
protected synchronized void checkResources(DeployedApplication app, boolean skipFileModificationResolutionCheck) {
    String[] resources = app.redeployResources.keySet().toArray(new String[0]);
    // Offset the current time by the resolution of File.lastModified()
    long currentTimeWithResolutionOffset = System.currentTimeMillis() - FILE_MODIFICATION_RESOLUTION_MS;
    for (int i = 0; i < resources.length; i++) {
        File resource = new File(resources[i]);
        if (log.isDebugEnabled())
            log.debug("Checking context[" + app.name + "] redeploy resource " + resource);
        long lastModified = app.redeployResources.get(resources[i]).longValue();
        if (resource.exists() || lastModified == 0) {
            // missed. See Bug 57765.
            if (resource.lastModified() != lastModified && (!host.getAutoDeploy() || resource.lastModified() < currentTimeWithResolutionOffset || skipFileModificationResolutionCheck)) {
                if (resource.isDirectory()) {
                    // No action required for modified directory
                    app.redeployResources.put(resources[i], Long.valueOf(resource.lastModified()));
                } else if (app.hasDescriptor && resource.getName().toLowerCase(Locale.ENGLISH).endsWith(".war")) {
                    // Modified WAR triggers a reload if there is an XML
                    // file present
                    // The only resource that should be deleted is the
                    // expanded WAR (if any)
                    Context context = (Context) host.findChild(app.name);
                    String docBase = context.getDocBase();
                    if (!docBase.toLowerCase(Locale.ENGLISH).endsWith(".war")) {
                        // This is an expanded directory
                        File docBaseFile = new File(docBase);
                        if (!docBaseFile.isAbsolute()) {
                            docBaseFile = new File(host.getAppBaseFile(), docBase);
                        }
                        reload(app, docBaseFile, resource.getAbsolutePath());
                    } else {
                        reload(app, null, null);
                    }
                    // Update times
                    app.redeployResources.put(resources[i], Long.valueOf(resource.lastModified()));
                    app.timestamp = System.currentTimeMillis();
                    boolean unpackWAR = unpackWARs;
                    if (unpackWAR && context instanceof StandardContext) {
                        unpackWAR = ((StandardContext) context).getUnpackWAR();
                    }
                    if (unpackWAR) {
                        addWatchedResources(app, context.getDocBase(), context);
                    } else {
                        addWatchedResources(app, null, context);
                    }
                    return;
                } else {
                    // Everything else triggers a redeploy
                    // (just need to undeploy here, deploy will follow)
                    undeploy(app);
                    deleteRedeployResources(app, resources, i, false);
                    return;
                }
            }
        } else {
            // temporarily eg renamed during a text editor save
            try {
                Thread.sleep(500);
            } catch (InterruptedException e1) {
            // Ignore
            }
            // Recheck the resource to see if it was really deleted
            if (resource.exists()) {
                continue;
            }
            // Undeploy application
            undeploy(app);
            deleteRedeployResources(app, resources, i, true);
            return;
        }
    }
    resources = app.reloadResources.keySet().toArray(new String[0]);
    boolean update = false;
    for (int i = 0; i < resources.length; i++) {
        File resource = new File(resources[i]);
        if (log.isDebugEnabled()) {
            log.debug("Checking context[" + app.name + "] reload resource " + resource);
        }
        long lastModified = app.reloadResources.get(resources[i]).longValue();
        // missed. See Bug 57765.
        if ((resource.lastModified() != lastModified && (!host.getAutoDeploy() || resource.lastModified() < currentTimeWithResolutionOffset || skipFileModificationResolutionCheck)) || update) {
            if (!update) {
                // Reload application
                reload(app, null, null);
                update = true;
            }
            // Update times. More than one file may have been updated. We
            // don't want to trigger a series of reloads.
            app.reloadResources.put(resources[i], Long.valueOf(resource.lastModified()));
        }
        app.timestamp = System.currentTimeMillis();
    }
}
Also used : Context(org.apache.catalina.Context) StandardContext(org.apache.catalina.core.StandardContext) StandardContext(org.apache.catalina.core.StandardContext) JarFile(java.util.jar.JarFile) File(java.io.File)

Example 12 with StandardContext

use of org.apache.catalina.core.StandardContext in project tomcat by apache.

the class HostConfig method manageApp.

/**
     * Add a new Context to be managed by us.
     * Entry point for the admin webapp, and other JMX Context controllers.
     * @param context The context instance
     */
public void manageApp(Context context) {
    String contextName = context.getName();
    if (deployed.containsKey(contextName))
        return;
    DeployedApplication deployedApp = new DeployedApplication(contextName, false);
    // Add the associated docBase to the redeployed list if it's a WAR
    boolean isWar = false;
    if (context.getDocBase() != null) {
        File docBase = new File(context.getDocBase());
        if (!docBase.isAbsolute()) {
            docBase = new File(host.getAppBaseFile(), context.getDocBase());
        }
        deployedApp.redeployResources.put(docBase.getAbsolutePath(), Long.valueOf(docBase.lastModified()));
        if (docBase.getAbsolutePath().toLowerCase(Locale.ENGLISH).endsWith(".war")) {
            isWar = true;
        }
    }
    host.addChild(context);
    // Add the eventual unpacked WAR and all the resources which will be
    // watched inside it
    boolean unpackWAR = unpackWARs;
    if (unpackWAR && context instanceof StandardContext) {
        unpackWAR = ((StandardContext) context).getUnpackWAR();
    }
    if (isWar && unpackWAR) {
        File docBase = new File(host.getAppBaseFile(), context.getBaseName());
        deployedApp.redeployResources.put(docBase.getAbsolutePath(), Long.valueOf(docBase.lastModified()));
        addWatchedResources(deployedApp, docBase.getAbsolutePath(), context);
    } else {
        addWatchedResources(deployedApp, null, context);
    }
    deployed.put(contextName, deployedApp);
}
Also used : StandardContext(org.apache.catalina.core.StandardContext) JarFile(java.util.jar.JarFile) File(java.io.File)

Example 13 with StandardContext

use of org.apache.catalina.core.StandardContext in project tomcat by apache.

the class HostConfig method deployDirectory.

/**
     * Deploy exploded webapp.
     * @param cn The context name
     * @param dir The path to the root folder of the weapp
     */
protected void deployDirectory(ContextName cn, File dir) {
    long startTime = 0;
    // Deploy the application in this directory
    if (log.isInfoEnabled()) {
        startTime = System.currentTimeMillis();
        log.info(sm.getString("hostConfig.deployDir", dir.getAbsolutePath()));
    }
    Context context = null;
    File xml = new File(dir, Constants.ApplicationContextXml);
    File xmlCopy = new File(host.getConfigBaseFile(), cn.getBaseName() + ".xml");
    DeployedApplication deployedApp;
    boolean copyThisXml = copyXML;
    try {
        if (deployXML && xml.exists()) {
            synchronized (digesterLock) {
                try {
                    context = (Context) digester.parse(xml);
                } catch (Exception e) {
                    log.error(sm.getString("hostConfig.deployDescriptor.error", xml), e);
                    context = new FailedContext();
                } finally {
                    digester.reset();
                    if (context == null) {
                        context = new FailedContext();
                    }
                }
            }
            if (copyThisXml == false && context instanceof StandardContext) {
                // Host is using default value. Context may override it.
                copyThisXml = ((StandardContext) context).getCopyXML();
            }
            if (copyThisXml) {
                Files.copy(xml.toPath(), xmlCopy.toPath());
                context.setConfigFile(xmlCopy.toURI().toURL());
            } else {
                context.setConfigFile(xml.toURI().toURL());
            }
        } else if (!deployXML && xml.exists()) {
            // Block deployment as META-INF/context.xml may contain security
            // configuration necessary for a secure deployment.
            log.error(sm.getString("hostConfig.deployDescriptor.blocked", cn.getPath(), xml, xmlCopy));
            context = new FailedContext();
        } else {
            context = (Context) Class.forName(contextClass).newInstance();
        }
        Class<?> clazz = Class.forName(host.getConfigClass());
        LifecycleListener listener = (LifecycleListener) clazz.newInstance();
        context.addLifecycleListener(listener);
        context.setName(cn.getName());
        context.setPath(cn.getPath());
        context.setWebappVersion(cn.getVersion());
        context.setDocBase(cn.getBaseName());
        host.addChild(context);
    } catch (Throwable t) {
        ExceptionUtils.handleThrowable(t);
        log.error(sm.getString("hostConfig.deployDir.error", dir.getAbsolutePath()), t);
    } finally {
        deployedApp = new DeployedApplication(cn.getName(), xml.exists() && deployXML && copyThisXml);
        // Fake re-deploy resource to detect if a WAR is added at a later
        // point
        deployedApp.redeployResources.put(dir.getAbsolutePath() + ".war", Long.valueOf(0));
        deployedApp.redeployResources.put(dir.getAbsolutePath(), Long.valueOf(dir.lastModified()));
        if (deployXML && xml.exists()) {
            if (copyThisXml) {
                deployedApp.redeployResources.put(xmlCopy.getAbsolutePath(), Long.valueOf(xmlCopy.lastModified()));
            } else {
                deployedApp.redeployResources.put(xml.getAbsolutePath(), Long.valueOf(xml.lastModified()));
                // Fake re-deploy resource to detect if a context.xml file is
                // added at a later point
                deployedApp.redeployResources.put(xmlCopy.getAbsolutePath(), Long.valueOf(0));
            }
        } else {
            // Fake re-deploy resource to detect if a context.xml file is
            // added at a later point
            deployedApp.redeployResources.put(xmlCopy.getAbsolutePath(), Long.valueOf(0));
            if (!xml.exists()) {
                deployedApp.redeployResources.put(xml.getAbsolutePath(), Long.valueOf(0));
            }
        }
        addWatchedResources(deployedApp, dir.getAbsolutePath(), context);
        // Add the global redeploy resources (which are never deleted) at
        // the end so they don't interfere with the deletion process
        addGlobalRedeployResources(deployedApp);
    }
    deployed.put(cn.getName(), deployedApp);
    if (log.isInfoEnabled()) {
        log.info(sm.getString("hostConfig.deployDir.finished", dir.getAbsolutePath(), Long.valueOf(System.currentTimeMillis() - startTime)));
    }
}
Also used : Context(org.apache.catalina.Context) StandardContext(org.apache.catalina.core.StandardContext) StandardContext(org.apache.catalina.core.StandardContext) LifecycleListener(org.apache.catalina.LifecycleListener) JarFile(java.util.jar.JarFile) File(java.io.File) IOException(java.io.IOException)

Example 14 with StandardContext

use of org.apache.catalina.core.StandardContext in project tomcat by apache.

the class TestMapper method createContext.

private Context createContext(String name) {
    Context context = new StandardContext();
    context.setName(name);
    return context;
}
Also used : Context(org.apache.catalina.Context) StandardContext(org.apache.catalina.core.StandardContext) StandardContext(org.apache.catalina.core.StandardContext)

Example 15 with StandardContext

use of org.apache.catalina.core.StandardContext in project tomcat by apache.

the class TestVirtualWebappLoader method testStartInternal.

@Test
public void testStartInternal() throws Exception {
    Tomcat tomcat = getTomcatInstance();
    File appDir = new File("test/webapp");
    StandardContext ctx = (StandardContext) tomcat.addContext("", appDir.getAbsolutePath());
    WebappLoader loader = new WebappLoader();
    loader.setContext(ctx);
    ctx.setLoader(loader);
    ctx.setResources(new StandardRoot(ctx));
    ctx.resourcesStart();
    File f1 = new File("test/webapp-fragments/WEB-INF/lib");
    ctx.getResources().createWebResourceSet(WebResourceRoot.ResourceSetType.POST, "/WEB-INF/lib", f1.getAbsolutePath(), null, "/");
    loader.start();
    String[] repos = loader.getLoaderRepositories();
    assertEquals(4, repos.length);
    loader.stop();
    repos = loader.getLoaderRepositories();
    assertEquals(0, repos.length);
    // no leak
    loader.start();
    repos = loader.getLoaderRepositories();
    assertEquals(4, repos.length);
    // clear loader
    ctx.setLoader(null);
    // see tearDown()!
    tomcat.start();
}
Also used : Tomcat(org.apache.catalina.startup.Tomcat) StandardContext(org.apache.catalina.core.StandardContext) StandardRoot(org.apache.catalina.webresources.StandardRoot) File(java.io.File) TomcatBaseTest(org.apache.catalina.startup.TomcatBaseTest) Test(org.junit.Test)

Aggregations

StandardContext (org.apache.catalina.core.StandardContext)91 File (java.io.File)36 Tomcat (org.apache.catalina.startup.Tomcat)24 Context (org.apache.catalina.Context)19 Test (org.junit.Test)19 StandardHost (org.apache.catalina.core.StandardHost)16 TomcatBaseTest (org.apache.catalina.startup.TomcatBaseTest)12 Container (org.apache.catalina.Container)11 Host (org.apache.catalina.Host)11 JarFile (java.util.jar.JarFile)10 IOException (java.io.IOException)9 URL (java.net.URL)9 ByteChunk (org.apache.tomcat.util.buf.ByteChunk)9 HashMap (java.util.HashMap)8 StandardRoot (org.apache.catalina.webresources.StandardRoot)8 ArrayList (java.util.ArrayList)7 List (java.util.List)6 SecurityConstraint (org.apache.tomcat.util.descriptor.web.SecurityConstraint)6 MalformedURLException (java.net.MalformedURLException)5 ServletContext (javax.servlet.ServletContext)5