PageRenderTime 41ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/amps-maven-plugin/src/main/java/com/atlassian/maven/plugins/amps/AbstractProductAwareMojo.java

https://bitbucket.org/atlassian/amps
Java | 149 lines | 88 code | 22 blank | 39 comment | 13 complexity | f84787febbccf788ad45ec37d55b7f18 MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause
  1. package com.atlassian.maven.plugins.amps;
  2. import com.atlassian.maven.plugins.amps.analytics.AnalyticsService;
  3. import com.atlassian.maven.plugins.amps.analytics.event.AnalyticsEvent;
  4. import com.atlassian.maven.plugins.amps.product.ProductHandler;
  5. import com.atlassian.maven.plugins.amps.product.ProductHandlerFactory;
  6. import com.atlassian.maven.plugins.amps.product.manager.WebAppManager;
  7. import org.apache.maven.artifact.resolver.ArtifactResolver;
  8. import org.apache.maven.plugin.MojoExecutionException;
  9. import org.apache.maven.plugins.annotations.Component;
  10. import org.apache.maven.plugins.annotations.Parameter;
  11. import org.apache.maven.project.ProjectBuilder;
  12. import org.apache.maven.repository.RepositorySystem;
  13. import javax.annotation.Nonnull;
  14. import java.util.concurrent.ConcurrentHashMap;
  15. import java.util.concurrent.ConcurrentMap;
  16. import java.util.prefs.Preferences;
  17. import static com.atlassian.maven.plugins.amps.analytics.event.impl.AnalyticsEventFactory.sdkFirstRun;
  18. import static com.atlassian.maven.plugins.amps.analytics.impl.GoogleAnalyticsService.newGoogleAnalyticsService;
  19. import static com.atlassian.maven.plugins.amps.product.ProductHandlerFactory.REFAPP;
  20. /**
  21. * Base class for Mojos that run a product.
  22. */
  23. public abstract class AbstractProductAwareMojo extends AbstractAmpsMojo {
  24. private static final String PREF_FIRSTRUN_PREFIX = "sdk-firstrun";
  25. // ------------------- Dependencies ----------------------
  26. private AnalyticsService analyticsService;
  27. @Component
  28. protected ArtifactResolver artifactResolver;
  29. @Component
  30. private ProjectBuilder projectBuilder;
  31. @Component
  32. protected RepositorySystem repositorySystem;
  33. @Component
  34. private WebAppManager webAppManager;
  35. // ------------------- User-configurable properties ----------------------
  36. /**
  37. * The Atlassian product id, e.g. {@value com.atlassian.maven.plugins.amps.product.ProductHandlerFactory#REFAPP}.
  38. *
  39. * @see ProductHandlerFactory
  40. */
  41. @Parameter(property = "product")
  42. private String product;
  43. /**
  44. * Instance to run. If provided, used to determine the product to use, instead of
  45. * using the product ID.
  46. */
  47. @Parameter(property = "instanceId")
  48. protected String instanceId;
  49. /**
  50. * <p>Flag to enable Google tracking.</p>
  51. *
  52. * <p>AMPS sends basic usage events to Google analytics by default. To disable tracking, either:</p>
  53. * <ol>
  54. * <li>Add <code>&lt;allow.google.tracking>false&lt;/allow.google.tracking></code> to the
  55. * <code>&lt;properties></code> section of your <code>.m2/settings.xml</code> file</li>
  56. * <li>Include <code>&lt;allowGoogleTracking>false&lt;/allowGoogleTracking></code> in
  57. * the amps plugin configuration in your <code>pom.xml</code></li>
  58. * <li>or pass <code>-Dallow.google.tracking=false</code> on the command line.
  59. * </ol>
  60. */
  61. @Parameter(property = "allow.google.tracking", defaultValue = "true")
  62. protected boolean allowGoogleTracking;
  63. protected String getDefaultProductId() throws MojoExecutionException {
  64. // If the [product]-maven-plugin didn't override this method, we fetch the name of the plugin
  65. String nameOfTheCurrentMavenPlugin = getPluginInformation().getId();
  66. if (ProductHandlerFactory.getIds().contains(nameOfTheCurrentMavenPlugin)) {
  67. return nameOfTheCurrentMavenPlugin;
  68. }
  69. return null;
  70. }
  71. @Nonnull
  72. protected final String getProductId() throws MojoExecutionException {
  73. if (product == null) {
  74. product = getDefaultProductId();
  75. if (product == null) {
  76. product = REFAPP;
  77. }
  78. }
  79. return product;
  80. }
  81. @Nonnull
  82. private AnalyticsService getAnalyticsService() throws MojoExecutionException {
  83. if (analyticsService == null) {
  84. analyticsService = newGoogleAnalyticsService(
  85. getProductId(), getAmpsPluginVersion(), getPluginInformation().getVersion(), getLog());
  86. getLog().info("Google Analytics Tracking is enabled to collect AMPS usage statistics.");
  87. getLog().info("Although no personal information is sent, you may disable tracking by adding" +
  88. " <allowGoogleTracking>false</allowGoogleTracking> to the AMPS plugin configuration in your pom.xml");
  89. }
  90. return analyticsService;
  91. }
  92. /**
  93. * Sends the given analytics event if {@link #allowGoogleTracking} is {@code true}.
  94. *
  95. * @param event the event to send
  96. * @throws MojoExecutionException if execution fails
  97. */
  98. protected final void sendAnalyticsEvent(@Nonnull final AnalyticsEvent event) throws MojoExecutionException {
  99. if (allowGoogleTracking) {
  100. getAnalyticsService().send(event);
  101. }
  102. }
  103. protected final void trackFirstRunIfNeeded() throws MojoExecutionException {
  104. final boolean runningShellScript = System.getenv("ATLAS_VERSION") != null;
  105. if (allowGoogleTracking && runningShellScript) {
  106. String firstRunKey = PREF_FIRSTRUN_PREFIX + "-" + getAmpsPluginVersion();
  107. Preferences prefs = Preferences.userNodeForPackage(getClass());
  108. String alreadyRan = prefs.get(firstRunKey, null);
  109. if (alreadyRan == null) {
  110. sendAnalyticsEvent(sdkFirstRun(getAmpsPluginVersion()));
  111. prefs.put(firstRunKey, "true");
  112. }
  113. }
  114. }
  115. private final ConcurrentMap<String, ProductHandler> productHandlerCache = new ConcurrentHashMap<>();
  116. /**
  117. * Creates a handler for the given product.
  118. *
  119. * @param productId the product nickname (not the instance id)
  120. * @return a product handler for this nickname
  121. */
  122. protected ProductHandler getProductHandler(final String productId) {
  123. return productHandlerCache.computeIfAbsent(productId, key ->
  124. ProductHandlerFactory.create(productId, getMavenContext(), getMavenGoals(), repositorySystem,
  125. projectBuilder, artifactResolver, webAppManager));
  126. }
  127. }