1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19 package org.apache.maven.cling.executor;
20
21 import org.apache.maven.api.annotations.Nonnull;
22 import org.apache.maven.api.cli.Executor;
23 import org.apache.maven.api.cli.ExecutorException;
24 import org.apache.maven.api.cli.ExecutorRequest;
25
26 /**
27 * Helper class for routing Maven execution based on preferences and/or issued execution requests.
28 */
29 public interface ExecutorHelper extends ExecutorTool {
30 /**
31 * The modes of execution.
32 */
33 enum Mode {
34 /**
35 * Automatically decide. For example, presence of {@link ExecutorRequest#environmentVariables()} or
36 * {@link ExecutorRequest#jvmArguments()} will result in choosing {@link #FORKED} executor. Otherwise,
37 * {@link #EMBEDDED} executor is preferred.
38 */
39 AUTO,
40 /**
41 * Forces embedded execution. May fail if {@link ExecutorRequest} contains input unsupported by executor.
42 */
43 EMBEDDED,
44 /**
45 * Forces forked execution. Always carried out, most isolated and "most correct", but is slow as it uses child process.
46 */
47 FORKED
48 }
49
50 /**
51 * Returns the preferred mode of this helper.
52 */
53 @Nonnull
54 Mode getDefaultMode();
55
56 /**
57 * Creates pre-populated builder for {@link ExecutorRequest}. Users of helper must use this method to create
58 * properly initialized request builder.
59 */
60 @Nonnull
61 ExecutorRequest.Builder executorRequest();
62
63 /**
64 * Executes the request with preferred mode executor.
65 */
66 default int execute(ExecutorRequest executorRequest) throws ExecutorException {
67 return execute(getDefaultMode(), executorRequest);
68 }
69
70 /**
71 * Executes the request with passed in mode executor.
72 */
73 int execute(Mode mode, ExecutorRequest executorRequest) throws ExecutorException;
74
75 /**
76 * High level operation, returns the version of the Maven covered by this helper. This method call caches
77 * underlying operation, and is safe to invoke as many times needed.
78 *
79 * @see Executor#mavenVersion(ExecutorRequest)
80 */
81 @Nonnull
82 String mavenVersion();
83 }