View Javadoc
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.invoker.mvnenc;
20  
21  import java.io.InterruptedIOException;
22  import java.util.ArrayList;
23  
24  import org.apache.maven.api.cli.InvokerRequest;
25  import org.apache.maven.api.cli.mvnenc.EncryptOptions;
26  import org.apache.maven.cling.invoker.LookupInvoker;
27  import org.apache.maven.cling.invoker.ProtoLookup;
28  import org.apache.maven.cling.utils.CLIReportingUtils;
29  import org.jline.reader.LineReaderBuilder;
30  import org.jline.reader.UserInterruptException;
31  import org.jline.terminal.Terminal;
32  import org.jline.utils.AttributedStyle;
33  import org.jline.utils.Colors;
34  
35  /**
36   * mvnenc invoker implementation.
37   */
38  public class EncryptInvoker extends LookupInvoker<EncryptContext> {
39  
40      public EncryptInvoker(ProtoLookup protoLookup) {
41          super(protoLookup);
42      }
43  
44      @Override
45      protected int execute(EncryptContext context) throws Exception {
46          return doExecute(context);
47      }
48  
49      @Override
50      protected EncryptContext createContext(InvokerRequest invokerRequest) {
51          return new EncryptContext(invokerRequest);
52      }
53  
54      @Override
55      protected void lookup(EncryptContext context) {
56          context.goals = context.lookup.lookupMap(Goal.class);
57      }
58  
59      public static final int OK = 0; // OK
60      public static final int ERROR = 1; // "generic" error
61      public static final int BAD_OPERATION = 2; // bad user input or alike
62      public static final int CANCELED = 3; // user canceled
63  
64      protected int doExecute(EncryptContext context) throws Exception {
65          try {
66              context.header = new ArrayList<>();
67              context.style = new AttributedStyle();
68              context.addInHeader(
69                      context.style.italic().bold().foreground(Colors.rgbColor("green")),
70                      "Maven Encryption " + CLIReportingUtils.showVersionMinimal());
71              context.addInHeader("Tool for secure password management on workstations.");
72              context.addInHeader("This tool is part of Apache Maven 4 distribution.");
73              context.addInHeader("");
74  
75              Thread executeThread = Thread.currentThread();
76              context.terminal.handle(Terminal.Signal.INT, signal -> executeThread.interrupt());
77  
78              context.reader =
79                      LineReaderBuilder.builder().terminal(context.terminal).build();
80  
81              EncryptOptions options = (EncryptOptions) context.invokerRequest.options();
82              if (options.goals().isEmpty() || options.goals().get().size() != 1) {
83                  return badGoalsErrorMessage("No goal or multiple goals specified, specify only one goal.", context);
84              }
85  
86              String goalName = options.goals().get().get(0);
87              Goal goal = context.goals.get(goalName);
88  
89              if (goal == null) {
90                  return badGoalsErrorMessage("Unknown goal: " + goalName, context);
91              }
92  
93              return goal.execute(context);
94          } catch (InterruptedException | InterruptedIOException | UserInterruptException e) {
95              context.logger.error("Goal canceled by user.");
96              return CANCELED;
97          } catch (Exception e) {
98              if (context.invokerRequest.options().showErrors().orElse(false)) {
99                  context.logger.error(e.getMessage(), e);
100             } else {
101                 context.logger.error(e.getMessage());
102             }
103             return ERROR;
104         } finally {
105             context.terminal.writer().flush();
106         }
107     }
108 
109     protected int badGoalsErrorMessage(String message, EncryptContext context) {
110         context.logger.error(message);
111         context.logger.error("Supported goals are: " + String.join(", ", context.goals.keySet()));
112         context.logger.error("Use -h to display help.");
113         return BAD_OPERATION;
114     }
115 }