Skip to content

Commit

Permalink
Simple deduplication of code
Browse files Browse the repository at this point in the history
  • Loading branch information
fredericBregier committed Oct 10, 2019
1 parent d95abf9 commit 30a439a
Showing 1 changed file with 100 additions and 135 deletions.
235 changes: 100 additions & 135 deletions src/main/java/org/waarp/vitam/common/waarp/ManagerToWaarpScript.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,84 +83,12 @@ public boolean sendBackInformation(
commandLine.addArgument(filename);
commandLine.addArgument(fileInfo);

final DefaultExecutor defaultExecutor = new DefaultExecutor();
ByteArrayOutputStream outputStream;
outputStream = new ByteArrayOutputStream();
final PumpStreamHandler pumpStreamHandler =
new PumpStreamHandler(outputStream);
defaultExecutor.setStreamHandler(pumpStreamHandler);
final int[] correctValues = { 0, 1 };
defaultExecutor.setExitValues(correctValues);
int status = -1;
try {
// Execute the command
status = defaultExecutor.execute(commandLine);//NOSONAR
} catch (final ExecuteException e) {
if (e.getExitValue() == -559038737) {
// Cannot run immediately so retry once
try {
Thread.sleep(10);
} catch (final InterruptedException e1) {//NOSONAR
SysErrLogger.FAKE_LOGGER.ignoreLog(e1);
}
try {
status = defaultExecutor.execute(commandLine);//NOSONAR
} catch (final ExecuteException e1) {
try {
pumpStreamHandler.stop();
} catch (final IOException ignored) {
// nothing
}
logger.error(
EXCEPTION + e.getMessage() + EXEC_IN_ERROR_WITH + commandLine);
FileUtils.close(outputStream);
return false;
} catch (final IOException e1) {
try {
pumpStreamHandler.stop();
} catch (final IOException ignored) {
// nothing
}
logger.error(
EXCEPTION + e.getMessage() + EXEC_IN_ERROR_WITH + commandLine);
FileUtils.close(outputStream);
return false;
}
} else {
try {
pumpStreamHandler.stop();
} catch (final IOException ignored) {
// nothing
}
logger.error(
EXCEPTION + e.getMessage() + EXEC_IN_ERROR_WITH + commandLine);
FileUtils.close(outputStream);
return false;
}
} catch (final IOException e) {
try {
pumpStreamHandler.stop();
} catch (final IOException ignored) {
// nothing
}
logger
.error(EXCEPTION + e.getMessage() + EXEC_IN_ERROR_WITH + commandLine);
FileUtils.close(outputStream);
StatusIdResult statusIdResult = new StatusIdResult(commandLine).invoke();
if (statusIdResult.isKO()) {
return false;
}
try {
pumpStreamHandler.stop();
} catch (final IOException ignored) {
// nothing
}
String response;
try {
response = outputStream.toString(WaarpStringUtils.UTF8.name());
} catch (final UnsupportedEncodingException e) {
response = outputStream.toString();
}
long waarpId = Long.parseLong(response);
FileUtils.close(outputStream);
int status = statusIdResult.getStatus();
long waarpId = statusIdResult.getWaarpId();
if (status == 0) {
ingestRequest.setWaarpId(waarpId);
ingestRequest.save(ingestRequestFactory);
Expand Down Expand Up @@ -188,39 +116,88 @@ public boolean sendBackInformation(final DipRequestFactory dipRequestFactory,
commandLine.addArgument(filename);
commandLine.addArgument(fileInfo);

final DefaultExecutor defaultExecutor = new DefaultExecutor();
ByteArrayOutputStream outputStream;
outputStream = new ByteArrayOutputStream();
final PumpStreamHandler pumpStreamHandler =
new PumpStreamHandler(outputStream);
defaultExecutor.setStreamHandler(pumpStreamHandler);
final int[] correctValues = { 0, 1 };
defaultExecutor.setExitValues(correctValues);
int status = -1;
try {
// Execute the command
status = defaultExecutor.execute(commandLine);//NOSONAR
} catch (final ExecuteException e) {
if (e.getExitValue() == -559038737) {
// Cannot run immediately so retry once
try {
Thread.sleep(10);
} catch (final InterruptedException e1) {//NOSONAR
SysErrLogger.FAKE_LOGGER.ignoreLog(e1);
}
try {
status = defaultExecutor.execute(commandLine);//NOSONAR
} catch (final ExecuteException e1) {
StatusIdResult statusIdResult = new StatusIdResult(commandLine).invoke();
if (statusIdResult.isKO()) {
return false;
}
int status = statusIdResult.getStatus();
long waarpId = statusIdResult.getWaarpId();
if (status == 0) {
dipRequest.setWaarpId(waarpId);
dipRequest.save(dipRequestFactory);
}
return status == 0;
}

private class StatusIdResult {
private final CommandLine commandLine;
private boolean myResult;
private int status;
private long waarpId;

public StatusIdResult(final CommandLine commandLine) {
this.commandLine = commandLine;
}

boolean isKO() {
return myResult;
}

public int getStatus() {
return status;
}

public long getWaarpId() {
return waarpId;
}

public StatusIdResult invoke() {
final DefaultExecutor defaultExecutor = new DefaultExecutor();
ByteArrayOutputStream outputStream;
outputStream = new ByteArrayOutputStream();
final PumpStreamHandler pumpStreamHandler =
new PumpStreamHandler(outputStream);
defaultExecutor.setStreamHandler(pumpStreamHandler);
final int[] correctValues = { 0, 1 };
defaultExecutor.setExitValues(correctValues);
status = -1;
try {
// Execute the command
status = defaultExecutor.execute(commandLine);//NOSONAR
} catch (final ExecuteException e) {
if (e.getExitValue() == -559038737) {
// Cannot run immediately so retry once
try {
pumpStreamHandler.stop();
} catch (final IOException ignored) {
// nothing
Thread.sleep(10);
} catch (final InterruptedException e1) {//NOSONAR
SysErrLogger.FAKE_LOGGER.ignoreLog(e1);
}
logger.error(
EXCEPTION + e.getMessage() + EXEC_IN_ERROR_WITH + commandLine);
FileUtils.close(outputStream);
return false;
} catch (final IOException e1) {
try {
status = defaultExecutor.execute(commandLine);//NOSONAR
} catch (final ExecuteException e1) {
try {
pumpStreamHandler.stop();
} catch (final IOException ignored) {
// nothing
}
logger.error(
EXCEPTION + e.getMessage() + EXEC_IN_ERROR_WITH + commandLine);
FileUtils.close(outputStream);
myResult = true;
return this;
} catch (final IOException e1) {
try {
pumpStreamHandler.stop();
} catch (final IOException ignored) {
// nothing
}
logger.error(
EXCEPTION + e.getMessage() + EXEC_IN_ERROR_WITH + commandLine);
FileUtils.close(outputStream);
myResult = true;
return this;
}
} else {
try {
pumpStreamHandler.stop();
} catch (final IOException ignored) {
Expand All @@ -229,48 +206,36 @@ public boolean sendBackInformation(final DipRequestFactory dipRequestFactory,
logger.error(
EXCEPTION + e.getMessage() + EXEC_IN_ERROR_WITH + commandLine);
FileUtils.close(outputStream);
return false;
myResult = true;
return this;
}
} else {
} catch (final IOException e) {
try {
pumpStreamHandler.stop();
} catch (final IOException ignored) {
// nothing
}
logger.error(
EXCEPTION + e.getMessage() + EXEC_IN_ERROR_WITH + commandLine);
logger
.error(EXCEPTION + e.getMessage() + EXEC_IN_ERROR_WITH + commandLine);
FileUtils.close(outputStream);
return false;
myResult = true;
return this;
}
} catch (final IOException e) {
try {
pumpStreamHandler.stop();
} catch (final IOException ignored) {
// nothing
}
logger
.error(EXCEPTION + e.getMessage() + EXEC_IN_ERROR_WITH + commandLine);
String response;
try {
response = outputStream.toString(WaarpStringUtils.UTF8.name());
} catch (final UnsupportedEncodingException e) {
response = outputStream.toString();
}
waarpId = Long.parseLong(response);
FileUtils.close(outputStream);
return false;
}
try {
pumpStreamHandler.stop();
} catch (final IOException ignored) {
// nothing
}
String response;
try {
response = outputStream.toString(WaarpStringUtils.UTF8.name());
} catch (final UnsupportedEncodingException e) {
response = outputStream.toString();
myResult = false;
return this;
}
long waarpId = Long.parseLong(response);
FileUtils.close(outputStream);
if (status == 0) {
dipRequest.setWaarpId(waarpId);
dipRequest.save(dipRequestFactory);
}
return status == 0;
}

}

0 comments on commit 30a439a

Please sign in to comment.