1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.miloss.fgsms.agentcore;
21
22 import java.io.File;
23 import java.io.FileInputStream;
24 import java.io.InputStream;
25 import java.net.URL;
26 import java.util.ArrayList;
27 import java.util.Arrays;
28 import java.util.HashMap;
29 import java.util.List;
30 import java.util.Properties;
31 import org.apache.log4j.Level;
32 import org.miloss.fgsms.common.Constants;
33 import org.miloss.fgsms.common.Constants.AuthMode;
34 import org.miloss.fgsms.common.Logger;
35 import org.miloss.fgsms.common.PropertyLoader;
36 import org.miloss.fgsms.common.Utility;
37 import org.miloss.fgsms.services.interfaces.automatedreportingservice.AutomatedReportingService;
38 import org.miloss.fgsms.services.interfaces.automatedreportingservice.AutomatedReportingService_Service;
39 import org.miloss.fgsms.services.interfaces.common.SecurityWrapper;
40 import org.miloss.fgsms.services.interfaces.dataaccessservice.DataAccessService;
41 import org.miloss.fgsms.services.interfaces.dataaccessservice.DataAccessService_Service;
42 import org.miloss.fgsms.services.interfaces.datacollector.DCS;
43 import org.miloss.fgsms.services.interfaces.datacollector.DataCollectorService;
44 import org.miloss.fgsms.services.interfaces.policyconfiguration.PCS;
45 import org.miloss.fgsms.services.interfaces.policyconfiguration.PolicyConfigurationService;
46 import org.miloss.fgsms.services.interfaces.reportingservice.ReportingService;
47 import org.miloss.fgsms.services.interfaces.reportingservice.ReportingService_Service;
48 import org.miloss.fgsms.services.interfaces.status.StatusService;
49 import org.miloss.fgsms.services.interfaces.status.StatusServiceService;
50 import us.gov.ic.ism.v2.ClassificationType;
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65 public class ConfigLoader {
66
67 public static final String PROP_MESSAGE_PROCESSOR_IMPL = "message.processor.impl";
68 static Logger log = Logger.getLogger(org.miloss.fgsms.common.Constants.LoggerName);
69 protected SecurityWrapper classlevel = new SecurityWrapper(ClassificationType.U, "None");
70 protected StatusService ssport = null;
71 protected StatusServiceService ss = null;
72 protected Algorithm SSalgo = Algorithm.FAILOVER;
73 protected int SSRetryCount = 2;
74 protected List<String> SS_URLS = new ArrayList<String>();
75 protected PolicyConfigurationService pcsservice = null;
76 protected PCS pcsport = null;
77 protected List<String> PCS_URLS = new ArrayList<String>();
78 protected Algorithm PCSalgo = Algorithm.FAILOVER;
79 protected int PCSRetryCount = 2;
80 protected DataCollectorService dcsservice = null;
81 protected DCS dcsport = null;
82 protected Algorithm DCSalgo = Algorithm.FAILOVER;
83 protected List<String> DCS_URLS = new ArrayList<String>();
84 protected int DCSRetryCount = 2;
85 protected Algorithm ACSalgo = Algorithm.FAILOVER;
86 protected int ACSRetryCount = 2;
87 protected List<String> ACS_URLS = new ArrayList<String>();
88 protected Algorithm ACSAalgo = Algorithm.FAILOVER;
89 protected int ACSARetryCount = 2;
90 protected List<String> ACSA_URLS = new ArrayList<String>();
91 protected ReportingService rsport = null;
92 protected ReportingService_Service rs = null;
93 protected DataAccessService dasport = null;
94 protected DataAccessService_Service das = null;
95
96 protected Algorithm RSAalgo = Algorithm.FAILOVER;
97 protected int RSRetryCount = 2;
98 protected List<String> RS_URLS = new ArrayList<String>();
99 protected AutomatedReportingService arsport = null;
100 protected AutomatedReportingService_Service ars = null;
101 protected Algorithm ARSAalgo = Algorithm.FAILOVER;
102 protected int ARSRetryCount = 2;
103 protected List<String> ARS_URLS = new ArrayList<String>();
104 protected Properties prop = null;
105 protected String username = "";
106 protected String password = "";
107 protected String certinfo = "";
108 protected org.miloss.fgsms.common.Constants.AuthMode mode_ = org.miloss.fgsms.common.Constants.AuthMode.None;
109 protected long discoveryInterval = 600000;
110 protected String offlinestorage = "";
111 protected UnavailableBehavior behavior = UnavailableBehavior.PURGE;
112 protected String javaxtruststore = null;
113 protected String javaxtruststorepass = null;
114 protected String javaxkeystore = null;
115 protected String javaxkeystorepass = null;
116 List<String> discover_providers = new ArrayList<String>();
117
118
119
120
121
122 public ConfigLoader(Properties p) throws ConfigurationException {
123 prop = p;
124 load();
125 }
126
127
128
129
130
131
132
133
134 public ConfigLoader(boolean p) throws ConfigurationException {
135
136 createClientProxies();
137
138 }
139
140
141
142
143
144
145
146
147 public static Properties loadProperties() throws ConfigurationException{
148
149 Properties prop=null;
150 try {
151
152 if (System.getProperties().containsKey("org.miloss.fgsms.agentConfigFileOverride")) {
153 InputStream is = null;
154 try {
155 is = new FileInputStream(System.getProperty("org.miloss.fgsms.agentConfigFileOverride"));
156 prop = new Properties();
157
158 prop.load(is);
159 log.log(Level.INFO, "Agent configuration loaded from System Property Override at " + System.getProperty("org.miloss.fgsms.agentConfigFileOverride") + Constants.Version);
160
161 } catch (Exception ex) {
162 log.log(Level.DEBUG, "", ex);
163 prop = null;
164 } finally {
165 if (is != null) {
166 try {
167 is.close();
168 } catch (Exception ex) {
169 log.log(Level.DEBUG, "", ex);
170 }
171 }
172 }
173 }
174 } catch (Exception ex) {
175 prop = null;
176 }
177
178 if (prop == null) {
179 try {
180 prop = new Properties();
181 URL file = (ClassLoader.getSystemClassLoader().getResource("fgsms-agent.properties"));
182 if (file != null) {
183 InputStream is = null;
184 try {
185 is = file.openStream();
186 prop.load(is);
187 log.log(Level.INFO, "Agent configuration loaded from System classloader at " + file.toString() + Constants.Version);
188 is.close();
189 } catch (Exception ex) {
190 log.log(Level.DEBUG, "", ex);
191 } finally {
192 if (is!=null)
193 try {
194 is.close();
195 } catch (Exception e2) {
196 log.log(Level.DEBUG, "", e2);
197 }
198 }
199 } else {
200 prop = null;
201 }
202 } catch (Exception ex) {
203 log.log(Level.DEBUG, "", ex);
204 prop = null;
205 }
206 }
207
208 if (prop == null) {
209 try {
210 prop = PropertyLoader.loadProperties("org/miloss/fgsms/agentcore/fgsms-agent");
211 log.log(Level.INFO, "Agent configuration loaded from embedded config file within fgsms-agentCore.jar. Use -Dorg.miloss.fgsms.agentConfigFileOverride=path/to/config.properties to override or include the file fgsms-agent.properties in the classpath." + Constants.Version);
212 } catch (Exception ex) {
213 log.log(Level.FATAL, "Could not load the fgsms-agent.properties files (embedded)" + Constants.Version, ex);
214
215 }
216 }
217 if (prop == null) {
218 throw new ConfigurationException("Cannot locate the fgsms-agent.properties file");
219 }
220 return prop;
221 }
222
223
224
225
226
227
228
229
230
231 public ConfigLoader() throws ConfigurationException {
232 this.prop = loadProperties();
233 load();
234
235 }
236 public Algorithm getRSAalgo() {
237 return RSAalgo;
238 }
239 public int getRSRetryCount() {
240 return RSRetryCount;
241 }
242 public List<String> getRS_URLS() {
243 return RS_URLS;
244 }
245
246 public int getACSARetryCount() {
247 return ACSARetryCount;
248 }
249
250 public List<String> getACSA_URLS() {
251 return ACSA_URLS;
252 }
253
254 public Algorithm getACSAalgo() {
255 return ACSAalgo;
256 }
257
258 public int getACSRetryCount() {
259 return ACSRetryCount;
260 }
261
262 public List<String> getACS_URLS() {
263 return ACS_URLS;
264 }
265
266 public Algorithm getACSalgo() {
267 return ACSalgo;
268 }
269
270 public Algorithm getARSAalgo() {
271 return ARSAalgo;
272 }
273
274 public int getARSRetryCount() {
275 return ARSRetryCount;
276 }
277
278 public List<String> getARS_URLS() {
279 return ARS_URLS;
280 }
281
282 public AutomatedReportingService getArsport() {
283 return arsport;
284 }
285
286 public ReportingService getRsport() {
287 return rsport;
288 }
289
290 public String getJavaxkeystore() {
291 return javaxkeystore;
292 }
293
294
295
296
297
298
299 public String getJavaxkeystorepass() {
300 return javaxkeystorepass;
301 }
302
303 public String getJavaxtruststore() {
304 return javaxtruststore;
305 }
306
307
308
309
310
311
312 public String getJavaxtruststorepass() {
313 return javaxtruststorepass;
314 }
315
316 public int getDCSRetryCount() {
317 return DCSRetryCount;
318 }
319
320 public List<String> getDCS_URLS() {
321 return DCS_URLS;
322 }
323
324 public Algorithm getDCSalgo() {
325 return DCSalgo;
326 }
327
328 public int getPCSRetryCount() {
329 return PCSRetryCount;
330 }
331
332 public List<String> getPCS_URLS() {
333 return PCS_URLS;
334 }
335
336 public Algorithm getPCSalgo() {
337 return PCSalgo;
338 }
339
340 public int getSSRetryCount() {
341 return SSRetryCount;
342 }
343
344 public List<String> getSS_URLS() {
345 return SS_URLS;
346 }
347
348 public Algorithm getSSalgo() {
349 return SSalgo;
350 }
351
352 public UnavailableBehavior getBehavior() {
353 return behavior;
354 }
355
356 public String getCertinfo() {
357 return certinfo;
358 }
359
360 public SecurityWrapper getClasslevel() {
361 return classlevel;
362 }
363
364 public DCS getDcsport() {
365 return dcsport;
366 }
367
368 public DataCollectorService getDcsservice() {
369 return dcsservice;
370 }
371
372 public long getDiscoveryInterval() {
373 return discoveryInterval;
374 }
375
376 public AuthMode getMode_() {
377 return mode_;
378 }
379
380 public String getOfflinestorage() {
381 return offlinestorage;
382 }
383
384
385
386
387
388
389
390 public String getPassword() {
391 return password;
392 }
393
394
395
396
397
398
399 public PCS getPcsport() {
400 return pcsport;
401 }
402
403
404
405
406
407
408 public PolicyConfigurationService getPcsservice() {
409 return pcsservice;
410 }
411
412
413
414
415
416
417
418 public Properties getProp() {
419 return prop;
420 }
421
422
423
424
425
426
427 public StatusServiceService getSs() {
428 return ss;
429 }
430
431
432
433
434
435
436 public StatusService getSsport() {
437 return ssport;
438 }
439
440
441
442
443
444
445 public String getUsername() {
446 return username;
447 }
448
449
450 private void createClientProxies() throws ConfigurationException {
451 String LastErrorMessage = "";
452
453
454
455
456
457
458
459
460
461
462
463 boolean fatalerror = false;
464 try {
465 pcsservice = new PolicyConfigurationService();
466 pcsport = pcsservice.getPCSPort();
467 } catch (Exception ex) {
468 fatalerror = true;
469 LastErrorMessage = "fgsms config loader, error caught initializing PCS client proxies. Unable to send data. Check the fgsms Agent properties file and ensure that the PCS WSDL location URL is defined and that the JAX-WS runtime is installed and configured within this container." + ex.getMessage();
470 log.log(Level.WARN, null, ex);
471 }
472 try {
473 dcsservice = new DataCollectorService();
474 dcsport = dcsservice.getDCSPort();
475 } catch (Exception ex) {
476 fatalerror = true;
477 LastErrorMessage = "fgsms config loader, error caught initializing DCS client proxies. Unable to send data. Check the fgsms Agent properties file and ensure that the DCS WSDL location URL is defined and that the JAX-WS runtime is installed and configured within this container." + ex.getMessage();
478 log.log(Level.WARN, null, ex);
479
480 }
481 try {
482 ss = new StatusServiceService();
483 ssport = ss.getStatusServicePort();
484 } catch (Exception ex) {
485 LastErrorMessage = "fgsms config loader, error caught initializing SS client proxies. Unable to send data. Check the fgsms Agent properties file and ensure that the DCS WSDL location URL is defined and that the JAX-WS runtime is installed and configured within this container." + ex.getMessage();
486 log.log(Level.WARN, null, ex);
487 }
488
489 try {
490 rs = new ReportingService_Service();
491 rsport = rs.getReportingServicePort();
492 } catch (Exception ex) {
493 LastErrorMessage = "fgsms config loader, error caught initializing RS client proxies. Unable to send data. Check the fgsms Agent properties file and ensure that the JAX-WS runtime is installed and configured within this container." + ex.getMessage();
494 log.log(Level.WARN, null, ex);
495 }
496
497 try {
498 ars = new AutomatedReportingService_Service();
499 arsport = ars.getAutomatedReportingServicePort();
500 } catch (Exception ex) {
501 LastErrorMessage = "fgsms config loader, error caught initializing ARS client proxies. Unable to send data. Check the fgsms Agent properties file and ensure that the JAX-WS runtime is installed and configured within this container." + ex.getMessage();
502 log.log(Level.WARN, null, ex);
503 }
504
505 try {
506 das = new DataAccessService_Service();
507 dasport = das.getDASPort();
508 } catch (Exception ex) {
509 LastErrorMessage = "fgsms config loader, error caught initializing DAS client proxies. Unable to send data. Check the fgsms Agent properties file and ensure that the JAX-WS runtime is installed and configured within this container." + ex.getMessage();
510 log.log(Level.WARN, null, ex);
511 }
512
513 if (!Utility.stringIsNullOrEmpty(LastErrorMessage)) {
514 log.log(Level.WARN, LastErrorMessage);
515 }
516 if (fatalerror) {
517 throw new ConfigurationException(LastErrorMessage);
518 }
519 }
520
521 private void load() throws ConfigurationException {
522
523 if (prop == null) {
524 throw new ConfigurationException("Cannot locate the fgsms-agent.properties file");
525 }
526 createClientProxies();
527
528 try {
529 behavior = UnavailableBehavior.valueOf(prop.getProperty("agent.unavailablebehavior").trim());
530 } catch (Exception ex) {
531 log.log(Level.WARN, null, ex);
532 behavior = UnavailableBehavior.PURGE;
533 }
534 try {
535 username = prop.getProperty("fgsms.AuthMode.Username").trim();
536 } catch (Exception ex) {
537 log.log(Level.WARN, null, ex);
538 }
539 try {
540 password = prop.getProperty("fgsms.AuthMode.Password").trim();
541 } catch (Exception ex) {
542 log.log(Level.WARN, null, ex);
543 }
544 try {
545 certinfo = prop.getProperty("fgsms.AuthMode.PKICert");
546 } catch (Exception ex) {
547 log.log(Level.WARN, null, ex);
548 }
549
550 try {
551 mode_ = org.miloss.fgsms.common.Constants.AuthMode.valueOf(prop.getProperty("fgsms.AuthMode").trim());
552 } catch (Exception ex) {
553 mode_ = AuthMode.UsernamePassword;
554 log.log(Level.WARN, null, ex);
555 }
556
557 try {
558 offlinestorage = prop.getProperty("agent.offlinestorage").trim();
559 if (behavior == UnavailableBehavior.HOLDPERSIST) {
560 EnsureFolderExists(offlinestorage);
561 }
562 } catch (Exception ex) {
563 log.log(Level.WARN, null, ex);
564 }
565 try {
566 PCSalgo = Algorithm.valueOf(prop.getProperty("policyconfigurationservice.algorithm").trim());
567 } catch (Exception ex) {
568 PCSalgo = Algorithm.FAILOVER;
569 log.log(Level.WARN, null, ex);
570 }
571 try {
572 DCSalgo = Algorithm.valueOf(prop.getProperty("datacollectorservice.algorithm").trim());
573 } catch (Exception ex) {
574 DCSalgo = Algorithm.FAILOVER;
575 log.log(Level.WARN, null, ex);
576 }
577 try {
578 SSalgo = Algorithm.valueOf(prop.getProperty("statusservice.algorithm").trim());
579 } catch (Exception ex) {
580 SSalgo = Algorithm.FAILOVER;
581 log.log(Level.WARN, null, ex);
582 }
583
584 try {
585 DCSRetryCount = Integer.parseInt(prop.getProperty("datacollectorservice.retry").trim());
586 } catch (Exception ex) {
587 DCSRetryCount = 2;
588 log.log(Level.WARN, null, ex);
589 }
590 try {
591 SSRetryCount = Integer.parseInt(prop.getProperty("statusservice.retry").trim());
592 } catch (Exception ex) {
593 SSRetryCount = 2;
594 log.log(Level.WARN, null, ex);
595 }
596
597 try {
598 PCSRetryCount = Integer.parseInt(prop.getProperty("policyconfigurationservice.retry").trim());
599 } catch (Exception ex) {
600 PCSRetryCount = 2;
601 log.log(Level.WARN, null, ex);
602 }
603 try {
604 discoveryInterval = Integer.parseInt(prop.getProperty("discovery.interval").trim());
605 } catch (Exception ex) {
606 log.log(Level.WARN, null, ex);
607 }
608 String t = prop.getProperty("policyconfigurationservice.URL");
609 if (t.contains("|")) {
610 String[] ts = t.split("\\|");
611 for (int i = 0; i < ts.length; i++) {
612 PCS_URLS.add(ts[i]);
613 }
614 } else {
615 PCS_URLS.add(t);
616 }
617
618 t = prop.getProperty("datacollectorservice.URL");
619 if (t.contains("|")) {
620 String[] ts = t.split("\\|");
621 for (int i = 0; i < ts.length; i++) {
622 DCS_URLS.add(ts[i]);
623 }
624 } else {
625 DCS_URLS.add(t);
626 }
627 t = prop.getProperty("statusservice.URL");
628 if (t.contains("|")) {
629 String[] ts = t.split("\\|");
630 for (int i = 0; i < ts.length; i++) {
631 SS_URLS.add(ts[i]);
632 }
633 } else {
634 SS_URLS.add(t);
635 }
636 t = prop.getProperty("fgsms.TrustStore.Password");
637 if (!Utility.stringIsNullOrEmpty(t)) {
638 this.javaxtruststorepass = t;
639 URL url = null;
640 try {
641 url = Thread.currentThread().getContextClassLoader().getResource("/META-INF/truststore.jks");
642 } catch (Exception ex) {
643 }
644 if (url == null) {
645 try {
646 url = Thread.currentThread().getContextClassLoader().getResource("META-INF/truststore.jks");
647 } catch (Exception ex) {
648 }
649 }
650 if (url != null) {
651 this.javaxtruststore = url.toString();
652 log.log(Level.INFO, "Loading truststore from " + javaxtruststore);
653 }
654 }
655 discover_providers = new ArrayList<String>();
656 t = prop.getProperty("discovery.impl");
657 if (!Utility.stringIsNullOrEmpty(t)) {
658 String[] s = t.split(",");
659 discover_providers.addAll(Arrays.asList(s));
660 }
661 t = prop.getProperty("fgsms.KeyStore.Password");
662 if (!Utility.stringIsNullOrEmpty(t)) {
663 this.javaxkeystorepass = t;
664 URL url = null;
665 try {
666 url = Thread.currentThread().getContextClassLoader().getResource("/META-INF/key.jks");
667 } catch (Exception ex) {
668 }
669 if (url == null) {
670 try {
671 url = Thread.currentThread().getContextClassLoader().getResource("META-INF/key.jks");
672
673 } catch (Exception ex) {
674 }
675 }
676 if (url != null) {
677 this.javaxkeystore = url.toString();
678 log.log(Level.INFO, "Loading keystore from " + javaxkeystore);
679 }
680 }
681
682 }
683
684
685
686
687
688
689
690 public List<String> getDiscovery_providers() {
691 return discover_providers;
692 }
693
694 public long getDeadMessageQueueDuration() {
695 String s = prop.getProperty("message.processor.dead.message.queue.duration");
696 long ret = 10000;
697 if (!Utility.stringIsNullOrEmpty(s)) {
698 try {
699 ret = Long.parseLong(s);
700 } catch (Exception ex) {
701 log.warn("error parsing message.processor.dead.message.queue.duration");
702 }
703 }
704
705 if (ret < 10000) {
706 ret = 10000;
707 }
708 return ret;
709 }
710
711 public boolean isDependencyInjectionEnabled() {
712 try {
713 return Boolean.parseBoolean(prop.getProperty("agent.dependencyinjection.enabled"));
714 } catch (Exception ex) {
715 log.warn("error parsing agent.dependencyinjection.enabled");
716 }
717 return false;
718 }
719
720 HashMap getWSAgentIgnoreList() {
721 HashMap IgnoreList = new HashMap();
722 String t = prop.getProperty("message.processor.ignoreList");
723 if (!Utility.stringIsNullOrEmpty(t)) {
724 if (t.contains("|")) {
725 String[] ts = t.split("\\|");
726 for (int i = 0; i < ts.length; i++) {
727 IgnoreList.put(ts[i].toLowerCase(), false);
728 }
729 } else {
730 IgnoreList.put(t.toLowerCase(), false);
731 }
732 }
733 return IgnoreList;
734 }
735 private void EnsureFolderExists(String folder) throws ConfigurationException {
736 File f = null;
737 try {
738 f = new File(folder);
739 if (f.exists()) {
740 return;
741 }
742 } catch (Exception ex) {
743 }
744 try {
745 new File(folder).mkdirs();
746 } catch (Exception ex) {
747 throw new ConfigurationException("Cannot ensure that the folder " + folder + " exists");
748 }
749 }
750
751 public enum Algorithm {
752
753 FAILOVER, ROUNDROBIN
754 }
755
756 public enum UnavailableBehavior {
757
758
759
760
761 PURGE,
762
763
764
765
766 HOLD,
767
768
769
770
771 HOLDPERSIST
772 }
773
774 }