Skip to content

Commit

Permalink
fix spotbug warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
erilong committed Mar 23, 2020
1 parent f4d483a commit 334bad9
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 95 deletions.
Expand Up @@ -105,32 +105,32 @@ private String getLaunchDaemonsName(String wrapperName) {

private boolean runLaunchCtlCmd(ArrayList<String> cmd) {
int ret = -1;
// Run command
ProcessBuilder pb = new ProcessBuilder(cmd);
pb.redirectErrorStream(true);
System.out.println("Running " + pb.command());
Process process = null;
try {
process = pb.start();
ret = process.waitFor();
} catch(IOException|InterruptedException e) {
} catch (IOException | InterruptedException e) {
System.err.println(e.getMessage());
}

// Get standard out and error
ArrayList<String> cmdOutput = new ArrayList<String>();
try(BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
String line = null;
while ((line = reader.readLine()) != null) {
cmdOutput.add(line);

if (process != null) {
ArrayList<String> cmdOutput = new ArrayList<String>();
try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
String line = null;
while ((line = reader.readLine()) != null) {
cmdOutput.add(line);
}
} catch (Exception e) {
}
} catch (Exception e) {
}

if(cmdOutput.size() > 0) {
System.err.println(commandToString(cmd));
for(String line : cmdOutput) {
System.err.println(line);

if (cmdOutput.size() > 0) {
System.err.println(commandToString(cmd));
for (String line : cmdOutput) {
System.err.println(line);
}
}
}
return ret == 0;
Expand All @@ -140,65 +140,51 @@ private boolean runLaunchCtlCmd(ArrayList<String> cmd) {
protected boolean isPidRunning(int pid) {
boolean ret = false;

if(pid != 0) {

// Find process using ps command
if (pid != 0) {
ArrayList<String> cmd = getPsCommand(pid);
// System.out.println("Running ps command: " + cmd);

// Run ps command
ProcessBuilder pb = new ProcessBuilder(cmd);
Process process = null;
try {
process = pb.start();
process.waitFor();
// int retCode = process.waitFor();
// System.out.println("Return code from ps command: " + retCode);
} catch(IOException|InterruptedException e) {
} catch (IOException | InterruptedException e) {
System.err.println(e.getMessage());
}
// Get standard out and error
ArrayList<String> cmdOutput = new ArrayList<String>();
ArrayList<String> cmdError = new ArrayList<String>();
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = null;
while ((line = reader.readLine()) != null) {
cmdOutput.add(line);

if (process != null) {
ArrayList<String> cmdOutput = new ArrayList<String>();
ArrayList<String> cmdError = new ArrayList<String>();
try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
String line = null;
while ((line = reader.readLine()) != null) {
cmdOutput.add(line);
}
} catch (Exception e) {
}
reader.close();
reader = new BufferedReader(new InputStreamReader(process.getErrorStream()));
while ((line = reader.readLine()) != null) {
cmdError.add(line);

try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getErrorStream()))) {
String line = null;
while ((line = reader.readLine()) != null) {
cmdError.add(line);
}
} catch (Exception e) {
}
} catch (Exception e) {
} finally {
if(reader != null) {
try {
reader.close();
} catch(Exception e) { }
}
}

// System.out.println("Output: " + cmdOutput);

for(String line : cmdOutput) {
if(line.contains(config.getJavaCommand())) {
ret = true;
break;

for (String line : cmdOutput) {
if (line.contains(config.getJavaCommand())) {
ret = true;
break;
}
}
}
if(cmdError.size() > 0) {
System.err.println(commandToString(cmd));
for(String line : cmdError) {
System.err.println(line);
if (cmdError.size() > 0) {
System.err.println(commandToString(cmd));
for (String line : cmdError) {
System.err.println(line);
}
throw new WrapperException(Constants.RC_FAIL_EXECUTION, 8, "Failed isPidRunning");
}
throw new WrapperException(Constants.RC_FAIL_EXECUTION, 8, "Failed isPidRunning");
}
}
// System.out.println("PID " + pid + (ret ? " is " : " is not " ) + "running.");
return ret;
}

Expand Down
Expand Up @@ -392,18 +392,20 @@ private int getuid(String login) {
System.err.println(e.getMessage());
e.printStackTrace();
}
ArrayList<String> cmdOutput = new ArrayList<String>();
try(BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
String line = null;
while ((line = reader.readLine()) != null) {
cmdOutput.add(line);
if (process != null) {
ArrayList<String> cmdOutput = new ArrayList<String>();
try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
String line = null;
while ((line = reader.readLine()) != null) {
cmdOutput.add(line);
}
} catch (Exception e) {
e.printStackTrace();
throw new WrapperException(Constants.RC_FAIL_EXECUTION, 0, "Unable to read from command: " + cmd, e);
}
if (cmdOutput != null && cmdOutput.size() > 0) {
ret = Integer.parseInt(cmdOutput.get(0));
}
} catch (Exception e) {
e.printStackTrace();
throw new WrapperException(Constants.RC_FAIL_EXECUTION, 0, "Unable to read from command: " + cmd, e);
}
if(cmdOutput != null && cmdOutput.size() > 0) {
ret = Integer.parseInt(cmdOutput.get(0));
}
return ret;
}
Expand Down Expand Up @@ -444,7 +446,6 @@ protected void stopProcesses(boolean isStopAbandoned) {

private boolean runServiceCommand(ArrayList<String> cmd) {
int ret = -1;
// Run command
ProcessBuilder pb = new ProcessBuilder(cmd);
pb.redirectErrorStream(true);
System.out.println("Running " + pb.command());
Expand All @@ -456,21 +457,23 @@ private boolean runServiceCommand(ArrayList<String> cmd) {
System.err.println(e.getMessage());
}

// Get standard out and error
ArrayList<String> cmdOutput = new ArrayList<String>();
try(BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
String line = null;
while ((line = reader.readLine()) != null) {
cmdOutput.add(line);
if (process != null) {
ArrayList<String> cmdOutput = new ArrayList<String>();
try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
String line = null;
while ((line = reader.readLine()) != null) {
cmdOutput.add(line);
}
} catch (Exception e) {
throw new WrapperException(Constants.RC_FAIL_EXECUTION, 0,
"Unable to read from service command: " + cmd, e);
}
} catch (Exception e) {
throw new WrapperException(Constants.RC_FAIL_EXECUTION, 0, "Unable to read from service command: " + cmd, e);
}

if(cmdOutput.size() > 0) {
System.err.println(commandToString(cmd));
for(String line : cmdOutput) {
System.err.println(line);

if (cmdOutput.size() > 0) {
System.err.println(commandToString(cmd));
for (String line : cmdOutput) {
System.err.println(line);
}
}
}
return ret == 0;
Expand Down
Expand Up @@ -114,9 +114,12 @@ protected static String getParentDir(String filepath) {
protected static String findConfigFile(String dirpath) {
File dir = new File(dirpath);
if (dir.exists() && dir.isDirectory()) {
for (String name : dir.list()) {
if (name.endsWith("_service.conf")) {
return dirpath + File.separator + name;
String[] files = dir.list();
if (files != null) {
for (String name : files) {
if (name.endsWith("_service.conf")) {
return dirpath + File.separator + name;
}
}
}
}
Expand Down
Expand Up @@ -222,11 +222,13 @@ public boolean accept(File dir, String name) {
}
});
StringBuilder sb = new StringBuilder();
for (int i = 0; i < files.length; i++) {
if (i > 0) {
sb.append(File.pathSeparator);
if (files != null) {
for (int i = 0; i < files.length; i++) {
if (i > 0) {
sb.append(File.pathSeparator);
}
sb.append(dirName).append(files[i].getName());
}
sb.append(dirName).append(files[i].getName());
}
classPath = sb.toString();
}
Expand Down
Expand Up @@ -27,7 +27,7 @@

public class WrapperLogFormatter extends Formatter {

protected static final SimpleDateFormat DATE_FORMATTER = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
protected final SimpleDateFormat DATE_FORMATTER = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

protected static final String NEWLINE = System.getProperty("line.separator");

Expand Down

0 comments on commit 334bad9

Please sign in to comment.