View | Details | Raw Unified | Return to bug 57431
Collapse All | Expand All

(-)a/java/org/apache/catalina/startup/Tomcat.java (-2 / +37 lines)
Lines 18-23 package org.apache.catalina.startup; Link Here
18
18
19
import java.io.File;
19
import java.io.File;
20
import java.io.IOException;
20
import java.io.IOException;
21
import java.lang.reflect.InvocationTargetException;
21
import java.net.MalformedURLException;
22
import java.net.MalformedURLException;
22
import java.net.URL;
23
import java.net.URL;
23
import java.security.Principal;
24
import java.security.Principal;
Lines 495-501 public class Tomcat { Link Here
495
    public Context addContext(Host host, String contextPath, String contextName,
496
    public Context addContext(Host host, String contextPath, String contextName,
496
            String dir) {
497
            String dir) {
497
        silence(host, contextPath);
498
        silence(host, contextPath);
498
        Context ctx = new StandardContext();
499
        Context ctx = createContext(host, contextPath);
499
        ctx.setName(contextName);
500
        ctx.setName(contextName);
500
        ctx.setPath(contextPath);
501
        ctx.setPath(contextPath);
501
        ctx.setDocBase(dir);
502
        ctx.setDocBase(dir);
Lines 522-528 public class Tomcat { Link Here
522
    public Context addWebapp(Host host, String url, String name, String path) {
523
    public Context addWebapp(Host host, String url, String name, String path) {
523
        silence(host, url);
524
        silence(host, url);
524
525
525
        Context ctx = new StandardContext();
526
        Context ctx = createContext(host, url);
526
        ctx.setName(name);
527
        ctx.setName(name);
527
        ctx.setPath(url);
528
        ctx.setPath(url);
528
        ctx.setDocBase(path);
529
        ctx.setDocBase(path);
Lines 688-693 public class Tomcat { Link Here
688
    }
689
    }
689
690
690
    /**
691
    /**
692
     * Create the configured {@link Context} for the given <code>host</code>.
693
     * The default constructor of the class that was configured with
694
     * {@link StandardHost#setContextClass(String)} will be used
695
     *
696
     * @param host
697
     *            host for which the {@link Context} should be created, or
698
     *            <code>null</code> if default host should be used
699
     * @param url
700
     *            path of the webapp which should get the {@link Context}
701
     * @return newly created {@link Context}
702
     */
703
    private Context createContext(Host host, String url) {
704
        String contextClass = StandardContext.class.getName();
705
        if (host == null) {
706
            host = this.getHost();
707
        }
708
        if (host instanceof StandardHost) {
709
            contextClass = ((StandardHost) host).getContextClass();
710
        }
711
        try {
712
            return (Context) Class.forName(contextClass).getConstructor()
713
                    .newInstance();
714
        } catch (InstantiationException | IllegalAccessException
715
                | IllegalArgumentException | InvocationTargetException
716
                | NoSuchMethodException | SecurityException
717
                | ClassNotFoundException e) {
718
            throw new IllegalArgumentException(
719
                    "Can't instantiate context-class " + contextClass
720
                            + " for host " + host + " and url "
721
                            + url, e);
722
        }
723
    }
724
725
    /**
691
     * Enables JNDI naming which is disabled by default. Server must implement
726
     * Enables JNDI naming which is disabled by default. Server must implement
692
     * {@link Lifecycle} in order for the {@link NamingContextListener} to be
727
     * {@link Lifecycle} in order for the {@link NamingContextListener} to be
693
     * used.
728
     * used.
(-)a/test/org/apache/catalina/startup/TestTomcat.java (-2 / +109 lines)
Lines 38-47 import static org.junit.Assert.assertEquals; Link Here
38
import static org.junit.Assert.assertNotNull;
38
import static org.junit.Assert.assertNotNull;
39
import static org.junit.Assert.assertNull;
39
import static org.junit.Assert.assertNull;
40
import static org.junit.Assert.assertTrue;
40
import static org.junit.Assert.assertTrue;
41
import static org.junit.Assert.fail;
41
42
42
import org.junit.Test;
43
import org.junit.Test;
43
44
import org.apache.catalina.Host;
44
import org.apache.catalina.core.StandardContext;
45
import org.apache.catalina.core.StandardContext;
46
import org.apache.catalina.core.StandardHost;
47
import org.apache.catalina.ha.context.ReplicatedContext;
45
import org.apache.tomcat.util.buf.ByteChunk;
48
import org.apache.tomcat.util.buf.ByteChunk;
46
import org.apache.tomcat.util.descriptor.web.ContextEnvironment;
49
import org.apache.tomcat.util.descriptor.web.ContextEnvironment;
47
import org.apache.tomcat.util.descriptor.web.ContextResourceLink;
50
import org.apache.tomcat.util.descriptor.web.ContextResourceLink;
Lines 417-420 public class TestTomcat extends TomcatBaseTest { Link Here
417
420
418
        assertEquals("WAR_CONTEXT", context.getSessionCookieName());
421
        assertEquals("WAR_CONTEXT", context.getSessionCookieName());
419
    }
422
    }
423
424
    @Test
425
    public void testGetDefaultContextPerAddWebapp() {
426
        Tomcat tomcat = getTomcatInstance();
427
428
        File appFile = new File("test/deployment/context.war");
429
        org.apache.catalina.Context context = tomcat.addWebapp(null,
430
                "/test", appFile.getAbsolutePath());
431
432
        assertEquals(StandardContext.class.getName(), context.getClass()
433
                .getName());
434
    }
435
436
    @Test
437
    public void testGetBrokenContextPerAddWepapp() {
438
        Tomcat tomcat = getTomcatInstance();
439
        Host host = tomcat.getHost();
440
        if (host instanceof StandardHost) {
441
            ((StandardHost) host).setContextClass("InvalidContextClassName");
442
        }
443
444
        try {
445
            File appFile = new File("test/deployment/context.war");
446
            tomcat.addWebapp(null, "/test", appFile.getAbsolutePath());
447
            fail();
448
        } catch (IllegalArgumentException e) {
449
            // OK
450
        }
451
    }
452
453
    @Test
454
    public void testGetCustomContextPerAddWebappWithNullHost() {
455
        Tomcat tomcat = getTomcatInstance();
456
        Host host = tomcat.getHost();
457
        if (host instanceof StandardHost) {
458
            ((StandardHost) host).setContextClass(ReplicatedContext.class
459
                    .getName());
460
        }
461
462
        File appFile = new File("test/deployment/context.war");
463
        org.apache.catalina.Context context = tomcat.addWebapp(null, "/test",
464
                appFile.getAbsolutePath());
465
466
        assertEquals(ReplicatedContext.class.getName(), context.getClass()
467
                .getName());
468
    }
469
470
    @Test
471
    public void testGetCustomContextPerAddWebappWithHost() {
472
        Tomcat tomcat = getTomcatInstance();
473
        Host host = tomcat.getHost();
474
        if (host instanceof StandardHost) {
475
            ((StandardHost) host).setContextClass(ReplicatedContext.class
476
                    .getName());
477
        }
478
479
        File appFile = new File("test/deployment/context.war");
480
        org.apache.catalina.Context context = tomcat.addWebapp(host, "/test",
481
                appFile.getAbsolutePath());
482
483
        assertEquals(ReplicatedContext.class.getName(), context.getClass()
484
                .getName());
485
    }
486
487
        @Test
488
    public void testGetDefaultContextPerAddContext() {
489
        Tomcat tomcat = getTomcatInstance();
490
491
        // No file system docBase required
492
        org.apache.catalina.Context ctx = tomcat.addContext(null, "", null);
493
        assertEquals(StandardContext.class.getName(), ctx.getClass().getName());
494
    }
495
496
    @Test
497
    public void testGetBrokenContextPerAddContext() {
498
        Tomcat tomcat = getTomcatInstance();
499
        Host host = tomcat.getHost();
500
        if (host instanceof StandardHost) {
501
            ((StandardHost) host).setContextClass("InvalidContextClassName");
502
        }
503
504
        // No file system docBase required
505
        try {
506
            tomcat.addContext(null, "", null);
507
            fail();
508
        } catch (IllegalArgumentException e) {
509
            // OK
510
        }
511
    }
512
513
    @Test
514
    public void testGetCustomContextPerAddContextWithHost() {
515
        Tomcat tomcat = getTomcatInstance();
516
        Host host = tomcat.getHost();
517
        if (host instanceof StandardHost) {
518
            ((StandardHost) host).setContextClass(ReplicatedContext.class
519
                    .getName());
520
        }
521
522
        // No file system docBase required
523
        org.apache.catalina.Context ctx = tomcat.addContext(host, "", null);
524
        assertEquals(ReplicatedContext.class.getName(), ctx.getClass()
525
                .getName());
526
    }
527
420
}
528
}
421
- 

Return to bug 57431