Skip to content

Commit 5e1b985

Browse files
Archive helper (#1367)
* experiment--do not use * intermediate checkpoint * intermediate checkpoint * unit tests for list command * intermediate checkpoint * remove first pass * remove first pass * completing archiveHelper Java code * first cut at shell scripts * fixing Windows script issue * cleaning up Sonar code smells * adding version support for archiveHelper * review feedback
1 parent 086c3d1 commit 5e1b985

File tree

116 files changed

+18077
-517
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

116 files changed

+18077
-517
lines changed

core/pom.xml

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@
3232
<groupId>org.yaml</groupId>
3333
<artifactId>snakeyaml</artifactId>
3434
</dependency>
35+
<dependency>
36+
<groupId>info.picocli</groupId>
37+
<artifactId>picocli</artifactId>
38+
</dependency>
3539
<dependency>
3640
<groupId>javax.xml.bind</groupId>
3741
<artifactId>jaxb-api</artifactId>
@@ -48,6 +52,11 @@
4852
<artifactId>junit-jupiter-engine</artifactId>
4953
<scope>test</scope>
5054
</dependency>
55+
<dependency>
56+
<groupId>org.junit.jupiter</groupId>
57+
<artifactId>junit-jupiter-params</artifactId>
58+
<scope>test</scope>
59+
</dependency>
5160
<dependency>
5261
<groupId>org.easymock</groupId>
5362
<artifactId>easymock</artifactId>
@@ -94,7 +103,7 @@
94103
<configuration>
95104
<archive>
96105
<manifestEntries>
97-
<Class-Path>antlr4-runtime-${antlr.version}.jar snakeyaml-${snakeyaml.version}.jar</Class-Path>
106+
<Class-Path>antlr4-runtime-${antlr.version}.jar snakeyaml-${snakeyaml.version}.jar picocli-${picocli.version}.jar</Class-Path>
98107
</manifestEntries>
99108
</archive>
100109
</configuration>
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/*
2+
* Copyright (c) 2023, Oracle and/or its affiliates.
3+
* Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
4+
*/
5+
package oracle.weblogic.deploy.tool;
6+
7+
import java.io.PrintWriter;
8+
9+
import oracle.weblogic.deploy.logging.PlatformLogger;
10+
import oracle.weblogic.deploy.logging.WLSDeployLogFactory;
11+
12+
import oracle.weblogic.deploy.tool.archive_helper.ArchiveHelperVersionProvider;
13+
import oracle.weblogic.deploy.tool.archive_helper.CommandResponse;
14+
import oracle.weblogic.deploy.tool.archive_helper.add.AddCommand;
15+
import oracle.weblogic.deploy.tool.archive_helper.extract.ExtractCommand;
16+
import oracle.weblogic.deploy.tool.archive_helper.list.ListCommand;
17+
import oracle.weblogic.deploy.tool.archive_helper.remove.RemoveCommand;
18+
import oracle.weblogic.deploy.util.ExitCode;
19+
20+
import picocli.CommandLine;
21+
import picocli.CommandLine.Command;
22+
import picocli.CommandLine.IParameterExceptionHandler;
23+
import picocli.CommandLine.Option;
24+
import picocli.CommandLine.ParameterException;
25+
import picocli.CommandLine.ParseResult;
26+
import picocli.CommandLine.UnmatchedArgumentException;
27+
28+
@Command(
29+
name = "archiveHelper",
30+
description = "%nA tool to create and modify a WebLogic Deploy Tooling archive file.%n",
31+
commandListHeading = "%nCommands:%n",
32+
subcommands = {
33+
AddCommand.class,
34+
ExtractCommand.class,
35+
ListCommand.class,
36+
RemoveCommand.class
37+
},
38+
sortOptions = false,
39+
versionProvider = ArchiveHelperVersionProvider.class
40+
)
41+
public class ArchiveHelper {
42+
public static final String LOGGER_NAME = "wlsdeploy.tool.archive-helper";
43+
private static final String CLASS = ArchiveHelper.class.getName();
44+
private static final PlatformLogger LOGGER = WLSDeployLogFactory.getLogger(LOGGER_NAME);
45+
46+
@Option(
47+
names = { "-help" },
48+
description = "Get help for the archiveHelper tool",
49+
usageHelp = true
50+
)
51+
private static boolean helpRequested = false;
52+
53+
@Option(
54+
names = { "-version" },
55+
description = "Get the WebLogic Deploy Tooling version",
56+
versionHelp = true
57+
)
58+
private static boolean versionRequested = false;
59+
60+
@SuppressWarnings("java:S106")
61+
public static void main(String[] args) {
62+
final String METHOD = "main";
63+
LOGGER.entering(CLASS, METHOD, (Object[]) args);
64+
65+
int exitCode = executeCommand(new PrintWriter(System.out, true),
66+
new PrintWriter(System.err, true), args);
67+
68+
LOGGER.exiting(CLASS, METHOD, exitCode);
69+
System.exit(exitCode);
70+
}
71+
72+
static int executeCommand(PrintWriter out, PrintWriter err, String... args) {
73+
CommandLine cmd = new CommandLine(ArchiveHelper.class)
74+
.setCaseInsensitiveEnumValuesAllowed(true)
75+
.setToggleBooleanFlags(false)
76+
.setUnmatchedArgumentsAllowed(false)
77+
.setTrimQuotes(true)
78+
.setParameterExceptionHandler(new ArgParsingExceptionHandler())
79+
.setOut(out)
80+
.setErr(err);
81+
82+
int exitCode = cmd.execute(args);
83+
if (exitCode != ExitCode.USAGE_ERROR) {
84+
CommandLine commandLine = getResponseCommandLine(cmd);
85+
CommandResponse response = commandLine.getExecutionResult();
86+
if (response != null) {
87+
exitCode = response.getStatus();
88+
response.printMessages(out, err);
89+
}
90+
}
91+
return exitCode;
92+
}
93+
94+
private static CommandLine getResponseCommandLine(CommandLine commandLine) {
95+
CommandLine result = commandLine;
96+
97+
ParseResult parseResult = commandLine.getParseResult().subcommand();
98+
if (parseResult != null) {
99+
result = getResponseCommandLine(parseResult.commandSpec().commandLine());
100+
}
101+
return result;
102+
}
103+
104+
static class ArgParsingExceptionHandler implements IParameterExceptionHandler {
105+
@Override
106+
public int handleParseException(ParameterException ex, String[] args) {
107+
CommandLine cmd = ex.getCommandLine();
108+
PrintWriter writer = cmd.getErr();
109+
110+
writer.println(ex.getMessage());
111+
UnmatchedArgumentException.printSuggestions(ex, writer);
112+
ex.getCommandLine().usage(writer, cmd.getColorScheme());
113+
114+
return ExitCode.USAGE_ERROR;
115+
}
116+
}
117+
}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/*
2+
* Copyright (c) 2023, Oracle Corporation and/or its affiliates. All rights reserved.
3+
* Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
4+
*/
5+
package oracle.weblogic.deploy.tool.archive_helper;
6+
7+
import oracle.weblogic.deploy.exception.BundleAwareException;
8+
import oracle.weblogic.deploy.exception.ExceptionHelper;
9+
10+
public class ArchiveHelperException extends BundleAwareException {
11+
private static final long serialVersionUID = 1L;
12+
13+
private final int exitCode;
14+
15+
/**
16+
* Constructs a default exception with exit code of 2.
17+
*/
18+
public ArchiveHelperException() {
19+
this.exitCode = 2;
20+
}
21+
22+
/**
23+
* Construct a default exception with specified exit code
24+
* @param exitCode the exit code to use
25+
*/
26+
public ArchiveHelperException(int exitCode) {
27+
this.exitCode = exitCode;
28+
}
29+
30+
/**
31+
* Constructs a new exception with the specified message id.
32+
*
33+
* @param exitCode the exit code to use
34+
* @param messageID the message ID
35+
*/
36+
public ArchiveHelperException(int exitCode, String messageID) {
37+
super(messageID);
38+
this.exitCode = exitCode;
39+
}
40+
41+
/**
42+
* Constructs a new exception with the specified message id and parameters.
43+
*
44+
* @param exitCode the exit code to use
45+
* @param messageID the message ID
46+
* @param params the parameters to use to fill in the message tokens
47+
*/
48+
public ArchiveHelperException(int exitCode, String messageID, Object... params) {
49+
super(messageID, params);
50+
this.exitCode = exitCode;
51+
}
52+
53+
/**
54+
* Constructs a new exception with the specified message id and cause.
55+
*
56+
* @param exitCode the exit code to use
57+
* @param messageID the message ID
58+
* @param cause the exception that triggered the creation of this exception
59+
*/
60+
public ArchiveHelperException(int exitCode, String messageID, Throwable cause) {
61+
super(messageID, cause);
62+
this.exitCode = exitCode;
63+
}
64+
65+
/**
66+
* Constructs a new exception with passed message id, cause, and parameters.
67+
*
68+
* @param exitCode the exit code to use
69+
* @param messageID the message ID
70+
* @param cause the exception that triggered the creation of this exception
71+
* @param params the parameters to use to fill in the message tokens
72+
*/
73+
public ArchiveHelperException(int exitCode, String messageID, Throwable cause, Object... params) {
74+
super(messageID, cause, params);
75+
this.exitCode = exitCode;
76+
}
77+
78+
/**
79+
* Constructs a new exception with the specified cause.
80+
*
81+
* @param exitCode the exit code to use
82+
* @param cause the exception that triggered the creation of this exception
83+
*/
84+
public ArchiveHelperException(int exitCode, Throwable cause) {
85+
super(cause);
86+
this.exitCode = exitCode;
87+
}
88+
89+
/**
90+
* {@inheritDoc}
91+
*/
92+
@Override
93+
public String getBundleName() {
94+
return ExceptionHelper.getResourceBundleName();
95+
}
96+
97+
/**
98+
* Get the exit code associated with this exception.
99+
*
100+
* @return the exit code associated with this exception
101+
*/
102+
public int getExitCode() {
103+
return this.exitCode;
104+
}
105+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* Copyright (c) 2023, Oracle and/or its affiliates.
3+
* Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
4+
*/
5+
package oracle.weblogic.deploy.tool.archive_helper;
6+
7+
import oracle.weblogic.deploy.util.WebLogicDeployToolingVersion;
8+
9+
import picocli.CommandLine.IVersionProvider;
10+
11+
public class ArchiveHelperVersionProvider implements IVersionProvider {
12+
@Override
13+
public String[] getVersion() {
14+
return new String[] {
15+
"WebLogic Deploy Tooling version: " + WebLogicDeployToolingVersion.getVersion(),
16+
"Build: " + WebLogicDeployToolingVersion.getBuildRevision() + ":" +
17+
WebLogicDeployToolingVersion.getBuildTimestamp()
18+
};
19+
}
20+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright (c) 2023, Oracle and/or its affiliates.
3+
* Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
4+
*/
5+
package oracle.weblogic.deploy.tool.archive_helper;
6+
7+
import java.io.PrintWriter;
8+
import java.text.MessageFormat;
9+
import java.util.ArrayList;
10+
import java.util.List;
11+
import java.util.ResourceBundle;
12+
13+
public class CommandResponse {
14+
private static final ResourceBundle RESOURCE_BUNDLE =
15+
ResourceBundle.getBundle("oracle.weblogic.deploy.messages.wlsdeploy_rb");
16+
17+
private int status;
18+
private final List<String> messages = new ArrayList<>();
19+
private final List<Object[]> messageParamsList = new ArrayList<>();
20+
21+
public CommandResponse(int status) {
22+
this.status = status;
23+
}
24+
25+
public CommandResponse(int status, String message, Object... messageParams) {
26+
this.status = status;
27+
this.messages.add(message);
28+
this.messageParamsList.add(messageParams);
29+
}
30+
31+
public int getStatus() {
32+
return status;
33+
}
34+
35+
public void setStatus(int status) {
36+
this.status = status;
37+
}
38+
39+
public String[] getMessages() {
40+
String[] formattedMessages = new String[this.messages.size()];
41+
42+
for (int index = 0; index < this.messages.size(); index++) {
43+
String message = this.messages.get(index);
44+
if (RESOURCE_BUNDLE.containsKey(message)) {
45+
message = MessageFormat.format(RESOURCE_BUNDLE.getString(message), this.messageParamsList.get(index));
46+
}
47+
formattedMessages[index] = message;
48+
}
49+
return formattedMessages;
50+
}
51+
52+
public void addMessage(String message, Object... messageParams) {
53+
this.messages.add(message);
54+
this.messageParamsList.add(messageParams);
55+
}
56+
57+
public void addMessages(List<String> messages) {
58+
this.messages.addAll(messages);
59+
for (int i = 0; i < messages.size(); i++) {
60+
this.messageParamsList.add(new Object[0]);
61+
}
62+
}
63+
64+
public void printMessages(PrintWriter out, PrintWriter err) {
65+
PrintWriter location = out;
66+
if (status != 0) {
67+
location = err;
68+
}
69+
70+
for (String message : getMessages()) {
71+
location.println(message);
72+
}
73+
}
74+
}

0 commit comments

Comments
 (0)