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

The following examples show how to use org.apache.catalina.startup.Tomcat#setConnector() . 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: TomcatOnlyApplication.java    From spring-graalvm-native with Apache License 2.0 6 votes vote down vote up
public static void main(String... args) throws Exception {
	Registry.disableRegistry();
	tomcatBase.mkdir();
	docBase.mkdir();
	serverBase.mkdir();

	Tomcat tomcat = new Tomcat();
	tomcat.setBaseDir(serverBase.getAbsolutePath());
	Connector connector = new Connector(Http11NioProtocol.class.getName());
	connector.setPort(8080);
	tomcat.setConnector(connector);

	Context context = tomcat.addContext("", docBase.getAbsolutePath());
	tomcat.addServlet(context, HelloFromTomcatServlet.class.getSimpleName(), new HelloFromTomcatServlet());
	context.addServletMappingDecoded("/*", HelloFromTomcatServlet.class.getSimpleName());
	tomcat.getHost().setAutoDeploy(false);
	tomcat.start();
}
 
Example 2
Source File: BootstrapHelper.java    From watcher with Apache License 2.0 6 votes vote down vote up
private void configTomcat(final Tomcat tomcat) throws ServletException {
	//设置tomcat工作目录,maven工程里面就放到target目录下,看起来爽点,注意,这行代码不要随便移动位置,不然你可以have a try。
	tomcat.setBaseDir("target");
	tomcat.setPort(port);
	Connector connector = new Connector("HTTP/1.1");
	connector.setPort(port);
	connector.setURIEncoding("utf-8");
	tomcat.setConnector(connector);
	tomcat.getService().addConnector(connector);
	String webappPath = getWebappsPath();
	System.out.println("webapp目录:" + webappPath);
	Context ctx = tomcat.addWebapp("/", webappPath);
	StandardJarScanner scanner = (StandardJarScanner) ctx.getJarScanner();
	if (!isServlet3Enable) {
		scanner.setScanAllDirectories(false);
		scanner.setScanClassPath(false);
	}
	tomcat.setSilent(true);
	System.setProperty("org.apache.catalina.SESSION_COOKIE_NAME", "JSESSIONID" + port);
}
 
Example 3
Source File: AbstractWebServiceTest.java    From openmeetings with Apache License 2.0 6 votes vote down vote up
@BeforeAll
public static void initialize() throws Exception {
	AbstractSpringTest.init();
	tomcat = new Tomcat();
	Connector connector = new Connector("HTTP/1.1");
	connector.setProperty("address", InetAddress.getByName(HOST).getHostAddress());
	connector.setPort(0);
	tomcat.getService().addConnector(connector);
	tomcat.setConnector(connector);
	File wd = Files.createTempDirectory("om" + randomUUID().toString()).toFile();
	tomcat.setBaseDir(wd.getCanonicalPath());
	tomcat.getHost().setAppBase(wd.getCanonicalPath());
	tomcat.getHost().setAutoDeploy(false);
	tomcat.getHost().setDeployOnStartup(false);
	tomcat.addWebapp(CONTEXT, getOmHome().getAbsolutePath());
	tomcat.getConnector(); // to init the connector
	tomcat.start();
	port = tomcat.getConnector().getLocalPort();
}
 
Example 4
Source File: ArkTomcatServletWebServerFactory.java    From sofa-ark with Apache License 2.0 5 votes vote down vote up
private Tomcat initEmbedTomcat() {
    Tomcat tomcat = new Tomcat();
    File baseDir = (this.baseDirectory != null) ? this.baseDirectory : createTempDir("tomcat");
    tomcat.setBaseDir(baseDir.getAbsolutePath());
    Connector connector = new Connector(this.protocol);
    tomcat.getService().addConnector(connector);
    customizeConnector(connector);
    tomcat.setConnector(connector);
    tomcat.getHost().setAutoDeploy(false);
    configureEngine(tomcat.getEngine());
    for (Connector additionalConnector : getAdditionalTomcatConnectors()) {
        tomcat.getService().addConnector(additionalConnector);
    }
    return tomcat;
}
 
Example 5
Source File: VirtualService.java    From api-layer with Eclipse Public License 2.0 5 votes vote down vote up
private void createTomcat(int port) {
    httpConnector = new Connector();
    httpConnector.setPort(port);
    httpConnector.setScheme("http");

    tomcat = new Tomcat();
    tomcat.setConnector(httpConnector);

    context = tomcat.addContext("", getContextPath());
    addServlet(HealthServlet.class.getSimpleName(), "/application/health", new HealthServlet());
    addServlet(InstanceServlet.class.getSimpleName(), "/application/instance", new InstanceServlet());
}
 
Example 6
Source File: TomcatServerFactory.java    From api-layer with Eclipse Public License 2.0 5 votes vote down vote up
public Tomcat startTomcat(HttpsConfig httpsConfig) throws IOException {
    Tomcat tomcat = new Tomcat();
    String contextPath = new File(".").getCanonicalPath();
    log.info("Tomcat contextPath: {}", contextPath);
    Context ctx = tomcat.addContext("", contextPath);
    tomcat.setConnector(createHttpsConnector(httpsConfig));
    Tomcat.addServlet(ctx, SERVLET_NAME, new HttpServlet() {
        private static final long serialVersionUID = 3405324813032378347L;

        @Override
        protected void service(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            response.setCharacterEncoding("UTF-8");
            response.setContentType("text/plain");
            try (Writer writer = response.getWriter()) {
                writer.write("OK");
                writer.flush();
            }
        }
    });
    ctx.addServletMappingDecoded("/*", SERVLET_NAME);
    try {
        tomcat.start();
    } catch (LifecycleException e) {
        throw new RuntimeException(e);  // NOSONAR
    }
    return tomcat;
}
 
Example 7
Source File: TomcatOnlyApplication.java    From spring-graalvm-native with Apache License 2.0 4 votes vote down vote up
public static void main(String... args) throws Exception {
	Registry.disableRegistry();

	tomcatBase.mkdir();
	docBase.mkdir();
	serverBase.mkdir();

	Tomcat tomcat = new Tomcat();
	tomcat.setBaseDir(serverBase.getAbsolutePath());
	Connector connector = new Connector(Http11NioProtocol.class.getName());
	connector.setThrowOnFailure(true);
	connector.setPort(8080);
	tomcat.getService().addConnector(connector);
	tomcat.setConnector(connector);
	tomcat.getHost().setAutoDeploy(false);

	TomcatEmbeddedContext context = new TomcatEmbeddedContext();
	context.setResources(new LoaderHidingResourceRoot(context));
	context.setName("ROOT");
	context.setDisplayName("sample-tomcat-context");
	context.setPath("");
	context.setDocBase(docBase.getAbsolutePath());
	context.addLifecycleListener(new Tomcat.FixContextListener());
	context.setParentClassLoader(Thread.currentThread().getContextClassLoader());
	context.addLocaleEncodingMappingParameter(Locale.ENGLISH.toString(), DEFAULT_CHARSET.displayName());
	context.addLocaleEncodingMappingParameter(Locale.FRENCH.toString(), DEFAULT_CHARSET.displayName());
	context.setUseRelativeRedirects(false);
	try {
		context.setCreateUploadTargets(true);
	}
	catch (NoSuchMethodError ex) {
		// Tomcat is < 8.5.39. Continue.
	}
	StandardJarScanFilter filter = new StandardJarScanFilter();
	filter.setTldSkip(collectionToDelimitedString(TldSkipPatterns.DEFAULT, ",", "", ""));
	context.getJarScanner().setJarScanFilter(filter);
	WebappLoader loader = new WebappLoader(context.getParentClassLoader());
	loader.setLoaderClass(TomcatEmbeddedWebappClassLoader.class.getName());
	loader.setDelegate(true);
	context.setLoader(loader);

	Wrapper helloServlet = context.createWrapper();
	String servletName = HelloFromTomcatServlet.class.getSimpleName();
	helloServlet.setName(servletName);
	helloServlet.setServletClass(HelloFromTomcatServlet.class.getName());
	helloServlet.setLoadOnStartup(1);
	helloServlet.setOverridable(true);
	context.addChild(helloServlet);
	context.addServletMappingDecoded("/", servletName);

	tomcat.getHost().addChild(context);
	tomcat.getHost().setAutoDeploy(false);
	TomcatWebServer server = new TomcatWebServer(tomcat);
	server.start();
}
 
Example 8
Source File: TomcatServer.java    From athena-rest with Apache License 2.0 4 votes vote down vote up
private void initTomcat() {
	serverStatus = ServerStatus.STARTING;

	tomcat = new Tomcat();
	tomcat.setPort(port);

	// Changed it to use NIO due to poor performance in burdon test
	Connector connector = new Connector(Utils.getStringProperty(properties, "web.connectorProtocol"));

	
	connector.setURIEncoding("UTF-8");
	connector.setPort(port);
	connector.setUseBodyEncodingForURI(true);
	connector.setAsyncTimeout(Utils.getIntegerValue(properties,
			WEB_ASYNC_TIMEOUT, DEFAULT_ASYNC_TIMEOUT));
	connector.setAttribute("minProcessors", Utils.getIntegerValue(
			properties, WEB_MIN_PROCESSORS, DEFAULT_MIN_PROCESSORS));
	connector.setAttribute("maxProcessors", Utils.getIntegerValue(
			properties, WEB_MAX_PROCESSORS, DEFAULT_MAX_PROCESSORS));
	connector.setAttribute("acceptCount", Utils.getIntegerValue(properties,
			WEB_ACCEPT_COUNT, DEFAULT_ACCEPT_COUNT));
	connector.setAttribute("minSpareThreads", Utils.getIntegerValue(
			properties, WEB_MIN_SPARE_THREADS, DEFAULT_MIN_SPARE_THREADS));
	connector.setAttribute("maxThreads", Utils.getIntegerValue(properties,
			WEB_MAX_THREADS, DEFAULT_MAX_THREADS));
	connector.setRedirectPort(Utils.getIntegerValue(properties,
			WEB_REDIRECT_PORT, DEFAULT_WEB_REDIRECT_PORT));
	
	if (this.minThreads != -1 && this.maxThreads != -1) {
		connector.setAttribute("minThreads", minThreads);
		connector.setAttribute("maxThreads", maxThreads);
	}

	Service tomcatService = tomcat.getService();
	tomcatService.addConnector(connector);
	tomcat.setConnector(connector);

	Context context = null;
	try {
		context = tomcat.addWebapp(contextPath,
				new File(webappPath).getAbsolutePath());
	} catch (ServletException e) {
		log.error("Failed to add webapp + " + webappPath, e);

		exit();
	}
	context.setLoader(new WebappLoader(Thread.currentThread()
			.getContextClassLoader()));

	String extraResourcePaths = properties
			.getProperty(WEB_EXTRA_RESOURCE_PATHS);
	if (!StringUtils.isBlank(extraResourcePaths)) {
		VirtualDirContext virtualDirContext = new VirtualDirContext();
		virtualDirContext.setExtraResourcePaths(extraResourcePaths);
		context.setResources(virtualDirContext);
	}

	StandardServer server = (StandardServer) tomcat.getServer();
	AprLifecycleListener listener = new AprLifecycleListener();
	server.addLifecycleListener(listener);
}
 
Example 9
Source File: ServingLayer.java    From oryx with Apache License 2.0 4 votes vote down vote up
private void configureTomcat(Tomcat tomcat, Connector connector) {
  tomcat.setBaseDir(noSuchBaseDir.toAbsolutePath().toString());
  tomcat.setConnector(connector);
}
 
Example 10
Source File: Runner.java    From myrrix-recommender with Apache License 2.0 4 votes vote down vote up
private void configureTomcat(Tomcat tomcat, Connector connector) {
  tomcat.setBaseDir(noSuchBaseDir.getAbsolutePath());
  tomcat.setConnector(connector);
  tomcat.getService().addConnector(connector);
}