From 26ff977dad7c58c6fbdc71242d09da965ca628f9 Mon Sep 17 00:00:00 2001 From: Mero Rai Date: Wed, 24 Jun 2020 09:44:53 +0100 Subject: [PATCH] Merge pull request #4699 from MeroRai/CUSTCOM-14 CUSTCOM-14 Improvements in stop-domain process --- .../servermgmt/cli/LocalServerCommand.java | 25 ++++++++----------- .../servermgmt/cli/StopDomainCommand.java | 6 ++--- 2 files changed, 13 insertions(+), 18 deletions(-) diff --git a/nucleus/admin/server-mgmt/src/main/java/com/sun/enterprise/admin/servermgmt/cli/LocalServerCommand.java b/nucleus/admin/server-mgmt/src/main/java/com/sun/enterprise/admin/servermgmt/cli/LocalServerCommand.java index 2a64abee847..f0fa66e0050 100644 --- a/nucleus/admin/server-mgmt/src/main/java/com/sun/enterprise/admin/servermgmt/cli/LocalServerCommand.java +++ b/nucleus/admin/server-mgmt/src/main/java/com/sun/enterprise/admin/servermgmt/cli/LocalServerCommand.java @@ -68,6 +68,8 @@ import com.sun.enterprise.util.SystemPropertyConstants; import com.sun.enterprise.util.io.FileUtils; import com.sun.enterprise.util.io.ServerDirs; +import java.net.InetAddress; +import java.net.InetSocketAddress; import org.glassfish.api.ActionReport; import org.glassfish.api.admin.CommandException; @@ -96,6 +98,7 @@ public abstract class LocalServerCommand extends CLICommand { //////////////////////////////////////////////////////////////// private ServerDirs serverDirs; private static final LocalStringsImpl STRINGS = new LocalStringsImpl(LocalDomainCommand.class); + private final static int IS_RUNNING_DEFAULT_TIMEOUT = 2000; //////////////////////////////////////////////////////////////// /// Section: protected variables @@ -356,24 +359,18 @@ protected final int getServerPid() { * @return boolean indicating whether the server is running */ protected final boolean isRunning(String host, int port) { - Socket server = null; - try { - server = new Socket(host, port); + + try(Socket server = new Socket()) { + if (host == null) { + host = InetAddress.getByName(null).getHostName(); + } + + server.connect( new InetSocketAddress(host, port), IS_RUNNING_DEFAULT_TIMEOUT); return true; - } - catch (Exception ex) { + }catch (Exception ex) { logger.log(Level.FINER, "\nisRunning got exception: {0}", ex); return false; } - finally { - if (server != null) { - try { - server.close(); - } - catch (IOException ex) { - } - } - } } /** diff --git a/nucleus/admin/server-mgmt/src/main/java/com/sun/enterprise/admin/servermgmt/cli/StopDomainCommand.java b/nucleus/admin/server-mgmt/src/main/java/com/sun/enterprise/admin/servermgmt/cli/StopDomainCommand.java index 03bdbfef5db..339012af4bf 100644 --- a/nucleus/admin/server-mgmt/src/main/java/com/sun/enterprise/admin/servermgmt/cli/StopDomainCommand.java +++ b/nucleus/admin/server-mgmt/src/main/java/com/sun/enterprise/admin/servermgmt/cli/StopDomainCommand.java @@ -46,6 +46,7 @@ import com.sun.enterprise.admin.cli.remote.RemoteCLICommand; import com.sun.enterprise.universal.process.ProcessUtils; import com.sun.enterprise.util.io.FileUtils; +import com.sun.enterprise.util.net.NetUtils; import java.io.File; import java.util.Map; import java.util.Map.Entry; @@ -95,10 +96,7 @@ protected void validate() protected void initDomain() throws CommandException { // only initialize local domain information if it's a local operation - // TODO Byron said in April 2013 that we should probably just check if - // NetUtils says that the getHost() --> isThisMe() rather than merely - // checking for the magic "localhost" string. Too risky to fool with it today. - if (programOpts.getHost().equals(CLIConstants.DEFAULT_HOSTNAME)) { + if (NetUtils.isThisHostLocal(programOpts.getHost())) { super.initDomain(); } else if (userArgDomainName != null) { // remote case throw new CommandException(Strings.get("StopDomain.noDomainNameAllowed"));