
org.ow2.petals.cli.shell.command.Undeploy Maven / Gradle / Ivy
/**
* Copyright (c) 2010-2012 EBM WebSourcing, 2012-2016 Linagora
*
* This program/library is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 2.1 of the License, or (at your
* option) any later version.
*
* This program/library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
* for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program/library; If not, see http://www.gnu.org/licenses/
* for the GNU Lesser General Public License version 2.1.
*/
package org.ow2.petals.cli.shell.command;
import static org.ow2.petals.cli.Constants.CommonOption.URL_OPTION;
import static org.ow2.petals.cli.Constants.CommonOption.URL_SHORT_OPTION;
import java.io.File;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Map;
import org.apache.commons.cli.AlreadySelectedException;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.MissingArgumentException;
import org.apache.commons.cli.MissingOptionException;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.apache.commons.cli.UnrecognizedOptionException;
import org.ow2.petals.admin.api.ArtifactAdministration;
import org.ow2.petals.admin.api.PetalsAdministrationFactory;
import org.ow2.petals.admin.api.exception.ArtifactAdministrationException;
import org.ow2.petals.admin.api.exception.DuplicatedServiceException;
import org.ow2.petals.admin.api.exception.MissingServiceException;
import org.ow2.petals.cli.Constants;
import org.ow2.petals.cli.api.command.AbstractCommand;
import org.ow2.petals.cli.api.command.CommandUtil;
import org.ow2.petals.cli.api.command.exception.CommandBadArgumentNumberException;
import org.ow2.petals.cli.api.command.exception.CommandException;
import org.ow2.petals.cli.api.command.exception.CommandInvalidException;
import org.ow2.petals.cli.api.command.exception.CommandMissingArgumentException;
import org.ow2.petals.cli.api.command.exception.CommandMissingOptionsException;
import org.ow2.petals.cli.api.command.exception.CommandMissingYesFlagException;
import org.ow2.petals.cli.api.command.exception.CommandTooManyArgumentsException;
import org.ow2.petals.cli.api.shell.exception.ShellException;
import org.ow2.petals.cli.shell.completer.AllArtifactNameCompleter;
import org.ow2.petals.cli.shell.util.Utils;
import jline.console.completer.Completer;
import jline.console.completer.FileNameCompleter;
/**
* This command undeploy an artifact from a petals esb.
* @author Sebastien Andre - EBM WebSourcing
*/
public class Undeploy extends AbstractCommand implements ArtifactBasedCommand, ArtifactVersionBasedCommand {
/**
* The bulk option
*/
public final static String BULK_SHORT_OPTION = Constants.CommonOption.BULK_SHORT_OPTION;
private final static String BULK_LONG_OPTION = Constants.CommonOption.BULK_LONG_OPTION;
protected static final Option BULK_OPTION = Option.builder(BULK_SHORT_OPTION).longOpt(BULK_LONG_OPTION)
.numberOfArgs(1).optionalArg(true).argName("artifacts-directory")
.desc("Stop and uninstall or undeploy JBI artifacts").build();
/**
* The confirmation skip option
*/
public final static String YESFLAG_SHORT_OPTION = "y";
private static final Option YESFLAG_OPTION = Option.builder(YESFLAG_SHORT_OPTION).hasArg(false)
.desc("A flag to skip confirmation.").build();
public final static String UNDEPLOY_ALL_CONFIRMATION_MSG = "Are you sure you want to undeploy all artifacts? (y/n) ";
private final ArtifactAdministration artifactAdministration;
public Undeploy() throws CommandException {
super();
this.setUsage(CommandUtil.formatCommandUsage(this));
this.setDescription("Stop and uninstall or undeploy JBI artifacts");
this.setOptionsDescription(CommandUtil.formatCommandOptionsDescription(this));
this.completers.put(URL_SHORT_OPTION, new FileNameCompleter());
this.completers.put(TYPE_SHORT_OPTION, ARTIFACT_TYPE_COMPLETER);
try {
this.artifactAdministration = PetalsAdministrationFactory.getInstance().newPetalsAdministrationAPI()
.newArtifactAdministration();
} catch (final DuplicatedServiceException e) {
throw new CommandException(this, e);
} catch (final MissingServiceException e) {
throw new CommandException(this, e);
}
}
@Override
public void doExecute(final String[] args) throws CommandException {
final CommandLineParser parser = new DefaultParser();
try {
final CommandLine cl = parser.parse(getOptions(), args, true);
if (cl.hasOption(BULK_SHORT_OPTION) && !cl.hasOption(URL_SHORT_OPTION)
&& !cl.hasOption(ARTIFACT_SHORT_OPTION)) {
final String directory = cl.getOptionValue(BULK_SHORT_OPTION);
if (directory != null && !directory.isEmpty()) {
final URL url = Utils.toURL(directory);
if (url.getProtocol().equals("file")) {
final File filePath = new File(url.toURI());
this.artifactAdministration.stopAndUndeployAllArtifacts(Utils
.getUrls(filePath));
} else {
throw new CommandException(this,
"Directory undeployment is only allowed for file protocol");
}
} else {
// Undeploy all artifacts of the container
if (!cl.hasOption(YESFLAG_SHORT_OPTION)) {
if (this.getShell().confirms(UNDEPLOY_ALL_CONFIRMATION_MSG)) {
this.artifactAdministration.stopAndUndeployAllArtifacts(null);
} else if (!this.getShell().isInteractive()) {
throw new CommandMissingYesFlagException(this, YESFLAG_OPTION);
}
} else {
this.artifactAdministration.stopAndUndeployAllArtifacts(null);
}
}
} else if (cl.hasOption(URL_SHORT_OPTION) && !cl.hasOption(BULK_SHORT_OPTION)
&& !cl.hasOption(ARTIFACT_SHORT_OPTION)) {
if (!this.checkArguments(args, 2)) {
throw new CommandBadArgumentNumberException(this);
}
for (String arg : cl.getOptionValues(URL_SHORT_OPTION)) {
this.artifactAdministration.stopAndUndeployArtifact(Utils.toURL(arg));
}
} else if (cl.hasOption(ARTIFACT_SHORT_OPTION) && !cl.hasOption(URL_SHORT_OPTION)
&& !cl.hasOption(BULK_SHORT_OPTION)) {
if (!this.checkArguments(args, 2, 5)) {
throw new CommandTooManyArgumentsException(this, args);
}
this.artifactAdministration.stopAndUndeployArtifact(cl.getOptionValue(TYPE_SHORT_OPTION),
cl.getOptionValue(ARTIFACT_SHORT_OPTION), cl.getOptionValue(VERSION_SHORT_OPTION));
} else {
throw new CommandBadArgumentNumberException(this);
}
} catch (final UnrecognizedOptionException e) {
throw new CommandBadArgumentNumberException(this, e);
} catch (final MissingArgumentException e) {
throw new CommandMissingArgumentException(this, e.getOption(), e);
} catch (final MissingOptionException e) {
throw new CommandMissingOptionsException(this, e.getMissingOptions(), e);
} catch (final AlreadySelectedException e) {
throw new CommandBadArgumentNumberException(this, e);
} catch (final ParseException e) {
throw new CommandInvalidException(this, e);
} catch (final ArtifactAdministrationException e) {
throw new CommandException(this, e);
} catch (final MalformedURLException e) {
throw new CommandException(this, e);
} catch (final ShellException e) {
throw new CommandException(this, e);
} catch (IOException e) {
throw new CommandException(this, e);
} catch (URISyntaxException e) {
throw new CommandException(this, e);
}
}
@Override
public Options createOptions() {
final Options options = new Options();
options.addOption(BULK_OPTION);
options.addOption(ARTIFACT_OPTION);
options.addOption(TYPE_OPTION);
options.addOption(VERSION_OPTION);
options.addOption(URL_OPTION);
options.addOption(YESFLAG_OPTION);
return options;
}
@Override
public Map getOptionCompleters() {
this.completers.put(ARTIFACT_SHORT_OPTION, new AllArtifactNameCompleter(
this.artifactAdministration));
return this.completers;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy