All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.webpieces.webserver.api.WebServerFactory Maven / Gradle / Ivy

There is a newer version: 2.1.1
Show newest version
package org.webpieces.webserver.api;

import java.net.InetAddress;
import java.net.UnknownHostException;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.webpieces.router.api.ProdRouterModule;
import org.webpieces.router.api.RouterConfig;
import org.webpieces.templating.api.ProdTemplateModule;
import org.webpieces.templating.api.TemplateConfig;
import org.webpieces.util.cmdline2.Arguments;
import org.webpieces.webserver.impl.PortConfigLookupImpl;
import org.webpieces.webserver.impl.WebServerImpl;
import org.webpieces.webserver.impl.WebServerModule;

import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Module;
import com.google.inject.util.Modules;

public abstract class WebServerFactory {

	private static final Logger log = LoggerFactory.getLogger(WebServerFactory.class);

    protected WebServerFactory() {}

	public static WebServer create(WebServerConfig config, RouterConfig routerConfig, TemplateConfig templateConfig, Arguments args) {
		if(!routerConfig.getMetaFile().exists())
			throw new RuntimeException("file not found="+routerConfig.getMetaFile());

		try {
			//logback called getLocalhost(resulting in 5 seconds)
			//H2 called getLocalHost like 3 times(resulting in 15 more seconds)
			//overall, it really screwed the startup time!!
			//https://stackoverflow.com/questions/33289695/inetaddress-getlocalhost-slow-to-run-30-seconds/33289897#33289897
			log.info("Checking timing on getLocalHost (seems very bad on many MAC computers) which makes webpieces startup look slow(and we like a fast startup");
			long start = System.currentTimeMillis();
			InetAddress.getLocalHost();
			long totalTimeSeconds = (System.currentTimeMillis() - start) / 1000;
			if(totalTimeSeconds > 3)
				throw new IllegalStateException("Your computer configuration is messed up.  getLocalHost "
						+ "is taking longer\nthan 3 seconds.  FIX THIS NOW!!!  You can typically edit your hosts file\n"
						+ "to do so.  See https://stackoverflow.com/questions/33289695/inetaddress-getlocalhost-slow-to-run-30-seconds/33289897#33289897 for more info");
			
		} catch (UnknownHostException e) {
			throw new RuntimeException(e);
		}
		
		Module allModules = getModules(config, routerConfig, templateConfig, args);

		Module platformOverrides = config.getPlatformOverrides();
		if(platformOverrides != null)
			allModules = Modules.override(allModules).with(platformOverrides);

		Injector injector = Guice.createInjector(allModules);
		WebServerImpl serverImpl = injector.getInstance(WebServerImpl.class);
		serverImpl.configureSync(args); //configure must be called as after configured, Arguments.checkConsumedCorrectly must
		  						//be called before start is called on the webserver
		return serverImpl;
	}

	private static Module getModules(WebServerConfig config, RouterConfig routerConfig, TemplateConfig templateConfig, Arguments args) {

		//Special wiring needed between webserver and router due to order of start.  See
		//PortConfigLookupImpl javadoc for more info
		PortConfigLookupImpl portLookup = new PortConfigLookupImpl();
		
		
		return Modules.combine(
			new WebServerModule(config, portLookup, args),
			new ProdRouterModule(routerConfig, portLookup),
			new ProdTemplateModule(templateConfig)
		);
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy