/
ExpandRunServer.java
757 lines (665 loc) · 28.8 KB
/
ExpandRunServer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
/*
* Copyright 2010 DTO Labs, Inc. (http://dtolabs.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.dtolabs.rundeck;
import java.io.*;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.*;
import java.security.ProtectionDomain;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Properties;
import java.util.jar.Attributes;
import java.util.jar.JarFile;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
/*
* Note that any non java/javax from the JavaSE runtime that are included in this file must also
* be copied into the resulting rundeck-lanucher.jar via rundeckapp/scripts/BuildLauncher.groovy.
*/
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.GnuParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.OptionBuilder;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
/**
* ExpandRunServer extracts some contents to particular locations, generates config files based on internal templates,
* and then loads and starts the jetty server configured for the application.
*/
public class ExpandRunServer {
private static final String CONFIG_DEFAULTS_PROPERTIES = "config-defaults.properties";
//system props for launcher config
private static final String SYS_PROP_RUNDECK_LAUNCHER_DEBUG = "rundeck.launcher.debug";
private static final String SYS_PROP_RUNDECK_LAUNCHER_REWRITE = "rundeck.launcher.rewrite";
private static final String SYS_PROP_RUNDECK_JAASLOGIN = "rundeck.jaaslogin";
private static final String RUN_SERVER_CLASS = "com.dtolabs.rundeck.RunServer";
private static final String PROP_LOGINMODULE_NAME = "loginmodule.name";
private static final String SERVER_DATASTORE_PATH = "server.datastore.path";
private static final String RUNDECK_SERVER_CONFIG_DIR = "rundeck.server.configDir";
/**
* Config properties are defaulted in config-defaults.properties, but can be overridden by system properties
*/
final static String[] configProperties = {
"server.http.port",
"server.https.port",
"server.hostname",
"server.reportservice.port",
"rdeck.base",
SERVER_DATASTORE_PATH,
"default.user.name",
"default.user.password",
PROP_LOGINMODULE_NAME,
"loginmodule.conf.name",
"rundeck.config.name"
};
/**
* line separator
*/
private static final String LINESEP = System.getProperty("line.separator");
//members
String basedir;
File serverdir;
File configDir;
File thisJar;
String coreJarName;
boolean debug = false;
boolean rewrite = false;
boolean useJaas = true;
String versionString;
private String runClassName;
private String jettyLibsString;
private String jettyLibPath;
private static final String RUNDECK_START_CLASS = "Rundeck-Start-Class";
private static final String RUNDECK_JETTY_LIBS = "Rundeck-Jetty-Libs";
private static final String RUNDECK_JETTY_LIB_PATH = "Rundeck-Jetty-Lib-Path";
private static final String RUNDECK_VERSION = "Rundeck-Version";
private final Options options = new Options();
public static void main(final String[] args) throws Exception {
new ExpandRunServer().run(args);
}
@SuppressWarnings("static-access")
public ExpandRunServer() {
Option baseDir = OptionBuilder.withLongOpt("basedir")
.hasArg()
.withDescription("The basedir")
.withArgName("PATH")
.create('b');
Option serverDir = OptionBuilder.withLongOpt("serverdir")
.hasArg()
.withDescription("The base directory for the server")
.withArgName("PATH")
.create();
Option binDir = OptionBuilder.withLongOpt("bindir")
.hasArg()
.withArgName("PATH")
.withDescription("The install directory for the tools used by users.")
.create('x');
Option sbinDir = OptionBuilder.withLongOpt("sbindir")
.hasArg()
.withArgName("PATH")
.withDescription("The install directory for the tools used by administrators.")
.create('s');
Option configDir = OptionBuilder.withLongOpt("configdir")
.hasArg()
.withArgName("PATH")
.withDescription("The location of the configuration.")
.create('c');
Option dataDir = OptionBuilder.withLongOpt("datadir")
.hasArg()
.withArgName("PATH")
.withDescription("The location of Rundeck's runtime data.")
.create();
Option projectDir = OptionBuilder.withLongOpt("projectdir")
.hasArg()
.withArgName("PATH")
.withDescription("The location of Rundeck's project data.")
.create('p');
Option help = OptionBuilder.withLongOpt("help")
.withDescription("Display this message.")
.create('h');
Option debugFlag = OptionBuilder.withDescription("Show debug information")
.create('d');
Option skipInstall = OptionBuilder.withLongOpt("skipinstall")
.withDescription("Skip the extraction of the utilities from the launcher.")
.create();
options.addOption(baseDir);
options.addOption(dataDir);
options.addOption(serverDir);
options.addOption(binDir);
options.addOption(sbinDir);
options.addOption(configDir);
options.addOption(help);
options.addOption(debugFlag);
options.addOption(skipInstall);
options.addOption(projectDir);
debug = Boolean.getBoolean(SYS_PROP_RUNDECK_LAUNCHER_DEBUG);
rewrite = Boolean.getBoolean(SYS_PROP_RUNDECK_LAUNCHER_REWRITE);
useJaas = null == System.getProperty(SYS_PROP_RUNDECK_JAASLOGIN) || Boolean.getBoolean(
SYS_PROP_RUNDECK_JAASLOGIN);
runClassName = RUN_SERVER_CLASS;
thisJar = thisJarFile();
//load jar attributes
final Attributes mainAttributes = getJarMainAttributes();
if (null == mainAttributes) {
throw new RuntimeException("Unable to load attributes");
}
versionString = mainAttributes.getValue(RUNDECK_VERSION);
if (null != versionString) {
DEBUG("Rundeck version: " + versionString);
} else {
throw new RuntimeException("Jar file attribute not found: " + RUNDECK_VERSION);
}
runClassName = mainAttributes.getValue(RUNDECK_START_CLASS);
if (null == runClassName) {
throw new RuntimeException("Jar file attribute not found: " + RUNDECK_START_CLASS);
}
jettyLibsString = mainAttributes.getValue(RUNDECK_JETTY_LIBS);
if (null == jettyLibsString) {
throw new RuntimeException("Jar file attribute not found: " + RUNDECK_JETTY_LIBS);
}
jettyLibPath = mainAttributes.getValue(RUNDECK_JETTY_LIB_PATH);
if (null == jettyLibPath) {
throw new RuntimeException("Jar file attribute not found: " + RUNDECK_JETTY_LIB_PATH);
}
}
public void run(final String[] args) throws Exception {
CommandLineParser parser = new GnuParser();
final File toolsdir = new File(basedir, "tools");
File bindir = new File(toolsdir, "bin");
File toolslibdir = new File(toolsdir, "lib");
CommandLine cl = null;
try {
cl = parser.parse(this.options, args);
if(cl.hasOption('h')) {
// automatically generate the help statement
HelpFormatter formatter = new HelpFormatter();
formatter.printHelp( "java [JAVA_OPTIONS] -jar rundeck-launcher.jar ", "\nRun the rundeck server, installing the " +
"necessary components if they do not exist.\n", options,
"\nhttp://rundeck.org\n", true );
return;
}
} catch (ParseException e) {
// oops, something went wrong
System.err.println( "Parsing failed. Reason: " + e.getMessage() );
return;
}
debug = cl.hasOption('d');
DEBUG("Debugging is turned on.");
this.basedir = cl.getOptionValue("basedir", new File(thisJar.getAbsolutePath()).getParentFile().getAbsolutePath()); // TODO: is the first getAbsolutePath required?
this.serverdir = new File(cl.getOptionValue("serverdir", basedir+"/server"));
this.configDir = new File(cl.getOptionValue("c", serverdir + "/config"));
DEBUG("configDir is " + configDir.getAbsolutePath());
bindir = new File(cl.getOptionValue("bindir"), toolsdir+"/bin");
initArgs();
this.coreJarName = "rundeck-core-" + versionString + ".jar";
// serverdir = new File(basedir + "/server");
if (null != basedir) {
System.setProperty("rdeck.base", basedir);
}
// configDir = new File(serverdir, "config");
final Properties defaults = loadDefaults(CONFIG_DEFAULTS_PROPERTIES);
Properties configuration = new Properties();
if(!cl.hasOption("skipinstall")) {
final File libdir = new File(serverdir, "lib");
DEBUG("Extracting libs to: " + libdir.getAbsolutePath() + " ... ");
extractLibs(libdir);
extractJettyLibs(libdir);
final File expdir = new File(serverdir, "exp");
DEBUG("Extracting webapp to: " + expdir.getAbsolutePath() + " ... ");
extractWar(expdir);
DEBUG("Extracting bin scripts to: " + bindir.getAbsolutePath() + " ... ");
extractBin(bindir, new File(serverdir, "exp/webapp/WEB-INF/lib/" + coreJarName));
copyToolLibs(toolslibdir, new File(serverdir, "exp/webapp/WEB-INF/lib/" + coreJarName));
configuration = createConfiguration(defaults);
configuration.put("realm.properties.location", configDir.getAbsolutePath() + "/realm.properties");
DEBUG("Runtime configuration properties: " + configuration);
expandTemplates(configuration, serverdir, rewrite);
setScriptFilesExecutable(new File(serverdir, "sbin"));
} else {
configuration.putAll(defaults);
configuration.put(SERVER_DATASTORE_PATH, cl.getOptionValue("datadir", serverdir.getAbsolutePath()));
configuration.put(RUNDECK_SERVER_CONFIG_DIR, this.configDir.getAbsoluteFile());
}
if(cl.hasOption('p')) {
System.setProperty("rdeck.projects",cl.getOptionValue('p'));
}
execute(cl.getArgs(), configDir, new File(basedir), serverdir, configuration);
}
/**
* Set executable bit on any script files in the directory if it exists
* @param sbindir
*/
private void setScriptFilesExecutable(final File sbindir) {
//set executable on shell scripts
final FilenameFilter filenameFilter = new FilenameFilter() {
public boolean accept(final File file, final String s) {
return s.endsWith(".sh");
}
};
if (sbindir.exists()) {
for (final String s : sbindir.list(filenameFilter)) {
final File script = new File(sbindir, s);
if (!script.setExecutable(true)) {
ERR("Unable to set executable permissions for file: " + script.getAbsolutePath());
}
}
}
}
private void copyToolLibs(final File toolslibdir, final File coreJar) throws IOException {
if (!toolslibdir.isDirectory()) {
if (!toolslibdir.mkdirs()) {
ERR("Couldn't create bin dir: " + toolslibdir.getAbsolutePath());
return;
}
}
//get dependencies info
final JarFile zf = new JarFile(coreJar);
final String depslist = zf.getManifest().getMainAttributes().getValue("Rundeck-Tools-Dependencies");
if (null == depslist) {
throw new RuntimeException(
"Rundeck Core jar file manifest attribute \"Rundeck-Tools-Dependencies\" was not found: " + coreJar
.getAbsolutePath());
}
final String[] jars = depslist.split(" ");
//copy jars from list to toolslibdir
final String jarpath = jettyLibPath;
for (final String jarName : jars) {
ZipUtil.extractZip(thisJar.getAbsolutePath(), toolslibdir, jarpath + "/" + jarName, jarpath + "/");
}
//finally, copy corejar to toolslibdir
final File destfile = new File(toolslibdir, coreJarName);
if(!destfile.exists()) {
if(!destfile.createNewFile()) {
ERR("Unable to create file: " + destfile.createNewFile());
}
}
ZipUtil.copyStream(new FileInputStream(coreJar), new FileOutputStream(destfile));
}
/**
* Extract scripts to bin dir
*
* @param destDir
*/
private void extractBin(final File destDir, final File coreJar) throws IOException {
if (!destDir.isDirectory()) {
if (!destDir.mkdirs()) {
ERR("Couldn't create bin dir: " + destDir.getAbsolutePath());
return;
}
}
ZipUtil.extractZip(coreJar.getAbsolutePath(), destDir, "com/dtolabs/rundeck/core/cli/templates",
"com/dtolabs/rundeck/core/cli/templates/");
//set executable on shell scripts
for (final String s : destDir.list(new FilenameFilter() {
public boolean accept(File file, String s) {
return !s.endsWith(".bat");
}
})) {
File script = new File(destDir, s);
if(!script.setExecutable(true)) {
ERR("Unable to set executable permissions for file: " + script.getAbsolutePath());
}
}
}
/**
* Look for *.template files in the directory, duplicate to file "name" and expand properties
*
* @param props
* @param directory
* @param overwrite
*/
private void expandTemplates(final Properties props, final File directory, final boolean overwrite) throws
IOException {
if (overwrite) {
DEBUG("Configuration overwrite is TRUE");
}
final String tmplPrefix = "templates/";
final String tmplSuffix = ".template";
if (!directory.isDirectory() && !directory.mkdirs()) {
throw new RuntimeException("Unable to create config dir: " + directory.getAbsolutePath());
}
/**
* rename by removing suffix and prefix dir
*/
final ZipUtil.renamer renamer = new ZipUtil.renamer() {
public String rename(String input) {
if (input.endsWith(tmplSuffix)) {
input = input.substring(0, input.length() - tmplSuffix.length());
}
if (input.startsWith(tmplPrefix)) {
input = input.substring(tmplPrefix.length());
}
return input;
}
};
/**
* accept .template files in templates/ directory
* and only accept if destination file doesn't exist, or overwrite==true
*/
final FilenameFilter filenameFilter = new FilenameFilter() {
public boolean accept(final File file, final String name) {
final String destName = renamer.rename(name);
final File destFile;
if (null != props.getProperty(destName + ".location")) {
destFile = new File(props.getProperty(destName + ".location"));
} else {
destFile = new File(file, destName);
}
final boolean accept = (overwrite || !destFile.isFile())
&& name.startsWith(tmplPrefix)
&& name.endsWith(tmplSuffix);
if (accept) {
DEBUG("Writing config file: " + destFile.getAbsolutePath());
}
return accept;
}
};
ZipUtil.extractZip(thisJar.getAbsolutePath(), directory,
filenameFilter,
renamer,
//expand properties in-place
new propertyExpander(props));
}
private static class propertyExpander implements ZipUtil.streamCopier {
Properties properties;
public propertyExpander(final Properties properties) {
this.properties = properties;
}
public void copyStream(final InputStream in, final OutputStream out) throws IOException {
expandTemplate(in, out, properties);
}
}
/**
* Copy from file to toFile, expanding properties in the contents
*
* @param inputStream input stream
* @param outputStream output stream
* @param props properties
*/
private static void expandTemplate(final InputStream inputStream, final OutputStream outputStream,
final Properties props) throws IOException {
final BufferedReader read = new BufferedReader(new InputStreamReader(inputStream));
final BufferedWriter write = new BufferedWriter(new OutputStreamWriter(outputStream));
String line = read.readLine();
while (null != line) {
write.write(expandProperties(props, line));
write.write(LINESEP);
line = read.readLine();
}
write.flush();
write.close();
read.close();
}
/**
* Load properties file with default values in the jar
*
* @param path
*
* @return
*/
private Properties loadDefaults(final String path) {
final Properties properties = new Properties();
try {
final InputStream is = loadResourceInternal(CONFIG_DEFAULTS_PROPERTIES);
if (null == is) {
throw new RuntimeException("Unable to read config-defaults.properties from jar");
}
properties.load(is);
} catch (IOException e) {
throw new RuntimeException("Unable to load config defaults: " + path + ": " + e.getMessage(), e);
}
return properties;
}
/**
* Create properties for template expansion
*
* @return
*/
private Properties createConfiguration(final Properties defaults) throws UnknownHostException {
final Properties properties = new Properties();
properties.putAll(defaults);
final String localhostname = getHostname();
if (null != localhostname) {
properties.put("server.hostname", localhostname);
}
properties.put("rdeck.base", basedir);
properties.put(SERVER_DATASTORE_PATH, serverdir + "/data/grailsdb");
properties.put("rundeck.log.dir", serverdir + "/logs");
for (final String configProperty : configProperties) {
if (null != System.getProperty(configProperty)) {
properties.put(configProperty, System.getProperty(configProperty));
}
}
return properties;
}
private String getHostname() {
String name = null;
try {
name = InetAddress.getLocalHost().getHostName();
DEBUG("Determined hostname: " + name);
} catch (UnknownHostException ignored) {
}
return name;
}
/**
* Extract any jars in the lib/ resource dir to the destination
*
* @param libdir
*
* @throws IOException
*/
private void extractLibs(final File libdir) throws IOException {
//expand contents
ZipUtil.extractZip(thisJar.getAbsolutePath(), libdir, "lib", "lib/");
}
/**
* Use the jar attributes to extract selective libs for jetty dependencies
*
* @param libdir
*
* @throws IOException
*/
private void extractJettyLibs(final File libdir) throws IOException {
//expand contents
final String[] jarNames = jettyLibsString.split(" ");
final String jarpath = jettyLibPath;
for (final String jarName : jarNames) {
ZipUtil.extractZip(thisJar.getAbsolutePath(), libdir, jarpath + "/" + jarName, jarpath + "/");
}
}
private void extractWar(final File expdir) throws IOException {
//expand contents
ZipUtil.extractZip(thisJar.getAbsolutePath(), expdir, "pkgs", "pkgs/");
}
private void execute(final String[] args, final File configDir, final File baseDir, final File serverDir,
final Properties configuration) throws
IOException {
//set some system properties used by the RunServer class
System.setProperty("server.http.port", configuration.getProperty("server.http.port"));
System.setProperty(RUNDECK_SERVER_CONFIG_DIR, configDir.getAbsolutePath());
System.setProperty("rundeck.server.serverDir", serverDir.getAbsolutePath());
System.setProperty("rundeck.config.location", new File(configDir, configuration.getProperty(
"rundeck.config.name")).getAbsolutePath());
if (useJaas) {
System.setProperty("java.security.auth.login.config", new File(configDir,
configuration.getProperty("loginmodule.conf.name")).getAbsolutePath());
System.setProperty(PROP_LOGINMODULE_NAME, configuration.getProperty(PROP_LOGINMODULE_NAME));
}
//configure commandline arguments
final ArrayList<String> execargs = new ArrayList<String>();
execargs.add(baseDir.getAbsolutePath());
if (args.length > 1) {
execargs.addAll(Arrays.asList(Arrays.copyOfRange(args.clone(), 1, args.length)));
}
//execute the RunServer.main method
int result = 500;
try {
invokeMain(runClassName, execargs.toArray(new String[execargs.size()]), new File(
serverdir, "lib"));
result = 0;//success
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
DEBUG("Finished, exit code: " + result);
System.exit(result);
}
/**
* Invoke the main method on the given class, using the specified args, with a classloader including all jars in the
* specified libdir
*
* @param CLASSNAME class to invoke
* @param args arguments to pass to main method
* @param libdir dir containing required jar files
*
* @throws NoSuchMethodException
* @throws InvocationTargetException
* @throws IllegalAccessException
* @throws ClassNotFoundException
* @throws MalformedURLException
*/
public void invokeMain(final String CLASSNAME, final String[] args, final File libdir) throws NoSuchMethodException,
InvocationTargetException,
IllegalAccessException, ClassNotFoundException, MalformedURLException {
final ClassLoaderUtil clload = new ClassLoaderUtil(libdir);
// load the class
final ClassLoader loader = clload.getClassLoader(ClassLoader.getSystemClassLoader());
final Class cls = Class.forName(CLASSNAME, true, loader);
// invokde the main method via reflection
final Method mainMethod = ClassLoaderUtil.findMain(cls);
Thread.currentThread().setContextClassLoader(loader);
DEBUG("Start server with " + CLASSNAME + ".main(" + Arrays.toString(args) + ")");
mainMethod.invoke(null, new Object[]{args});
}
/**
* Return inputstream for a resource from the enclosing jar file
*
* @param path resource path
*
* @return InputStream for the resource
*
* @throws IOException if an error occurs
*/
private InputStream loadResourceInternal(final String path) throws IOException {
final ZipFile jar = new ZipFile(thisJar);
return jar.getInputStream(new ZipEntry(path));
}
/**
* Initialize field values based on parsed args and system properties. Sets the basedir to parent dir of the
* launcher jar if unset, and loads necessary manifest attributes for extracting the launcher jar contents.
*/
private void initArgs() {
if (null == basedir) {
//locate basedir based on this jar's location
//set basedir to the dir containing
final File base = new File(thisJar.getAbsolutePath()).getParentFile();
basedir = base.getAbsolutePath();
LOG("Rundeck basedir: " + basedir);
}
}
/**
* Load the manifest main attributes from the enclosing jar
*
* @return
*/
private static Attributes getJarMainAttributes() {
Attributes mainAttributes = null;
try {
final File file = thisJarFile();
final JarFile jarFile = new JarFile(file);
mainAttributes = jarFile.getManifest().getMainAttributes();
} catch (IOException e) {
e.printStackTrace();
}
return mainAttributes;
}
/**
* Return file for the enclosing jar
*
* @return
*
* @throws URISyntaxException
*/
private static File thisJarFile() {
final ProtectionDomain protectionDomain = ExpandRunServer.class.getProtectionDomain();
final URL location = protectionDomain.getCodeSource().getLocation();
try {
return new File(location.toURI());
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
}
/**
* Print log message
*
* @param s
*/
private void LOG(final String s) {
System.out.println(s);
}
/**
* Print err message
*
* @param s
*/
private void ERR(final String s) {
System.err.println("ERROR: " + s);
}
/**
* Print debug message if debug is enabled
*
* @param msg
*/
private void DEBUG(final String msg) {
if (debug) {
System.err.println("VERBOSE: " + msg);
}
}
private static final String PROPERTY_PATTERN = "\\$\\{([^\\}]+?)\\}";
/**
* Return the input with embedded property references expanded
*
* @param properties the properties to select form
* @param input the input
*
* @return string with references expanded
*/
public static String expandProperties(final Properties properties, final String input) {
final Pattern pattern = Pattern.compile(PROPERTY_PATTERN);
final Matcher matcher = pattern.matcher(input);
final StringBuffer sb = new StringBuffer();
while (matcher.find()) {
final String match = matcher.group(1);
if (null != properties.get(match)) {
matcher.appendReplacement(sb, Matcher.quoteReplacement(properties.getProperty(match)));
} else {
matcher.appendReplacement(sb, Matcher.quoteReplacement(matcher.group(0)));
}
}
matcher.appendTail(sb);
return sb.toString();
}
}