1   package com.atlassian.maven.plugins.amps.product;
2   
3   import java.io.File;
4   import java.io.IOException;
5   import java.net.URI;
6   import java.net.URISyntaxException;
7   import java.util.ArrayList;
8   import java.util.Arrays;
9   import java.util.Collection;
10  import java.util.Collections;
11  import java.util.List;
12  import java.util.Map;
13  
14  import com.atlassian.maven.plugins.amps.MavenContext;
15  import com.atlassian.maven.plugins.amps.MavenGoals;
16  import com.atlassian.maven.plugins.amps.Product;
17  import com.atlassian.maven.plugins.amps.ProductArtifact;
18  import com.atlassian.maven.plugins.amps.util.ConfigFileUtils;
19  
20  import com.google.common.collect.ImmutableMap;
21  
22  import org.apache.commons.io.FileUtils;
23  import org.apache.maven.plugin.MojoExecutionException;
24  
25  public class CrowdProductHandler extends AbstractWebappProductHandler
26  {
27      public CrowdProductHandler(final MavenContext context, final MavenGoals goals)
28      {
29          super(context, goals, new CrowdPluginProvider());
30      }
31  
32      public String getId()
33      {
34          return ProductHandlerFactory.CROWD;
35      }
36  
37      @Override
38      public ProductArtifact getArtifact()
39      {
40          return new ProductArtifact("com.atlassian.crowd", "crowd-web-app", "RELEASE");
41      }
42  
43      @Override
44      public ProductArtifact getTestResourcesArtifact()
45      {
46          return new ProductArtifact("com.atlassian.crowd.distribution", "crowd-plugin-test-resources");
47      }
48  
49      public int getDefaultHttpPort()
50      {
51          return 4990;
52      }
53  
54      @Override
55      public Map<String, String> getSystemProperties(final Product ctx)
56      {
57          return ImmutableMap.of("crowd.home", getHomeDirectory(ctx).getPath());
58      }
59  
60      @Override
61      public File getUserInstalledPluginsDirectory(final File webappDir, final File homeDir)
62      {
63          return new File(homeDir, "plugins");
64      }
65  
66      @Override
67      public List<ProductArtifact> getExtraContainerDependencies()
68      {
69          return Arrays.asList(
70                  new ProductArtifact("hsqldb", "hsqldb", "1.8.0.7"),
71                  new ProductArtifact("jta", "jta", "1.0.1B"),
72                  new ProductArtifact("javax.mail", "mail", "1.4"),
73                  new ProductArtifact("javax.activation", "activation", "1.0.2")
74          );
75      }
76  
77      @Override
78      public String getBundledPluginPath(Product ctx)
79      {
80          return "WEB-INF/classes/atlassian-bundled-plugins.zip";
81      }
82  
83      @Override
84      public void processHomeDirectory(final Product ctx, final File homeDir) throws MojoExecutionException
85      {
86          String baseUrl = MavenGoals.getBaseUrl(ctx, ctx.getHttpPort());
87  
88          /* Crowd connects back to itself; use 'localhost' rather than the hostname an external client would see */
89          try
90          {
91              baseUrl = withLocalhostAsHostname(baseUrl);
92          }
93          catch (URISyntaxException e)
94          {
95              throw new MojoExecutionException("Unable to process Crowd service URL", e);
96          }
97  
98          try
99          {
100             ConfigFileUtils.replaceAll(new File(homeDir, "crowd.cfg.xml"),
101                     "jdbc:hsqldb:.*/(crowd-)?home/database/defaultdb",
102                     "jdbc:hsqldb:" + getHomeDirectory(ctx).getCanonicalPath().replace("\\", "/") + "/database/defaultdb");
103 
104             Map<String, String> newProperties = ImmutableMap.of(
105                     "crowd.server.url", baseUrl + "/services",
106                     "application.login.url", baseUrl
107             );
108             ConfigFileUtils.setProperties(new File(homeDir, "crowd.properties"), newProperties);
109         }
110         catch (final IOException e)
111         {
112             throw new MojoExecutionException(e.getMessage());
113         }
114     }
115 
116     static String withLocalhostAsHostname(String uri) throws URISyntaxException
117     {
118         URI base = new URI(uri);
119 
120         URI baseWithLocalhost = new URI(
121                 base.getScheme(),
122                 base.getUserInfo(),
123                 "localhost",
124                 base.getPort(),
125                 base.getPath(),
126                 base.getQuery(),
127                 base.getFragment());
128 
129         return baseWithLocalhost.toString();
130     }
131 
132     @Override
133     public List<ProductArtifact> getDefaultLibPlugins()
134     {
135         return Collections.emptyList();
136     }
137 
138     @Override
139     public List<ProductArtifact> getDefaultBundledPlugins()
140     {
141         return Collections.emptyList();
142     }
143 
144     private static class CrowdPluginProvider extends AbstractPluginProvider
145     {
146 
147         @Override
148         protected Collection<ProductArtifact> getSalArtifacts(final String salVersion)
149         {
150             return Arrays.asList(
151                     new ProductArtifact("com.atlassian.sal", "sal-api", salVersion),
152                     new ProductArtifact("com.atlassian.sal", "sal-crowd-plugin", salVersion));
153         }
154 
155         @Override
156         protected Collection<ProductArtifact> getPdkInstallArtifacts(final String pdkInstallVersion)
157         {
158             final List<ProductArtifact> plugins = new ArrayList<ProductArtifact>();
159             plugins.addAll(super.getPdkInstallArtifacts(pdkInstallVersion));
160             plugins.add(new ProductArtifact("commons-fileupload", "commons-fileupload", "1.2.1"));
161             return plugins;
162         }
163     }
164 
165     @Override
166     public void cleanupProductHomeForZip(Product product, File homeDirectory) throws MojoExecutionException, IOException
167     {
168         super.cleanupProductHomeForZip(product, homeDirectory);
169         FileUtils.deleteQuietly(new File(homeDirectory, "caches/transformed-plugins"));
170         FileUtils.deleteQuietly(new File(homeDirectory, "caches/felix/felix-cache"));
171         FileUtils.deleteQuietly(new File(homeDirectory, "logs"));
172     }
173 
174     @Override
175     public List<File> getConfigFiles(Product product, File snapshotDir)
176     {
177         List<File> configFiles = super.getConfigFiles(product, snapshotDir);
178         configFiles.add(new File(snapshotDir, "database.log"));
179         configFiles.add(new File(snapshotDir, "crowd.cfg.xml"));
180         configFiles.add(new File(snapshotDir, "crowd.properties"));
181         return configFiles;
182     }
183 }