View Javadoc
1   /*
2    * copy-rename-maven-plugin
3    *
4    * Copyright (c) 2014 Aneesh Joseph
5    * Copyright 2023 Christian Kohlschütter
6    *
7    * SPDX-Identifier: MIT
8    */
9   package com.kohlschutter.mavenplugins.copyrename;
10  
11  import java.util.function.Supplier;
12  
13  import org.apache.maven.plugin.AbstractMojo;
14  import org.apache.maven.plugin.MojoExecutionException;
15  import org.apache.maven.plugin.logging.Log;
16  
17  /**
18   * Helper class to simplify logging and encapsulating the execution context.
19   *
20   * @author Christian Kohlschütter
21   */
22  abstract class MojoExecutionContext {
23    private final Log log; // NOPMD.ProperLogger
24  
25    protected MojoExecutionContext(AbstractMojo mojo) {
26      this.log = mojo.getLog();
27    }
28  
29    public abstract void execute() throws MojoExecutionException;
30  
31    protected void logDebug(Object... msg) {
32      if (log.isDebugEnabled()) {
33        log.debug(concatenate(msg));
34      }
35    }
36  
37    protected void logInfo(Object... msg) {
38      if (log.isInfoEnabled()) {
39        log.info(concatenate(msg));
40      }
41    }
42  
43    protected void logWarn(Object... msg) {
44      if (log.isWarnEnabled()) {
45        log.warn(concatenate(msg));
46      }
47    }
48  
49    protected void logError(Object... msg) {
50      if (log.isErrorEnabled()) {
51        log.error(concatenate(msg));
52      }
53    }
54  
55    private static String concatenate(Object... parts) {
56      switch (parts.length) {
57        case 0:
58          return "";
59        case 1:
60          Object p1 = parts[0];
61          if (p1 instanceof Supplier<?>) {
62            p1 = ((Supplier<?>) p1).get();
63          }
64          return String.valueOf(p1);
65        default:
66          StringBuilder sb = new StringBuilder();
67          for (Object p : parts) {
68            if (p instanceof Supplier<?>) {
69              p = ((Supplier<?>) p).get(); // NOPMD.AvoidReassigningLoopVariables
70            }
71            sb.append(p);
72          }
73          return sb.toString();
74      }
75    }
76  }