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

The following examples show how to use org.apache.catalina.startup.Tomcat#getServer() . 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: ITLoggingWebapp.java    From kite-examples with Apache License 2.0 6 votes vote down vote up
private void startTomcat() throws Exception {
  tomcat = new Tomcat();
  tomcat.setPort(8080);

  File tomcatBaseDir = new File("target/tomcat");
  tomcatBaseDir.mkdirs();
  tomcat.setBaseDir(tomcatBaseDir.getAbsolutePath());

  StandardServer server = (StandardServer) tomcat.getServer();
  server.addLifecycleListener(new AprLifecycleListener());

  String contextPath = "/logging-webapp";
  File[] warFiles = new File("target").listFiles(new FileFilter() {
    @Override
    public boolean accept(File pathname) {
      return pathname.getName().endsWith(".war");
    }
  });
  assertEquals("Not exactly one war file found", 1, warFiles.length);
  tomcat.addWebapp(contextPath, warFiles[0].getAbsolutePath());
  tomcat.start();
}
 
Example 2
Source File: SAMLSSOTest.java    From cxf-fediz with Apache License 2.0 5 votes vote down vote up
private static void shutdownServer(Tomcat server) {
    try {
        if (server != null && server.getServer() != null
            && server.getServer().getState() != LifecycleState.DESTROYED) {
            if (server.getServer().getState() != LifecycleState.STOPPED) {
                server.stop();
            }
            server.destroy();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 3
Source File: SpringTest.java    From cxf-fediz with Apache License 2.0 5 votes vote down vote up
private static void shutdownServer(Tomcat server) {
    try {
        if (server != null && server.getServer() != null
            && server.getServer().getState() != LifecycleState.DESTROYED) {
            if (server.getServer().getState() != LifecycleState.STOPPED) {
                server.stop();
            }
            server.destroy();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 4
Source File: IdpTest.java    From cxf-fediz with Apache License 2.0 5 votes vote down vote up
private static void shutdownServer(Tomcat server) {
    try {
        if (server != null && server.getServer() != null
            && server.getServer().getState() != LifecycleState.DESTROYED) {
            if (server.getServer().getState() != LifecycleState.STOPPED) {
                server.stop();
            }
            server.destroy();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 5
Source File: LDAPTest.java    From cxf-fediz with Apache License 2.0 5 votes vote down vote up
private static void shutdownServer(Tomcat server) {
    try {
        if (server != null && server.getServer() != null
            && server.getServer().getState() != LifecycleState.DESTROYED) {
            if (server.getServer().getState() != LifecycleState.STOPPED) {
                server.stop();
            }
            server.destroy();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 6
Source File: FederationTest.java    From cxf-fediz with Apache License 2.0 5 votes vote down vote up
private static void shutdownServer(Tomcat server) {
    try {
        if (server != null && server.getServer() != null
            && server.getServer().getState() != LifecycleState.DESTROYED) {
            if (server.getServer().getState() != LifecycleState.STOPPED) {
                server.stop();
            }
            server.destroy();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 7
Source File: TokenExpiryTest.java    From cxf-fediz with Apache License 2.0 5 votes vote down vote up
private static void shutdownServer(Tomcat server) {
    try {
        if (server != null && server.getServer() != null
            && server.getServer().getState() != LifecycleState.DESTROYED) {
            if (server.getServer().getState() != LifecycleState.STOPPED) {
                server.stop();
            }
            server.destroy();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 8
Source File: WSFedTest.java    From cxf-fediz with Apache License 2.0 5 votes vote down vote up
private static void shutdownServer(Tomcat server) {
    try {
        if (server != null && server.getServer() != null
            && server.getServer().getState() != LifecycleState.DESTROYED) {
            if (server.getServer().getState() != LifecycleState.STOPPED) {
                server.stop();
            }
            server.destroy();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 9
Source File: TomcatPluginTest.java    From cxf-fediz with Apache License 2.0 5 votes vote down vote up
private static void shutdownServer(Tomcat server) {
    try {
        if (server != null && server.getServer() != null
            && server.getServer().getState() != LifecycleState.DESTROYED) {
            if (server.getServer().getState() != LifecycleState.STOPPED) {
                server.stop();
            }
            server.destroy();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 10
Source File: CustomParametersTest.java    From cxf-fediz with Apache License 2.0 5 votes vote down vote up
private static void shutdownServer(Tomcat server) {
    try {
        if (server != null && server.getServer() != null
            && server.getServer().getState() != LifecycleState.DESTROYED) {
            if (server.getServer().getState() != LifecycleState.STOPPED) {
                server.stop();
            }
            server.destroy();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 11
Source File: CXFTest.java    From cxf-fediz with Apache License 2.0 5 votes vote down vote up
private static void shutdownServer(Tomcat server) {
    try {
        if (server != null && server.getServer() != null
            && server.getServer().getState() != LifecycleState.DESTROYED) {
            if (server.getServer().getState() != LifecycleState.STOPPED) {
                server.stop();
            }
            server.destroy();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 12
Source File: TokenExpiryTest.java    From cxf-fediz with Apache License 2.0 5 votes vote down vote up
private static void shutdownServer(Tomcat server) {
    try {
        if (server != null && server.getServer() != null
            && server.getServer().getState() != LifecycleState.DESTROYED) {
            if (server.getServer().getState() != LifecycleState.STOPPED) {
                server.stop();
            }
            server.destroy();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 13
Source File: SpringTest.java    From cxf-fediz with Apache License 2.0 5 votes vote down vote up
private static void shutdownServer(Tomcat server) {
    try {
        if (server != null && server.getServer() != null
            && server.getServer().getState() != LifecycleState.DESTROYED) {
            if (server.getServer().getState() != LifecycleState.STOPPED) {
                server.stop();
            }
            server.destroy();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example 14
Source File: DebugTomcat.java    From kylin with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    setupDebugEnv();

    int port = 7070;
    if (args.length >= 1) {
        port = Integer.parseInt(args[0]);
    }

    File webBase = new File("../webapp/app");
    File webInfDir = new File(webBase, "WEB-INF");
    FileUtils.deleteDirectory(webInfDir);
    FileUtils.copyDirectoryToDirectory(new File("../server/src/main/webapp/WEB-INF"), webBase);

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

    // Add AprLifecycleListener
    StandardServer server = (StandardServer) tomcat.getServer();
    AprLifecycleListener listener = new AprLifecycleListener();
    server.addLifecycleListener(listener);

    Context webContext = tomcat.addWebapp("/kylin", webBase.getAbsolutePath());
    ErrorPage notFound = new ErrorPage();
    notFound.setErrorCode(404);
    notFound.setLocation("/index.html");
    webContext.addErrorPage(notFound);
    webContext.addWelcomeFile("index.html");

    // tomcat start
    tomcat.start();
    tomcat.getServer().await();
}
 
Example 15
Source File: TomcatSetup.java    From component-runtime with Apache License 2.0 5 votes vote down vote up
@Override
public void accept(final Tomcat tomcat) {
    final Server server = tomcat.getServer();
    server.addLifecycleListener(event -> {
        if (Server.class.isInstance(event.getData()) && Lifecycle.AFTER_DESTROY_EVENT.equals(event.getType())
                && Boolean.getBoolean("talend.component.exit-on-destroy")) {
            System.exit(0);
        }
    });
    // if we want it to be really configurable we should add it in ComponentServerConfiguration
    // and set this instance in the standard context to be able to configure it from cdi side
    final boolean dev = Boolean.getBoolean("talend.component.server.tomcat.valve.error.debug");
    if (!dev) {
        Stream
                .of(server.findServices())
                .map(Service::getContainer)
                .flatMap(e -> Stream.of(e.findChildren()))
                .filter(StandardHost.class::isInstance)
                .map(StandardHost.class::cast)
                .forEach(host -> host.addLifecycleListener(event -> {
                    if (event.getType().equals(Lifecycle.BEFORE_START_EVENT)) {
                        StandardHost.class
                                .cast(host)
                                .setErrorReportValveClass(MinimalErrorReportValve.class.getName());
                    }
                }));
    }
}
 
Example 16
Source File: DebugTomcat.java    From kylin-on-parquet-v2 with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
    setupDebugEnv();

    int port = 7070;
    if (args.length >= 1) {
        port = Integer.parseInt(args[0]);
    }

    File webBase = new File("../webapp/app");
    File webInfDir = new File(webBase, "WEB-INF");
    FileUtils.deleteDirectory(webInfDir);
    FileUtils.copyDirectoryToDirectory(new File("../server/src/main/webapp/WEB-INF"), webBase);

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

    // Add AprLifecycleListener
    StandardServer server = (StandardServer) tomcat.getServer();
    AprLifecycleListener listener = new AprLifecycleListener();
    server.addLifecycleListener(listener);

    Context webContext = tomcat.addWebapp("/kylin", webBase.getAbsolutePath());
    ErrorPage notFound = new ErrorPage();
    notFound.setErrorCode(404);
    notFound.setLocation("/index.html");
    webContext.addErrorPage(notFound);
    webContext.addWelcomeFile("index.html");

    // tomcat start
    tomcat.start();
    tomcat.getServer().await();
}
 
Example 17
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 18
Source File: CatalinaContainer.java    From scipio-erp with Apache License 2.0 4 votes vote down vote up
@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: TomcatParentClassLoaderSetup.java    From component-runtime with Apache License 2.0 4 votes vote down vote up
@Override
public void accept(final Tomcat tomcat) {
    final Server server = tomcat.getServer();
    server.setParentClassLoader(server.getClass().getClassLoader());
}
 
Example 20
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);
}