From 9b9c11226334fcf4666235cea9fa9aa71f75ddda Mon Sep 17 00:00:00 2001 From: luccioman Date: Tue, 12 Jul 2016 01:53:01 +0200 Subject: [PATCH] Handle more propertly local port configuration by system property And prefixed property with "net.yacy" to avoid ambiguity. --- Procfile | 2 +- htroot/ConfigBasic.html | 2 +- htroot/ConfigBasic.java | 2 ++ source/net/yacy/server/serverSwitch.java | 36 +++++++++++++++++------- 4 files changed, 30 insertions(+), 12 deletions(-) diff --git a/Procfile b/Procfile index 65a9880e6..e6944606a 100755 --- a/Procfile +++ b/Procfile @@ -1 +1 @@ -web: java $JAVA_OPTS -Dhttp.port=$PORT -classpath target/classes:lib/* net.yacy.yacy \ No newline at end of file +web: java $JAVA_OPTS -Dnet.yacy.server.localPort=$PORT -classpath target/classes:lib/* net.yacy.yacy \ No newline at end of file diff --git a/htroot/ConfigBasic.html b/htroot/ConfigBasic.html index daef4777a..1d0c0df91 100644 --- a/htroot/ConfigBasic.html +++ b/htroot/ConfigBasic.html @@ -90,7 +90,7 @@
-     + #(hasSystemDefinedPort)#::(Set by system property '#[systemProperty]#')#(/hasSystemDefinedPort)#    with SSL (https enabled#(withsslenabled)#:: on port #[sslport]##(/withsslenabled)#)
#(upnp)#::
diff --git a/htroot/ConfigBasic.java b/htroot/ConfigBasic.java index 6144bd5af..155b23d6c 100644 --- a/htroot/ConfigBasic.java +++ b/htroot/ConfigBasic.java @@ -266,6 +266,8 @@ public class ConfigBasic { // set default values prop.putHTML("defaultName", sb.peers.mySeed().getName()); + prop.put("hasSystemDefinedPort", env.getLocalPortSystemProperty() != null ? 1 : 0); + prop.put("hasSystemDefinedPort_systemProperty", serverSwitch.LOCAL_PORT_SYSTEM_PROPERTY); prop.put("defaultPort", env.getLocalPort()); prop.put("withsslenabled", env.getConfigBool("server.https", false) ? 1 : 0); lang = env.getConfig("locale.language", "default"); // re-assign lang, may have changed diff --git a/source/net/yacy/server/serverSwitch.java b/source/net/yacy/server/serverSwitch.java index 0f3bbcbf0..fbcc982e5 100644 --- a/source/net/yacy/server/serverSwitch.java +++ b/source/net/yacy/server/serverSwitch.java @@ -58,6 +58,9 @@ import net.yacy.peers.Seed; import net.yacy.search.SwitchboardConstants; public class serverSwitch { + + /** Key of system property defining locally open http port */ + public static final String LOCAL_PORT_SYSTEM_PROPERTY = "net.yacy.server.localPort"; // configuration management private final File configFile; @@ -217,26 +220,39 @@ public class serverSwitch { return getConfigInt(key, dflt); } + + /** + * @return local http server port or null if system property is not defined + */ + public Integer getLocalPortSystemProperty() { + String systemDefinedPort = System.getProperty(LOCAL_PORT_SYSTEM_PROPERTY); + Integer localPort = null; + if(systemDefinedPort != null) { + try { + localPort = Integer.parseInt(systemDefinedPort); + } catch(NumberFormatException e) { + log.warn("System property " + LOCAL_PORT_SYSTEM_PROPERTY + " is not valid : it should be a integer."); + } + } + return localPort; + } /** * Wrapper for {@link #getConfigInt(String, int)} to have a more consistent * API. * - * Default value 8090 will be used if no value is found - * - * @return the local port of this system + * Default value 8090 will be used if no value is found in system properties and in configuration. + * + * @return the local http port of this system * @see #getPublicPort(String, int) */ public int getLocalPort() { - /* A system property "http.port" may override configuration + /* A system property "net.yacy.server.localPort" may override configuration * This is useful when running YaCy inside a container manager such as Heroku which decide which http port to use */ - String systemDefinedPort = System.getProperty("http.port"); - if(systemDefinedPort != null) { - try { - return Integer.parseInt(systemDefinedPort); - } catch(NumberFormatException e) { - } + Integer localPort = getLocalPortSystemProperty(); + if(localPort != null) { + return localPort; } return getConfigInt("port", 8090); }