View Javadoc
1   /**
2    * Copyright (C) 2008-2010 Matt Gumbley, DevZendo.org <http://devzendo.org>;
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *         http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  /**
18   * 
19   */
20  package org.devzendo.xplp;
21  
22  import java.io.File;
23  import java.io.IOException;
24  import java.util.ArrayList;
25  import java.util.Arrays;
26  import java.util.List;
27  import java.util.Properties;
28  import java.util.Set;
29  
30  import org.apache.maven.artifact.Artifact;
31  import org.apache.maven.plugin.AbstractMojo;
32  import org.codehaus.plexus.util.StringUtils;
33  
34  /**
35   * Create a UNIX script launcher directory structure.
36   * @author matt
37   *
38   */
39  public class UnixScriptLauncherCreator extends LauncherCreator {
40      private final String mOsOutputSubDirectoryName;
41  
42      /**
43       * @param mojo the parent mojo class
44       * @param outputDirectory where to create the .app structure 
45       * @param osOutputSubDirectoryName the name of the directory under the
46       * output directory in which this lancher structure will be created, e.g
47       * linux or macosx
48       * @param mainClassName the main class
49       * @param applicationName the name of the application
50       * @param libraryDirectory where the libraries are stored
51       * @param transitiveArtifacts the set of transitive artifact dependencies
52       * @param resourceDirectories the project's resource directories
53       * @param parameterProperties the plugin configuration parameters, as properties
54       * @param systemProperties an array of name=value system properties
55       * @param vmArguments an array of arguments to the VM
56       * @param narClassifierTypes an array of NAR classifier:types
57       */
58      public UnixScriptLauncherCreator(final AbstractMojo mojo,
59              final File outputDirectory,
60              final String osOutputSubDirectoryName,
61              final String mainClassName,
62              final String applicationName,
63              final String libraryDirectory,
64              final Set<Artifact> transitiveArtifacts,
65              final Set<File> resourceDirectories,
66              final Properties parameterProperties, 
67              final String[] systemProperties, 
68              final String[] vmArguments,
69              final String[] narClassifierTypes) {
70          super(mojo, outputDirectory, mainClassName,
71              applicationName, libraryDirectory,
72              transitiveArtifacts, resourceDirectories, parameterProperties,
73              systemProperties, vmArguments, narClassifierTypes);
74          mOsOutputSubDirectoryName = osOutputSubDirectoryName;
75      }
76  
77      /**
78       * {@inheritDoc}
79       */
80      @Override
81      public void createLauncher() throws IOException {
82          final File osOutputDir = new File(getOutputDirectory(), mOsOutputSubDirectoryName);
83          final File binDir = new File(osOutputDir, "bin");
84          final File libDir = new File(osOutputDir, "lib");
85          osOutputDir.mkdirs();
86          binDir.mkdirs();
87          libDir.mkdirs();
88          final boolean allDirsOK = osOutputDir.exists() 
89              && binDir.exists() && libDir.exists();
90          if (!allDirsOK) {
91              throw new IOException("Could not create required directories under " + getOutputDirectory().getAbsolutePath());
92          }
93          
94          final List<String> jvmArgs = new ArrayList<String>();
95          jvmArgs.addAll(systemPropertiesAsJVMArgs(getSystemProperties()));
96          jvmArgs.addAll(vmArgumentsAsJVMArgs(getVmArguments()));
97          final StringBuilder jvmArgsString = new StringBuilder();
98          for (final String jvmArg : jvmArgs) {
99              jvmArgsString.append(jvmArg);
100             jvmArgsString.append(' '); // a space at the end is needed
101         }
102         getParameterProperties().put("xplp.linuxjvmargs", jvmArgsString.toString());
103         getParameterProperties().put("xplp.linuxclasspatharray", transitiveArtifactsAsClassPathArray(getTransitiveArtifacts()));
104 
105         final File outputRunScript = new File(binDir, getApplicationName());
106         copyInterpolatedPluginResource("linux/launcher.sh", outputRunScript);
107         makeExecutable(outputRunScript);
108         
109         copyTransitiveArtifacts(libDir);
110     }
111 
112     private List<String> vmArgumentsAsJVMArgs(final String[] vmArguments) {
113         return Arrays.asList(vmArguments);
114     }
115 
116     private List<String> systemPropertiesAsJVMArgs(final String[] systemProperties) {
117         final List<String> addDList = new ArrayList<String>();
118         for (final String sysProp : systemProperties) {
119             addDList.add("-D" + sysProp);
120         }
121         return addDList;
122     }
123 
124     private String transitiveArtifactsAsClassPathArray(final Set<Artifact> transitiveArtifacts) {
125         final ArrayList<String> libsAsArtifacts = new ArrayList<String>();
126         final Set<String> transitiveArtifactFileNames = getTransitiveJarOrNarArtifactFileNames(transitiveArtifacts);
127         for (final String fileName : transitiveArtifactFileNames) {
128             libsAsArtifacts.add("$progdir/../lib/" + fileName);
129         }
130 
131         return StringUtils.join(libsAsArtifacts.iterator(), ":");
132     }
133 }