use of org.jboss.vfs.spi.MountHandle in project wildfly-core by wildfly.
the class DeployArchiveCommand method buildRequest.
/**
* null attachments means that the command is in a batch, non null means
* command executed.
*
* Inside a batch, the attachments must be added to the existing batch and
* NOT to the temporary batch created to build the composite request.
* Outside of a batch, the attachments MUST be added to the passed non null
* attachments.
*
* @param context
* @param attachments
* @return
* @throws CommandFormatException
*/
@Override
public ModelNode buildRequest(CommandContext context, Attachments attachments) throws CommandFormatException {
CommandContext ctx = context;
TempFileProvider tempFileProvider;
MountHandle root;
try {
String name = "cli-" + System.currentTimeMillis();
tempFileProvider = TempFileProvider.create(name, Executors.newSingleThreadScheduledExecutor((r) -> new Thread(r, "CLI (un)deploy archive tempFile")), true);
root = extractArchive(file, tempFileProvider, name);
} catch (IOException e) {
e.printStackTrace();
throw new OperationFormatException("Unable to extract archive '" + file.getAbsolutePath() + "' to temporary location");
}
Consumer<Attachments> cl = (a) -> {
VFSUtils.safeClose(root, tempFileProvider);
};
if (attachments != null) {
attachments.addConsumer(cl);
}
final File currentDir = ctx.getCurrentDir();
ctx.setCurrentDir(root.getMountSource());
String holdbackBatch = activateNewBatch(ctx);
try {
if (script == null) {
script = getDefaultScript();
}
File scriptFile = new File(ctx.getCurrentDir(), script);
if (!scriptFile.exists()) {
throw new CommandFormatException("ERROR: script '" + script + "' not found.");
}
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(scriptFile));
String line = reader.readLine();
while (!ctx.isTerminated() && line != null) {
context.handle(line);
line = reader.readLine();
}
} catch (FileNotFoundException e) {
throw new CommandFormatException("ERROR: script '" + script + "' not found.");
} catch (IOException e) {
throw new CommandFormatException("Failed to read the next command from " + scriptFile.getName() + ": " + e.getMessage(), e);
} catch (CommandLineException ex) {
throw new CommandFormatException(ex);
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
}
}
}
return ctx.getBatchManager().getActiveBatch().toRequest();
} catch (CommandFormatException cfex) {
cl.accept(attachments);
throw cfex;
} finally {
// reset current dir in context
ctx.setCurrentDir(currentDir);
discardBatch(ctx, holdbackBatch, attachments, cl);
}
}
Aggregations