1 package com.atlassian.maven.plugins.amps.product;
2
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 static com.atlassian.maven.plugins.amps.util.FileUtils.doesFileNameMatchArtifact;
7 import org.apache.commons.io.FileUtils;
8 import static org.apache.commons.io.FileUtils.copyFile;
9 import static org.apache.commons.io.FileUtils.iterateFiles;
10 import org.apache.maven.plugin.MojoExecutionException;
11 import org.apache.maven.project.MavenProject;
12
13 import java.io.File;
14 import java.io.IOException;
15 import java.util.HashMap;
16 import java.util.Iterator;
17 import java.util.List;
18 import java.util.Map;
19
20 public abstract class AbstractProductHandler implements ProductHandler
21 {
22 protected final MavenGoals goals;
23 protected final MavenProject project;
24
25 protected AbstractProductHandler(MavenProject project, MavenGoals goals)
26 {
27 this.project = project;
28 this.goals = goals;
29 }
30
31 protected boolean isStaticPlugin() throws IOException
32 {
33 final File atlassianPluginXml = new File(project.getBasedir(), "src/main/resources/atlassian-plugin.xml");
34 if (atlassianPluginXml.exists())
35 {
36 String text = FileUtils.readFileToString(atlassianPluginXml);
37 return !text.contains("pluginsVersion=\"2\"") && !text.contains("plugins-version=\"2\"");
38 }
39 else
40 {
41
42 return false;
43 }
44 }
45
46 protected void addThisPluginToDirectory(final File targetDir) throws IOException
47 {
48 final File thisPlugin = getPluginFile();
49
50
51 for (final Iterator<?> iterateFiles = iterateFiles(targetDir, null, false); iterateFiles.hasNext();)
52 {
53 final File file = (File) iterateFiles.next();
54 if (doesFileNameMatchArtifact(file.getName(), project.getArtifactId()))
55 {
56 file.delete();
57 }
58 }
59
60
61 copyFile(thisPlugin, new File(targetDir, thisPlugin.getName()));
62 }
63
64 protected void addTestPluginToDirectory(final File targetDir) throws IOException
65 {
66 final File testPluginFile = getTestPluginFile();
67 if (testPluginFile.exists())
68 {
69
70 copyFile(testPluginFile, new File(targetDir, testPluginFile.getName()));
71 }
72
73 }
74
75 protected File getPluginFile()
76 {
77 return new File(project.getBuild().getDirectory(), project.getBuild().getFinalName() + ".jar");
78 }
79
80 protected File getTestPluginFile()
81 {
82 return new File(project.getBuild().getDirectory(), project.getBuild().getFinalName() + "-tests.jar");
83 }
84
85 protected void addArtifactsToDirectory(final List<ProductArtifact> artifacts, final File pluginsDir) throws MojoExecutionException
86 {
87
88 if (pluginsDir.isDirectory() && pluginsDir.exists())
89 {
90 for (final Iterator<?> iterateFiles = FileUtils.iterateFiles(pluginsDir, null, false); iterateFiles.hasNext();)
91 {
92 final File file = (File) iterateFiles.next();
93 for (final ProductArtifact webappArtifact : artifacts)
94 {
95 if (!file.isDirectory() && doesFileNameMatchArtifact(file.getName(), webappArtifact.getArtifactId()))
96 {
97 file.delete();
98 }
99 }
100 }
101 }
102
103 if (!artifacts.isEmpty())
104 {
105 goals.copyPlugins(pluginsDir, artifacts);
106 }
107 }
108
109 public File getHomeDirectory(Product ctx)
110 {
111 return new File(new File(project.getBuild().getDirectory(), ctx.getInstanceId()), "home");
112 }
113
114 protected File createHomeDirectory(Product ctx)
115 {
116 File homeDir = getHomeDirectory(ctx);
117
118 if (!homeDir.exists())
119 {
120 homeDir.mkdirs();
121 }
122 return homeDir;
123 }
124
125 protected Map<String, String> mergeSystemProperties(Product ctx)
126 {
127 final Map<String, String> properties = new HashMap<String, String>();
128 properties.putAll(getSystemProperties(ctx));
129 for (Map.Entry<String, Object> entry : ctx.getSystemPropertyVariables().entrySet())
130 {
131 properties.put(entry.getKey(), (String) entry.getValue());
132 }
133 return properties;
134 }
135
136 protected abstract Map<String, String> getSystemProperties(Product ctx);
137
138 }