X-Git-Url: https://git.argeo.org/?a=blobdiff_plain;f=org.argeo.cms%2Fsrc%2Forg%2Fargeo%2Fcms%2Finternal%2Fruntime%2FCmsStateImpl.java;h=c9109c8561fb2500485998c033cbb4ac4d58a419;hb=8c6e16aa43d9523e1ec57a41a06b3ceba7d23fdb;hp=902fe793b7a28ea98b377d65fb717c19d41f1274;hpb=79c14508549e1af042485b4f888a3c54f5a68833;p=lgpl%2Fargeo-commons.git diff --git a/org.argeo.cms/src/org/argeo/cms/internal/runtime/CmsStateImpl.java b/org.argeo.cms/src/org/argeo/cms/internal/runtime/CmsStateImpl.java index 902fe793b..c9109c856 100644 --- a/org.argeo.cms/src/org/argeo/cms/internal/runtime/CmsStateImpl.java +++ b/org.argeo.cms/src/org/argeo/cms/internal/runtime/CmsStateImpl.java @@ -2,7 +2,9 @@ package org.argeo.cms.internal.runtime; import java.io.IOException; import java.io.Reader; +import java.net.InetAddress; import java.net.URL; +import java.net.UnknownHostException; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; @@ -21,6 +23,11 @@ import java.util.Objects; import java.util.Set; import java.util.StringJoiner; import java.util.UUID; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ForkJoinPool; +import java.util.concurrent.ForkJoinTask; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; import javax.security.auth.login.Configuration; @@ -43,7 +50,7 @@ public class CmsStateImpl implements CmsState { private UUID uuid; // private final boolean cleanState; -// private String hostname; + private String hostname; private UuidFactory uuidFactory; @@ -96,11 +103,27 @@ public class CmsStateImpl implements CmsState { // this.uuid = UUID.fromString(stateUuidStr); this.uuid = uuidFactory.timeUUID(); // this.cleanState = stateUuid.equals(frameworkUuid); -// try { -// this.hostname = InetAddress.getLocalHost().getHostName(); -// } catch (UnknownHostException e) { -// log.error("Cannot set hostname: " + e); -// } + + // hostname + this.hostname = getDeployProperty(CmsDeployProperty.HOST); + // TODO verify we have access to the IP address + if (hostname == null) { + final String LOCALHOST_IP = "::1"; + ForkJoinTask hostnameFJT = ForkJoinPool.commonPool().submit(() -> { + try { + String hostname = InetAddress.getLocalHost().getHostName(); + return hostname; + } catch (UnknownHostException e) { + throw new IllegalStateException("Cannot get local hostname", e); + } + }); + try { + this.hostname = hostnameFJT.get(5, TimeUnit.SECONDS); + } catch (InterruptedException | ExecutionException | TimeoutException e) { + this.hostname = LOCALHOST_IP; + log.warn("Could not get local hostname, using " + this.hostname); + } + } availableSince = System.currentTimeMillis(); if (log.isDebugEnabled()) { @@ -373,6 +396,10 @@ public class CmsStateImpl implements CmsState { this.uuidFactory = uuidFactory; } + public String getHostname() { + return hostname; + } + /** * Called before node initialisation, in order populate OSGi instance are with * some files (typically LDIF, etc).