From 856bfa759bec64b3cfc710fa26b755855bcc61c8 Mon Sep 17 00:00:00 2001
From: jvergara <jvergara@localhost>
Date: Wed, 28 Feb 2007 11:35:55 +0000
Subject: [PATCH] Fix for issue 1285.
---
opends/src/server/org/opends/server/util/SetupUtils.java | 105 +++++++++++++++++++++++++++++++++++++++++++++++++++-
1 files changed, 103 insertions(+), 2 deletions(-)
diff --git a/opends/src/server/org/opends/server/util/SetupUtils.java b/opends/src/server/org/opends/server/util/SetupUtils.java
index b954cc1..a4908be 100644
--- a/opends/src/server/org/opends/server/util/SetupUtils.java
+++ b/opends/src/server/org/opends/server/util/SetupUtils.java
@@ -32,8 +32,12 @@
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
+import java.net.InetSocketAddress;
+import java.net.ServerSocket;
+import java.net.Socket;
import java.util.LinkedList;
+import org.opends.server.types.OperatingSystem;
/**
@@ -116,7 +120,27 @@
return templateFile;
}
+ /**
+ * Returns {@code true} if we are running under Mac OS and
+ * {@code false} otherwise.
+ * @return {@code true} if we are running under Mac OS and
+ * {@code false} otherwise.
+ */
+ public static boolean isMacOS()
+ {
+ return OperatingSystem.MACOS == getOperatingSystem();
+ }
+ /**
+ * Returns {@code true} if we are running under Unix and
+ * {@code false} otherwise.
+ * @return {@code true} if we are running under Unix and
+ * {@code false} otherwise.
+ */
+ public static boolean isUnix()
+ {
+ return OperatingSystem.isUNIXBased(getOperatingSystem());
+ }
/**
* Indicates whether the underlying operating system is a Windows variant.
@@ -126,8 +150,17 @@
*/
public static boolean isWindows()
{
- String osName = System.getProperty("os.name");
- return ((osName != null) && (osName.toLowerCase().indexOf("windows") >= 0));
+ return OperatingSystem.WINDOWS == getOperatingSystem();
+ }
+
+
+ /**
+ * Commodity method to help identifying the OS we are running on.
+ * @return the OperatingSystem we are running on.
+ */
+ private static OperatingSystem getOperatingSystem()
+ {
+ return OperatingSystem.forName(System.getProperty("os.name"));
}
@@ -200,5 +233,73 @@
return setJavaHomeFile;
}
+
+ /**
+ * Returns {@code true} if the provided port is free and we can use it,
+ * {@code false} otherwise.
+ * @param port the port we are analyzing.
+ * @return {@code true} if the provided port is free and we can use it,
+ * {@code false} otherwise.
+ */
+ public static boolean canUseAsPort(int port)
+ {
+ boolean canUseAsPort = false;
+ ServerSocket serverSocket = null;
+ try
+ {
+ InetSocketAddress socketAddress = new InetSocketAddress(port);
+ serverSocket = new ServerSocket();
+ if (!isWindows())
+ {
+ serverSocket.setReuseAddress(true);
+ }
+ serverSocket.bind(socketAddress);
+ canUseAsPort = true;
+
+ serverSocket.close();
+
+ /* Try to create a socket because sometimes even if we can create a server
+ * socket there is already someone listening to the port (is the case
+ * of products as Sun DS 6.0).
+ */
+ try
+ {
+ new Socket("localhost", port);
+ canUseAsPort = false;
+
+ } catch (IOException ioe)
+ {
+ }
+
+ } catch (IOException ex)
+ {
+ canUseAsPort = false;
+ } finally
+ {
+ try
+ {
+ if (serverSocket != null)
+ {
+ serverSocket.close();
+ }
+ } catch (Exception ex)
+ {
+ }
+ }
+
+ return canUseAsPort;
+ }
+
+ /**
+ * Returns {@code true} if the provided port is a priviledged port,
+ * {@code false} otherwise.
+ * @param port the port we are analyzing.
+ * @return {@code true} if the provided port is a priviledged port,
+ * {@code false} otherwise.
+ */
+ public static boolean isPriviledgedPort(int port)
+ {
+ return (port <= 1024) && !isWindows();
+ }
}
--
Gitblit v1.10.0