private boolean bindSocket() { try { serverSocket.bind(new InetSocketAddress(serverConfig.getListenPort())); } catch (IOException e) { LOGGER.log(Level.SEVERE, "Unable to start server: unable to listen on port " + serverConfig.getListenPort(), e); return false; } return true; }
/** * Starts the web server. */ public boolean startServer() { listen = true; if (!(new File(serverConfig. getDocumentRootPath()) .isDirectory())) { LOGGER.log(Level.WARNING, "DocumentRoot does not exist! PATH {0}", new Object[]{ serverConfig.getDocumentRootPath() }); } if (!isNumberOfThreadsSufficient() || !isTempPathWritable() || !bindSocket()) { listen = false; return false; } LOGGER.log(Level.INFO, "Server has been started. Listening on port {0}", new Object[]{ serverConfig.getListenPort() }); FileUtilities.clearDirectory(serverConfig.getTempPath()); start(); return true; }
@Test public void shouldReadSystemPropertiesWhenNoFileIsPresent() throws IOException { System.setProperty("server.port", "9090"); System.setProperty("server.static.path", "path"); System.setProperty("server.static.directoryIndex", "i.php,i.html"); System.setProperty("server.maxThreads", "66"); System.setProperty("server.keepAlive.enabled", "false"); System.setProperty("server.errorDocument.404", "ERROR404.html"); System.setProperty("server.errorDocument.403", "ERROR403.html"); System.setProperty(ADDITIONAL_ATTRIBUTE_NAME, "YET_ANOTHER_VALUE"); ServerConfig serverConfig = new ServerConfigImpl("/tmp/", "/tmp/", new Properties()); assertThat(serverConfig.getDocumentRootPath(), is("/tmp/path")); assertThat(serverConfig.getDirectoryIndex(), hasItem("i.php")); assertThat(serverConfig.getDirectoryIndex(), hasItem("i.html")); assertThat(serverConfig.getDirectoryIndex().size(), is(2)); assertThat(serverConfig.getErrorDocument403Path(), is("/tmp/ERROR403.html")); assertThat(serverConfig.getErrorDocument404Path(), is("/tmp/ERROR404.html")); assertThat(serverConfig.getListenPort(), is(9090)); assertThat(serverConfig.getMaxServerThreads(), is(66)); assertThat(serverConfig.isKeepAlive(), is(false)); // assertThat(serverConfig.getMimeTypeMapping().getMimeTypeByExtension("ANY"), is("mime/text")); assertThat(serverConfig.getAttribute(ADDITIONAL_ATTRIBUTE_NAME), is("YET_ANOTHER_VALUE")); } }
@Test public void shouldCreateFromPath() throws IOException { writeFiles(DEFAULT_CONFIG_DATA + "DefaultMimeType mime/text\n"); ServerConfig serverConfig = ServerConfigImpl.createFromPath(workingDirectory, tempDirectory); assertThat(serverConfig.getTempPath(), is(tempDirectory)); assertThat(serverConfig.getBasePath(), is(workingDirectory)); assertThat(serverConfig.getDocumentRootPath(), is(workingDirectory + "wwwx")); assertThat(serverConfig.getDirectoryIndex(), hasItem("index.php")); assertThat(serverConfig.getDirectoryIndex(), hasItem("index.html")); assertThat(serverConfig.getDirectoryIndex().size(), is(2)); assertThat(serverConfig.getErrorDocument403Path(), is(workingDirectory + "error403.html")); assertThat(serverConfig.getErrorDocument404Path(), is(workingDirectory + "error404.html")); assertThat(serverConfig.getListenPort(), is(8090)); assertThat(serverConfig.getMaxServerThreads(), is(3)); assertThat(serverConfig.isKeepAlive(), is(true)); assertThat(serverConfig.getMimeTypeMapping().getMimeTypeByExtension("ANY"), is("mime/text")); assertThat(serverConfig.getAttribute(ADDITIONAL_ATTRIBUTE_NAME), is(ORIGINAL_VALUE)); }
/** * Returns current service state. * * @return */ public ServiceStateDTO getServiceState() { String accessUrl = "Initializing"; if (controller != null && controller.getWebServer() != null) { accessUrl = "http://" + getLocalIpAddress() + getPort(controller.getWebServer().getServerConfig().getListenPort()) + '/'; } boolean isWebserverStarted = controller != null && controller.getWebServer() != null && controller.getWebServer().isRunning(); return new ServiceStateDTO(isServiceStarted, isWebserverStarted, accessUrl); }