mirror of
https://github.com/apache/sqoop.git
synced 2025-05-02 20:39:58 +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:
parent
f7b460b3f5
commit
3153c3610d
@ -18,15 +18,11 @@
|
||||
|
||||
package org.apache.sqoop.manager.mysql;
|
||||
|
||||
import org.apache.sqoop.SqoopOptions;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.apache.commons.logging.Log;
|
||||
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;
|
||||
|
||||
/**
|
||||
@ -95,50 +91,19 @@ public String[] addUserNameAndPasswordToArgs(String[] extraArgs) {
|
||||
return moreArgs;
|
||||
}
|
||||
|
||||
public static String getCurrentUser() {
|
||||
private static String getCurrentUser() {
|
||||
// First, check the $USER environment variable.
|
||||
String envUser = System.getenv("USER");
|
||||
if (null != envUser) {
|
||||
return envUser;
|
||||
}
|
||||
// Try `whoami`
|
||||
String[] whoamiArgs = new String[1];
|
||||
whoamiArgs[0] = "whoami";
|
||||
Process p = null;
|
||||
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.
|
||||
}
|
||||
}
|
||||
|
||||
// Fall back to user.name system property
|
||||
envUser = System.getProperty("user.name");
|
||||
if (null != envUser) {
|
||||
return envUser;
|
||||
}
|
||||
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) {
|
||||
|
Loading…
Reference in New Issue
Block a user