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