Skip to content

Commit

Permalink
Split invoker in two
Browse files Browse the repository at this point in the history
  • Loading branch information
cstamas committed Sep 30, 2024
1 parent 7131fb4 commit c1ef311
Show file tree
Hide file tree
Showing 28 changed files with 1,871 additions and 1,410 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,15 @@
*
* @param <R> The request type.
*/
public interface Invoker<R extends InvokerRequest<? extends Options>> {
public interface Invoker<R extends InvokerRequest<? extends Options>> extends AutoCloseable {
/**
* Invokes application and returns exit code.
*/
int invoke(@Nonnull R invokerRequest) throws InvokerException;

/**
* Closes and disposes invoker instance.
*/
@Override
default void close() throws InvokerException {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@
* @param <O> the type of Options.
*/
public interface InvokerRequest<O extends Options> {
/**
* Mandatory: command.
*/
@Nonnull
String command();

/**
* Mandatory: the current working directory, usually the {@code "user.dir"} Java System Property.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ public interface Options {
@Nonnull
Optional<Boolean> showErrors();

@Nonnull
Optional<String> failOnSeverity();

@Nonnull
Optional<Boolean> nonInteractive();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,14 @@

public interface Parser<R extends InvokerRequest<? extends Options>> {
@Nonnull
default R parse(@Nonnull String[] args, Logger logger, MessageBuilderFactory messageBuilderFactory)
default R parse(
@Nonnull String command,
@Nonnull String[] args,
@Nonnull Logger logger,
@Nonnull MessageBuilderFactory messageBuilderFactory)
throws ParserException, IOException {
return parse(ParserRequest.builder(args, logger, messageBuilderFactory).build());
return parse(ParserRequest.builder(command, args, logger, messageBuilderFactory)
.build());
}

@Nonnull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@
import static java.util.Objects.requireNonNull;

public interface ParserRequest {
/**
* Mandatory: command.
*/
@Nonnull
String command();

/**
* Mandatory: Logger to use at very early stages.
Expand Down Expand Up @@ -86,25 +91,30 @@ public interface ParserRequest {

@Nonnull
static Builder builder(
@Nonnull String[] args, @Nonnull Logger logger, @Nonnull MessageBuilderFactory messageBuilderFactory) {
return new Builder(args, logger, messageBuilderFactory);
@Nonnull String command,
@Nonnull String[] args,
@Nonnull Logger logger,
@Nonnull MessageBuilderFactory messageBuilderFactory) {
return new Builder(command, args, logger, messageBuilderFactory);
}

class Builder {
private final String command;
private final String[] args;
private Logger logger;
private MessageBuilderFactory messageBuilderFactory;
private final Logger logger;
private final MessageBuilderFactory messageBuilderFactory;
private Path cwd;
private Path mavenHome;
private Path userHome;
private InputStream in;
private OutputStream out;
private OutputStream err;

private Builder(String[] args, Logger logger, MessageBuilderFactory messageBuilderFactory) {
this.args = requireNonNull(args);
this.logger = requireNonNull(logger);
this.messageBuilderFactory = requireNonNull(messageBuilderFactory);
private Builder(String command, String[] args, Logger logger, MessageBuilderFactory messageBuilderFactory) {
this.command = requireNonNull(command, "appName");
this.args = requireNonNull(args, "args");
this.logger = requireNonNull(logger, "logger");
this.messageBuilderFactory = requireNonNull(messageBuilderFactory, "messageBuilderFactory");
}

public Builder cwd(Path cwd) {
Expand Down Expand Up @@ -138,11 +148,13 @@ public Builder err(OutputStream err) {
}

public ParserRequest build() {
return new ParserRequestImpl(args, logger, messageBuilderFactory, cwd, mavenHome, userHome, in, out, err);
return new ParserRequestImpl(
command, args, logger, messageBuilderFactory, cwd, mavenHome, userHome, in, out, err);
}

@SuppressWarnings("ParameterNumber")
private static class ParserRequestImpl implements ParserRequest {
private final String command;
private final Logger logger;
private final MessageBuilderFactory messageBuilderFactory;
private final String[] args;
Expand All @@ -154,6 +166,7 @@ private static class ParserRequestImpl implements ParserRequest {
private final OutputStream err;

private ParserRequestImpl(
String command,
String[] args,
Logger logger,
MessageBuilderFactory messageBuilderFactory,
Expand All @@ -163,9 +176,10 @@ private ParserRequestImpl(
InputStream in,
OutputStream out,
OutputStream err) {
this.args = requireNonNull(args);
this.logger = requireNonNull(logger);
this.messageBuilderFactory = requireNonNull(messageBuilderFactory);
this.command = requireNonNull(command, "appName");
this.args = requireNonNull(args, "args");
this.logger = requireNonNull(logger, "logger");
this.messageBuilderFactory = requireNonNull(messageBuilderFactory, "messageBuilderFactory");
this.cwd = cwd;
this.mavenHome = mavenHome;
this.userHome = userHome;
Expand All @@ -174,6 +188,11 @@ private ParserRequestImpl(
this.err = err;
}

@Override
public String command() {
return command;
}

@Override
public Logger logger() {
return logger;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@

/**
* Component responsible to invoke Maven using information provided in invoker request.
*
* @param <R> The request type
*/
public interface MavenInvoker extends Invoker<MavenInvokerRequest> {
public interface MavenInvoker<R extends MavenInvokerRequest> extends Invoker<R> {
@Override
int invoke(MavenInvokerRequest invokerRequest) throws InvokerException;
int invoke(R invokerRequest) throws InvokerException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,6 @@ public interface MavenOptions extends Options {
@Nonnull
Optional<Boolean> relaxedChecksums();

@Nonnull
Optional<String> failOnSeverity();

@Nonnull
Optional<Boolean> failFast();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
import org.apache.maven.api.cli.ParserException;
import org.apache.maven.api.cli.ParserRequest;

public interface MavenParser extends Parser<MavenInvokerRequest> {
public interface MavenParser<R extends MavenInvokerRequest> extends Parser<R> {
@Nonnull
MavenInvokerRequest parse(@Nonnull ParserRequest parserRequest) throws ParserException, IOException;
R parse(@Nonnull ParserRequest parserRequest) throws ParserException, IOException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.maven.api.cli.mvn.forked;

import java.util.List;
import java.util.Optional;

import org.apache.maven.api.annotations.Nonnull;
import org.apache.maven.api.cli.mvn.MavenInvokerRequest;

public interface ForkedMavenInvokerRequest extends MavenInvokerRequest {
/**
* Optional: extra arguments for the forked JVM.
*/
@Nonnull
Optional<List<String>> jvmArguments();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.maven.api.cli.mvn.forked;

import java.io.IOException;

import org.apache.maven.api.annotations.Nonnull;
import org.apache.maven.api.cli.ParserException;
import org.apache.maven.api.cli.ParserRequest;
import org.apache.maven.api.cli.mvn.MavenParser;

public interface ForkedMavenParser extends MavenParser<ForkedMavenInvokerRequest> {
@Nonnull
ForkedMavenInvokerRequest parse(@Nonnull ParserRequest parserRequest) throws ParserException, IOException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@
import org.apache.maven.api.cli.InvokerException;
import org.apache.maven.api.cli.ParserException;
import org.apache.maven.cling.invoker.ProtoLogger;
import org.apache.maven.cling.invoker.mvn.local.LocalInvoker;
import org.apache.maven.cling.invoker.mvn.local.LocalParser;
import org.apache.maven.cling.invoker.mvn.local.LocalMavenInvoker;
import org.apache.maven.cling.invoker.mvn.local.LocalMavenParser;
import org.apache.maven.jline.JLineMessageBuilderFactory;
import org.apache.maven.jline.MessageUtils;
import org.codehaus.plexus.classworlds.ClassWorld;
Expand Down Expand Up @@ -80,8 +80,9 @@ public int run(String[] args) throws IOException {
MessageUtils.systemInstall();
MessageUtils.registerShutdownHook();
try {
return new LocalInvoker(classWorld)
.invoke(new LocalParser().parse(args, new ProtoLogger(), new JLineMessageBuilderFactory()));
return new LocalMavenInvoker(classWorld)
.invoke(new LocalMavenParser()
.parse("mvn", args, new ProtoLogger(), new JLineMessageBuilderFactory()));
} catch (ParserException e) {
System.err.println(e.getMessage());
return 1;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
import org.apache.maven.api.cli.ParserException;
import org.apache.maven.api.cli.ParserRequest;
import org.apache.maven.cling.invoker.ProtoLogger;
import org.apache.maven.cling.invoker.mvn.local.LocalInvoker;
import org.apache.maven.cling.invoker.mvn.local.LocalParser;
import org.apache.maven.cling.invoker.mvn.local.LocalMavenInvoker;
import org.apache.maven.cling.invoker.mvn.local.LocalMavenParser;
import org.apache.maven.jline.JLineMessageBuilderFactory;
import org.apache.maven.jline.MessageUtils;
import org.codehaus.plexus.classworlds.ClassWorld;
Expand Down Expand Up @@ -63,9 +63,10 @@ public int run(InputStream in, OutputStream out, OutputStream err, String... arg
MessageUtils.systemInstall();
MessageUtils.registerShutdownHook();
try {
return new LocalInvoker(classWorld)
.invoke(new LocalParser()
.parse(ParserRequest.builder(arguments, logger, new JLineMessageBuilderFactory())
return new LocalMavenInvoker(classWorld)
.invoke(new LocalMavenParser()
.parse(ParserRequest.builder(
name(), arguments, logger, new JLineMessageBuilderFactory())
.in(in)
.out(out)
.err(err)
Expand Down
11 changes: 6 additions & 5 deletions maven-cli/src/main/java/org/apache/maven/cling/MavenTool.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
import org.apache.maven.api.cli.ParserException;
import org.apache.maven.api.cli.ParserRequest;
import org.apache.maven.cling.invoker.ProtoLogger;
import org.apache.maven.cling.invoker.mvn.local.LocalInvoker;
import org.apache.maven.cling.invoker.mvn.local.LocalParser;
import org.apache.maven.cling.invoker.mvn.local.LocalMavenInvoker;
import org.apache.maven.cling.invoker.mvn.local.LocalMavenParser;
import org.apache.maven.jline.JLineMessageBuilderFactory;
import org.apache.maven.jline.MessageUtils;
import org.codehaus.plexus.classworlds.ClassWorld;
Expand Down Expand Up @@ -61,9 +61,10 @@ public int run(InputStream in, OutputStream out, OutputStream err, String... arg
MessageUtils.systemInstall();
MessageUtils.registerShutdownHook();
try {
return new LocalInvoker(classWorld)
.invoke(new LocalParser()
.parse(ParserRequest.builder(arguments, logger, new JLineMessageBuilderFactory())
return new LocalMavenInvoker(classWorld)
.invoke(new LocalMavenParser()
.parse(ParserRequest.builder(
name(), arguments, logger, new JLineMessageBuilderFactory())
.in(in)
.out(out)
.err(err)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import static java.util.Objects.requireNonNull;

public abstract class BaseInvokerRequest<T extends Options> implements InvokerRequest<T> {
private final String command;
private final Path cwd;
private final Path installationDirectory;
private final Path userHomeDirectory;
Expand All @@ -53,6 +54,7 @@ public abstract class BaseInvokerRequest<T extends Options> implements InvokerRe

@SuppressWarnings("ParameterNumber")
public BaseInvokerRequest(
@Nonnull String command,
@Nonnull Path cwd,
@Nonnull Path installationDirectory,
@Nonnull Path userHomeDirectory,
Expand All @@ -66,6 +68,7 @@ public BaseInvokerRequest(
@Nullable OutputStream out,
@Nullable OutputStream err,
@Nullable List<CoreExtension> coreExtensions) {
this.command = requireNonNull(command);
this.cwd = requireNonNull(cwd);
this.installationDirectory = requireNonNull(installationDirectory);
this.userHomeDirectory = requireNonNull(userHomeDirectory);
Expand All @@ -82,6 +85,11 @@ public BaseInvokerRequest(
this.coreExtensions = coreExtensions;
}

@Override
public String command() {
return command;
}

@Override
public Path cwd() {
return cwd;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,14 @@ public Optional<Boolean> showErrors() {
return Optional.empty();
}

@Override
public Optional<String> failOnSeverity() {
if (commandLine.hasOption(CLIManager.FAIL_ON_SEVERITY)) {
return Optional.of(commandLine.getOptionValue(CLIManager.FAIL_ON_SEVERITY));
}
return Optional.empty();
}

@Override
public Optional<Boolean> nonInteractive() {
if (commandLine.hasOption(CLIManager.NON_INTERACTIVE) || commandLine.hasOption(CLIManager.BATCH_MODE)) {
Expand Down
Loading

0 comments on commit c1ef311

Please sign in to comment.