1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.miloss.fgsms.common;
21
22 import com.sun.org.apache.xerces.internal.dom.DeferredElementImpl;
23 import java.io.ByteArrayInputStream;
24 import java.io.InputStream;
25 import java.net.InetAddress;
26 import java.sql.Connection;
27 import java.sql.Driver;
28 import java.sql.DriverManager;
29 import java.sql.PreparedStatement;
30 import java.text.ParsePosition;
31 import java.text.SimpleDateFormat;
32 import java.util.ArrayList;
33 import java.util.Arrays;
34 import java.util.Date;
35 import java.util.GregorianCalendar;
36 import java.util.List;
37 import java.util.Properties;
38 import java.util.UUID;
39 import javax.xml.bind.JAXBContext;
40 import javax.xml.bind.JAXBException;
41 import javax.xml.datatype.Duration;
42 import java.util.Calendar;
43 import javax.xml.parsers.DocumentBuilderFactory;
44 import javax.xml.xpath.XPath;
45 import javax.xml.xpath.XPathConstants;
46 import javax.xml.xpath.XPathFactory;
47 import org.apache.commons.lang3.StringEscapeUtils;
48 import org.apache.log4j.Level;
49
50 import org.miloss.fgsms.services.interfaces.automatedreportingservice.*;
51 import org.miloss.fgsms.services.interfaces.common.*;
52 import org.miloss.fgsms.services.interfaces.datacollector.AddData;
53 import org.miloss.fgsms.services.interfaces.datacollector.AddDataRequestMsg;
54 import org.miloss.fgsms.services.interfaces.datacollector.AddMoreData;
55 import org.miloss.fgsms.services.interfaces.policyconfiguration.*;
56 import org.miloss.fgsms.services.interfaces.reportingservice.ArrayOfReportTypeContainer;
57 import org.miloss.fgsms.services.interfaces.reportingservice.ExportRecordsEnum;
58 import org.miloss.fgsms.services.interfaces.reportingservice.ReportTypeContainer;
59 import org.w3c.dom.Document;
60 import org.w3c.dom.Node;
61 import us.gov.ic.ism.v2.ClassificationType;
62
63
64
65
66
67
68
69
70 public class Utility {
71
72 public static final String logname = "fgsms.Utility";
73 static final Logger log = Logger.getLogger(logname);
74
75 public static final String JDBC_PERFORMANCE = "jdbc/fgsmsPerformance";
76 public static final String JDBC_CONFIG = "jdbc/fgsmsConfig";
77 public static final String SECONDARY_POSTIFX = "Secondary";
78 private static final SimpleDateFormat dtg = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ");
79 private static JAXBContext serialctx = null;
80 private static JAXBContext arsctx = null;
81
82 private static final String PerformanceDBURL = "performancedbURL";
83 private static final String ConfigDBURL = "configdbURL";
84 private static final String ConfigUsername = "configUser";
85 private static final String ConfigPassword = "configPass";
86 private static final String PerformanceUsername = "perfUser";
87 private static final String PerformancePassword = "perfPass";
88
89 private static final String PerformanceDBURL_FAILOVER = "performancedbURL_FAILOVER";
90 private static final String ConfigDBURL_FAILOVER = "configdbURL_FAILOVER";
91 private static final String ConfigUsername_FAILOVER = "configUser_FAILOVER";
92 private static final String ConfigPassword_FAILOVER = "configPass_FAILOVER";
93 private static final String PerformanceUsername_FAILOVER = "perfUser_FAILOVER";
94 private static final String PerformancePassword_FAILOVER = "perfPass_FAILOVER";
95 private static final String DBdriver = "driver";
96 public static final String PropertyFilePath = "org/miloss/fgsms/common/database";
97 private static String myHostname = null;
98
99 public static String formatDateTime(Calendar cal) {
100 synchronized (dtg) {
101 if (cal == null) {
102 return "unknown";
103 }
104 return dtg.format(cal.getTime());
105 }
106 }
107
108 public static String formatDateTime(Date cal) {
109 synchronized (dtg) {
110 if (cal == null) {
111 return "unknown";
112 }
113 return dtg.format(cal);
114 }
115 }
116
117 public static String formatDateTime(long timestampEpoch) {
118 synchronized (dtg) {
119 return dtg.format(new Date(timestampEpoch));
120 }
121 }
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136 public static Calendar parseDateTime(String s) throws Exception {
137
138 SimpleDateFormat f1 = new SimpleDateFormat("MM/dd/yyyy");
139 SimpleDateFormat f2 = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss");
140
141 SimpleDateFormat f3 = new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy");
142
143 SimpleDateFormat f4 = new SimpleDateFormat("EEE MM/dd/yyyy HH:mm:ss.ms");
144 SimpleDateFormat f5 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.zzzzzzz");
145 Date d = null;
146
147 try {
148 d = f1.parse(s, new ParsePosition(0));
149 } catch (Exception ex) {
150 }
151 if (d == null) {
152 try {
153 d = f2.parse(s, new ParsePosition(0));
154 } catch (Exception ex) {
155 }
156 }
157 if (d == null) {
158 try {
159 d = f3.parse(s, new ParsePosition(0));
160 } catch (Exception ex) {
161 }
162 }
163 if (d == null) {
164 try {
165 d = f4.parse(s, new ParsePosition(0));
166 } catch (Exception ex) {
167 }
168 }
169 if (d == null) {
170 try {
171 d = f5.parse(s, new ParsePosition(0));
172 } catch (Exception ex) {
173 }
174 }
175 if (d != null) {
176 GregorianCalendar gcal = new GregorianCalendar();
177 gcal.setTime(d);
178 return (gcal);
179 }
180 try {
181 long timestamp = Long.parseLong(s);
182 GregorianCalendar gcal = new GregorianCalendar();
183 gcal.setTimeInMillis(timestamp);
184 return (gcal);
185 } catch (Exception x) {
186 }
187 throw new Exception("Unable to parse the date, see documentation for correct usage");
188 }
189
190
191
192
193
194
195 public static void addStatusChangeSLA(ServicePolicy ret) {
196 if (ret.getServiceLevelAggrements() == null) {
197 ret.setServiceLevelAggrements(new ArrayOfSLA());
198 }
199 boolean existingSla = false;
200 for (int i = 0; i < ret.getServiceLevelAggrements().getSLA().size(); i++) {
201 if (ret.getServiceLevelAggrements().getSLA().get(i).getRule() instanceof SLARuleGeneric) {
202 SLARuleGeneric r = (SLARuleGeneric) ret.getServiceLevelAggrements().getSLA().get(i).getRule();
203 if (r.getClassName().equalsIgnoreCase("org.miloss.fgsms.sla.rules.ChangeInAvailabilityStatus")) {
204 existingSla = true;
205 }
206 }
207 }
208 if (!existingSla) {
209 SLA s = new SLA();
210 SLARuleGeneric r = new SLARuleGeneric();
211 r.setClassName("org.miloss.fgsms.sla.rules.ChangeInAvailabilityStatus");
212 r.setProcessAt(RunAtLocation.FGSMS_SERVER);
213
214 s.setRule(r);
215 s.setGuid(UUID.randomUUID().toString());
216 s.setAction(new ArrayOfSLAActionBaseType());
217
218 SLAAction e = Utility.newEmailAction(null, "Change of status for " + Utility.encodeHTML(ret.getURL()),
219 "Change of status for " + Utility.encodeHTML(ret.getURL()) + ". This is the default status alert. This can be changed by changing the policy for this service.");
220 s.getAction().getSLAAction().add(e);
221 ret.getServiceLevelAggrements().getSLA().add(s);
222 }
223
224 existingSla = false;
225 for (int i = 0; i < ret.getServiceLevelAggrements().getSLA().size(); i++) {
226 if (ret.getServiceLevelAggrements().getSLA().get(i).getRule() instanceof SLARuleGeneric) {
227 SLARuleGeneric r = (SLARuleGeneric) ret.getServiceLevelAggrements().getSLA().get(i).getRule();
228 if (r.getClassName().equalsIgnoreCase("org.miloss.fgsms.sla.rules.StaleData")) {
229 existingSla = true;
230 }
231 }
232 }
233 if (!existingSla) {
234 SLA s = new SLA();
235 SLARuleGeneric r = new SLARuleGeneric();
236 r.setProcessAt(RunAtLocation.FGSMS_SERVER);
237 r.setClassName("org.miloss.fgsms.sla.rules.StaleData");
238
239 s.setRule(r);
240 s.setGuid(UUID.randomUUID().toString());
241 s.setAction(new ArrayOfSLAActionBaseType());
242 SLAAction e = Utility.newEmailAction(null, "Stale Data Alert for " + Utility.encodeHTML(ret.getURL()),
243 "Stale Data Alert for " + Utility.encodeHTML(ret.getURL()) + ". This is the default alert that indicates that either a server or agent is offline. This alert can be changed by changing the policy for this service.");
244
245 s.getAction().getSLAAction().add(e);
246 ret.getServiceLevelAggrements().getSLA().add(s);
247
248 }
249 }
250
251
252
253
254
255
256
257
258 public synchronized static JAXBContext getARSSerializationContext() throws JAXBException {
259
260 if (arsctx == null) {
261 arsctx = JAXBContext.newInstance(ReportDefinition.class, TimeRangeDiff.class, ScheduleDefinition.class,
262 AbstractSchedule.class, ExportCSVDataRequestMsg.class, ExportDataRequestMsg.class, Daynames.class, DailySchedule.class, WeeklySchedule.class, MonthlySchedule.class,
263 SLAAction.class, NameValuePair.class, OneTimeSchedule.class,
264 SecurityWrapper.class, TimeRange.class, ArrayOfReportTypeContainer.class, ReportTypeContainer.class,
265 ExportDataRequestMsg.class, String.class, Calendar.class,
266 ExportCSVDataRequestMsg.class, ExportRecordsEnum.class);
267 }
268 return arsctx;
269
270 }
271
272
273
274
275
276
277
278
279
280 public synchronized static JAXBContext getSerializationContext() throws JAXBException {
281
282 if (serialctx == null) {
283 serialctx = JAXBContext.newInstance(
284 ArrayOfSLA.class,
285 SLA.class, SLAAction.class,
286
287 RuleBaseType.class,
288 GeoTag.class, Long.class, double.class,
289 String.class, ServicePolicy.class, GeoTag.class, ArrayOfUserIdentity.class, Duration.class,
290 PolicyType.class, ArrayOfSLAActionBaseType.class, ArrayOfServicePolicy.class,
291
292
293
294
295
296
297 FederationPolicy.class,
298
299 FederationPolicyCollection.class,
300 AddData.class, AddMoreData.class, AddDataRequestMsg.class, Header.class,
301 ProcessPolicy.class, MachinePolicy.class, AlertMessageDefinition.class, StatusServicePolicy.class, StatisticalServicePolicy.class,
302 TransactionalWebServicePolicy.class,
303 RunAtLocation.class,
304 DriveInformation.class,
305 SetProcessListByMachineResponseMsg.class,
306 SetProcessListByMachineRequestMsg.class,
307 MachineInformation.class, NetworkAdapterInfo.class, PropertyPair.class, NetworkAdapterPerformanceData.class,
308
309 NameValuePair.class,
310 SLARuleGeneric.class,
311
312 org.miloss.fgsms.services.interfaces.common.NameValuePairSet.class);
313 }
314
315 return serialctx;
316
317 }
318
319
320
321
322
323
324
325 public static void validateClassification(SecurityWrapper w) {
326 if (w == null || w.getClassification() == null) {
327 throw new IllegalArgumentException("A classification level must be specified");
328 }
329 }
330
331
332
333
334
335
336 public static String getHostName() {
337 if (myHostname == null) {
338 try {
339 InetAddress addr = InetAddress.getLocalHost();
340
341
342
343
344 myHostname = addr.getHostName().toLowerCase();
345 } catch (Exception e) {
346 myHostname = "ADDRESS_UNKNOWN";
347 }
348 }
349 return myHostname;
350 }
351
352
353
354
355
356
357
358 public static boolean hasEmailSLA(ServicePolicy pc) {
359 if (pc == null) {
360 return false;
361 }
362 if (pc.getServiceLevelAggrements() == null
363 || pc.getServiceLevelAggrements().getSLA().isEmpty()) {
364 return false;
365 }
366 for (int i = 0; i < pc.getServiceLevelAggrements().getSLA().size(); i++) {
367 if (pc.getServiceLevelAggrements().getSLA().get(i).getAction() != null
368 && !pc.getServiceLevelAggrements().getSLA().get(i).getAction().getSLAAction().isEmpty()) {
369 for (int k = 0; k < pc.getServiceLevelAggrements().getSLA().get(i).getAction().getSLAAction().size(); k++) {
370 if (pc.getServiceLevelAggrements().getSLA().get(i).getAction().getSLAAction().get(k).getImplementingClassName().equals("org.miloss.fgsms.sla.actions.EmailAlerter")) {
371 return true;
372 }
373 }
374 }
375
376 }
377 return false;
378 }
379
380
381
382
383
384
385
386 public static boolean hasEmailSLA(ArrayOfSLAActionBaseType pc) {
387 if (pc == null) {
388 return false;
389 }
390 if (pc.getSLAAction().isEmpty()) {
391 return false;
392 }
393
394 for (int i = 0; i < pc.getSLAAction().size(); i++) {
395
396 if (pc.getSLAAction().get(i).getImplementingClassName().equals("org.miloss.fgsms.sla.actions.EmailAlerter")) {
397 return true;
398 }
399 }
400 return false;
401 }
402
403
404
405
406
407
408
409
410 public static String EN(String clear) {
411 if (stringIsNullOrEmpty(clear)) {
412 return "";
413 }
414 try {
415 return AES.EN(clear);
416 } catch (Exception ex) {
417 log.log(Level.FATAL, "############################################# fgsms cannot encrypt! Check to make sure the unlimited strength JCE is installed: " + ex.getMessage());
418 }
419 return "";
420 }
421
422
423
424
425
426
427
428
429
430
431 public static String DE(String cipher) {
432 if (stringIsNullOrEmpty(cipher)) {
433 return "";
434 }
435 try {
436 return AES.DE(cipher);
437 } catch (Exception ex) {
438 log.log(Level.FATAL, "############################################# fgsms cannot decrypt! Check to make sure the unlimited strength JCE is installed: " + ex.getMessage());
439 }
440 return cipher;
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 public static String durationToString(Duration d) {
475 if (d == null) {
476 return "";
477 }
478 String s = "";
479 if (d.getYears() > 0) {
480 s += d.getYears() + "yr ";
481 }
482 if (d.getMonths() > 0) {
483 s += d.getMonths() + "mo ";
484 }
485 if (d.getDays() > 0) {
486 s += d.getDays() + "d ";
487 }
488 if (d.getHours() > 0) {
489 s += d.getHours() + "hr ";
490 }
491 if (d.getMinutes() > 0) {
492 s += d.getMinutes() + "min ";
493 }
494 if (d.getSeconds() > 0) {
495 s += d.getSeconds() + "s ";
496 }
497 return s.trim();
498 }
499
500
501
502
503
504
505
506 public static String durationLargestUnitToString(Duration d) {
507 if (d == null) {
508 return "";
509 }
510 if (d.getYears() > 0) {
511 return d.getYears() + "yr";
512 }
513 if (d.getMonths() > 0) {
514 return d.getMonths() + "mo";
515 }
516 if (d.getDays() > 0) {
517 return d.getDays() + "d";
518 }
519 if (d.getHours() > 0) {
520 return d.getHours() + "hr";
521 }
522 if (d.getMinutes() > 0) {
523 return d.getMinutes() + "min";
524 }
525 if (d.getSeconds() > 0) {
526 return d.getSeconds() + "s";
527 }
528 return "";
529 }
530
531
532
533
534
535
536
537 public static String ICMClassificationToString(ClassificationType type) {
538
539 if (type == null) {
540 return "UNCLASSIFIED";
541 }
542 switch (type) {
543 case U:
544 return "UNCLASSIFIED";
545 case R:
546 return "RESTRICTED";
547 case C:
548 return "CONFIDENTIAL";
549 case S:
550 return "SECRET";
551 case TS:
552 return "TOP SECRET";
553 case CTS:
554 return "COSMIC TS";
555 case NU:
556 return "NATO UNCLASSIFIED";
557 case NC:
558 return "NATO CONFIDENTIAL";
559 case NR:
560 return "NATO RESTRICTED";
561 case NS:
562 return "NATO SECRET";
563 case CTSA:
564 return "COSMIC TOP SECRET ATOMAL";
565 case NSAT:
566 return "NATO SECRET ATOMAL";
567 case NCA:
568 return "NATO CLASSIFIED ATOMAL";
569 case CTS_BALK:
570 return "COSMIC TOP SECRET BALK";
571 case CTS_B:
572 return "COSMIC TOP SECRET Bohemia";
573 default:
574 return "CLASSIFIED: " + type.value();
575 }
576 }
577
578
579
580
581
582
583
584 public static String ICMClassificationToColorCodeString(ClassificationType type) {
585
586 if (type == null) {
587 return "lime";
588 }
589 switch (type) {
590 case U:
591 return "lime";
592 case R:
593 return "yellow";
594 case C:
595 return "blue";
596 case S:
597 return "red";
598 case TS:
599 return "orange";
600 case CTS:
601 return "red";
602 case NU:
603 return "lime";
604 case NC:
605 return "purple";
606 case NR:
607 return "purple";
608 case NS:
609 return "purple";
610 case CTSA:
611 return "orange";
612 case NSAT:
613 return "purple";
614 case NCA:
615 return "purple";
616 case CTS_BALK:
617 return "orange";
618 case CTS_B:
619 return "orange";
620 default:
621 return "red";
622 }
623 }
624
625
626
627
628
629 public static String listStringtoString(List<String> data) {
630 if (data == null || data.isEmpty()) {
631 return "";
632 }
633 StringBuilder sb = new StringBuilder();
634
635 for (int i = 0; i < data.size(); i++) {
636 if (!stringIsNullOrEmpty(data.get(i))) {
637 String t = data.get(i);
638 t = t.trim();
639 if (!stringIsNullOrEmpty(t)) {
640 sb = sb.append(t).append(" ");
641
642 }
643
644 }
645 }
646 return sb.toString().trim();
647 }
648
649
650
651
652
653
654
655
656 public static String getActionNameFromXML(String xml) {
657 if (stringIsNullOrEmpty(xml)) {
658 return "";
659 }
660 try {
661 InputStream is = null;
662 Document xmlDocument = null;
663 try {
664 is = new ByteArrayInputStream(xml.trim().getBytes(Constants.CHARSET));
665 xmlDocument = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(is);
666 } catch (Exception ex) {
667 log.log(Level.DEBUG, "fgsms Utility, error identifying request method by building request message as xml document: " + ex.getLocalizedMessage());
668 }
669 if (xmlDocument == null) {
670 String workingdoc = "";
671 try {
672 workingdoc = xml.substring(xml.indexOf("<"), xml.lastIndexOf(">"));
673 } catch (Exception ex) {
674 }
675 if (Utility.stringIsNullOrEmpty(workingdoc)) {
676 log.log(Level.WARN, "fgsms Utility, error identifying request method by building request message as xml document: this most likely isn't xml");
677 return "";
678 }
679
680 if (Utility.stringIsNullOrEmpty(xml))
681 {
682 is = new ByteArrayInputStream(workingdoc.getBytes(Constants.CHARSET));
683 }
684 xmlDocument = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(is);
685
686 }
687
688 XPath xpath = XPathFactory.newInstance().newXPath();
689 javax.xml.xpath.XPathExpression xp = xpath.compile("/Envelope/Body");
690 try {
691 DeferredElementImpl j = (DeferredElementImpl) xp.evaluate(xmlDocument, XPathConstants.NODE);
692
693
694 for (int i = 0; i < j.getChildNodes().getLength(); i++) {
695 if (!j.getChildNodes().item(i).getNodeName().equalsIgnoreCase("#text")) {
696 log.log(Level.DEBUG, "Found action via com.sun.org.apache.xerces.internal.dom.DeferredElementImpl");
697 return j.getChildNodes().item(i).getNodeName();
698
699 }
700 }
701 } catch (NoClassDefFoundError c) {
702 log.log(Level.DEBUG, "couldn't find com.sun.org.apache.xerces.internal.dom.DeferredElementImpl, trying alternate method...");
703 } catch (ClassCastException e) {
704 log.log(Level.DEBUG, "couldn't find com.sun.org.apache.xerces.internal.dom.DeferredElementImpl, trying alternate method...");
705 } catch (Exception ex) {
706 log.log(Level.DEBUG, "error caught using com.sun.org.apache.xerces.internal.dom.DeferredElementImpl, trying alternate method...", ex);
707 }
708
709 try {
710 org.apache.xerces.dom.DeferredElementImpl j = (org.apache.xerces.dom.DeferredElementImpl) xp.evaluate(xmlDocument, XPathConstants.NODE);
711 for (int i = 0; i < j.getChildNodes().getLength(); i++) {
712 if (!j.getChildNodes().item(i).getNodeName().equalsIgnoreCase("#text")) {
713 Node n = j.getChildNodes().item(i);
714 String s = n.getNodeName();
715 s = n.getNodeValue();
716 s = n.getNodeValue();
717 s = n.getBaseURI();
718 log.log(Level.DEBUG, "Found action via org.apache.xerces.dom.DeferredElementImpl");
719 return j.getChildNodes().item(i).getNodeName();
720
721 }
722 }
723
724 } catch (NoClassDefFoundError c) {
725 log.log(Level.DEBUG, "couldn't find org.apache.xerces.dom.DeferredElementImpl, giving up determining action");
726 } catch (Exception ex) {
727 log.log(Level.DEBUG, "error caught using org.apache.xerces.dom.DeferredElementImpl", ex);
728 }
729
730 } catch (Exception ex) {
731 log.log(Level.DEBUG, "fgsms Utility, error identifying request method from xml, defaulted to urn:undeterminable", ex);
732 }
733 return "";
734 }
735
736
737
738
739
740
741
742
743 public static String encodeHTML(String s) {
744 if (s == null || s.length() == 0) {
745 return "";
746 }
747 return StringEscapeUtils.escapeHtml4(s);
748
749
750
751
752
753
754
755
756
757
758 }
759
760
761
762
763
764
765
766 public static boolean stringIsNullOrEmpty(String s) {
767 if (s == null) {
768 return true;
769 }
770 if (s.length() == 0) {
771 return true;
772 }
773 return false;
774 }
775
776
777
778
779
780
781
782 public static long durationToTimeInMS(Duration d) {
783 if (d == null) {
784 throw new IllegalArgumentException("duration cannot be null");
785 }
786
787 return d.getTimeInMillis(new GregorianCalendar());
788
789
790
791
792
793
794 }
795
796
797
798
799
800
801
802
803
804 public static String truncate(String x, int length) {
805 if (x == null || x.length() == 0) {
806 return "";
807 }
808 String ret = x;
809 if (x.length() > length) {
810 ret = x.substring(0, length);
811 }
812 return ret;
813 }
814
815 @Deprecated
816 private static Connection getPerformanceDB_NONPOOLED_Connection_FAILOVER() {
817 try {
818 Properties prop = PropertyLoader.loadProperties(PropertyFilePath);
819 Driver d = (Driver) Class.forName(prop.getProperty(DBdriver)).newInstance();
820 DriverManager.registerDriver(d);
821 Connection con = DriverManager.getConnection(
822 prop.getProperty(PerformanceDBURL_FAILOVER),
823 prop.getProperty(PerformanceUsername_FAILOVER),
824 DE(prop.getProperty(PerformancePassword_FAILOVER)));
825 PreparedStatement com = con.prepareStatement("select 1;");
826 try {
827 com.execute();
828 DBUtils.safeClose(com);
829 return con;
830 } catch (Exception e) {
831 log.log(Level.FATAL, "Error obtaining secondary perf database connection, msg:" + e.getLocalizedMessage(), e);
832 return null;
833 } finally {
834 DBUtils.safeClose(com);
835 }
836 } catch (Exception ex) {
837 log.log(Level.FATAL, "No connection to the secondary database could be made, msg:" + ex.getLocalizedMessage(), ex);
838 return null;
839 }
840 }
841
842
843
844
845
846
847
848
849
850
851
852 @Deprecated
853 public static Connection getPerformanceDB_NONPOOLED_Connection() {
854 try {
855 Properties prop = PropertyLoader.loadProperties(PropertyFilePath);
856 Driver d = (Driver) Class.forName(prop.getProperty(DBdriver)).newInstance();
857 DriverManager.registerDriver(d);
858 Connection con = DriverManager.getConnection(
859 prop.getProperty(PerformanceDBURL),
860 prop.getProperty(PerformanceUsername),
861 DE(prop.getProperty(PerformancePassword)));
862 PreparedStatement com = con.prepareStatement("select 1;");
863 try {
864 com.execute();
865 DBUtils.safeClose(com);
866 return con;
867 } catch (Exception e) {
868
869 log.log(Level.ERROR, "Error obtaining primary perf database connection, attempting alternate. msg:" + e.getLocalizedMessage(), e);
870 return getPerformanceDB_NONPOOLED_Connection_FAILOVER();
871 } finally {
872 DBUtils.safeClose(com);
873 }
874
875 } catch (Exception ex) {
876 log.log(Level.ERROR, "No connection to the performance database could be made, msg:" + ex.getLocalizedMessage(), ex);
877 return null;
878 }
879 }
880
881
882
883
884
885
886
887
888
889
890
891 @Deprecated
892 public static Connection getConfigurationDB_NONPOOLED_Connection() {
893 try {
894 Properties prop = PropertyLoader.loadProperties(PropertyFilePath);
895 Driver d = (Driver) Class.forName(prop.getProperty(DBdriver)).newInstance();
896 DriverManager.registerDriver(d);
897 Connection con = DriverManager.getConnection(
898 prop.getProperty(ConfigDBURL),
899 prop.getProperty(ConfigUsername),
900 DE(prop.getProperty(ConfigPassword)));
901 PreparedStatement com = con.prepareStatement("select 1;");
902 try {
903 com.execute();
904 DBUtils.safeClose(com);
905 return con;
906 } catch (Exception e) {
907 log.log(Level.ERROR, "Error obtaining primary config database connection, attempting alternate. msg:" + e.getLocalizedMessage(), e);
908 return getConfigurationDB_NONPOOLED_Connection_FAILOVER();
909 } finally {
910 DBUtils.safeClose(com);
911 }
912
913 } catch (Exception ex) {
914 log.log(Level.ERROR, "Error obtaining primary conf database connection:" + ex.getLocalizedMessage(), ex);
915 return getConfigurationDB_NONPOOLED_Connection_FAILOVER();
916 }
917 }
918
919 @Deprecated
920 private static Connection getConfigurationDB_NONPOOLED_Connection_FAILOVER() {
921 try {
922 Properties prop = PropertyLoader.loadProperties(PropertyFilePath);
923 Driver d = (Driver) Class.forName(prop.getProperty(DBdriver)).newInstance();
924 DriverManager.registerDriver(d);
925 Connection con = DriverManager.getConnection(
926 prop.getProperty(ConfigDBURL_FAILOVER),
927 prop.getProperty(ConfigUsername_FAILOVER),
928 DE(prop.getProperty(ConfigPassword_FAILOVER)));
929 PreparedStatement com = con.prepareStatement("select 1;");
930 try {
931 com.execute();
932 DBUtils.safeClose(com);
933 return con;
934 } catch (Exception e) {
935 log.log(Level.FATAL, "Error obtaining failover config database connection, attempting alternate. msg:" + e.getLocalizedMessage(), e);
936 return null;
937 } finally {
938 DBUtils.safeClose(com);
939 }
940
941 } catch (Exception ex) {
942 log.log(Level.FATAL, "Error obtaining primary conf database connection:" + ex.getLocalizedMessage(), ex);
943 return null;
944 }
945 }
946
947
948
949
950
951
952
953
954
955
956 public static Connection getPerformanceDBConnection() {
957 Exception root = null;
958 try {
959 javax.naming.Context ctx1 = new javax.naming.InitialContext();
960 javax.sql.DataSource ds = (javax.sql.DataSource) ctx1.lookup("java:fgsmsPerformance");
961 Connection con = ds.getConnection();
962 PreparedStatement com = con.prepareStatement("select 1;");
963 try {
964 com.execute();
965 DBUtils.safeClose(com);
966 return con;
967 } catch (Exception e) {
968 root = e;
969
970
971 } finally {
972 DBUtils.safeClose(com);
973 }
974
975 } catch (Exception ex) {
976 root = ex;
977
978
979 }
980
981 try {
982 javax.naming.Context ctx1 = new javax.naming.InitialContext();
983 javax.sql.DataSource ds = (javax.sql.DataSource) ctx1.lookup("java:/comp/env/" + JDBC_PERFORMANCE);
984 Connection con = ds.getConnection();
985 PreparedStatement com = con.prepareStatement("select 1;");
986 try {
987 com.execute();
988 DBUtils.safeClose(com);
989 return con;
990 } catch (Exception e) {
991 root = e;
992
993 } finally {
994 DBUtils.safeClose(com);
995 }
996 } catch (Exception ex) {
997 root = ex;
998
999 }
1000
1001 try {
1002 javax.naming.Context ctx1 = new javax.naming.InitialContext();
1003 javax.sql.DataSource ds = (javax.sql.DataSource) ctx1.lookup(JDBC_PERFORMANCE);
1004 Connection con = ds.getConnection();
1005 PreparedStatement com = con.prepareStatement("select 1;");
1006 try {
1007 com.execute();
1008 DBUtils.safeClose(com);
1009 return con;
1010 } catch (Exception e) {
1011 root = e;
1012
1013 } finally {
1014 DBUtils.safeClose(com);
1015 }
1016 } catch (Exception ex) {
1017 root = ex;
1018
1019 }
1020 if (root != null) {
1021 log.log(Level.WARN, "Error obtaining primary pooled performance database connection, attempting alternate. msg:" + root.getLocalizedMessage(), root);
1022 }
1023 return getAlternatePerformanceDBConnection();
1024 }
1025
1026 private static Connection getAlternatePerformanceDBConnection() {
1027 Exception root = null;
1028 try {
1029 javax.naming.Context ctx1 = new javax.naming.InitialContext();
1030 javax.sql.DataSource ds = (javax.sql.DataSource) ctx1.lookup("java:fgsmsPerformance" + SECONDARY_POSTIFX);
1031 Connection con = ds.getConnection();
1032 PreparedStatement com = con.prepareStatement("select 1;");
1033 try {
1034 com.execute();
1035 DBUtils.safeClose(com);
1036 return con;
1037 } catch (Exception e) {
1038 root = e;
1039
1040 } finally {
1041 DBUtils.safeClose(com);
1042 }
1043 } catch (Exception ex) {
1044 root = ex;
1045
1046 }
1047 try {
1048 javax.naming.Context ctx1 = new javax.naming.InitialContext();
1049 javax.sql.DataSource ds = (javax.sql.DataSource) ctx1.lookup("java:/comp/env/" + JDBC_PERFORMANCE + SECONDARY_POSTIFX);
1050 Connection con = ds.getConnection();
1051 PreparedStatement com = con.prepareStatement("select 1;");
1052 try {
1053 com.execute();
1054 DBUtils.safeClose(com);
1055 return con;
1056 } catch (Exception e) {
1057 root = e;
1058
1059 } finally {
1060 DBUtils.safeClose(com);
1061 }
1062 } catch (Exception ex) {
1063 root = ex;
1064
1065 }
1066 if (root != null) {
1067 if (root != null) {
1068 log.log(Level.FATAL, "Error obtaining secondary pooled performance database connection, giving up. Ensure that the database is available. msg:" + root.getLocalizedMessage(), root);
1069 }
1070 }
1071 return null;
1072 }
1073
1074
1075
1076
1077
1078
1079
1080
1081 public static Connection getConfigurationDBConnection() {
1082 Exception root = null;
1083 try {
1084 javax.naming.Context ctx1 = new javax.naming.InitialContext();
1085 javax.naming.Context env = (javax.naming.Context) ctx1.lookup("java:/comp/env");
1086 javax.sql.DataSource ds = (javax.sql.DataSource) env.lookup(JDBC_CONFIG);
1087
1088 Connection con = ds.getConnection();
1089 PreparedStatement com = con.prepareStatement("select 1;");
1090 try {
1091 com.execute();
1092 DBUtils.safeClose(com);
1093 return con;
1094 } catch (Exception e) {
1095 e.printStackTrace();
1096 root = e;
1097 if (System.getProperties().containsKey("debug")) {
1098 log.log(Level.WARN, "Error obtaining primary conf pooled performance database connection, attempting alternate. msg:" + e.getLocalizedMessage(), e);
1099 }
1100 } finally {
1101 DBUtils.safeClose(com);
1102 }
1103 } catch (Exception ex) {
1104 root = ex;
1105 if (System.getProperties().containsKey("debug")) {
1106 log.log(Level.WARN, "Error obtaining primary conf pooled database connection, attempting alternate. msg:" + ex.getLocalizedMessage(), ex);
1107 }
1108 }
1109 try {
1110 javax.naming.Context ctx1 = new javax.naming.InitialContext();
1111 javax.sql.DataSource ds = (javax.sql.DataSource) ctx1.lookup("java:fgsmsConfig");
1112 Connection con = ds.getConnection();
1113 PreparedStatement com = con.prepareStatement("select 1;");
1114 try {
1115 com.execute();
1116 DBUtils.safeClose(com);
1117 return con;
1118 } catch (Exception e) {
1119 e.printStackTrace();
1120 root = e;
1121 if (System.getProperties().containsKey("debug")) {
1122 log.log(Level.WARN, "Error obtaining primary conf pooled performance database connection, attempting alternate. msg:" + e.getLocalizedMessage(), e);
1123 }
1124 } finally {
1125 DBUtils.safeClose(com);
1126 }
1127
1128 } catch (Exception ex) {
1129 root = ex;
1130 if (System.getProperties().containsKey("debug")) {
1131 log.log(Level.WARN, "Error obtaining primary conf pooled database connection, attempting alternate. msg:" + ex.getLocalizedMessage(), ex);
1132 }
1133 }
1134 try {
1135 javax.naming.Context ctx1 = new javax.naming.InitialContext();
1136 javax.sql.DataSource ds = (javax.sql.DataSource) ctx1.lookup(JDBC_CONFIG);
1137 Connection con = ds.getConnection();
1138 PreparedStatement com = con.prepareStatement("select 1;");
1139 try {
1140 com.execute();
1141 DBUtils.safeClose(com);
1142 return con;
1143 } catch (Exception e) {
1144 e.printStackTrace();
1145 root = e;
1146 if (System.getProperties().containsKey("debug")) {
1147 log.log(Level.WARN, "Error obtaining primary conf pooled performance database connection, attempting alternate. msg:" + e.getLocalizedMessage(), e);
1148 }
1149 } finally {
1150 DBUtils.safeClose(com);
1151 }
1152
1153 } catch (Exception ex) {
1154 root = ex;
1155 if (System.getProperties().containsKey("debug")) {
1156 log.log(Level.WARN, "Error obtaining primary conf pooled database connection, attempting alternate. msg:" + ex.getLocalizedMessage(), ex);
1157 }
1158 }
1159 try {
1160 javax.naming.Context ctx1 = new javax.naming.InitialContext();
1161 javax.sql.DataSource ds = (javax.sql.DataSource) ctx1.lookup("fgsmsConfig");
1162 Connection con = ds.getConnection();
1163 PreparedStatement com = con.prepareStatement("select 1;");
1164 try {
1165 com.execute();
1166 DBUtils.safeClose(com);
1167 return con;
1168 } catch (Exception e) {
1169 e.printStackTrace();
1170 root = e;
1171 if (System.getProperties().containsKey("debug")) {
1172 log.log(Level.WARN, "Error obtaining primary conf pooled performance database connection, attempting alternate. msg:" + e.getLocalizedMessage(), e);
1173 }
1174 } finally {
1175 DBUtils.safeClose(com);
1176 }
1177 } catch (Exception ex) {
1178 root = ex;
1179 if (System.getProperties().containsKey("debug")) {
1180 log.log(Level.WARN, "Error obtaining primary conf pooled database connection, attempting alternate. msg:" + ex.getLocalizedMessage(), ex);
1181 }
1182 }
1183 if (root != null) {
1184 log.log(Level.WARN, "Error obtaining primary conf pooled database connection, attempting alternate. msg:" + root.getLocalizedMessage(), root);
1185 }
1186 return getAlternateConfigurationDBConnection();
1187 }
1188
1189 private static Connection getAlternateConfigurationDBConnection() {
1190 Exception root = null;
1191 try {
1192 javax.naming.Context ctx1 = new javax.naming.InitialContext();
1193 javax.sql.DataSource ds = (javax.sql.DataSource) ctx1.lookup("java:fgsmsConfig" + SECONDARY_POSTIFX);
1194 Connection con = ds.getConnection();
1195 PreparedStatement com = con.prepareStatement("select 1;");
1196 try {
1197 com.execute();
1198 DBUtils.safeClose(com);
1199 return con;
1200 } catch (Exception e) {
1201 root = e;
1202 if (System.getProperties().containsKey("debug")) {
1203 log.log(Level.WARN, "Error obtaining secondary pooled config database connection, attempting alternate. msg:" + e.getLocalizedMessage(), e);
1204 }
1205 } finally {
1206 DBUtils.safeClose(com);
1207 }
1208
1209 } catch (Exception ex) {
1210 root = ex;
1211 if (System.getProperties().containsKey("debug")) {
1212 log.log(Level.WARN, "Error obtaining secondary pooled conf database connection, attempting alternate. msg:" + ex.getLocalizedMessage(), ex);
1213 }
1214 }
1215 try {
1216 javax.naming.Context ctx1 = new javax.naming.InitialContext();
1217 javax.sql.DataSource ds = (javax.sql.DataSource) ctx1.lookup("java:/comp/env/" + JDBC_CONFIG + SECONDARY_POSTIFX);
1218 Connection con = ds.getConnection();
1219 PreparedStatement com = con.prepareStatement("select 1;");
1220 try {
1221 com.execute();
1222 DBUtils.safeClose(com);
1223 return con;
1224 } catch (Exception e) {
1225 root = e;
1226 if (System.getProperties().containsKey("debug")) {
1227 log.log(Level.FATAL, "Error obtaining secondary pooled config database connection, attempting alternate. msg:" + e.getLocalizedMessage(), e);
1228 }
1229 } finally {
1230 DBUtils.safeClose(com);
1231 }
1232 } catch (Exception ex) {
1233 root = ex;
1234 if (System.getProperties().containsKey("debug")) {
1235 log.log(Level.FATAL, "Error obtaining secondary pooled conf database connection, attempting alternate. msg:" + ex.getLocalizedMessage(), ex);
1236 }
1237 }
1238 if (root != null) {
1239 log.log(Level.FATAL, "Error obtaining secondary pooled config database connection, attempting alternate. msg:" + root.getLocalizedMessage(), root);
1240 }
1241 return null;
1242 }
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253 public static NameValuePair newNameValuePair(String name, String val, boolean encrypted, boolean encryptOnSave) {
1254 NameValuePair r = new NameValuePair();
1255 r.setName(name);
1256 r.setValue(val);
1257 r.setEncrypted(encrypted);
1258 r.setEncryptOnSave(encryptOnSave);
1259 return r;
1260 }
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270 public static SLAAction newEmailAction(String from, String subject, String body) {
1271 SLAAction r = new SLAAction();
1272 r.setImplementingClassName("org.miloss.fgsms.sla.actions.EmailAlerter");
1273 if (!stringIsNullOrEmpty(from)) {
1274 r.getParameterNameValue().add(newNameValuePair("From", from, false, false));
1275 }
1276 r.getParameterNameValue().add(newNameValuePair("Body", body, false, false));
1277 r.getParameterNameValue().add(newNameValuePair("Subject", subject, false, false));
1278 return r;
1279 }
1280
1281
1282
1283
1284
1285
1286
1287
1288 public static NameValuePair getNameValuePairByName(List<NameValuePair> set, String name) {
1289 if (set == null) {
1290 return null;
1291 }
1292 for (int i = 0; i < set.size(); i++) {
1293 if (set.get(i).getName().equalsIgnoreCase(name)) {
1294 return set.get(i);
1295 }
1296 }
1297 return null;
1298 }
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308 public static Plugin newPlugin(String classname, String displayname, String appliesto) {
1309 Plugin p = new Plugin();
1310 p.setClassname(classname);
1311 p.setDisplayname(displayname);
1312 p.setPlugintype(appliesto);
1313 return p;
1314 }
1315
1316 public static List<PolicyType> getAllPolicyTypes() {
1317 List<PolicyType> r = new ArrayList<PolicyType>();
1318 PolicyType[] values = PolicyType.values();
1319 r.addAll(Arrays.asList(values));
1320 return r;
1321 }
1322
1323
1324
1325
1326
1327
1328
1329
1330 public static boolean containsPolicyType(List<PolicyType> appliesTo, PolicyType optionalPolicyTypeFilter) {
1331 if (appliesTo == null) {
1332 return false;
1333 }
1334 if (optionalPolicyTypeFilter == null) {
1335 return false;
1336 }
1337 for (int i = 0; i < appliesTo.size(); i++) {
1338 if (appliesTo.get(i).equals(optionalPolicyTypeFilter)) {
1339 return true;
1340 }
1341 }
1342 return false;
1343
1344 }
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354 public static KeyNameValue newKeyNameValue(String key, String name, String value) {
1355 KeyNameValue x = new KeyNameValue();
1356 x.setPropertyKey(key);
1357 x.setPropertyName(name);
1358 x.setPropertyValue(value);
1359 return x;
1360 }
1361
1362
1363
1364
1365
1366
1367
1368
1369 public static KeyNameValueEnc newKeyNameValueEnc(KeyNameValue value, boolean shouldEncrypt) {
1370 KeyNameValueEnc x = new KeyNameValueEnc();
1371 x.setKeyNameValue(value);
1372 x.setShouldEncrypt(shouldEncrypt);
1373 return x;
1374 }
1375 }