1   package com.atlassian.maven.plugins.amps;
2   
3   import java.util.ArrayList;
4   import java.util.List;
5   
6   import com.atlassian.maven.plugins.amps.product.ProductHandlerFactory;
7   import com.atlassian.maven.plugins.amps.util.GoogleAmpsTracker;
8   
9   import org.apache.maven.plugin.MojoExecutionException;
10  import org.jfrog.maven.annomojo.annotations.MojoParameter;
11  
12  public abstract class AbstractProductAwareMojo extends AbstractAmpsMojo
13  {
14      /**
15       * Product id
16       */
17      @MojoParameter(expression = "${product}")
18      private String product;
19  
20      /**
21       * Instance to run. If provided, used to determine the product to use, instead of
22       * using the product ID.
23       */
24      @MojoParameter(expression = "${instanceId}")
25      protected String instanceId;
26  
27  
28      /**
29       * <p>Flag to enable Google tracking.</p>
30       *
31       * <p>AMPS sends basic usage events to Google analytics by default. To disable tracking, either:</p>
32       * <ol>
33       * <li>Add <code>&lt;allow.google.tracking>false&lt;/allow.google.tracking></code> to the
34       *  <code>&lt;properties></code> section of your <code>.m2/settings.xml</code> file</li>
35       * <li>Include <code>&lt;allowGoogleTracking>false&lt;/allowGoogleTracking></code> in
36       * the amps plugin configuration in your <code>pom.xml</code></li>
37       * <li>or pass <code>-Dallow.google.tracking=false</code> on the command line.
38       * </ol>
39       */
40      @MojoParameter(expression = "${allow.google.tracking}", defaultValue = "true")
41      protected boolean allowGoogleTracking;
42  
43      private GoogleAmpsTracker googleTracker;
44  
45  
46      protected String getDefaultProductId() throws MojoExecutionException
47      {
48          // If maven-[product]-plugin didn't override this method, we fetch the
49          // name of the plugin
50          String nameOfTheCurrentMavenPlugin = getPluginInformation().getId();
51          if (ProductHandlerFactory.getIds().contains(nameOfTheCurrentMavenPlugin))
52          {
53              return nameOfTheCurrentMavenPlugin;
54          }
55          return null;
56      }
57  
58      protected final String getProductId() throws MojoExecutionException
59      {
60          if (product == null)
61          {
62              product = getDefaultProductId();
63              if (product == null)
64              {
65                  product = ProductHandlerFactory.REFAPP;
66              }
67          }
68          return product;
69      }
70  
71      protected GoogleAmpsTracker getGoogleTracker() throws MojoExecutionException
72      {
73          if(null == googleTracker)
74          {
75              googleTracker = new GoogleAmpsTracker(getProductId(),getLog());
76  
77              if(googleTrackingAllowed()) {
78                  getLog().info("Google Analytics Tracking is enabled to collect AMPS usage statistics.");
79                  getLog().info("Although no personal information is sent, you may disable tracking by adding <allowGoogleTracking>false</allowGoogleTracking> to the amps plugin configuration in your pom.xml");
80              }
81          }
82  
83          googleTracker.setEnabled(googleTrackingAllowed());
84  
85          return googleTracker;
86      }
87  
88      protected boolean googleTrackingAllowed() {
89          return allowGoogleTracking;
90      }
91  }