Skip to content

Commit 66b7e2c

Browse files
authored
Cleanup after last commit (#1798)
LookupInvoker does terminal stuff now, remove them from "early" entry classes, also adopt mvnenc
1 parent 740100b commit 66b7e2c

File tree

10 files changed

+110
-116
lines changed

10 files changed

+110
-116
lines changed

maven-cli/src/main/java/org/apache/maven/cling/MavenCling.java

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import org.apache.maven.cling.invoker.mvn.DefaultMavenParser;
3030
import org.apache.maven.cling.invoker.mvn.local.DefaultLocalMavenInvoker;
3131
import org.apache.maven.jline.JLineMessageBuilderFactory;
32-
import org.apache.maven.jline.MessageUtils;
3332
import org.codehaus.plexus.classworlds.ClassWorld;
3433

3534
/**
@@ -60,17 +59,6 @@ public MavenCling(ClassWorld classWorld) {
6059
super(classWorld);
6160
}
6261

63-
@Override
64-
public int run(String[] args) throws IOException {
65-
MessageUtils.systemInstall();
66-
MessageUtils.registerShutdownHook();
67-
try {
68-
return super.run(args);
69-
} finally {
70-
MessageUtils.systemUninstall();
71-
}
72-
}
73-
7462
@Override
7563
protected Invoker<MavenInvokerRequest<MavenOptions>> createInvoker() {
7664
return new DefaultLocalMavenInvoker(

maven-cli/src/main/java/org/apache/maven/cling/MavenEncCling.java

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,7 @@
3030
import org.apache.maven.cling.invoker.mvnenc.DefaultEncryptInvoker;
3131
import org.apache.maven.cling.invoker.mvnenc.DefaultEncryptParser;
3232
import org.apache.maven.jline.JLineMessageBuilderFactory;
33-
import org.apache.maven.jline.MessageUtils;
3433
import org.codehaus.plexus.classworlds.ClassWorld;
35-
import org.jline.terminal.Terminal;
36-
import org.jline.terminal.TerminalBuilder;
3734

3835
/**
3936
* Maven encrypt CLI "new-gen".
@@ -55,8 +52,6 @@ public static int main(String[] args, ClassWorld world) throws IOException {
5552
return new MavenEncCling(world).run(args);
5653
}
5754

58-
private Terminal terminal;
59-
6055
public MavenEncCling() {
6156
super();
6257
}
@@ -65,24 +60,10 @@ public MavenEncCling(ClassWorld classWorld) {
6560
super(classWorld);
6661
}
6762

68-
@Override
69-
public int run(String[] args) throws IOException {
70-
terminal = TerminalBuilder.builder().build();
71-
MessageUtils.systemInstall(terminal);
72-
MessageUtils.registerShutdownHook();
73-
try {
74-
return super.run(args);
75-
} finally {
76-
MessageUtils.systemUninstall();
77-
}
78-
}
79-
8063
@Override
8164
protected Invoker<EncryptInvokerRequest> createInvoker() {
82-
return new DefaultEncryptInvoker(ProtoLookup.builder()
83-
.addMapping(ClassWorld.class, classWorld)
84-
.addMapping(Terminal.class, terminal)
85-
.build());
65+
return new DefaultEncryptInvoker(
66+
ProtoLookup.builder().addMapping(ClassWorld.class, classWorld).build());
8667
}
8768

8869
@Override

maven-cli/src/main/java/org/apache/maven/cling/invoker/LookupInvoker.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,7 @@ protected void helpOrVersionAndMayExit(C context) throws Exception {
405405
R invokerRequest = context.invokerRequest;
406406
if (invokerRequest.options().help().isPresent()) {
407407
invokerRequest.options().displayHelp(context.invokerRequest.parserRequest(), context.terminal.writer());
408+
context.terminal.writer().flush();
408409
throw new ExitException(0);
409410
}
410411
if (invokerRequest.options().showVersionAndExit().isPresent()) {
@@ -413,6 +414,7 @@ protected void helpOrVersionAndMayExit(C context) throws Exception {
413414
} else {
414415
context.terminal.writer().println(CLIReportingUtils.showVersion());
415416
}
417+
context.terminal.writer().flush();
416418
throw new ExitException(0);
417419
}
418420
}

maven-cli/src/main/java/org/apache/maven/cling/invoker/mvnenc/DefaultEncryptInvoker.java

Lines changed: 37 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,8 @@ public void addInHeader(AttributedStyle style, String text) {
7373
}
7474
}
7575

76-
private final Terminal terminal;
77-
7876
public DefaultEncryptInvoker(ProtoLookup protoLookup) {
7977
super(protoLookup);
80-
this.terminal = protoLookup.lookup(Terminal.class);
8178
}
8279

8380
@Override
@@ -101,29 +98,34 @@ protected void lookup(LocalContext context) {
10198
public static final int CANCELED = 3; // user canceled
10299

103100
protected int doExecute(LocalContext context) throws Exception {
104-
if (!context.interactive) {
105-
System.out.println("This tool works only in interactive mode!");
106-
System.out.println("Tool purpose is to configure password management on developer workstations.");
107-
System.out.println(
108-
"Note: Generated configuration can be moved/copied to headless environments, if configured as such.");
109-
return BAD_OPERATION;
110-
}
111-
112-
context.header = new ArrayList<>();
113-
context.style = new AttributedStyle();
114-
context.addInHeader(
115-
context.style.italic().bold().foreground(Colors.rgbColor("green")),
116-
"Maven Encryption " + CLIReportingUtils.showVersionMinimal());
117-
context.addInHeader("Tool for secure password management on workstations.");
118-
context.addInHeader("This tool is part of Apache Maven 4 distribution.");
119-
context.addInHeader("");
120101
try {
102+
if (!context.interactive) {
103+
context.terminal.writer().println("This tool works only in interactive mode!");
104+
context.terminal
105+
.writer()
106+
.println("Tool purpose is to configure password management on developer workstations.");
107+
context.terminal
108+
.writer()
109+
.println(
110+
"Note: Generated configuration can be moved/copied to headless environments, if configured as such.");
111+
return BAD_OPERATION;
112+
}
113+
114+
context.header = new ArrayList<>();
115+
context.style = new AttributedStyle();
116+
context.addInHeader(
117+
context.style.italic().bold().foreground(Colors.rgbColor("green")),
118+
"Maven Encryption " + CLIReportingUtils.showVersionMinimal());
119+
context.addInHeader("Tool for secure password management on workstations.");
120+
context.addInHeader("This tool is part of Apache Maven 4 distribution.");
121+
context.addInHeader("");
122+
121123
Thread executeThread = Thread.currentThread();
122-
terminal.handle(Terminal.Signal.INT, signal -> executeThread.interrupt());
124+
context.terminal.handle(Terminal.Signal.INT, signal -> executeThread.interrupt());
123125
ConsolePrompt.UiConfig config;
124-
if (terminal.getType().equals(Terminal.TYPE_DUMB)
125-
|| terminal.getType().equals(Terminal.TYPE_DUMB_COLOR)) {
126-
System.out.println(terminal.getName() + ": " + terminal.getType());
126+
if (context.terminal.getType().equals(Terminal.TYPE_DUMB)
127+
|| context.terminal.getType().equals(Terminal.TYPE_DUMB_COLOR)) {
128+
context.terminal.writer().println(context.terminal.getName() + ": " + context.terminal.getType());
127129
throw new IllegalStateException("Dumb terminal detected.\nThis tool requires real terminal to work!\n"
128130
+ "Note: On Windows Jansi or JNA library must be included in classpath.");
129131
} else if (OSUtils.IS_WINDOWS) {
@@ -133,8 +135,9 @@ protected int doExecute(LocalContext context) throws Exception {
133135
}
134136
config.setCancellableFirstPrompt(true);
135137

136-
context.reader = LineReaderBuilder.builder().terminal(terminal).build();
137-
context.prompt = new ConsolePrompt(context.reader, terminal, config);
138+
context.reader =
139+
LineReaderBuilder.builder().terminal(context.terminal).build();
140+
context.prompt = new ConsolePrompt(context.reader, context.terminal, config);
138141

139142
if (context.invokerRequest.options().goals().isEmpty()
140143
|| context.invokerRequest.options().goals().get().size() != 1) {
@@ -150,22 +153,25 @@ protected int doExecute(LocalContext context) throws Exception {
150153

151154
return goal.execute(context);
152155
} catch (InterruptedException | InterruptedIOException | UserInterruptException e) {
153-
System.out.println("Goal canceled by user.");
156+
context.terminal.writer().println("Goal canceled by user.");
154157
return CANCELED;
155158
} catch (Exception e) {
156159
if (context.invokerRequest.options().showErrors().orElse(false)) {
157-
context.logger.error(e.getMessage(), e);
160+
context.terminal.writer().println(e.getMessage());
161+
e.printStackTrace(context.terminal.writer());
158162
} else {
159-
context.logger.error(e.getMessage());
163+
context.terminal.writer().println(e.getMessage());
160164
}
161165
return ERROR;
166+
} finally {
167+
context.terminal.writer().flush();
162168
}
163169
}
164170

165171
protected int badGoalsErrorMessage(String message, LocalContext context) {
166-
System.out.println(message);
167-
System.out.println("Supported goals are: " + String.join(", ", context.goals.keySet()));
168-
System.out.println("Use -h to display help.");
172+
context.terminal.writer().println(message);
173+
context.terminal.writer().println("Supported goals are: " + String.join(", ", context.goals.keySet()));
174+
context.terminal.writer().println("Use -h to display help.");
169175
return BAD_OPERATION;
170176
}
171177
}

maven-cli/src/main/java/org/apache/maven/cling/invoker/mvnenc/goals/ConfiguredGoalSupport.java

Lines changed: 39 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -38,55 +38,65 @@ protected ConfiguredGoalSupport(MessageBuilderFactory messageBuilderFactory, Sec
3838

3939
@Override
4040
public int execute(DefaultEncryptInvoker.LocalContext context) throws Exception {
41-
if (!validateConfiguration()) {
42-
logger.error(messageBuilderFactory
43-
.builder()
44-
.error("Maven Encryption is not configured, run `mvnenc init` first.")
45-
.build());
41+
if (!validateConfiguration(context)) {
42+
context.terminal
43+
.writer()
44+
.println(messageBuilderFactory
45+
.builder()
46+
.error("Maven Encryption is not configured, run `mvnenc init` first.")
47+
.build());
4648
return ERROR;
4749
}
4850
return doExecute(context);
4951
}
5052

51-
protected boolean validateConfiguration() {
53+
protected boolean validateConfiguration(DefaultEncryptInvoker.LocalContext context) {
5254
SecDispatcher.ValidationResponse response = secDispatcher.validateConfiguration();
53-
if (!response.isValid() || logger.isDebugEnabled()) {
54-
dumpResponse("", response);
55+
if (!response.isValid() || context.invokerRequest.options().verbose().orElse(false)) {
56+
dumpResponse(context, "", response);
5557
}
5658
return response.isValid();
5759
}
5860

59-
protected void dumpResponse(String indent, SecDispatcher.ValidationResponse response) {
60-
logger.info(
61-
response.isValid()
62-
? messageBuilderFactory
63-
.builder()
64-
.success("{}Configuration validation of {}: {}")
65-
.build()
66-
: messageBuilderFactory
67-
.builder()
68-
.failure("{}Configuration validation of {}: {}")
69-
.build(),
70-
indent,
71-
response.getSource(),
72-
response.isValid() ? "VALID" : "INVALID");
61+
protected void dumpResponse(
62+
DefaultEncryptInvoker.LocalContext context, String indent, SecDispatcher.ValidationResponse response) {
63+
context.terminal
64+
.writer()
65+
.println(messageBuilderFactory
66+
.builder()
67+
.format(
68+
response.isValid()
69+
? messageBuilderFactory
70+
.builder()
71+
.success("%sConfiguration validation of %s: %s")
72+
.build()
73+
: messageBuilderFactory
74+
.builder()
75+
.failure("%sConfiguration validation of %s: %s")
76+
.build(),
77+
indent,
78+
response.getSource(),
79+
response.isValid() ? "VALID" : "INVALID"));
7380
for (Map.Entry<SecDispatcher.ValidationResponse.Level, List<String>> entry :
7481
response.getReport().entrySet()) {
75-
Consumer<String> consumer =
76-
s -> logger.info(messageBuilderFactory.builder().info(s).build());
82+
Consumer<String> consumer = s -> context.terminal
83+
.writer()
84+
.println(messageBuilderFactory.builder().info(s).build());
7785
if (entry.getKey() == SecDispatcher.ValidationResponse.Level.ERROR) {
78-
consumer = s ->
79-
logger.error(messageBuilderFactory.builder().error(s).build());
86+
consumer = s -> context.terminal
87+
.writer()
88+
.println(messageBuilderFactory.builder().error(s).build());
8089
} else if (entry.getKey() == SecDispatcher.ValidationResponse.Level.WARNING) {
81-
consumer = s ->
82-
logger.warn(messageBuilderFactory.builder().warning(s).build());
90+
consumer = s -> context.terminal
91+
.writer()
92+
.println(messageBuilderFactory.builder().warning(s).build());
8393
}
8494
for (String line : entry.getValue()) {
8595
consumer.accept(indent + " " + line);
8696
}
8797
}
8898
for (SecDispatcher.ValidationResponse subsystem : response.getSubsystems()) {
89-
dumpResponse(indent + " ", subsystem);
99+
dumpResponse(context, indent + " ", subsystem);
90100
}
91101
}
92102

maven-cli/src/main/java/org/apache/maven/cling/invoker/mvnenc/goals/Decrypt.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@ public Decrypt(MessageBuilderFactory messageBuilderFactory, SecDispatcher secDis
4444
protected int doExecute(DefaultEncryptInvoker.LocalContext context) throws Exception {
4545
String encrypted = context.reader.readLine("Enter the password to decrypt: ");
4646
if (secDispatcher.isAnyEncryptedString(encrypted)) {
47-
logger.info(secDispatcher.decrypt(encrypted));
47+
context.terminal.writer().println(secDispatcher.decrypt(encrypted));
4848
return OK;
4949
} else {
50-
logger.error("Malformed encrypted string");
50+
context.terminal.writer().println(messageBuilderFactory.builder().error("Malformed encrypted string"));
5151
return BAD_OPERATION;
5252
}
5353
}

maven-cli/src/main/java/org/apache/maven/cling/invoker/mvnenc/goals/Diag.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public Diag(MessageBuilderFactory messageBuilderFactory, SecDispatcher secDispat
4141

4242
@Override
4343
protected int doExecute(DefaultEncryptInvoker.LocalContext context) {
44-
dumpResponse("", secDispatcher.validateConfiguration());
44+
dumpResponse(context, "", secDispatcher.validateConfiguration());
4545
return OK;
4646
}
4747
}

maven-cli/src/main/java/org/apache/maven/cling/invoker/mvnenc/goals/Encrypt.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public Encrypt(MessageBuilderFactory messageBuilderFactory, SecDispatcher secDis
4242
@Override
4343
protected int doExecute(DefaultEncryptInvoker.LocalContext context) throws Exception {
4444
String cleartext = context.reader.readLine("Enter the password to encrypt: ", '*');
45-
logger.info(secDispatcher.encrypt(cleartext, null));
45+
context.terminal.writer().println(secDispatcher.encrypt(cleartext, null));
4646
return OK;
4747
}
4848
}

maven-cli/src/main/java/org/apache/maven/cling/invoker/mvnenc/goals/GoalSupport.java

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,11 @@
2323
import org.apache.maven.api.services.MessageBuilderFactory;
2424
import org.apache.maven.cling.invoker.mvnenc.Goal;
2525
import org.codehaus.plexus.components.secdispatcher.SecDispatcher;
26-
import org.slf4j.Logger;
27-
import org.slf4j.LoggerFactory;
2826

2927
/**
3028
* The support class for goal implementations.
3129
*/
3230
public abstract class GoalSupport implements Goal {
33-
protected final Logger logger = LoggerFactory.getLogger(getClass());
3431
protected final MessageBuilderFactory messageBuilderFactory;
3532
protected final SecDispatcher secDispatcher;
3633

0 commit comments

Comments
 (0)