5
0
mirror of https://github.com/apache/sqoop.git synced 2025-05-03 08:00:10 +08:00

SQOOP-3283: Fixing MySQL 3rd party test hanging issue by getting username

from System env/property instead of depending on whoami and Process#waitFor

(Daniel Voros by Attila Szabo)
This commit is contained in:
Attila Szabo 2018-02-14 18:58:08 +01:00
parent f7b460b3f5
commit 3153c3610d

View File

@ -18,15 +18,11 @@
package org.apache.sqoop.manager.mysql; package org.apache.sqoop.manager.mysql;
import org.apache.sqoop.SqoopOptions;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.apache.sqoop.SqoopOptions;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList; import java.util.ArrayList;
/** /**
@ -95,50 +91,19 @@ public String[] addUserNameAndPasswordToArgs(String[] extraArgs) {
return moreArgs; return moreArgs;
} }
public static String getCurrentUser() { private static String getCurrentUser() {
// First, check the $USER environment variable. // First, check the $USER environment variable.
String envUser = System.getenv("USER"); String envUser = System.getenv("USER");
if (null != envUser) { if (null != envUser) {
return envUser; return envUser;
} }
// Try `whoami` // Fall back to user.name system property
String[] whoamiArgs = new String[1]; envUser = System.getProperty("user.name");
whoamiArgs[0] = "whoami"; if (null != envUser) {
Process p = null; return envUser;
BufferedReader r = null;
try {
p = Runtime.getRuntime().exec(whoamiArgs);
InputStream is = p.getInputStream();
r = new BufferedReader(new InputStreamReader(is));
return r.readLine();
} catch (IOException ioe) {
LOG.error("IOException reading from `whoami`: " + ioe.toString());
return null;
} finally {
// close our stream.
if (null != r) {
try {
r.close();
} catch (IOException ioe) {
LOG.warn("IOException closing input stream from `whoami`: "
+ ioe.toString());
}
}
// wait for whoami to exit.
while (p != null) {
try {
int ret = p.waitFor();
if (0 != ret) {
LOG.error("whoami exited with error status " + ret);
// suppress original return value from this method.
return null;
}
} catch (InterruptedException ie) {
continue; // loop around.
}
}
} }
throw new RuntimeException("MySQL username not set and unable to get system user. Please set it"
+ " with '-Dsqoop.test.mysql.username=...' or USER environment variable!");
} }
public void addPasswordIfIsSet(ArrayList<String> args) { public void addPasswordIfIsSet(ArrayList<String> args) {