Skip to content

Archive helper #1367

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 20 commits into from
Jan 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@
<groupId>org.yaml</groupId>
<artifactId>snakeyaml</artifactId>
</dependency>
<dependency>
<groupId>info.picocli</groupId>
<artifactId>picocli</artifactId>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
Expand All @@ -48,6 +52,11 @@
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.easymock</groupId>
<artifactId>easymock</artifactId>
Expand Down Expand Up @@ -94,7 +103,7 @@
<configuration>
<archive>
<manifestEntries>
<Class-Path>antlr4-runtime-${antlr.version}.jar snakeyaml-${snakeyaml.version}.jar</Class-Path>
<Class-Path>antlr4-runtime-${antlr.version}.jar snakeyaml-${snakeyaml.version}.jar picocli-${picocli.version}.jar</Class-Path>
</manifestEntries>
</archive>
</configuration>
Expand Down
117 changes: 117 additions & 0 deletions core/src/main/java/oracle/weblogic/deploy/tool/ArchiveHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
/*
* Copyright (c) 2023, Oracle and/or its affiliates.
* Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
*/
package oracle.weblogic.deploy.tool;

import java.io.PrintWriter;

import oracle.weblogic.deploy.logging.PlatformLogger;
import oracle.weblogic.deploy.logging.WLSDeployLogFactory;

import oracle.weblogic.deploy.tool.archive_helper.ArchiveHelperVersionProvider;
import oracle.weblogic.deploy.tool.archive_helper.CommandResponse;
import oracle.weblogic.deploy.tool.archive_helper.add.AddCommand;
import oracle.weblogic.deploy.tool.archive_helper.extract.ExtractCommand;
import oracle.weblogic.deploy.tool.archive_helper.list.ListCommand;
import oracle.weblogic.deploy.tool.archive_helper.remove.RemoveCommand;
import oracle.weblogic.deploy.util.ExitCode;

import picocli.CommandLine;
import picocli.CommandLine.Command;
import picocli.CommandLine.IParameterExceptionHandler;
import picocli.CommandLine.Option;
import picocli.CommandLine.ParameterException;
import picocli.CommandLine.ParseResult;
import picocli.CommandLine.UnmatchedArgumentException;

@Command(
name = "archiveHelper",
description = "%nA tool to create and modify a WebLogic Deploy Tooling archive file.%n",
commandListHeading = "%nCommands:%n",
subcommands = {
AddCommand.class,
ExtractCommand.class,
ListCommand.class,
RemoveCommand.class
},
sortOptions = false,
versionProvider = ArchiveHelperVersionProvider.class
)
public class ArchiveHelper {
public static final String LOGGER_NAME = "wlsdeploy.tool.archive-helper";
private static final String CLASS = ArchiveHelper.class.getName();
private static final PlatformLogger LOGGER = WLSDeployLogFactory.getLogger(LOGGER_NAME);

@Option(
names = { "-help" },
description = "Get help for the archiveHelper tool",
usageHelp = true
)
private static boolean helpRequested = false;

@Option(
names = { "-version" },
description = "Get the WebLogic Deploy Tooling version",
versionHelp = true
)
private static boolean versionRequested = false;

@SuppressWarnings("java:S106")
public static void main(String[] args) {
final String METHOD = "main";
LOGGER.entering(CLASS, METHOD, (Object[]) args);

int exitCode = executeCommand(new PrintWriter(System.out, true),
new PrintWriter(System.err, true), args);

LOGGER.exiting(CLASS, METHOD, exitCode);
System.exit(exitCode);
}

static int executeCommand(PrintWriter out, PrintWriter err, String... args) {
CommandLine cmd = new CommandLine(ArchiveHelper.class)
.setCaseInsensitiveEnumValuesAllowed(true)
.setToggleBooleanFlags(false)
.setUnmatchedArgumentsAllowed(false)
.setTrimQuotes(true)
.setParameterExceptionHandler(new ArgParsingExceptionHandler())
.setOut(out)
.setErr(err);

int exitCode = cmd.execute(args);
if (exitCode != ExitCode.USAGE_ERROR) {
CommandLine commandLine = getResponseCommandLine(cmd);
CommandResponse response = commandLine.getExecutionResult();
if (response != null) {
exitCode = response.getStatus();
response.printMessages(out, err);
}
}
return exitCode;
}

private static CommandLine getResponseCommandLine(CommandLine commandLine) {
CommandLine result = commandLine;

ParseResult parseResult = commandLine.getParseResult().subcommand();
if (parseResult != null) {
result = getResponseCommandLine(parseResult.commandSpec().commandLine());
}
return result;
}

static class ArgParsingExceptionHandler implements IParameterExceptionHandler {
@Override
public int handleParseException(ParameterException ex, String[] args) {
CommandLine cmd = ex.getCommandLine();
PrintWriter writer = cmd.getErr();

writer.println(ex.getMessage());
UnmatchedArgumentException.printSuggestions(ex, writer);
ex.getCommandLine().usage(writer, cmd.getColorScheme());

return ExitCode.USAGE_ERROR;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
* Copyright (c) 2023, Oracle Corporation and/or its affiliates. All rights reserved.
* Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
*/
package oracle.weblogic.deploy.tool.archive_helper;

import oracle.weblogic.deploy.exception.BundleAwareException;
import oracle.weblogic.deploy.exception.ExceptionHelper;

public class ArchiveHelperException extends BundleAwareException {
private static final long serialVersionUID = 1L;

private final int exitCode;

/**
* Constructs a default exception with exit code of 2.
*/
public ArchiveHelperException() {
this.exitCode = 2;
}

/**
* Construct a default exception with specified exit code
* @param exitCode the exit code to use
*/
public ArchiveHelperException(int exitCode) {
this.exitCode = exitCode;
}

/**
* Constructs a new exception with the specified message id.
*
* @param exitCode the exit code to use
* @param messageID the message ID
*/
public ArchiveHelperException(int exitCode, String messageID) {
super(messageID);
this.exitCode = exitCode;
}

/**
* Constructs a new exception with the specified message id and parameters.
*
* @param exitCode the exit code to use
* @param messageID the message ID
* @param params the parameters to use to fill in the message tokens
*/
public ArchiveHelperException(int exitCode, String messageID, Object... params) {
super(messageID, params);
this.exitCode = exitCode;
}

/**
* Constructs a new exception with the specified message id and cause.
*
* @param exitCode the exit code to use
* @param messageID the message ID
* @param cause the exception that triggered the creation of this exception
*/
public ArchiveHelperException(int exitCode, String messageID, Throwable cause) {
super(messageID, cause);
this.exitCode = exitCode;
}

/**
* Constructs a new exception with passed message id, cause, and parameters.
*
* @param exitCode the exit code to use
* @param messageID the message ID
* @param cause the exception that triggered the creation of this exception
* @param params the parameters to use to fill in the message tokens
*/
public ArchiveHelperException(int exitCode, String messageID, Throwable cause, Object... params) {
super(messageID, cause, params);
this.exitCode = exitCode;
}

/**
* Constructs a new exception with the specified cause.
*
* @param exitCode the exit code to use
* @param cause the exception that triggered the creation of this exception
*/
public ArchiveHelperException(int exitCode, Throwable cause) {
super(cause);
this.exitCode = exitCode;
}

/**
* {@inheritDoc}
*/
@Override
public String getBundleName() {
return ExceptionHelper.getResourceBundleName();
}

/**
* Get the exit code associated with this exception.
*
* @return the exit code associated with this exception
*/
public int getExitCode() {
return this.exitCode;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*
* Copyright (c) 2023, Oracle and/or its affiliates.
* Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
*/
package oracle.weblogic.deploy.tool.archive_helper;

import oracle.weblogic.deploy.util.WebLogicDeployToolingVersion;

import picocli.CommandLine.IVersionProvider;

public class ArchiveHelperVersionProvider implements IVersionProvider {
@Override
public String[] getVersion() {
return new String[] {
"WebLogic Deploy Tooling version: " + WebLogicDeployToolingVersion.getVersion(),
"Build: " + WebLogicDeployToolingVersion.getBuildRevision() + ":" +
WebLogicDeployToolingVersion.getBuildTimestamp()
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright (c) 2023, Oracle and/or its affiliates.
* Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
*/
package oracle.weblogic.deploy.tool.archive_helper;

import java.io.PrintWriter;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;

public class CommandResponse {
private static final ResourceBundle RESOURCE_BUNDLE =
ResourceBundle.getBundle("oracle.weblogic.deploy.messages.wlsdeploy_rb");

private int status;
private final List<String> messages = new ArrayList<>();
private final List<Object[]> messageParamsList = new ArrayList<>();

public CommandResponse(int status) {
this.status = status;
}

public CommandResponse(int status, String message, Object... messageParams) {
this.status = status;
this.messages.add(message);
this.messageParamsList.add(messageParams);
}

public int getStatus() {
return status;
}

public void setStatus(int status) {
this.status = status;
}

public String[] getMessages() {
String[] formattedMessages = new String[this.messages.size()];

for (int index = 0; index < this.messages.size(); index++) {
String message = this.messages.get(index);
if (RESOURCE_BUNDLE.containsKey(message)) {
message = MessageFormat.format(RESOURCE_BUNDLE.getString(message), this.messageParamsList.get(index));
}
formattedMessages[index] = message;
}
return formattedMessages;
}

public void addMessage(String message, Object... messageParams) {
this.messages.add(message);
this.messageParamsList.add(messageParams);
}

public void addMessages(List<String> messages) {
this.messages.addAll(messages);
for (int i = 0; i < messages.size(); i++) {
this.messageParamsList.add(new Object[0]);
}
}

public void printMessages(PrintWriter out, PrintWriter err) {
PrintWriter location = out;
if (status != 0) {
location = err;
}

for (String message : getMessages()) {
location.println(message);
}
}
}
Loading