protected void prepareContext(Host host, ServletContextInitializer[] initializers) {
   File docBase = getValidDocumentRoot();
   docBase = (docBase != null ? docBase : createTempDir("tomcat-docbase"));
   TomcatEmbeddedContext context = new TomcatEmbeddedContext();
   context.setName(getContextPath());
   context.setPath(getContextPath());
   context.setDocBase(docBase.getAbsolutePath());
   context.addLifecycleListener(new FixContextListener());
   context.setParentClassLoader(
       this.resourceLoader != null
           ? this.resourceLoader.getClassLoader()
           : ClassUtils.getDefaultClassLoader());
   SkipPatternJarScanner.apply(context, this.tldSkip);
   WebappLoader loader = new WebappLoader(context.getParentClassLoader());
   loader.setLoaderClass(TomcatEmbeddedWebappClassLoader.class.getName());
   loader.setDelegate(true);
   context.setLoader(loader);
   if (isRegisterDefaultServlet()) {
     addDefaultServlet(context);
   }
   if (isRegisterJspServlet()
       && ClassUtils.isPresent(getJspServletClassName(), getClass().getClassLoader())) {
     addJspServlet(context);
     addJasperInitializer(context);
     context.addLifecycleListener(new StoreMergedWebXmlListener());
   }
   ServletContextInitializer[] initializersToUse = mergeInitializers(initializers);
   configureContext(context, initializersToUse);
   host.addChild(context);
   postProcessContext(context);
 }
예제 #2
0
 @Override
 protected void startInternal() throws LifecycleException {
   if (fail) {
     throw new RuntimeException("Start fail (test)");
   }
   super.startInternal();
 }
예제 #3
0
  public EmbeddedTomcat(String contextPath, int port, String jvmRoute)
      throws MalformedURLException {
    this.contextPath = contextPath;
    this.port = port;

    // create server
    container = new Embedded();
    container.setCatalinaHome(catalinaHome);
    // Not really necessasry, but let's still do it...
    container.setRealm(new MemoryRealm());

    // create webapp loader
    WebappLoader loader = new WebappLoader(this.getClass().getClassLoader());
    if (classesDir != null) {
      loader.addRepository(new File(classesDir).toURI().toURL().toString());
    }

    rootContext = container.createContext("", webappDir);
    rootContext.setLoader(loader);
    rootContext.setReloadable(true);
    // Otherwise we get NPE when instantiating servlets
    rootContext.setIgnoreAnnotations(true);

    // create host
    Host localHost = container.createHost("127.0.0.1", new File("").getAbsolutePath());
    localHost.addChild(rootContext);

    localHost.setDeployOnStartup(true);

    // create engine
    engine = container.createEngine();
    engine.setName("localEngine");
    engine.addChild(localHost);
    engine.setDefaultHost(localHost.getName());
    engine.setJvmRoute(jvmRoute);
    engine.setService(new StandardService());
    container.addEngine(engine);

    // create http connector
    Connector httpConnector = container.createConnector((InetAddress) null, port, false);
    container.addConnector(httpConnector);
    container.setAwait(true);

    // Create the JVMRoute valve for session failover
    ValveBase valve = new JvmRouteBinderValve();
    ((StandardEngine) engine).addValve(valve);
  }
예제 #4
0
 protected void prepareContext(Host host, ServletContextInitializer[] initializers) {
   File docBase = getValidDocumentRoot();
   docBase = (docBase != null ? docBase : createTempDir("tomcat-docbase"));
   TomcatEmbeddedContext context = new TomcatEmbeddedContext();
   context.setName(getContextPath());
   context.setDisplayName(getDisplayName());
   context.setPath(getContextPath());
   context.setDocBase(docBase.getAbsolutePath());
   context.addLifecycleListener(new FixContextListener());
   context.setParentClassLoader(
       this.resourceLoader != null
           ? this.resourceLoader.getClassLoader()
           : ClassUtils.getDefaultClassLoader());
   try {
     context.setUseRelativeRedirects(false);
     context.setMapperContextRootRedirectEnabled(true);
   } catch (NoSuchMethodError ex) {
     // Tomcat is < 8.0.30. Continue
   }
   SkipPatternJarScanner.apply(context, this.tldSkip);
   WebappLoader loader = new WebappLoader(context.getParentClassLoader());
   loader.setLoaderClass(TomcatEmbeddedWebappClassLoader.class.getName());
   loader.setDelegate(true);
   context.setLoader(loader);
   if (isRegisterDefaultServlet()) {
     addDefaultServlet(context);
   }
   if (shouldRegisterJspServlet()) {
     addJspServlet(context);
     addJasperInitializer(context);
     context.addLifecycleListener(new StoreMergedWebXmlListener());
   }
   ServletContextInitializer[] initializersToUse = mergeInitializers(initializers);
   configureContext(context, initializersToUse);
   host.addChild(context);
   postProcessContext(context);
 }