View Javadoc
1   /*
2     Copyright (C) 2020 - 2022 Alexander Kapitman
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  package ru.akman.maven.plugins.jpackage;
18  
19  import java.io.BufferedReader;
20  import java.io.File;
21  import java.io.IOException;
22  import java.nio.charset.Charset;
23  import java.nio.file.Files;
24  import java.util.Properties;
25  import org.apache.commons.lang3.StringUtils;
26  
27  /**
28   * Additional launcher.
29   */
30  public class Launcher {
31  
32    /**
33     * Launcher command (script) name.
34     */
35    private String name;
36  
37    /**
38     * Launcher properties file.
39     */
40    private File file;
41  
42    /**
43     * Main module name.
44     */
45    private String module;
46  
47    /**
48     * Main jar filename.
49     */
50    private String mainjar;
51  
52    /**
53     * Main class name.
54     */
55    private String mainclass;
56    
57    /**
58     * Command arguments.
59     */
60    private String arguments;
61  
62    /**
63     * Java runtime options.
64     */
65    private String javaoptions;
66  
67    /**
68     * Launcher command (script) version.
69     */
70    private String appversion;
71  
72    /**
73     * Launcher command (script) icon.
74     */
75    private File icon;
76  
77    /**
78     * Enable creating a console launcher (which requires console interactions).
79     */
80    private String winconsole;
81  
82    /**
83     * Get command (script) name.
84     *
85     * @return the command (script) name without extension
86     */
87    public String getName() {
88      return this.name;
89    }
90    
91    /**
92     * Set command (script) name.
93     *
94     * @param name the name of command (script)
95     */
96    public void setName(final String name) {
97      this.name = name;
98    }
99    
100   /**
101    * Get main module name.
102    *
103    * @return the main module name
104    */
105   public String getModule() {
106     return this.module;
107   }
108   
109   /**
110    * Set main module name.
111    *
112    * @param module the name of main module
113    */
114   public void setModule(final String module) {
115     this.module = module;
116   }
117 
118   /**
119    * Get main jar filename.
120    *
121    * @return the main jar filename
122    */
123   public String getMainJar() {
124     return this.mainjar;
125   }
126   
127   /**
128    * Set main jar filename.
129    *
130    * @param mainjar the main jar filename
131    */
132   public void setMainJar(final String mainjar) {
133     this.mainjar = mainjar;
134   }
135   
136   /**
137    * Get main class name.
138    *
139    * @return the main class name
140    */
141   public String getMainClass() {
142     return this.mainclass;
143   }
144   
145   /**
146    * Set main class name.
147    *
148    * @param mainclass the name of main class
149    */
150   public void setMainClass(final String mainclass) {
151     this.mainclass = mainclass;
152   }
153 
154   /**
155    * Get command (script) arguments.
156    *
157    * @return the command (script) arguments
158    */
159   public String getArguments() {
160     return this.arguments;
161   }
162   
163   /**
164    * Set command (script) arguments.
165    *
166    * @param arguments the command (script) arguments
167    */
168   public void setArguments(final String arguments) {
169     this.arguments = arguments;
170   }
171 
172   /**
173    * Get Java runtime options.
174    *
175    * @return the Java runtime options
176    */
177   public String getJavaOptions() {
178     return this.javaoptions;
179   }
180   
181   /**
182    * Set Java runtime options.
183    *
184    * @param javaoptions Java runtime options
185    */
186   public void setJavaOptions(final String javaoptions) {
187     this.javaoptions = javaoptions;
188   }
189 
190   /**
191    * Get launcher command (script) version.
192    *
193    * @return the launcher command (script) version
194    */
195   public String getAppVersion() {
196     return this.appversion;
197   }
198   
199   /**
200    * Set launcher command (script) version.
201    *
202    * @param appversion Launcher command (script) version.
203    */
204   public void setAppVersion(final String appversion) {
205     this.appversion = appversion;
206   }
207 
208   /**
209    * Get launcher command (script) icon.
210    *
211    * @return the launcher command (script) icon
212    */
213   public File getIcon() {
214     return this.icon;
215   }
216   
217   /**
218    * Set launcher command (script) icon.
219    *
220    * @param icon Launcher command (script) icon.
221    */
222   public void setIcon(final File icon) {
223     this.icon = icon;
224   }
225 
226   /**
227    * Is enabled creating a console launcher.
228    *
229    * @return true if creating a console launcher is enabled
230    */
231   public boolean isWinConsole() {
232     return String.valueOf(true).equals(this.winconsole);
233   }
234   
235   /**
236    * Enable or disable creating a console launcher.
237    *
238    * @param winconsole is creating a console launcher enabled
239    */
240   public void setWinConsole(final String winconsole) {
241     this.winconsole = winconsole;
242   }
243 
244   /**
245    * Get launcher properties file.
246    *
247    * @return the launcher properties file
248    */
249   public File getFile() {
250     return this.file;
251   }
252   
253   /**
254    * Set launcher properties file.
255    *
256    * @param file Launcher properties file.
257    */
258   public void setFile(final File file) {
259     this.file = file;
260   }
261 
262   /**
263    * Get the launcher properties.
264    *
265    * @param charset The charset using to read properties file.
266    *
267    * @return the launcher properties
268    *
269    * @throws IOException if IO errors occured
270    */
271   public Properties getProperties(final Charset charset) throws IOException {
272     final Properties props = new Properties();
273     if (file != null) {
274       try (BufferedReader br =
275           Files.newBufferedReader(file.toPath(), charset)) {
276         props.load(br);
277       }
278     }
279     if (!StringUtils.isBlank(module)) {
280       props.setProperty("module", module);
281     }
282     if (!StringUtils.isBlank(mainjar)) {
283       props.setProperty("main-jar", mainjar);
284     }
285     if (!StringUtils.isBlank(mainclass)) {
286       props.setProperty("main-class", mainclass);
287     }
288     if (!StringUtils.isBlank(arguments)) {
289       props.setProperty("arguments", arguments);
290     }
291     if (!StringUtils.isBlank(javaoptions)) {
292       props.setProperty("java-options", javaoptions);
293     }
294     if (!StringUtils.isBlank(appversion)) {
295       props.setProperty("app-version", appversion);
296     }
297     if (icon != null) {
298       props.setProperty("icon", icon.toString());
299     }
300     if (winconsole != null) {
301       props.setProperty("win-console", String.valueOf(isWinConsole()));
302     }
303     return props;
304   }
305 
306 }