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.Properties;
25  import java.util.Set;
26  
27  import org.apache.maven.artifact.Artifact;
28  import org.apache.maven.plugin.AbstractMojo;
29  
30  /**
31   * Creates a Windows launcher directory structure.
32   * @author matt
33   *
34   */
35  public class WindowsLauncherCreator extends LauncherCreator {
36      private static final String LINE_SEPARATOR = System.getProperty("line.separator");
37      private static final String MSVCR71_DLL = "msvcr71.dll";
38      private final String mLauncherType;
39      private final String[] mJanelCustomLines;
40  
41      /**
42       * @param mojo the parent mojo class
43       * @param outputDirectory where to create the .app structure 
44       * @param mainClassName the main class
45       * @param applicationName the name of the application
46       * @param libraryDirectory where the libraries are stored
47       * @param transitiveArtifacts the set of transitive artifact dependencies
48       * @param resourceDirectories the project's resource directories
49       * @param parameterProperties the plugin configuration parameters, as properties
50       * @param systemProperties an array of name=value system properties
51       * @param vmArguments an array of arguments to the VM
52       * @param narClassifierTypes an array of NAR classifier:types
53       * @param launcherType the launcher type, Console or GUI.
54       * @param janelCustomLines an array of extra lines to be added to the launcher file
55       */
56      public WindowsLauncherCreator(final AbstractMojo mojo,
57              final File outputDirectory,
58              final String mainClassName,
59              final String applicationName,
60              final String libraryDirectory,
61              final Set<Artifact> transitiveArtifacts,
62              final Set<File> resourceDirectories,
63              final Properties parameterProperties,
64              final String[] systemProperties, 
65              final String[] vmArguments, 
66              final String[] narClassifierTypes,
67              final String launcherType,
68              final String[] janelCustomLines) {
69          super(mojo, outputDirectory, mainClassName,
70              applicationName, libraryDirectory,
71              transitiveArtifacts, resourceDirectories,
72              parameterProperties, systemProperties, vmArguments,
73              narClassifierTypes);
74          mLauncherType = launcherType;
75          mJanelCustomLines = janelCustomLines;
76      }
77      
78      private void validate() {
79          if (mLauncherType == null || mLauncherType.length() == 0) {
80              final String message = "No launcherType specified - this is mandatory for Windows";
81              getMojo().getLog().warn(message);
82              throw new IllegalStateException(message);
83          }
84          if (!(mLauncherType.equals("Console") || mLauncherType.equals("GUI"))) {
85              final String message = "launcherType must be either 'Console' or 'GUI' (GUI is the default if not specified)";
86              getMojo().getLog().warn(message);
87              throw new IllegalStateException(message);
88          }
89      }
90  
91      /**
92       * {@inheritDoc}
93       */
94      @Override
95      public void createLauncher() throws IOException {
96          validate();
97          getParameterProperties().put("xplp.windowssystemproperties", systemPropertiesAsJanelLines(getSystemProperties()));
98          getParameterProperties().put("xplp.windowsvmarguments", stringsToSeparatedJanelLines(getVmArguments()));
99          getParameterProperties().put("xplp.janelcustomlines", stringsToSeparatedJanelLines(mJanelCustomLines));
100 
101         getMojo().getLog().info("Janel .EXE type:   " + mLauncherType);
102         
103         final File osOutputDir = new File(getOutputDirectory(), "windows");
104         final File libDir = new File(osOutputDir, "lib");
105         osOutputDir.mkdirs();
106         libDir.mkdirs();
107         final boolean allDirsOK = osOutputDir.exists() && libDir.exists();
108         if (!allDirsOK) {
109             throw new IOException("Could not create required directories under " + getOutputDirectory().getAbsolutePath());
110         }
111         
112         final File outputJanelEXE = new File(osOutputDir, getApplicationName() + ".exe");
113         final String janelEXEResource = "windows/" + (mLauncherType.equals("Console") ? "JanelConsole.exe" : "JanelWindows.exe");
114         copyPluginResource(janelEXEResource, outputJanelEXE);
115         // TODO icon munging in the launcher .EXE
116         copyPluginResource("windows/" + MSVCR71_DLL, new File(osOutputDir, MSVCR71_DLL));
117         
118         copyInterpolatedPluginResource("windows/launcher.lap", new File(osOutputDir, getApplicationName() + ".lap"));
119 
120         copyTransitiveArtifacts(libDir);
121     }
122 
123     private String stringsToSeparatedJanelLines(final String[] strings) {
124         final StringBuilder stringLines = new StringBuilder();
125         if (strings.length > 0) {
126             for (final String string : strings) {
127                 stringLines.append(string);
128                 stringLines.append(LINE_SEPARATOR);
129             }
130         }
131         return stringLines.toString();
132     }
133 
134     private String systemPropertiesAsJanelLines(final String[] systemProperties) {
135         final StringBuilder sysPropLines = new StringBuilder();
136         if (systemProperties.length > 0) {
137             for (final String sysProp : systemProperties) {
138                 sysPropLines.append("-D");
139                 sysPropLines.append(sysProp);
140                 sysPropLines.append(LINE_SEPARATOR);
141             }
142         }
143         return sysPropLines.toString();
144     }
145 }