Java Code Examples for org.apache.catalina.startup.Tomcat#setHostname()

The following examples show how to use org.apache.catalina.startup.Tomcat#setHostname() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Application.java    From spring-reactive-sample with GNU General Public License v3.0 6 votes vote down vote up
@Bean
@Profile("default")
public Tomcat embeddedTomcatServer(ApplicationContext context) {
    HttpHandler handler = WebHttpHandlerBuilder.applicationContext(context).build();

    Servlet servlet = new TomcatHttpHandlerAdapter(handler);
    Tomcat tomcat = new Tomcat();

    File base = new File(System.getProperty("java.io.tmpdir"));
    Context rootContext = tomcat.addContext("", base.getAbsolutePath());
    Tomcat.addServlet(rootContext, "main", servlet).setAsyncSupported(true);
    rootContext.addServletMappingDecoded("/", "main");

    tomcat.setHostname("localhost");
    tomcat.setPort(this.port);
    tomcat.setBaseDir(System.getProperty("java.io.tmpdir"));

    return tomcat;
}
 
Example 2
Source File: AuthenticatorTestCase.java    From hadoop with Apache License 2.0 6 votes vote down vote up
protected void startTomcat() throws Exception {
  tomcat = new Tomcat();
  File base = new File(System.getProperty("java.io.tmpdir"));
  org.apache.catalina.Context ctx =
    tomcat.addContext("/foo",base.getAbsolutePath());
  FilterDef fd = new FilterDef();
  fd.setFilterClass(TestFilter.class.getName());
  fd.setFilterName("TestFilter");
  FilterMap fm = new FilterMap();
  fm.setFilterName("TestFilter");
  fm.addURLPattern("/*");
  fm.addServletName("/bar");
  ctx.addFilterDef(fd);
  ctx.addFilterMap(fm);
  tomcat.addServlet(ctx, "/bar", TestServlet.class.getName());
  ctx.addServletMapping("/bar", "/bar");
  host = "localhost";
  port = getLocalPort();
  tomcat.setHostname(host);
  tomcat.setPort(port);
  tomcat.start();
}
 
Example 3
Source File: AuthenticatorTestCase.java    From big-c with Apache License 2.0 6 votes vote down vote up
protected void startTomcat() throws Exception {
  tomcat = new Tomcat();
  File base = new File(System.getProperty("java.io.tmpdir"));
  org.apache.catalina.Context ctx =
    tomcat.addContext("/foo",base.getAbsolutePath());
  FilterDef fd = new FilterDef();
  fd.setFilterClass(TestFilter.class.getName());
  fd.setFilterName("TestFilter");
  FilterMap fm = new FilterMap();
  fm.setFilterName("TestFilter");
  fm.addURLPattern("/*");
  fm.addServletName("/bar");
  ctx.addFilterDef(fd);
  ctx.addFilterMap(fm);
  tomcat.addServlet(ctx, "/bar", TestServlet.class.getName());
  ctx.addServletMapping("/bar", "/bar");
  host = "localhost";
  port = getLocalPort();
  tomcat.setHostname(host);
  tomcat.setPort(port);
  tomcat.start();
}
 
Example 4
Source File: AuthenticatorTestCase.java    From registry with Apache License 2.0 6 votes vote down vote up
protected void startTomcat() throws Exception {
    tomcat = new Tomcat();
    File base = new File(System.getProperty("java.io.tmpdir"));
    org.apache.catalina.Context ctx =
            tomcat.addContext("/foo", base.getAbsolutePath());
    FilterDef fd = new FilterDef();
    fd.setFilterClass(TestFilter.class.getName());
    fd.setFilterName("TestFilter");
    FilterMap fm = new FilterMap();
    fm.setFilterName("TestFilter");
    fm.addURLPattern("/*");
    fm.addServletName("/bar");
    ctx.addFilterDef(fd);
    ctx.addFilterMap(fm);
    tomcat.addServlet(ctx, "/bar", TestServlet.class.getName());
    ctx.addServletMapping("/bar", "/bar");
    host = "localhost";
    port = getLocalPort();
    tomcat.setHostname(host);
    tomcat.setPort(port);
    tomcat.start();
}
 
Example 5
Source File: TomcatServer.java    From Lealone-Plugins with Apache License 2.0 6 votes vote down vote up
@Override
public void init(Map<String, String> config) {
    if (!config.containsKey("port"))
        config.put("port", String.valueOf(DEFAULT_HTTP_PORT));
    super.init(config);

    contextPath = config.get("context_path");
    docBase = config.get("doc_base");

    tomcat = new Tomcat();
    tomcat.setBaseDir(baseDir);
    tomcat.setHostname(host);
    tomcat.setPort(port);
    try {
        tomcat.init();
    } catch (LifecycleException e) {
        logger.error("Failed to init tomcat", e);
    }
    ctx = tomcat.addContext(contextPath, docBase);
}
 
Example 6
Source File: FunctionalWebApplication.java    From tutorials with MIT License 6 votes vote down vote up
WebServer start() throws Exception {
    WebHandler webHandler = (WebHandler) toHttpHandler(routingFunction());
    HttpHandler httpHandler = WebHttpHandlerBuilder.webHandler(webHandler)
        .filter(new IndexRewriteFilter())
        .build();

    Tomcat tomcat = new Tomcat();
    tomcat.setHostname("localhost");
    tomcat.setPort(9090);
    Context rootContext = tomcat.addContext("", System.getProperty("java.io.tmpdir"));
    ServletHttpHandlerAdapter servlet = new ServletHttpHandlerAdapter(httpHandler);
    Wrapper servletWrapper = Tomcat.addServlet(rootContext, "httpHandlerServlet", servlet);
    servletWrapper.setAsyncSupported(true);
    rootContext.addServletMappingDecoded("/", "httpHandlerServlet");

    TomcatWebServer server = new TomcatWebServer(tomcat);
    server.start();
    return server;

}
 
Example 7
Source File: FunctionalWebApplication.java    From tutorials with MIT License 6 votes vote down vote up
WebServer start() throws Exception {
    WebHandler webHandler = (WebHandler) toHttpHandler(routingFunction());
    HttpHandler httpHandler = WebHttpHandlerBuilder.webHandler(webHandler)
        .filter(new IndexRewriteFilter())
        .build();

    Tomcat tomcat = new Tomcat();
    tomcat.setHostname("localhost");
    tomcat.setPort(9090);
    Context rootContext = tomcat.addContext("", System.getProperty("java.io.tmpdir"));
    ServletHttpHandlerAdapter servlet = new ServletHttpHandlerAdapter(httpHandler);
    Tomcat.addServlet(rootContext, "httpHandlerServlet", servlet);
    rootContext.addServletMappingDecoded("/", "httpHandlerServlet");

    TomcatWebServer server = new TomcatWebServer(tomcat);
    server.start();
    return server;

}
 
Example 8
Source File: ExploreSpring5URLPatternUsingRouterFunctions.java    From tutorials with MIT License 6 votes vote down vote up
WebServer start() throws Exception {
    WebHandler webHandler = (WebHandler) toHttpHandler(routingFunction());
    HttpHandler httpHandler = WebHttpHandlerBuilder.webHandler(webHandler)
        .filter(new IndexRewriteFilter())
        .build();

    Tomcat tomcat = new Tomcat();
    tomcat.setHostname("localhost");
    tomcat.setPort(9090);
    Context rootContext = tomcat.addContext("", System.getProperty("java.io.tmpdir"));
    ServletHttpHandlerAdapter servlet = new ServletHttpHandlerAdapter(httpHandler);
    Wrapper servletWrapper = Tomcat.addServlet(rootContext, "httpHandlerServlet", servlet);
    servletWrapper.setAsyncSupported(true);
    rootContext.addServletMappingDecoded("/", "httpHandlerServlet");

    TomcatWebServer server = new TomcatWebServer(tomcat);
    server.start();
    return server;

}
 
Example 9
Source File: Main.java    From executable-embeded-tomcat-sample with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws ServletException, LifecycleException, IOException {

		String hostName = "localhost";
		int port = 8080;
		String contextPath = "";

		String tomcatBaseDir = TomcatUtil.createTempDir("tomcat", port).getAbsolutePath();
		String contextDocBase = TomcatUtil.createTempDir("tomcat-docBase", port).getAbsolutePath();

		Tomcat tomcat = new Tomcat();
		tomcat.setBaseDir(tomcatBaseDir);

		tomcat.setPort(port);
		tomcat.setHostname(hostName);

		Host host = tomcat.getHost();
		Context context = tomcat.addWebapp(host, contextPath, contextDocBase, new EmbededContextConfig());

		context.setJarScanner(new EmbededStandardJarScanner());

		ClassLoader classLoader = Main.class.getClassLoader();
		context.setParentClassLoader(classLoader);

		// context load WEB-INF/web.xml from classpath
		context.addLifecycleListener(new WebXmlMountListener());

		tomcat.start();
		tomcat.getServer().await();
	}
 
Example 10
Source File: ProgrammaticTomcat.java    From tutorials with MIT License 5 votes vote down vote up
public void startTomcat() throws LifecycleException {
    tomcat = new Tomcat();
    tomcat.setPort(randomPort);
    tomcat.setHostname("localhost");
    String appBase = ".";
    tomcat.getHost().setAppBase(appBase);

    File docBase = new File(System.getProperty("java.io.tmpdir"));
    Context context = tomcat.addContext("", docBase.getAbsolutePath());

    // add a servlet
    Class servletClass = MyServlet.class;
    Tomcat.addServlet(context, servletClass.getSimpleName(), servletClass.getName());
    context.addServletMappingDecoded("/my-servlet/*", servletClass.getSimpleName());

    // add a filter and filterMapping
    Class filterClass = MyFilter.class;
    FilterDef myFilterDef = new FilterDef();
    myFilterDef.setFilterClass(filterClass.getName());
    myFilterDef.setFilterName(filterClass.getSimpleName());
    context.addFilterDef(myFilterDef);

    FilterMap myFilterMap = new FilterMap();
    myFilterMap.setFilterName(filterClass.getSimpleName());
    myFilterMap.addURLPattern("/my-servlet/*");
    context.addFilterMap(myFilterMap);

    tomcat.start();
    // uncomment for live test
    // tomcat
    // .getServer()
    // .await();
}
 
Example 11
Source File: JCacheFilterTest.java    From commons-jcs with Apache License 2.0 5 votes vote down vote up
@Test
public void testFilterNoOutput() throws Exception
{
    Empty.COUNTER.set(0);
    final Tomcat tomcat = new Tomcat();
    tomcat.setHostname("localhost");
    tomcat.setPort(0);
    try {
        tomcat.getEngine();
        tomcat.start();
        final Context ctx = tomcat.addWebapp("/sample", docBase.getAbsolutePath());
        Tomcat.addServlet(ctx, "empty", Empty.class.getName());
        ctx.addServletMapping("/", "empty");
        addJcsFilter(ctx);
        StandardContext.class.cast(ctx).filterStart();

        final URL url = new URL("http://localhost:" + tomcat.getConnector().getLocalPort() + "/sample/");

        assertEquals("", IOUtils.toString(url.openStream()));
        assertEquals(1, Empty.COUNTER.get());

        assertEquals("", IOUtils.toString(url.openStream()));
        assertEquals(1, Empty.COUNTER.get());
    } finally {
        stop(tomcat);
    }
}
 
Example 12
Source File: HugeGraphStudio.java    From hugegraph-studio with Apache License 2.0 4 votes vote down vote up
/**
 * Run tomcat with configuration
 *
 * @param config the studio configuration
 * @throws Exception the exception
 */
public static void run(StudioServerConfig config) throws Exception {

    String address = config.getHttpBindAddress();
    int port = config.getHttpPort();
    validateHttpPort(address, port);

    String baseDir = config.getServerBasePath();
    String uiDir = String.format("%s/%s", baseDir,
                                 config.getServerUIDirectory());
    String apiWarFile = String.format("%s/%s", baseDir,
                                      config.getServerWarDirectory());
    validatePathExists(new File(uiDir));
    validateFileExists(new File(apiWarFile));

    Tomcat tomcat = new Tomcat();
    tomcat.setPort(config.getHttpPort());

    ProtocolHandler ph = tomcat.getConnector().getProtocolHandler();
    if (ph instanceof AbstractProtocol) {
        ((AbstractProtocol) ph).setAddress(InetAddress.getByName(address));
    }
    tomcat.setHostname(address);

    StandardContext ui = configureUi(tomcat, uiDir);
    StandardContext api = configureWarFile(tomcat, apiWarFile, "/api");

    tomcat.start();

    server = tomcat.getServer();
    while (!server.getState().equals(LifecycleState.STARTED)) {
        Thread.sleep(100L);
    }

    if (!ui.getState().equals(LifecycleState.STARTED)) {
        LOG.error("Studio-ui failed to start. " +
                  "Please check logs for details");
        System.exit(1);
    }
    if (!api.getState().equals(LifecycleState.STARTED)) {
        LOG.error("Studio-api failed to start. " +
                  "Please check logs for details");
        System.exit(1);
    }

    String upMessage = String.format("HugeGraphStudio is now running on: " +
                                     "http://%s:%d\n", address, port);
    LOG.info(upMessage);
}