1 package com.github.sbugat.rundeckmonitor.configuration;
2
3 import java.io.FileNotFoundException;
4 import java.io.IOException;
5 import java.io.Reader;
6 import java.io.Writer;
7 import java.nio.charset.StandardCharsets;
8 import java.nio.file.Files;
9 import java.nio.file.Path;
10 import java.nio.file.Paths;
11 import java.text.SimpleDateFormat;
12 import java.util.Date;
13 import java.util.Properties;
14
15 import com.github.sbugat.rundeckmonitor.wizard.InterfaceType;
16 import com.github.sbugat.rundeckmonitor.wizard.JobTabRedirection;
17
18
19
20
21
22
23
24 public final class RundeckMonitorConfiguration {
25
26
27 public static final String RUNDECK_MONITOR_PROPERTIES_FILE = "rundeckMonitor.properties";
28
29
30 public static final String RUNDECK_MONITOR_PROPERTY_URL = "rundeck.monitor.url";
31
32 public static final String RUNDECK_MONITOR_PROPERTY_API_KEY = "rundeck.monitor.api.key";
33
34 public static final String RUNDECK_MONITOR_PROPERTY_LOGIN = "rundeck.monitor.login";
35
36 public static final String RUNDECK_MONITOR_PROPERTY_PASSWORD = "rundeck.monitor.password";
37
38 public static final String RUNDECK_MONITOR_PROPERTY_PROJECT = "rundeck.monitor.project";
39
40 private static final String RUNDECK_MONITOR_PROPERTY_NAME = "rundeck.monitor.name";
41
42 public static final String RUNDECK_MONITOR_PROPERTY_NAME_DEFAULT_VALUE = "RundeckMonitor";
43
44 private static final String RUNDECK_MONITOR_PROPERTY_REFRESH_DELAY = "rundeck.monitor.refresh.delay";
45
46 private static final int RUNDECK_MONITOR_PROPERTY_REFRESH_DELAY_DEFAULT_VALUE = 60;
47
48 private static final String RUNDECK_MONITOR_PROPERTY_EXECUTION_LATE_THRESHOLD = "rundeck.monitor.execution.late.threshold";
49
50 private static final int RUNDECK_MONITOR_PROPERTY_EXECUTION_LATE_THRESHOLD_DEFAULT_VALUE = 1800;
51
52 private static final String RUNDECK_MONITOR_PROPERTY_FAILED_JOB_NUMBER = "rundeck.monitor.failed.job.number";
53
54 private static final int RUNDECK_MONITOR_PROPERTY_FAILED_JOB_NUMBER_DEFAULT_VALUE = 10;
55
56 private static final String RUNDECK_MONITOR_PROPERTY_DATE_FORMAT = "rundeck.monitor.date.format";
57
58 public static final String RUNDECK_MONITOR_PROPERTY_DATE_FORMAT_DEFAULT_VALUE = "dd/MM/yyyy HH:mm:ss";
59
60 private static final String RUNDECK_MONITOR_PROPERTY_API_VERSION = "rundeck.monitor.api.version";
61
62 private static final int RUNDECK_MONITOR_PROPERTY_API_VERSION_DEFAULT_VALUE = 5;
63
64 private static final String RUNDECK_MONITOR_PROPERTY_FAILED_JOB_REDIRECTION = "rundeck.monitor.failed.job.redirection";
65
66 private static final String RUNDECK_MONITOR_PROPERTY_FAILED_JOB_REDIRECTION_DEFAULT_VALUE = JobTabRedirection.SUMMARY.name();
67
68 private static final String RUNDECK_MONITOR_PROPERTY_DISABLE_VERSION_CHECKER = "rundeck.monitor.disable.version.checker";
69
70 private static final boolean RUNDECK_MONITOR_PROPERTY_DISABLE_VERSION_CHECKER_DEFAULT_VALUE = false;
71
72 private static final String RUNDECK_MONITOR_PROPERTY_INTERFACE_TYPE = "rundeck.monitor.interface.type";
73
74 private static final String RUNDECK_MONITOR_PROPERTY_INTERFACE_TYPE_DEFAULT_VALUE = InterfaceType.SWING.name();
75
76
77 private String rundeckUrl;
78
79
80 private String rundeckAPIKey;
81
82
83 private String rundeckLogin;
84
85
86 private String rundeckPassword;
87
88
89 private String rundeckProject;
90
91
92 private String rundeckMonitorName;
93
94
95 private int refreshDelay;
96
97
98 private int lateThreshold;
99
100
101 private int failedJobNumber;
102
103
104 private String dateFormat;
105
106
107 private int rundeckAPIversion;
108
109
110 private String jobTabRedirection;
111
112
113 private boolean versionCheckerDisabled;
114
115
116 private String interfaceType;
117
118
119
120
121 public RundeckMonitorConfiguration() {
122
123 }
124
125
126
127
128
129
130 public RundeckMonitorConfiguration(final RundeckMonitorConfiguration rundeckMonitorConfiguration) {
131
132 rundeckUrl = rundeckMonitorConfiguration.rundeckUrl;
133 rundeckAPIKey = rundeckMonitorConfiguration.rundeckAPIKey;
134 rundeckLogin = rundeckMonitorConfiguration.rundeckLogin;
135 rundeckPassword = rundeckMonitorConfiguration.rundeckPassword;
136
137 rundeckProject = rundeckMonitorConfiguration.rundeckProject;
138 rundeckMonitorName = rundeckMonitorConfiguration.rundeckMonitorName;
139 refreshDelay = rundeckMonitorConfiguration.refreshDelay;
140 lateThreshold = rundeckMonitorConfiguration.lateThreshold;
141 failedJobNumber = rundeckMonitorConfiguration.failedJobNumber;
142 dateFormat = rundeckMonitorConfiguration.dateFormat;
143 rundeckAPIversion = rundeckMonitorConfiguration.rundeckAPIversion;
144 jobTabRedirection = rundeckMonitorConfiguration.jobTabRedirection;
145 versionCheckerDisabled = rundeckMonitorConfiguration.versionCheckerDisabled;
146 }
147
148
149
150
151
152
153 public void loadConfigurationPropertieFile() throws IOException {
154
155
156 final Path propertyFile = Paths.get(RUNDECK_MONITOR_PROPERTIES_FILE);
157 if (!Files.exists(propertyFile)) {
158
159 throw new FileNotFoundException(RUNDECK_MONITOR_PROPERTIES_FILE);
160 }
161
162
163 final Properties properties = new Properties();
164 try (final Reader propertyFileReader = Files.newBufferedReader(propertyFile, StandardCharsets.UTF_8)) {
165 properties.load(propertyFileReader);
166 }
167
168 rundeckUrl = properties.getProperty(RUNDECK_MONITOR_PROPERTY_URL);
169 rundeckProject = properties.getProperty(RUNDECK_MONITOR_PROPERTY_PROJECT);
170
171 rundeckAPIKey = properties.getProperty(RUNDECK_MONITOR_PROPERTY_API_KEY);
172
173 rundeckLogin = properties.getProperty(RUNDECK_MONITOR_PROPERTY_LOGIN);
174 rundeckPassword = properties.getProperty(RUNDECK_MONITOR_PROPERTY_PASSWORD);
175
176 rundeckMonitorName = properties.getProperty(RUNDECK_MONITOR_PROPERTY_NAME, RUNDECK_MONITOR_PROPERTY_NAME_DEFAULT_VALUE);
177 refreshDelay = getIntegerProperty(properties, RUNDECK_MONITOR_PROPERTY_REFRESH_DELAY, RUNDECK_MONITOR_PROPERTY_REFRESH_DELAY_DEFAULT_VALUE);
178 lateThreshold = getIntegerProperty(properties, RUNDECK_MONITOR_PROPERTY_EXECUTION_LATE_THRESHOLD, RUNDECK_MONITOR_PROPERTY_EXECUTION_LATE_THRESHOLD_DEFAULT_VALUE);
179 failedJobNumber = getIntegerProperty(properties, RUNDECK_MONITOR_PROPERTY_FAILED_JOB_NUMBER, RUNDECK_MONITOR_PROPERTY_FAILED_JOB_NUMBER_DEFAULT_VALUE);
180 dateFormat = properties.getProperty(RUNDECK_MONITOR_PROPERTY_DATE_FORMAT, RUNDECK_MONITOR_PROPERTY_DATE_FORMAT_DEFAULT_VALUE);
181 rundeckAPIversion = getIntegerProperty(properties, RUNDECK_MONITOR_PROPERTY_API_VERSION, RUNDECK_MONITOR_PROPERTY_API_VERSION_DEFAULT_VALUE);
182 jobTabRedirection = properties.getProperty(RUNDECK_MONITOR_PROPERTY_FAILED_JOB_REDIRECTION, RUNDECK_MONITOR_PROPERTY_FAILED_JOB_REDIRECTION_DEFAULT_VALUE);
183 versionCheckerDisabled = getBooleanProperty(properties, RUNDECK_MONITOR_PROPERTY_DISABLE_VERSION_CHECKER, RUNDECK_MONITOR_PROPERTY_DISABLE_VERSION_CHECKER_DEFAULT_VALUE);
184 interfaceType = properties.getProperty(RUNDECK_MONITOR_PROPERTY_INTERFACE_TYPE, RUNDECK_MONITOR_PROPERTY_INTERFACE_TYPE_DEFAULT_VALUE);
185 }
186
187
188
189
190
191
192
193 public void verifyConfiguration() throws MissingPropertyException, InvalidPropertyException {
194
195
196
197 checkMandatoryStringProperty(rundeckUrl, RUNDECK_MONITOR_PROPERTY_URL);
198 checkMandatoryStringProperty(rundeckProject, RUNDECK_MONITOR_PROPERTY_PROJECT);
199
200 boolean missingAPIKey = false;
201 try {
202 checkMandatoryStringProperty(rundeckAPIKey, RUNDECK_MONITOR_PROPERTY_API_KEY);
203 }
204 catch (final MissingPropertyException | InvalidPropertyException e) {
205 missingAPIKey = false;
206 }
207
208 if (missingAPIKey) {
209 checkMandatoryStringProperty(rundeckLogin, RUNDECK_MONITOR_PROPERTY_LOGIN);
210 checkMandatoryStringProperty(rundeckPassword, RUNDECK_MONITOR_PROPERTY_PASSWORD);
211 }
212
213
214 try {
215 new SimpleDateFormat(dateFormat);
216 }
217 catch (final IllegalArgumentException e) {
218 dateFormat = RUNDECK_MONITOR_PROPERTY_DATE_FORMAT_DEFAULT_VALUE;
219 }
220
221
222 try {
223 JobTabRedirection.valueOf(jobTabRedirection);
224 }
225 catch (final IllegalArgumentException e) {
226 jobTabRedirection = RUNDECK_MONITOR_PROPERTY_FAILED_JOB_REDIRECTION_DEFAULT_VALUE;
227 }
228
229 }
230
231
232
233
234
235
236
237
238
239 private static void checkMandatoryStringProperty(final String property, final String propertyName) throws MissingPropertyException, InvalidPropertyException {
240
241 if (null == property) {
242 throw new MissingPropertyException(propertyName);
243 }
244 else if (property.isEmpty()) {
245 throw new InvalidPropertyException(propertyName, property);
246 }
247 }
248
249
250
251
252
253
254
255
256
257 private static int getIntegerProperty(final Properties properties, final String propertyName, final int defaultValue) {
258
259 final String propertyValue = properties.getProperty(propertyName, String.valueOf(defaultValue));
260
261 if (propertyValue.isEmpty()) {
262 return defaultValue;
263 }
264
265 try {
266 return Integer.parseInt(propertyValue);
267 }
268 catch (final NumberFormatException e) {
269 return defaultValue;
270 }
271 }
272
273
274
275
276
277
278
279
280
281 private static boolean getBooleanProperty(final Properties properties, final String propertyName, final boolean defaultValue) {
282
283 return Boolean.parseBoolean(properties.getProperty(propertyName, String.valueOf(defaultValue)));
284 }
285
286
287
288
289
290
291 public void saveMonitorConfigurationPropertieFile() throws IOException {
292
293
294 final Properties properties = new Properties();
295
296 properties.put(RUNDECK_MONITOR_PROPERTY_URL, rundeckUrl);
297 properties.put(RUNDECK_MONITOR_PROPERTY_API_KEY, rundeckAPIKey);
298 properties.put(RUNDECK_MONITOR_PROPERTY_LOGIN, rundeckLogin);
299 properties.put(RUNDECK_MONITOR_PROPERTY_PASSWORD, rundeckPassword);
300 properties.put(RUNDECK_MONITOR_PROPERTY_PROJECT, rundeckProject);
301 properties.put(RUNDECK_MONITOR_PROPERTY_NAME, rundeckMonitorName);
302 properties.put(RUNDECK_MONITOR_PROPERTY_REFRESH_DELAY, String.valueOf(refreshDelay));
303 properties.put(RUNDECK_MONITOR_PROPERTY_EXECUTION_LATE_THRESHOLD, String.valueOf(lateThreshold));
304 properties.put(RUNDECK_MONITOR_PROPERTY_FAILED_JOB_NUMBER, String.valueOf(failedJobNumber));
305 properties.put(RUNDECK_MONITOR_PROPERTY_DATE_FORMAT, dateFormat);
306 properties.put(RUNDECK_MONITOR_PROPERTY_API_VERSION, String.valueOf(rundeckAPIversion));
307 properties.put(RUNDECK_MONITOR_PROPERTY_FAILED_JOB_REDIRECTION, jobTabRedirection);
308 properties.put(RUNDECK_MONITOR_PROPERTY_DISABLE_VERSION_CHECKER, String.valueOf(versionCheckerDisabled));
309 properties.put(RUNDECK_MONITOR_PROPERTY_INTERFACE_TYPE, interfaceType);
310
311
312 final StringBuilder commentStringBuilder = new StringBuilder();
313 commentStringBuilder.append("Generated by RundeckMonitor wizard at: ");
314 try {
315 final SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);
316 commentStringBuilder.append(formatter.format(new Date()));
317 }
318 catch (final IllegalArgumentException e) {
319 commentStringBuilder.append(new Date());
320 }
321 commentStringBuilder.append(System.lineSeparator());
322 commentStringBuilder.append("-------------------------------------------------------");
323
324
325 final Path propertyFile = Paths.get(RUNDECK_MONITOR_PROPERTIES_FILE);
326 try (final Writer propertyFilewriter = Files.newBufferedWriter(propertyFile, StandardCharsets.UTF_8)) {
327 properties.store(propertyFilewriter, commentStringBuilder.toString());
328 }
329 }
330
331
332
333
334 public void disableVersionChecker() {
335
336 versionCheckerDisabled = true;
337 }
338
339
340
341
342
343
344 public static boolean propertiesFileExists() {
345
346 final Path propertyFile = Paths.get(RUNDECK_MONITOR_PROPERTIES_FILE);
347 return Files.exists(propertyFile) && Files.isReadable(propertyFile);
348 }
349
350
351
352
353
354
355
356 public static boolean propertiesFileUpdated(final Date date) {
357
358 final Path propertyFile = Paths.get(RUNDECK_MONITOR_PROPERTIES_FILE);
359 if (Files.exists(propertyFile)) {
360
361 final Date fileTime;
362 try {
363 fileTime = new Date(Files.getLastModifiedTime(propertyFile).toMillis());
364 }
365 catch (final IOException e) {
366 return false;
367 }
368
369 return fileTime.after(date);
370 }
371
372 return false;
373 }
374
375
376
377
378
379
380 public String getRundeckUrl() {
381 return rundeckUrl;
382 }
383
384
385
386
387
388
389 public String getRundeckAPIKey() {
390 return rundeckAPIKey;
391 }
392
393
394
395
396
397
398 public String getRundeckLogin() {
399 return rundeckLogin;
400 }
401
402
403
404
405
406
407 public String getRundeckPassword() {
408 return rundeckPassword;
409 }
410
411
412
413
414
415
416 public String getRundeckProject() {
417 return rundeckProject;
418 }
419
420
421
422
423
424
425 public String getRundeckMonitorName() {
426 return rundeckMonitorName;
427 }
428
429
430
431
432
433
434 public int getRefreshDelay() {
435 return refreshDelay;
436 }
437
438
439
440
441
442
443 public int getLateThreshold() {
444 return lateThreshold;
445 }
446
447
448
449
450
451
452 public int getFailedJobNumber() {
453 return failedJobNumber;
454 }
455
456
457
458
459
460
461 public String getDateFormat() {
462 return dateFormat;
463 }
464
465
466
467
468
469
470 public int getRundeckAPIversion() {
471 return rundeckAPIversion;
472 }
473
474
475
476
477
478
479 public String getJobTabRedirection() {
480 return jobTabRedirection;
481 }
482
483
484
485
486
487
488 public String getInterfaceType() {
489 return interfaceType;
490 }
491
492
493
494
495
496
497 public boolean isVersionCheckerEnabled() {
498 return !versionCheckerDisabled;
499 }
500
501
502
503
504
505
506 public void setRundeckUrl(final String rundeckUrlArg) {
507 rundeckUrl = rundeckUrlArg;
508 }
509
510
511
512
513
514
515 public void setRundeckAPIKey(final String rundeckAPIKeyArg) {
516 this.rundeckAPIKey = rundeckAPIKeyArg;
517 }
518
519
520
521
522
523
524 public void setRundeckLogin(final String rundeckLoginArg) {
525 this.rundeckLogin = rundeckLoginArg;
526 }
527
528
529
530
531
532
533 public void setRundeckPassword(final String rundeckPasswordArg) {
534 this.rundeckPassword = rundeckPasswordArg;
535 }
536
537
538
539
540
541
542 public void setRundeckProject(final String rundeckProjectArg) {
543 this.rundeckProject = rundeckProjectArg;
544 }
545
546
547
548
549
550
551 public void setRundeckMonitorName(final String rundeckMonitorNameArg) {
552 this.rundeckMonitorName = rundeckMonitorNameArg;
553 }
554
555
556
557
558
559
560 public void setRefreshDelay(final int refreshDelayArg) {
561 this.refreshDelay = refreshDelayArg;
562 }
563
564
565
566
567
568
569 public void setLateThreshold(final int lateThresholdArg) {
570 this.lateThreshold = lateThresholdArg;
571 }
572
573
574
575
576
577
578 public void setFailedJobNumber(final int failedJobNumberArg) {
579 this.failedJobNumber = failedJobNumberArg;
580 }
581
582
583
584
585
586
587 public void setDateFormat(final String dateFormatArg) {
588 this.dateFormat = dateFormatArg;
589 }
590
591
592
593
594
595
596 public void setRundeckAPIversion(final int rundeckAPIversionArg) {
597 this.rundeckAPIversion = rundeckAPIversionArg;
598 }
599
600
601
602
603
604
605 public void setJobTabRedirection(final String jobTabRedirectionArg) {
606 this.jobTabRedirection = jobTabRedirectionArg;
607 }
608
609
610
611
612
613
614 public void setInterfaceType(final String interfaceTypeArg) {
615 this.interfaceType = interfaceTypeArg;
616 }
617
618 }