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.mvnup.goals;
20  
21  import java.nio.file.Path;
22  import java.util.Map;
23  import java.util.Set;
24  
25  import org.apache.maven.api.cli.mvnup.UpgradeOptions;
26  import org.apache.maven.cling.invoker.mvnup.UpgradeContext;
27  import org.jdom2.Document;
28  
29  /**
30   * Abstract base class for upgrade strategies that provides common functionality
31   * and reduces code duplication across strategy implementations.
32   */
33  public abstract class AbstractUpgradeStrategy implements UpgradeStrategy {
34  
35      /**
36       * Template method that handles common logging and error handling.
37       * Subclasses implement the actual upgrade logic in doApply().
38       */
39      @Override
40      public final UpgradeResult apply(UpgradeContext context, Map<Path, Document> pomMap) {
41          context.info(getDescription());
42          context.indent();
43  
44          try {
45              UpgradeResult result = doApply(context, pomMap);
46  
47              // Log summary
48              logSummary(context, result);
49  
50              return result;
51          } catch (Exception e) {
52              context.failure("Strategy execution failed: " + e.getMessage());
53              return UpgradeResult.failure(pomMap.keySet(), Set.of());
54          } finally {
55              context.unindent();
56          }
57      }
58  
59      /**
60       * Subclasses implement the actual upgrade logic here.
61       *
62       * @param context the upgrade context
63       * @param pomMap map of all POM files in the project
64       * @return the result of the upgrade operation
65       */
66      protected abstract UpgradeResult doApply(UpgradeContext context, Map<Path, Document> pomMap);
67  
68      /**
69       * Gets the upgrade options from the context.
70       *
71       * @param context the upgrade context
72       * @return the upgrade options
73       */
74      protected final UpgradeOptions getOptions(UpgradeContext context) {
75          return context.options();
76      }
77  
78      /**
79       * Logs a summary of the upgrade results.
80       *
81       * @param context the upgrade context
82       * @param result the upgrade result
83       */
84      protected void logSummary(UpgradeContext context, UpgradeResult result) {
85          context.println();
86          context.info(getDescription() + " Summary:");
87          context.indent();
88          context.info(result.modifiedCount() + " POM(s) modified");
89          context.info(result.unmodifiedCount() + " POM(s) needed no changes");
90          if (result.errorCount() > 0) {
91              context.info(result.errorCount() + " POM(s) had errors");
92          }
93          context.unindent();
94      }
95  }