Search in sources :

Example 11 with ContextName

use of org.apache.catalina.util.ContextName in project tomcat by apache.

the class ContextConfig method antiLocking.

protected void antiLocking() {
    if ((context instanceof StandardContext) && ((StandardContext) context).getAntiResourceLocking()) {
        Host host = (Host) context.getParent();
        String docBase = context.getDocBase();
        if (docBase == null) {
            return;
        }
        originalDocBase = docBase;
        File docBaseFile = new File(docBase);
        if (!docBaseFile.isAbsolute()) {
            docBaseFile = new File(host.getAppBaseFile(), docBase);
        }
        String path = context.getPath();
        if (path == null) {
            return;
        }
        ContextName cn = new ContextName(path, context.getWebappVersion());
        docBase = cn.getBaseName();
        if (originalDocBase.toLowerCase(Locale.ENGLISH).endsWith(".war")) {
            antiLockingDocBase = new File(System.getProperty("java.io.tmpdir"), deploymentCount++ + "-" + docBase + ".war");
        } else {
            antiLockingDocBase = new File(System.getProperty("java.io.tmpdir"), deploymentCount++ + "-" + docBase);
        }
        antiLockingDocBase = antiLockingDocBase.getAbsoluteFile();
        if (log.isDebugEnabled()) {
            log.debug("Anti locking context[" + context.getName() + "] setting docBase to " + antiLockingDocBase.getPath());
        }
        // Cleanup just in case an old deployment is lying around
        ExpandWar.delete(antiLockingDocBase);
        if (ExpandWar.copy(docBaseFile, antiLockingDocBase)) {
            context.setDocBase(antiLockingDocBase.getPath());
        }
    }
}
Also used : StandardContext(org.apache.catalina.core.StandardContext) Host(org.apache.catalina.Host) StandardHost(org.apache.catalina.core.StandardHost) File(java.io.File) ContextName(org.apache.catalina.util.ContextName)

Example 12 with ContextName

use of org.apache.catalina.util.ContextName in project tomcat by apache.

the class HostConfig method deployApps.

/**
     * Deploy applications for any directories or WAR files that are found
     * in our "application root" directory.
     * @param name The context name which should be deployed
     */
protected void deployApps(String name) {
    File appBase = host.getAppBaseFile();
    File configBase = host.getConfigBaseFile();
    ContextName cn = new ContextName(name, false);
    String baseName = cn.getBaseName();
    if (deploymentExists(cn.getName())) {
        return;
    }
    // Deploy XML descriptor from configBase
    File xml = new File(configBase, baseName + ".xml");
    if (xml.exists()) {
        deployDescriptor(cn, xml);
        return;
    }
    // Deploy WAR
    File war = new File(appBase, baseName + ".war");
    if (war.exists()) {
        deployWAR(cn, war);
        return;
    }
    // Deploy expanded folder
    File dir = new File(appBase, baseName);
    if (dir.exists())
        deployDirectory(cn, dir);
}
Also used : JarFile(java.util.jar.JarFile) File(java.io.File) ContextName(org.apache.catalina.util.ContextName)

Example 13 with ContextName

use of org.apache.catalina.util.ContextName in project tomcat by apache.

the class HostConfig method deployWARs.

/**
     * Deploy WAR files.
     * @param appBase The base path for applications
     * @param files The WARs to deploy
     */
protected void deployWARs(File appBase, String[] files) {
    if (files == null)
        return;
    ExecutorService es = host.getStartStopExecutor();
    List<Future<?>> results = new ArrayList<>();
    for (int i = 0; i < files.length; i++) {
        if (files[i].equalsIgnoreCase("META-INF"))
            continue;
        if (files[i].equalsIgnoreCase("WEB-INF"))
            continue;
        File war = new File(appBase, files[i]);
        if (files[i].toLowerCase(Locale.ENGLISH).endsWith(".war") && war.isFile() && !invalidWars.contains(files[i])) {
            ContextName cn = new ContextName(files[i], true);
            if (isServiced(cn.getName())) {
                continue;
            }
            if (deploymentExists(cn.getName())) {
                DeployedApplication app = deployed.get(cn.getName());
                boolean unpackWAR = unpackWARs;
                if (unpackWAR && host.findChild(cn.getName()) instanceof StandardContext) {
                    unpackWAR = ((StandardContext) host.findChild(cn.getName())).getUnpackWAR();
                }
                if (!unpackWAR && app != null) {
                    // Need to check for a directory that should not be
                    // there
                    File dir = new File(appBase, cn.getBaseName());
                    if (dir.exists()) {
                        if (!app.loggedDirWarning) {
                            log.warn(sm.getString("hostConfig.deployWar.hiddenDir", dir.getAbsoluteFile(), war.getAbsoluteFile()));
                            app.loggedDirWarning = true;
                        }
                    } else {
                        app.loggedDirWarning = false;
                    }
                }
                continue;
            }
            // Check for WARs with /../ /./ or similar sequences in the name
            if (!validateContextPath(appBase, cn.getBaseName())) {
                log.error(sm.getString("hostConfig.illegalWarName", files[i]));
                invalidWars.add(files[i]);
                continue;
            }
            results.add(es.submit(new DeployWar(this, cn, war)));
        }
    }
    for (Future<?> result : results) {
        try {
            result.get();
        } catch (Exception e) {
            log.error(sm.getString("hostConfig.deployWar.threaded.error"), e);
        }
    }
}
Also used : ArrayList(java.util.ArrayList) IOException(java.io.IOException) StandardContext(org.apache.catalina.core.StandardContext) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) JarFile(java.util.jar.JarFile) File(java.io.File) ContextName(org.apache.catalina.util.ContextName)

Example 14 with ContextName

use of org.apache.catalina.util.ContextName in project tomcat by apache.

the class HostConfig method checkUndeploy.

/**
     * Check for old versions of applications using parallel deployment that are
     * now unused (have no active sessions) and undeploy any that are found.
     */
public synchronized void checkUndeploy() {
    // Need ordered set of names
    SortedSet<String> sortedAppNames = new TreeSet<>();
    sortedAppNames.addAll(deployed.keySet());
    if (sortedAppNames.size() < 2) {
        return;
    }
    Iterator<String> iter = sortedAppNames.iterator();
    ContextName previous = new ContextName(iter.next(), false);
    do {
        ContextName current = new ContextName(iter.next(), false);
        if (current.getPath().equals(previous.getPath())) {
            // Current and previous are same path - current will always
            // be a later version
            Context previousContext = (Context) host.findChild(previous.getName());
            Context currentContext = (Context) host.findChild(current.getName());
            if (previousContext != null && currentContext != null && currentContext.getState().isAvailable() && !isServiced(previous.getName())) {
                Manager manager = previousContext.getManager();
                if (manager != null) {
                    int sessionCount;
                    if (manager instanceof DistributedManager) {
                        sessionCount = ((DistributedManager) manager).getActiveSessionsFull();
                    } else {
                        sessionCount = manager.getActiveSessions();
                    }
                    if (sessionCount == 0) {
                        if (log.isInfoEnabled()) {
                            log.info(sm.getString("hostConfig.undeployVersion", previous.getName()));
                        }
                        DeployedApplication app = deployed.get(previous.getName());
                        String[] resources = app.redeployResources.keySet().toArray(new String[0]);
                        // Version is unused - undeploy it completely
                        // The -1 is a 'trick' to ensure all redeploy
                        // resources are removed
                        undeploy(app);
                        deleteRedeployResources(app, resources, -1, true);
                    }
                }
            }
        }
        previous = current;
    } while (iter.hasNext());
}
Also used : Context(org.apache.catalina.Context) StandardContext(org.apache.catalina.core.StandardContext) TreeSet(java.util.TreeSet) DistributedManager(org.apache.catalina.DistributedManager) StringManager(org.apache.tomcat.util.res.StringManager) Manager(org.apache.catalina.Manager) ContextName(org.apache.catalina.util.ContextName) DistributedManager(org.apache.catalina.DistributedManager)

Example 15 with ContextName

use of org.apache.catalina.util.ContextName in project tomcat by apache.

the class FailedContext method getMBeanKeyProperties.

@Override
public String getMBeanKeyProperties() {
    Container c = this;
    StringBuilder keyProperties = new StringBuilder();
    int containerCount = 0;
    // each container
    while (!(c instanceof Engine)) {
        if (c instanceof Context) {
            keyProperties.append(",context=");
            ContextName cn = new ContextName(c.getName(), false);
            keyProperties.append(cn.getDisplayName());
        } else if (c instanceof Host) {
            keyProperties.append(",host=");
            keyProperties.append(c.getName());
        } else if (c == null) {
            // May happen in unit testing and/or some embedding scenarios
            keyProperties.append(",container");
            keyProperties.append(containerCount++);
            keyProperties.append("=null");
            break;
        } else {
            // Should never happen...
            keyProperties.append(",container");
            keyProperties.append(containerCount++);
            keyProperties.append('=');
            keyProperties.append(c.getName());
        }
        c = c.getParent();
    }
    return keyProperties.toString();
}
Also used : Context(org.apache.catalina.Context) ServletContext(javax.servlet.ServletContext) Container(org.apache.catalina.Container) Host(org.apache.catalina.Host) SecurityConstraint(org.apache.tomcat.util.descriptor.web.SecurityConstraint) Engine(org.apache.catalina.Engine) ContextName(org.apache.catalina.util.ContextName)

Aggregations

ContextName (org.apache.catalina.util.ContextName)23 File (java.io.File)10 Context (org.apache.catalina.Context)8 IOException (java.io.IOException)7 Container (org.apache.catalina.Container)7 Host (org.apache.catalina.Host)6 ObjectName (javax.management.ObjectName)5 StandardContext (org.apache.catalina.core.StandardContext)5 StringManager (org.apache.tomcat.util.res.StringManager)5 JarFile (java.util.jar.JarFile)4 ArrayList (java.util.ArrayList)3 ExecutorService (java.util.concurrent.ExecutorService)3 Future (java.util.concurrent.Future)3 MBeanServer (javax.management.MBeanServer)3 Server (org.apache.catalina.Server)3 StandardHost (org.apache.catalina.core.StandardHost)3 PrintWriter (java.io.PrintWriter)2 URL (java.net.URL)2 UnknownHostException (java.net.UnknownHostException)2 ServletException (javax.servlet.ServletException)2