Java Code Examples for org.apache.catalina.startup.Tomcat#setBaseDir()
The following examples show how to use
org.apache.catalina.startup.Tomcat#setBaseDir() .
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: InvokeJaxwsWebServiceInTomcat.java From glowroot with Apache License 2.0 | 6 votes |
public void executeApp(String webapp, String contextPath, String url) throws Exception { int port = getAvailablePort(); Tomcat tomcat = new Tomcat(); tomcat.setBaseDir("target/tomcat"); tomcat.setPort(port); Context context = tomcat.addWebapp(contextPath, new File("src/test/resources/" + webapp).getAbsolutePath()); WebappLoader webappLoader = new WebappLoader(InvokeJaxwsWebServiceInTomcat.class.getClassLoader()); context.setLoader(webappLoader); tomcat.start(); JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean(); factory.setServiceClass(ForBothHelloAndRootService.class); factory.setAddress("http://localhost:" + port + contextPath + url); ForBothHelloAndRootService client = (ForBothHelloAndRootService) factory.create(); client.echo("abc"); checkForRequestThreads(webappLoader); tomcat.stop(); tomcat.destroy(); }
Example 2
Source File: AbstractWebServiceTest.java From openmeetings with Apache License 2.0 | 6 votes |
@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 3
Source File: InvokeJaxrsResourceInTomcat.java From glowroot with Apache License 2.0 | 6 votes |
public void executeApp(String webapp, String contextPath, String url) throws Exception { int port = getAvailablePort(); Tomcat tomcat = new Tomcat(); tomcat.setBaseDir("target/tomcat"); tomcat.setPort(port); Context context = tomcat.addWebapp(contextPath, new File("src/test/resources/" + webapp).getAbsolutePath()); WebappLoader webappLoader = new WebappLoader(InvokeJaxrsResourceInTomcat.class.getClassLoader()); context.setLoader(webappLoader); tomcat.start(); AsyncHttpClient asyncHttpClient = new AsyncHttpClient(); int statusCode = asyncHttpClient.prepareGet("http://localhost:" + port + contextPath + url) .execute().get().getStatusCode(); asyncHttpClient.close(); if (statusCode != 200) { throw new IllegalStateException("Unexpected status code: " + statusCode); } tomcat.stop(); tomcat.destroy(); }
Example 4
Source File: DubboApplicationContextInitializerTest.java From dubbo-2.6.5 with Apache License 2.0 | 6 votes |
@Test public void testSpringContextLoaderListenerInWebXml() throws Exception { Tomcat tomcat = new Tomcat(); tomcat.setBaseDir("target/test-classes"); tomcat.setPort(12345); StandardContext context = new StandardContext(); context.setName("test"); context.setDocBase("test"); context.setPath("/test"); context.addLifecycleListener(new ContextConfig()); tomcat.getHost().addChild(context); tomcat.start(); // there should be 1 application listener Assert.assertEquals(1, context.getApplicationLifecycleListeners().length); // the first one should be Spring's built in ContextLoaderListener. Assert.assertTrue(context.getApplicationLifecycleListeners()[0] instanceof ContextLoaderListener); tomcat.stop(); tomcat.destroy(); }
Example 5
Source File: EmbeddedTomcat.java From junit-servers with MIT License | 6 votes |
private Tomcat initServer() { log.debug("Initializing tomcat instance using configuration: {}", configuration); Tomcat tomcat = new Tomcat(); tomcat.setBaseDir(configuration.getBaseDir()); tomcat.setPort(configuration.getPort()); tomcat.getHost().setAutoDeploy(true); tomcat.getHost().setDeployOnStartup(true); if (configuration.isEnableNaming()) { tomcat.enableNaming(); } return tomcat; }
Example 6
Source File: TomcatServletITest.java From java-agent with Apache License 2.0 | 6 votes |
@Before public void beforeTest() throws Exception { tomcatServer = new Tomcat(); tomcatServer.setPort(serverPort); File baseDir = new File("tomcat"); tomcatServer.setBaseDir(baseDir.getAbsolutePath()); File applicationDir = new File(baseDir + "/webapps", "/ROOT"); if (!applicationDir.exists()) { applicationDir.mkdirs(); } Context appContext = tomcatServer.addWebapp("", applicationDir.getAbsolutePath()); Tomcat.addServlet(appContext, "helloWorldServlet", new TestServlet()); appContext.addServletMappingDecoded("/hello", "helloWorldServlet"); tomcatServer.start(); System.out.println("Tomcat server: http://" + tomcatServer.getHost().getName() + ":" + serverPort + "/"); }
Example 7
Source File: GrailsIT.java From glowroot with Apache License 2.0 | 6 votes |
@Override public void executeApp() throws Exception { int port = getAvailablePort(); Tomcat tomcat = new Tomcat(); tomcat.setBaseDir("target/tomcat"); tomcat.setPort(port); Context context = tomcat.addWebapp("", new File("src/test/resources").getAbsolutePath()); WebappLoader webappLoader = new WebappLoader(RenderInTomcat.class.getClassLoader()); context.setLoader(webappLoader); // this is needed in order for Tomcat to find annotated classes VirtualDirContext resources = new VirtualDirContext(); resources.setExtraResourcePaths("/WEB-INF/classes=target/test-classes"); context.setResources(resources); tomcat.start(); doTest(port); tomcat.stop(); tomcat.destroy(); }
Example 8
Source File: StrutsTwoIT.java From glowroot with Apache License 2.0 | 5 votes |
@Override public void executeApp() throws Exception { int port = getAvailablePort(); Tomcat tomcat = new Tomcat(); tomcat.setBaseDir("target/tomcat"); tomcat.setPort(port); String subdir; try { Class.forName("org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter"); subdir = "struts2.5"; } catch (ClassNotFoundException e) { subdir = "struts2"; } Context context = tomcat.addWebapp("", new File("src/test/resources/" + subdir).getAbsolutePath()); WebappLoader webappLoader = new WebappLoader(ExecuteActionInTomcat.class.getClassLoader()); context.setLoader(webappLoader); tomcat.start(); AsyncHttpClient asyncHttpClient = new AsyncHttpClient(); int statusCode = asyncHttpClient.prepareGet("http://localhost:" + port + "/hello.action") .execute().get().getStatusCode(); asyncHttpClient.close(); if (statusCode != 200) { throw new IllegalStateException("Unexpected status code: " + statusCode); } tomcat.stop(); tomcat.destroy(); }
Example 9
Source File: AbstractWebAppIntegrationTest.java From jbrotli with Apache License 2.0 | 5 votes |
@BeforeClass public void startTomcatServer() throws Throwable { webAppBaseDir = createTempDirectory("jbrotli-examples-test").toString(); tomcat = new Tomcat(); tomcat.setBaseDir(webAppBaseDir); tomcat.setPort(0); tomcat.getConnector().setProperty("address", serverAddress); tomcat.getHost().setAppBase(webAppBaseDir); tomcat.getHost().setAutoDeploy(false); tomcat.getHost().setDeployOnStartup(true); tomcat.addWebapp(tomcat.getHost(), "/", webAppBaseDir); doCopyAllFiles(findWebAppSourceFolder(), webAppBaseDir); tomcat.start(); }
Example 10
Source File: FederationTest.java From cxf-fediz with Apache License 2.0 | 5 votes |
private static void initRp() { try { rpServer = new Tomcat(); rpServer.setPort(0); String currentDir = new File(".").getCanonicalPath(); rpServer.setBaseDir(currentDir + File.separator + "target"); rpServer.getHost().setAppBase("tomcat/rp/webapps"); rpServer.getHost().setAutoDeploy(true); rpServer.getHost().setDeployOnStartup(true); Connector httpsConnector = new Connector(); httpsConnector.setPort(Integer.parseInt(rpHttpsPort)); httpsConnector.setSecure(true); httpsConnector.setScheme("https"); httpsConnector.setAttribute("keyAlias", "mytomidpkey"); httpsConnector.setAttribute("keystorePass", "tompass"); httpsConnector.setAttribute("keystoreFile", "test-classes/server.jks"); httpsConnector.setAttribute("truststorePass", "tompass"); httpsConnector.setAttribute("truststoreFile", "test-classes/server.jks"); // httpsConnector.setAttribute("clientAuth", "false"); httpsConnector.setAttribute("clientAuth", "want"); httpsConnector.setAttribute("sslProtocol", "TLS"); httpsConnector.setAttribute("SSLEnabled", true); rpServer.getService().addConnector(httpsConnector); rpServer.addWebapp("/fedizhelloworld", "cxfWebapp"); rpServer.addWebapp("/fedizhelloworldnoreqvalidation", "cxfWebapp"); rpServer.start(); } catch (Exception e) { e.printStackTrace(); } }
Example 11
Source File: StatsServer.java From cxf with Apache License 2.0 | 5 votes |
public static void main(final String[] args) throws Exception { // Register and map the dispatcher servlet final File base = new File(System.getProperty("java.io.tmpdir")); final Tomcat server = new Tomcat(); server.setPort(8686); server.setBaseDir(base.getAbsolutePath()); final StandardContext context = (StandardContext)server.addWebapp("/", base.getAbsolutePath()); context.setConfigFile(StatsServer.class.getResource("/META-INF/context.xml")); context.addApplicationListener(ContextLoaderListener.class.getName()); context.setAddWebinfClassesResources(true); context.setResources(resourcesFrom(context, "target/classes")); context.setParentClassLoader(Thread.currentThread().getContextClassLoader()); final Wrapper cxfServlet = Tomcat.addServlet(context, "cxfServlet", new CXFServlet()); cxfServlet.setAsyncSupported(true); context.addServletMappingDecoded("/rest/*", "cxfServlet"); final Context staticContext = server.addWebapp("/static", base.getAbsolutePath()); Tomcat.addServlet(staticContext, "cxfStaticServlet", new DefaultServlet()); staticContext.addServletMappingDecoded("/static/*", "cxfStaticServlet"); staticContext.setResources(resourcesFrom(staticContext, "target/classes/web-ui")); staticContext.setParentClassLoader(Thread.currentThread().getContextClassLoader()); server.start(); server.getServer().await(); }
Example 12
Source File: UnmanagedTomcatServiceTest.java From armeria with Apache License 2.0 | 5 votes |
@Override protected void configure(ServerBuilder sb) throws Exception { // Prepare Tomcat instances. tomcatWithWebApp = new Tomcat(); tomcatWithWebApp.setPort(0); tomcatWithWebApp.setBaseDir("build" + File.separatorChar + "tomcat-" + UnmanagedTomcatServiceTest.class.getSimpleName() + "-1"); tomcatWithWebApp.addWebapp("", WebAppContainerTest.webAppRoot().getAbsolutePath()); TomcatUtil.engine(tomcatWithWebApp.getService(), "foo").setName("tomcatWithWebApp"); tomcatWithoutWebApp = new Tomcat(); tomcatWithoutWebApp.setPort(0); tomcatWithoutWebApp.setBaseDir("build" + File.separatorChar + "tomcat-" + UnmanagedTomcatServiceTest.class.getSimpleName() + "-2"); assertThat(TomcatUtil.engine(tomcatWithoutWebApp.getService(), "bar")).isNotNull(); // Start the Tomcats. tomcatWithWebApp.start(); tomcatWithoutWebApp.start(); // Bind them to the Server. sb.serviceUnder("/empty/", TomcatService.of(new Connector(), "someHost")) .serviceUnder("/some-webapp-nohostname/", TomcatService.of(tomcatWithWebApp.getConnector())) .serviceUnder("/no-webapp/", TomcatService.of(tomcatWithoutWebApp)) .serviceUnder("/some-webapp/", TomcatService.of(tomcatWithWebApp)); }
Example 13
Source File: TomcatHttpServer.java From dubbo-2.6.5 with Apache License 2.0 | 5 votes |
public TomcatHttpServer(URL url, final HttpHandler handler) { super(url, handler); this.url = url; DispatcherServlet.addHttpHandler(url.getPort(), handler); String baseDir = new File(System.getProperty("java.io.tmpdir")).getAbsolutePath(); tomcat = new Tomcat(); tomcat.setBaseDir(baseDir); tomcat.setPort(url.getPort()); tomcat.getConnector().setProperty( "maxThreads", String.valueOf(url.getParameter(Constants.THREADS_KEY, Constants.DEFAULT_THREADS))); // tomcat.getConnector().setProperty( // "minSpareThreads", String.valueOf(url.getParameter(Constants.THREADS_KEY, Constants.DEFAULT_THREADS))); tomcat.getConnector().setProperty( "maxConnections", String.valueOf(url.getParameter(Constants.ACCEPTS_KEY, -1))); tomcat.getConnector().setProperty("URIEncoding", "UTF-8"); tomcat.getConnector().setProperty("connectionTimeout", "60000"); tomcat.getConnector().setProperty("maxKeepAliveRequests", "-1"); tomcat.getConnector().setProtocol("org.apache.coyote.http11.Http11NioProtocol"); Context context = tomcat.addContext("/", baseDir); Tomcat.addServlet(context, "dispatcher", new DispatcherServlet()); context.addServletMapping("/*", "dispatcher"); ServletManager.getInstance().addServletContext(url.getPort(), context.getServletContext()); try { tomcat.start(); } catch (LifecycleException e) { throw new IllegalStateException("Failed to start tomcat server at " + url.getAddress(), e); } }
Example 14
Source File: CRUDClientServerTest.java From TeaStore with Apache License 2.0 | 5 votes |
/** * Setup the test by deploying an embedded tomcat and adding the rest endpoints. * @throws Throwable Throws uncaught throwables for test to fail. */ @Before public void setup() throws Throwable { testTomcat = new Tomcat(); testTomcat.setPort(0); testTomcat.setBaseDir(testWorkingDir); Context context = testTomcat.addWebapp(CONTEXT, testWorkingDir); ResourceConfig restServletConfig = new ResourceConfig(); restServletConfig.register(TestEntityEndpoint.class); ServletContainer restServlet = new ServletContainer(restServletConfig); testTomcat.addServlet(CONTEXT, "restServlet", restServlet); context.addServletMappingDecoded("/rest/*", "restServlet"); testTomcat.start(); }
Example 15
Source File: KerberosTest.java From cxf-fediz with Apache License 2.0 | 4 votes |
private static Tomcat startServer(boolean idp, String port) throws ServletException, LifecycleException, IOException { Tomcat server = new Tomcat(); server.setPort(0); String currentDir = new File(".").getCanonicalPath(); String baseDir = currentDir + File.separator + "target"; server.setBaseDir(baseDir); if (idp) { server.getHost().setAppBase("tomcat/idp/webapps"); } else { server.getHost().setAppBase("tomcat/rp/webapps"); } server.getHost().setAutoDeploy(true); server.getHost().setDeployOnStartup(true); Connector httpsConnector = new Connector(); httpsConnector.setPort(Integer.parseInt(port)); httpsConnector.setSecure(true); httpsConnector.setScheme("https"); httpsConnector.setAttribute("keyAlias", "mytomidpkey"); httpsConnector.setAttribute("keystorePass", "tompass"); httpsConnector.setAttribute("keystoreFile", "test-classes/server.jks"); httpsConnector.setAttribute("truststorePass", "tompass"); httpsConnector.setAttribute("truststoreFile", "test-classes/server.jks"); httpsConnector.setAttribute("clientAuth", "want"); // httpsConnector.setAttribute("clientAuth", "false"); httpsConnector.setAttribute("sslProtocol", "TLS"); httpsConnector.setAttribute("SSLEnabled", true); server.getService().addConnector(httpsConnector); if (idp) { File stsWebapp = new File(baseDir + File.separator + server.getHost().getAppBase(), "fediz-idp-sts"); server.addWebapp("/fediz-idp-sts", stsWebapp.getAbsolutePath()); File idpWebapp = new File(baseDir + File.separator + server.getHost().getAppBase(), "fediz-idp"); server.addWebapp("/fediz-idp", idpWebapp.getAbsolutePath()); } else { File rpWebapp = new File(baseDir + File.separator + server.getHost().getAppBase(), "simpleWebapp"); Context cxt = server.addWebapp("/fedizhelloworld", rpWebapp.getAbsolutePath()); FederationAuthenticator fa = new FederationAuthenticator(); fa.setConfigFile(currentDir + File.separator + "target" + File.separator + "test-classes" + File.separator + "fediz_config.xml"); cxt.getPipeline().addValve(fa); } server.start(); return server; }
Example 16
Source File: Runner.java From myrrix-recommender with Apache License 2.0 | 4 votes |
private void configureTomcat(Tomcat tomcat, Connector connector) { tomcat.setBaseDir(noSuchBaseDir.getAbsolutePath()); tomcat.setConnector(connector); tomcat.getService().addConnector(connector); }
Example 17
Source File: ServingLayer.java From oryx with Apache License 2.0 | 4 votes |
private void configureTomcat(Tomcat tomcat, Connector connector) { tomcat.setBaseDir(noSuchBaseDir.toAbsolutePath().toString()); tomcat.setConnector(connector); }
Example 18
Source File: CatalinaContainer.java From scipio-erp with Apache License 2.0 | 4 votes |
@Override public void init(String[] args, String name, String configFile) throws ContainerException { this.name = name; // get the container config ContainerConfig.Container cc = ContainerConfig.getContainer(name, configFile); if (cc == null) { throw new ContainerException("No catalina-container configuration found in container config!"); } // embedded properties boolean useNaming = ContainerConfig.getPropertyValue(cc, "use-naming", false); //int debug = ContainerConfig.getPropertyValue(cc, "debug", 0); // grab some global context settings this.contextReloadable = ContainerConfig.getPropertyValue(cc, "apps-context-reloadable", false); this.crossContext = ContainerConfig.getPropertyValue(cc, "apps-cross-context", true); this.distribute = ContainerConfig.getPropertyValue(cc, "apps-distributable", true); this.catalinaRuntimeHome = ContainerConfig.getPropertyValue(cc, "catalina-runtime-home", "runtime/catalina"); // set catalina_home System.setProperty(Globals.CATALINA_HOME_PROP, System.getProperty("ofbiz.home") + "/" + this.catalinaRuntimeHome); System.setProperty(Globals.CATALINA_BASE_PROP, System.getProperty(Globals.CATALINA_HOME_PROP)); // create the instance of embedded Tomcat System.setProperty("catalina.useNaming", String.valueOf(useNaming)); tomcat = new Tomcat(); tomcat.setBaseDir(System.getProperty("ofbiz.home")); // configure JNDI in the StandardServer StandardServer server = (StandardServer) tomcat.getServer(); if (useNaming) { tomcat.enableNaming(); } try { server.setGlobalNamingContext(new InitialContext()); } catch (NamingException e) { throw new ContainerException(e); } // create the engine List<ContainerConfig.Container.Property> engineProps = cc.getPropertiesWithValue("engine"); if (UtilValidate.isEmpty(engineProps)) { throw new ContainerException("Cannot load CatalinaContainer; no engines defined."); } if (engineProps.size() > 1) { throw new ContainerException("Cannot load CatalinaContainer; more than one engine configuration found; only one is supported."); } createEngine(engineProps.get(0)); // create the connectors List<ContainerConfig.Container.Property> connectorProps = cc.getPropertiesWithValue("connector"); if (UtilValidate.isEmpty(connectorProps)) { throw new ContainerException("Cannot load CatalinaContainer; no connectors defined!"); } List<ScipioConnectorInfo> scipioConnectorInfoList = new ArrayList<>(); // SCIPIO for (ContainerConfig.Container.Property connectorProp: connectorProps) { Connector connector = createConnector(connectorProp); scipioConnectorInfoList.add(new ScipioConnectorInfo(connectorProp, connector)); // SCIPIO } ScipioConnectorInfo.registerConnectors(this.name, scipioConnectorInfoList); // SCIPIO }
Example 19
Source File: AbstractUiTest.java From TeaStore with Apache License 2.0 | 4 votes |
@Before public void setup() throws ServletException, LifecycleException, InterruptedException, JsonProcessingException { webUITomcat = new Tomcat(); webUITomcat.setPort(3000); webUITomcat.setBaseDir(testWorkingDir); webUITomcat.enableNaming(); Context context = webUITomcat.addWebapp(CONTEXT, System.getProperty("user.dir") + File.separator + "src" + File.separator + "main" + File.separator + "webapp"); ContextEnvironment registryURL = new ContextEnvironment(); registryURL.setDescription(""); registryURL.setOverride(false); registryURL.setType("java.lang.String"); registryURL.setName("registryURL"); registryURL.setValue("http://localhost:9001/tools.descartes.teastore.registry/rest/services/"); context.getNamingResources().addEnvironment(registryURL); webUITomcat.addServlet(CONTEXT, "servlet", getServlet()); webUITomcat.addServlet(CONTEXT, "index", new IndexServlet()); webUITomcat.addServlet(CONTEXT, "login", new LoginServlet()); webUITomcat.addServlet(CONTEXT, "order", new OrderServlet()); context.addServletMappingDecoded("/test", "servlet"); context.addServletMappingDecoded("/index", "index"); context.addServletMappingDecoded("/login", "login"); context.addServletMappingDecoded("/order", "order"); context.addWelcomeFile("/index"); webUITomcat.start(); // Mock registry List<String> strings = new LinkedList<String>(); strings.add("localhost:9001"); String json = new ObjectMapper().writeValueAsString(strings); wireMockRule.stubFor(get(urlEqualTo( "/tools.descartes.teastore.registry/rest/services/" + Service.IMAGE.getServiceName() + "/")) .willReturn(okJson(json))); wireMockRule.stubFor(get(urlEqualTo( "/tools.descartes.teastore.registry/rest/services/" + Service.AUTH.getServiceName() + "/")) .willReturn(okJson(json))); wireMockRule.stubFor(get(urlEqualTo( "/tools.descartes.teastore.registry/rest/services/" + Service.PERSISTENCE.getServiceName() + "/")) .willReturn(okJson(json))); wireMockRule.stubFor(get(urlEqualTo( "/tools.descartes.teastore.registry/rest/services/" + Service.RECOMMENDER.getServiceName() + "/")) .willReturn(okJson(json))); // Mock images HashMap<String, String> img = new HashMap<>(); img.put("andreBauer", "andreBauer"); img.put("johannesGrohmann", "johannesGrohmann"); img.put("joakimKistowski", "joakimKistowski"); img.put("simonEismann", "simonEismann"); img.put("norbertSchmitt", "norbertSchmitt"); img.put("descartesLogo", "descartesLogo"); img.put("icon", "icon"); mockValidPostRestCall(img, "/tools.descartes.teastore.image/rest/image/getWebImages"); }
Example 20
Source File: JspRenderIT.java From glowroot with Apache License 2.0 | 3 votes |
@Override public void executeApp() throws Exception { int port = getAvailablePort(); Tomcat tomcat = new Tomcat(); tomcat.setBaseDir("target/tomcat"); tomcat.setPort(port); Context context = tomcat.addContext("", new File("src/test/resources").getAbsolutePath()); WebappLoader webappLoader = new WebappLoader(RenderJspInTomcat.class.getClassLoader()); context.setLoader(webappLoader); Tomcat.addServlet(context, "hello", new ForwardingServlet()); context.addServletMapping("/hello", "hello"); Tomcat.addServlet(context, "jsp", new JspServlet()); context.addServletMapping("*.jsp", "jsp"); tomcat.start(); AsyncHttpClient asyncHttpClient = new AsyncHttpClient(); int statusCode = asyncHttpClient.prepareGet("http://localhost:" + port + "/hello") .execute().get().getStatusCode(); asyncHttpClient.close(); if (statusCode != 200) { throw new IllegalStateException("Unexpected status code: " + statusCode); } tomcat.stop(); tomcat.destroy(); }