PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/atlassian/amps
Java | 189 lines | 151 code | 23 blank | 15 comment | 7 complexity | 99ea47c6298660a2159b298ae983b46a MD5 | raw file
Possible License(s): Apache-2.0, BSD-3-Clause
  1. package com.atlassian.maven.plugins.amps.product;
  2. import com.atlassian.maven.plugins.amps.MavenContext;
  3. import com.atlassian.maven.plugins.amps.MavenGoals;
  4. import com.atlassian.maven.plugins.amps.Product;
  5. import com.atlassian.maven.plugins.amps.ProductArtifact;
  6. import com.atlassian.maven.plugins.amps.product.manager.WebAppManager;
  7. import com.atlassian.maven.plugins.amps.util.JvmArgsFix;
  8. import com.atlassian.maven.plugins.amps.util.VersionUtils;
  9. import com.google.common.annotations.VisibleForTesting;
  10. import com.google.common.collect.ImmutableMap;
  11. import org.apache.maven.artifact.resolver.ArtifactResolver;
  12. import org.apache.maven.repository.RepositorySystem;
  13. import javax.annotation.Nonnull;
  14. import java.io.File;
  15. import java.util.Arrays;
  16. import java.util.Collection;
  17. import java.util.List;
  18. import java.util.Map;
  19. import java.util.Optional;
  20. import static java.lang.String.format;
  21. import static java.util.Collections.emptyList;
  22. import static java.util.Collections.emptyMap;
  23. import static java.util.Optional.empty;
  24. import static org.apache.commons.lang3.StringUtils.isBlank;
  25. import static org.apache.commons.lang3.StringUtils.isNotBlank;
  26. /**
  27. * The {@link ProductHandler} for the Atlassian Reference Application (Refapp).
  28. */
  29. public class RefappProductHandler extends AbstractWebappProductHandler {
  30. @VisibleForTesting
  31. static final String ATLASSIAN_BUNDLED_PLUGINS_ZIP = "WEB-INF/classes/atlassian-bundled-plugins.zip";
  32. @VisibleForTesting
  33. static final String ATLASSIAN_BUNDLED_PLUGINS_DIR = "WEB-INF/atlassian-bundled-plugins";
  34. public RefappProductHandler(final MavenContext context, final MavenGoals goals,
  35. final RepositorySystem repositorySystem, final ArtifactResolver artifactResolver,
  36. final WebAppManager webAppManager) {
  37. super(context, goals, new RefappPluginProvider(), repositorySystem, artifactResolver, webAppManager);
  38. }
  39. @Nonnull
  40. @Override
  41. public String getDefaultContainerId() {
  42. return "tomcat7x";
  43. }
  44. @Override
  45. @Nonnull
  46. public String getId() {
  47. return "refapp";
  48. }
  49. @Override
  50. public int getDefaultHttpPort() {
  51. return 5990;
  52. }
  53. @Override
  54. public int getDefaultHttpsPort() {
  55. return 8445;
  56. }
  57. @Nonnull
  58. @Override
  59. protected Optional<File> getUserInstalledPluginsDirectory(final Product product, final File webappDir, File homeDir) {
  60. return empty();
  61. }
  62. @Nonnull
  63. @Override
  64. protected File getBundledPluginPath(final Product product, final File productDir) {
  65. // if the bundled plugin directory exists, use it, otherwise fallback to the old zip behaviour.
  66. final File bundleDir = new File(productDir, ATLASSIAN_BUNDLED_PLUGINS_DIR);
  67. if (bundleDir.exists() && bundleDir.isDirectory()) {
  68. return bundleDir;
  69. }
  70. return new File(productDir, ATLASSIAN_BUNDLED_PLUGINS_ZIP);
  71. }
  72. @Override
  73. @Nonnull
  74. protected Map<String, String> getProductSpecificSystemProperties(final Product product, final int nodeIndex) {
  75. final ImmutableMap.Builder<String, String> properties = ImmutableMap.builder();
  76. properties.put("refapp.home", getHomeDirectory(product).getPath());
  77. properties.put("osgi.cache", getHomeDirectory(product).getPath() + "/osgi-cache");
  78. properties.put("bundledplugins.cache", getHomeDirectory(product).getPath() + "/bundled-plugins");
  79. properties.put("cargo.servlet.uriencoding", "UTF-8");
  80. properties.putAll(getExternalDatabaseSystemProperties(product));
  81. return properties.build();
  82. }
  83. private File getHomeDirectory(final Product product) {
  84. final List<File> homeDirectories = getHomeDirectories(product);
  85. if (homeDirectories.size() == 1) {
  86. return homeDirectories.get(0);
  87. }
  88. // Refapp doesn't support multi-node
  89. throw new IllegalStateException("Expected one home directory but found " + homeDirectories);
  90. }
  91. /**
  92. * Returns the system properties that Refapp expects when connecting to an external database.
  93. *
  94. * @param refapp the Refapp instance being started
  95. * @return a non-null map
  96. * @since 8.3
  97. */
  98. @Nonnull
  99. private Map<String, String> getExternalDatabaseSystemProperties(final Product refapp) {
  100. return refapp.getDataSources().stream().findFirst().map(ds -> {
  101. final ImmutableMap.Builder<String, String> externalDbProperties = ImmutableMap.builder();
  102. // Enable the external database
  103. externalDbProperties.put("refapp.jdbc.external", "true");
  104. // Mandatory properties, according to Refapp's ConnectionProviderImpl
  105. externalDbProperties.put("refapp.jdbc.app.url", nonBlankValue(ds.getUrl(), "url"));
  106. externalDbProperties.put("refapp.jdbc.app.user", nonBlankValue(ds.getUsername(), "username"));
  107. externalDbProperties.put("refapp.jdbc.app.pass", nonBlankValue(ds.getPassword(), "password"));
  108. externalDbProperties.put("refapp.jdbc.driver.class.name", nonBlankValue(ds.getDriver(), "driver"));
  109. // Optional properties, according to Refapp's ConnectionProviderImpl
  110. if (isNotBlank(ds.getSchema())) {
  111. externalDbProperties.put("refapp.jdbc.app.schema", ds.getSchema());
  112. }
  113. if (isNotBlank(ds.getValidationQuery())) {
  114. externalDbProperties.put("refapp.jdbc.validation.query", ds.getValidationQuery());
  115. }
  116. return (Map<String, String>) externalDbProperties.build();
  117. }).orElse(emptyMap());
  118. }
  119. private static String nonBlankValue(final String value, final String name) {
  120. if (isBlank(value)) {
  121. throw new IllegalArgumentException(format("Invalid dataSource.%s value '%s'", name, value));
  122. }
  123. return value.trim();
  124. }
  125. @Nonnull
  126. @Override
  127. public ProductArtifact getArtifact() {
  128. return new ProductArtifact("com.atlassian.refapp", "atlassian-refapp", VersionUtils.getVersion());
  129. }
  130. @Nonnull
  131. @Override
  132. public Optional<ProductArtifact> getTestResourcesArtifact() {
  133. return empty();
  134. }
  135. private static class RefappPluginProvider extends AbstractPluginProvider {
  136. @Override
  137. protected Collection<ProductArtifact> getSalArtifacts(String salVersion) {
  138. return Arrays.asList(
  139. new ProductArtifact("com.atlassian.sal", "sal-api", salVersion),
  140. new ProductArtifact("com.atlassian.sal", "sal-refimpl-appproperties-plugin", salVersion),
  141. new ProductArtifact("com.atlassian.sal", "sal-refimpl-component-plugin", salVersion),
  142. new ProductArtifact("com.atlassian.sal", "sal-refimpl-executor-plugin", salVersion),
  143. new ProductArtifact("com.atlassian.sal", "sal-refimpl-lifecycle-plugin", salVersion),
  144. new ProductArtifact("com.atlassian.sal", "sal-refimpl-message-plugin", salVersion),
  145. new ProductArtifact("com.atlassian.sal", "sal-refimpl-net-plugin", salVersion),
  146. new ProductArtifact("com.atlassian.sal", "sal-refimpl-pluginsettings-plugin", salVersion),
  147. new ProductArtifact("com.atlassian.sal", "sal-refimpl-project-plugin", salVersion),
  148. new ProductArtifact("com.atlassian.sal", "sal-refimpl-search-plugin", salVersion),
  149. new ProductArtifact("com.atlassian.sal", "sal-refimpl-transaction-plugin", salVersion),
  150. new ProductArtifact("com.atlassian.sal", "sal-refimpl-upgrade-plugin", salVersion),
  151. new ProductArtifact("com.atlassian.sal", "sal-refimpl-user-plugin", salVersion));
  152. }
  153. @Override
  154. protected Collection<ProductArtifact> getPdkInstallArtifacts(String pdkInstallVersion) {
  155. return emptyList();
  156. }
  157. }
  158. @Override
  159. protected void fixJvmArgs(Product product) {
  160. final String jvmArgs = JvmArgsFix.defaults()
  161. .withAddOpens(ADD_OPENS_FOR_TOMCAT)
  162. .withAddOpens(ADD_OPENS_FOR_FELIX)
  163. .apply(product.getJvmArgs());
  164. product.setJvmArgs(jvmArgs);
  165. }
  166. }