ConfigLoader.java

  1. package org.drip.param.config;

  2. /*
  3.  * -*- mode: java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  4.  */

  5. /*!
  6.  * Copyright (C) 2020 Lakshmi Krishnamurthy
  7.  * Copyright (C) 2019 Lakshmi Krishnamurthy
  8.  * Copyright (C) 2018 Lakshmi Krishnamurthy
  9.  * Copyright (C) 2017 Lakshmi Krishnamurthy
  10.  * Copyright (C) 2016 Lakshmi Krishnamurthy
  11.  * Copyright (C) 2015 Lakshmi Krishnamurthy
  12.  * Copyright (C) 2014 Lakshmi Krishnamurthy
  13.  * Copyright (C) 2013 Lakshmi Krishnamurthy
  14.  * Copyright (C) 2012 Lakshmi Krishnamurthy
  15.  * Copyright (C) 2011 Lakshmi Krishnamurthy
  16.  *
  17.  *  This file is part of DROP, an open-source library targeting analytics/risk, transaction cost analytics,
  18.  *      asset liability management analytics, capital, exposure, and margin analytics, valuation adjustment
  19.  *      analytics, and portfolio construction analytics within and across fixed income, credit, commodity,
  20.  *      equity, FX, and structured products. It also includes auxiliary libraries for algorithm support,
  21.  *      numerical analysis, numerical optimization, spline builder, model validation, statistical learning,
  22.  *      and computational support.
  23.  *  
  24.  *      https://lakshmidrip.github.io/DROP/
  25.  *  
  26.  *  DROP is composed of three modules:
  27.  *  
  28.  *  - DROP Product Core - https://lakshmidrip.github.io/DROP-Product-Core/
  29.  *  - DROP Portfolio Core - https://lakshmidrip.github.io/DROP-Portfolio-Core/
  30.  *  - DROP Computational Core - https://lakshmidrip.github.io/DROP-Computational-Core/
  31.  *
  32.  *  DROP Product Core implements libraries for the following:
  33.  *  - Fixed Income Analytics
  34.  *  - Loan Analytics
  35.  *  - Transaction Cost Analytics
  36.  *
  37.  *  DROP Portfolio Core implements libraries for the following:
  38.  *  - Asset Allocation Analytics
  39.  *  - Asset Liability Management Analytics
  40.  *  - Capital Estimation Analytics
  41.  *  - Exposure Analytics
  42.  *  - Margin Analytics
  43.  *  - XVA Analytics
  44.  *
  45.  *  DROP Computational Core implements libraries for the following:
  46.  *  - Algorithm Support
  47.  *  - Computation Support
  48.  *  - Function Analysis
  49.  *  - Model Validation
  50.  *  - Numerical Analysis
  51.  *  - Numerical Optimizer
  52.  *  - Spline Builder
  53.  *  - Statistical Learning
  54.  *
  55.  *  Documentation for DROP is Spread Over:
  56.  *
  57.  *  - Main                     => https://lakshmidrip.github.io/DROP/
  58.  *  - Wiki                     => https://github.com/lakshmiDRIP/DROP/wiki
  59.  *  - GitHub                   => https://github.com/lakshmiDRIP/DROP
  60.  *  - Repo Layout Taxonomy     => https://github.com/lakshmiDRIP/DROP/blob/master/Taxonomy.md
  61.  *  - Javadoc                  => https://lakshmidrip.github.io/DROP/Javadoc/index.html
  62.  *  - Technical Specifications => https://github.com/lakshmiDRIP/DROP/tree/master/Docs/Internal
  63.  *  - Release Versions         => https://lakshmidrip.github.io/DROP/version.html
  64.  *  - Community Credits        => https://lakshmidrip.github.io/DROP/credits.html
  65.  *  - Issues Catalog           => https://github.com/lakshmiDRIP/DROP/issues
  66.  *  - JUnit                    => https://lakshmidrip.github.io/DROP/junit/index.html
  67.  *  - Jacoco                   => https://lakshmidrip.github.io/DROP/jacoco/index.html
  68.  *
  69.  *  Licensed under the Apache License, Version 2.0 (the "License");
  70.  *      you may not use this file except in compliance with the License.
  71.  *  
  72.  *  You may obtain a copy of the License at
  73.  *      http://www.apache.org/licenses/LICENSE-2.0
  74.  *  
  75.  *  Unless required by applicable law or agreed to in writing, software
  76.  *      distributed under the License is distributed on an "AS IS" BASIS,
  77.  *      WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  78.  *  
  79.  *  See the License for the specific language governing permissions and
  80.  *      limitations under the License.
  81.  */

  82. /**
  83.  * <i>ConfigLoader</i> implements the configuration functionality. It exposes the following:
  84.  *
  85.  * <br><br>
  86.  *  <ul>
  87.  *      <li>
  88.  *          Parses the XML configuration file and extract the tag pairs information.
  89.  *      </li>
  90.  *      <li>
  91.  *          Retrieve the logger.
  92.  *      </li>
  93.  *      <li>
  94.  *          Load the holiday calendars and retrieve the location holidays.
  95.  *      </li>
  96.  *      <li>
  97.  *          Connect to analytics server and the database server.
  98.  *      </li>
  99.  *  </ul>
  100.  *
  101.  * <br><br>
  102.  *  Depending on the configuration setting, ConfigLoader loads the above from either a file or the specified
  103.  *      database.
  104.  *
  105.  *  <br><br>
  106.  *  <ul>
  107.  *      <li><b>Module </b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/ProductCore.md">Product Core Module</a></li>
  108.  *      <li><b>Library</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/FixedIncomeAnalyticsLibrary.md">Fixed Income Analytics</a></li>
  109.  *      <li><b>Project</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/param/README.md">Product Cash Flow, Valuation, Market, Pricing, and Quoting Parameters</a></li>
  110.  *      <li><b>Package</b> = <a href = "https://github.com/lakshmiDRIP/DROP/tree/master/src/main/java/org/drip/param/config/README.md">Library Level Configuration Parameters Setting</a></li>
  111.  *  </ul>
  112.  *
  113.  * @author Lakshmi Krishnamurthy
  114.  */

  115. public class ConfigLoader {
  116.     private static boolean s_bInit = false;
  117.     private static java.sql.Statement s_Statement = null;

  118.     private static final int IntegerTagValue (
  119.         final org.w3c.dom.Element eTag,
  120.         final java.lang.String strTag)
  121.         throws java.lang.Exception {
  122.         if (null == eTag || null == strTag || null == eTag.getElementsByTagName (strTag))
  123.             throw new java.lang.Exception ("Cannot get int value for <" + strTag + ">");

  124.         org.w3c.dom.NodeList nl = eTag.getElementsByTagName (strTag);

  125.         if (null == nl.item (0) || !(nl.item (0) instanceof org.w3c.dom.Element))
  126.             throw new java.lang.Exception ("Cannot get int value for <" + strTag + ">");

  127.         org.w3c.dom.Element elem = (org.w3c.dom.Element) nl.item (0);

  128.         if (null == elem || null == elem.getChildNodes() || null == elem.getChildNodes().item (0) ||
  129.             !(elem.getChildNodes().item (0) instanceof org.w3c.dom.Node))
  130.             throw new java.lang.Exception ("Cannot get int value for <" + strTag + ">");

  131.         org.w3c.dom.Node node = elem.getChildNodes().item (0);

  132.         if (null == node || null == node.getNodeValue())
  133.             throw new java.lang.Exception ("Cannot get int value for <" + strTag + ">");

  134.         return Integer.parseInt (node.getNodeValue());
  135.     }

  136.     private static final boolean BooleanTagValue (
  137.         final org.w3c.dom.Element eTag,
  138.         final java.lang.String strTag)
  139.         throws java.lang.Exception
  140.     {
  141.         if (null == eTag || null == strTag || null == eTag.getElementsByTagName (strTag))
  142.             throw new java.lang.Exception ("Cannot get bool value for <" + strTag + ">");

  143.         org.w3c.dom.NodeList nl = eTag.getElementsByTagName (strTag);

  144.         if (null == nl.item (0) || !(nl.item (0) instanceof org.w3c.dom.Element))
  145.             throw new java.lang.Exception ("Cannot get bool value for <" + strTag + ">");

  146.         org.w3c.dom.Element elem = (org.w3c.dom.Element) nl.item (0);

  147.         if (null == elem || null == elem.getChildNodes() || null == elem.getChildNodes().item (0) ||
  148.             !(elem.getChildNodes().item (0) instanceof org.w3c.dom.Node))
  149.             throw new java.lang.Exception ("Cannot get bool value for <" + strTag + ">");

  150.         org.w3c.dom.Node node = elem.getChildNodes().item (0);

  151.         if (null == node || null == node.getNodeValue())
  152.             throw new java.lang.Exception ("Cannot get bool value for <" + strTag + ">");

  153.         return java.lang.Boolean.parseBoolean (node.getNodeValue());
  154.     }

  155.     private static final java.lang.String StringTagValue (
  156.         final org.w3c.dom.Element eTag,
  157.         final java.lang.String strTag)
  158.     {
  159.         if (null == eTag || null == strTag || null == eTag.getElementsByTagName (strTag)) return null;

  160.         org.w3c.dom.NodeList nl = eTag.getElementsByTagName (strTag);

  161.         if (null == nl.item (0) || !(nl.item (0) instanceof org.w3c.dom.Element)) return null;

  162.         org.w3c.dom.Element elem = (org.w3c.dom.Element) nl.item (0);

  163.         if (null == elem || null == elem.getChildNodes() || null == elem.getChildNodes().item (0) ||
  164.             !(elem.getChildNodes().item (0) instanceof org.w3c.dom.Node))
  165.             return null;

  166.         org.w3c.dom.Node node = elem.getChildNodes().item (0);

  167.         if (null == node || null == node.getNodeValue()) return null;

  168.         return node.getNodeValue();
  169.     }

  170.     private static final int[] IntegerArrayTagValue (
  171.         final org.w3c.dom.Element eTag,
  172.         final java.lang.String strTag)
  173.     {
  174.         if (null == eTag || null == strTag || null == eTag.getElementsByTagName (strTag)) return null;

  175.         org.w3c.dom.NodeList nl = eTag.getElementsByTagName (strTag);

  176.         if (!(nl.item (0) instanceof org.w3c.dom.Element)) return null;

  177.         org.w3c.dom.Element elem = (org.w3c.dom.Element) nl.item (0);

  178.         if (null == elem || null == elem.getChildNodes() || null == elem.getChildNodes().item (0) ||
  179.             !(elem.getChildNodes().item (0) instanceof org.w3c.dom.Node))
  180.             return null;

  181.         java.lang.String strValue = elem.getChildNodes().item (0).getNodeValue();

  182.         if (null == strValue || strValue.isEmpty()) return null;

  183.         java.lang.String[] astrValue = strValue.split (",");

  184.         int[] ai = new int[astrValue.length];

  185.         for (int i = 0; i < astrValue.length; ++i)
  186.             ai[i] = Integer.parseInt (astrValue[i]);

  187.         return ai;
  188.     }

  189.     private static final org.w3c.dom.Document NormalizedXMLDoc (
  190.         final java.lang.String strXMLFile)
  191.     {
  192.         if (null == strXMLFile || strXMLFile.isEmpty()) return null;

  193.         org.w3c.dom.Document doc = null;

  194.         try {
  195.             doc = javax.xml.parsers.DocumentBuilderFactory.newInstance().newDocumentBuilder().parse (new
  196.                 java.io.File (strXMLFile));
  197.         } catch (java.lang.Exception e) {
  198.             e.printStackTrace();

  199.             return null;
  200.         }

  201.         if (null == doc || null == doc.getDocumentElement()) return null;

  202.         doc.getDocumentElement().normalize();

  203.         return doc;
  204.     }

  205.     /**
  206.      * Create a LocHolidays object from the XML Document and the Location Tag
  207.      *
  208.      * @param doc XML Document
  209.      * @param strLoc Location Tag
  210.      *
  211.      * @return LocHolidays
  212.      */

  213.     public static org.drip.analytics.eventday.Locale LocationHolidays (
  214.         final org.w3c.dom.Document doc,
  215.         final java.lang.String strLoc)
  216.     {
  217.         if (null == doc || null == strLoc) return null;

  218.         org.w3c.dom.NodeList nlLoc = doc.getElementsByTagName (strLoc);

  219.         if (null == nlLoc || null == nlLoc.item (0) || org.w3c.dom.Node.ELEMENT_NODE != nlLoc.item
  220.             (0).getNodeType())
  221.             return null;

  222.         org.drip.analytics.eventday.Locale locHols = new org.drip.analytics.eventday.Locale();

  223.         org.w3c.dom.Element e = (org.w3c.dom.Element) nlLoc.item (0);

  224.         org.w3c.dom.NodeList nlHols = e.getElementsByTagName ("Weekend");

  225.         if (null != nlHols && null != nlHols.item (0) && org.w3c.dom.Node.ELEMENT_NODE == nlHols.item
  226.             (0).getNodeType())
  227.             locHols.addWeekend (IntegerArrayTagValue ((org.w3c.dom.Element) nlHols.item (0), "DaysInWeek"));

  228.         if (null != (nlHols = e.getElementsByTagName ("FixedHoliday"))) {
  229.             for (int j = 0; j < nlHols.getLength(); ++j) {
  230.                 if (null == nlHols.item (j) || org.w3c.dom.Node.ELEMENT_NODE != nlHols.item
  231.                     (j).getNodeType())
  232.                     continue;

  233.                 org.w3c.dom.Element elemHol = (org.w3c.dom.Element) nlHols.item (j);

  234.                 if (null != elemHol) {
  235.                     try {
  236.                         locHols.addFixedHoliday (IntegerTagValue (elemHol, "Date"), IntegerTagValue (elemHol,
  237.                             "Month"), "");
  238.                     } catch (java.lang.Exception ex) {
  239.                         ex.printStackTrace();
  240.                     }
  241.                 }
  242.             }
  243.         }  

  244.         if (null != (nlHols = e.getElementsByTagName ("FloatingHoliday"))) {
  245.             for (int j = 0; j < nlHols.getLength(); ++j) {
  246.                 if (null == nlHols.item (j) || org.w3c.dom.Node.ELEMENT_NODE != nlHols.item
  247.                     (j).getNodeType())
  248.                     continue;

  249.                 org.w3c.dom.Element elemHol = (org.w3c.dom.Element) nlHols.item (j);

  250.                 if (null != elemHol) {
  251.                     try {
  252.                         locHols.addFloatingHoliday (IntegerTagValue (elemHol, "WeekInMonth"), IntegerTagValue
  253.                             (elemHol, "WeekDay"), IntegerTagValue (elemHol, "Month"), BooleanTagValue
  254.                                 (elemHol, "FromFront"), "");
  255.                     } catch (java.lang.Exception ex) {
  256.                         ex.printStackTrace();
  257.                     }
  258.                 }
  259.             }
  260.         }

  261.         return locHols;
  262.     }

  263.     /**
  264.      * Get the logger location from the XML Configuration file
  265.      *
  266.      * @param strConfigFile XML Configuration file
  267.      *
  268.      * @return String representing the logger location's full path
  269.      */

  270.     public static java.lang.String LoggerLocation (
  271.         final java.lang.String strConfigFile)
  272.     {
  273.         org.w3c.dom.Document doc = NormalizedXMLDoc (strConfigFile);

  274.         if (null == doc) return null;

  275.         org.w3c.dom.NodeList nlLogger = doc.getElementsByTagName ("logger");

  276.         if (null == nlLogger || null == nlLogger.item (0) || org.w3c.dom.Node.ELEMENT_NODE !=
  277.             nlLogger.item (0).getNodeType())
  278.             return null;

  279.         return StringTagValue ((org.w3c.dom.Element) nlLogger.item (0), "Location");
  280.     }

  281.     /**
  282.      * Connect to the analytics server from the connection parameters set in the XML Configuration file
  283.      *
  284.      * @param strConfigFile XML Configuration file
  285.      *
  286.      * @return java.net.Socket
  287.      */

  288.     public static java.net.Socket ConnectToAnalServer (
  289.         final java.lang.String strConfigFile)
  290.     {
  291.         org.w3c.dom.Document doc = NormalizedXMLDoc (strConfigFile);

  292.         if (null == doc) return null;

  293.         org.w3c.dom.NodeList nlLogger = doc.getElementsByTagName ("analserver");

  294.         if (null == nlLogger || null == nlLogger.item (0) || org.w3c.dom.Node.ELEMENT_NODE !=
  295.             nlLogger.item (0).getNodeType())
  296.             return null;

  297.         try {
  298.             return new java.net.Socket (StringTagValue ((org.w3c.dom.Element) nlLogger.item (0), "host"),
  299.                 IntegerTagValue ((org.w3c.dom.Element) nlLogger.item (0), "port"));
  300.         } catch (java.lang.Exception e) {
  301.             e.printStackTrace();
  302.         }

  303.         return null;
  304.     }

  305.     /**
  306.      * Initialize the analytics server from the connection parameters set in the XML Configuration file
  307.      *
  308.      * @param strConfigFile XML Configuration file
  309.      *
  310.      * @return java.net.ServerSocket
  311.      */

  312.     public static java.net.ServerSocket InitAnalServer (
  313.         final java.lang.String strConfigFile)
  314.     {
  315.         org.w3c.dom.Document doc = NormalizedXMLDoc (strConfigFile);

  316.         if (null == doc) return null;

  317.         org.w3c.dom.NodeList nlLogger = doc.getElementsByTagName ("analserver");

  318.         if (null == nlLogger || null == nlLogger.item (0) || org.w3c.dom.Node.ELEMENT_NODE !=
  319.             nlLogger.item (0).getNodeType())
  320.             return null;

  321.         try {
  322.             return new java.net.ServerSocket (IntegerTagValue ((org.w3c.dom.Element) nlLogger.item (0),
  323.                 "port"), IntegerTagValue ((org.w3c.dom.Element) nlLogger.item (0), "maxconn"));
  324.         } catch (java.lang.Exception e) {
  325.             e.printStackTrace();
  326.         }

  327.         return null;
  328.     }

  329.     /**
  330.      * Load the map of the holiday calendars from the entries set in the XML Configuration file
  331.      *
  332.      * @param strConfigFile XML Configuration file
  333.      *
  334.      * @return Map of the holiday calendars
  335.      */

  336.     public static org.drip.analytics.support.CaseInsensitiveTreeMap<org.drip.analytics.eventday.Locale>
  337.         LoadHolidayCalendars (
  338.             final java.lang.String strConfigFile)
  339.         {
  340.         org.w3c.dom.Document doc = NormalizedXMLDoc (strConfigFile);

  341.         if (null == doc) return null;

  342.         org.drip.analytics.eventday.Locale lhNYB = LocationHolidays (doc, "NYB");

  343.         if (null == lhNYB) return null;

  344.         org.drip.analytics.support.CaseInsensitiveTreeMap<org.drip.analytics.eventday.Locale> mapHols = new
  345.             org.drip.analytics.support.CaseInsensitiveTreeMap<org.drip.analytics.eventday.Locale>();

  346.         mapHols.put ("NYB", lhNYB);

  347.         return mapHols;
  348.     }

  349.     /**
  350.      * Initialize the Oracle database from the connection parameters set in the XML Configuration file
  351.      *
  352.      * @param strConfigFile XML Configuration file
  353.      *
  354.      * @return Connection Statement object
  355.      */

  356.     public static java.sql.Statement OracleInit (
  357.         final java.lang.String strConfigFile)
  358.     {
  359.         if (s_bInit) return s_Statement;

  360.         s_bInit = true;

  361.         org.w3c.dom.Document doc = NormalizedXMLDoc (strConfigFile);

  362.         if (null == doc) return null;

  363.         org.w3c.dom.NodeList nlDBConn = doc.getElementsByTagName ("dbconn");

  364.         if (null == nlDBConn || null == nlDBConn.item (0) || org.w3c.dom.Node.ELEMENT_NODE !=
  365.             nlDBConn.item (0).getNodeType())
  366.             return null;

  367.         org.w3c.dom.Element elemDBConn = (org.w3c.dom.Element) nlDBConn.item (0);

  368.         try {
  369.             java.lang.Class.forName ("oracle.jdbc.driver.OracleDriver");

  370.             java.lang.String strURL = "jdbc:oracle:thin:@//" + StringTagValue (elemDBConn, "host") + ":" +
  371.                 StringTagValue (elemDBConn, "port") + "/" + StringTagValue (elemDBConn, "dbname");

  372.             // java.lang.String strURL = "jdbc:oracle:thin:@//localhost:1521/XE";

  373.             System.out.println ("URL: " + strURL);

  374.             java.sql.Connection conn = java.sql.DriverManager.getConnection (strURL, "hr", "hr");

  375.             System.out.println ("Conn: " + conn);

  376.             conn.setAutoCommit (false);

  377.             return s_Statement = conn.createStatement();
  378.         } catch (java.lang.Exception e) {
  379.             e.printStackTrace();
  380.         }

  381.         return null;
  382.     }

  383.     /**
  384.      * Load the map of the holiday calendars from the database settings set in the XML Configuration file
  385.      *
  386.      * @param strConfigFile XML Configuration file
  387.      *
  388.      * @return Map of the holiday calendars
  389.      */

  390.     public static final org.drip.analytics.support.CaseInsensitiveTreeMap<org.drip.analytics.eventday.Locale>
  391.         LoadHolidayCalendarsFromDB (
  392.             final java.lang.String strConfigFile)
  393.     {
  394.         org.drip.analytics.support.CaseInsensitiveTreeMap<org.drip.analytics.eventday.Locale> mapHols = new
  395.             org.drip.analytics.support.CaseInsensitiveTreeMap<org.drip.analytics.eventday.Locale>();

  396.         java.sql.Statement stmt = OracleInit (strConfigFile);

  397.         if (null == stmt) return null;

  398.         long lStart = System.nanoTime();

  399.         try {
  400.             java.sql.ResultSet rs = stmt.executeQuery ("SELECT Location, Holiday FROM Holidays");

  401.             while (null != rs && rs.next()) {
  402.                 java.lang.String strLocation = rs.getString ("Location");

  403.                 java.util.Date dtSQLHoliday = rs.getDate ("Holiday");

  404.                 if (null != dtSQLHoliday) {
  405.                     org.drip.analytics.eventday.Locale lh = mapHols.get (strLocation);

  406.                     if (null == lh) lh = new org.drip.analytics.eventday.Locale();

  407.                     lh.addStaticHoliday (org.drip.analytics.date.DateUtil.CreateFromYMD
  408.                         (org.drip.analytics.date.DateUtil.Year (dtSQLHoliday),
  409.                             org.drip.analytics.date.DateUtil.Month (dtSQLHoliday),
  410.                                 org.drip.analytics.date.DateUtil.Year (dtSQLHoliday)), "");

  411.                     mapHols.put (strLocation, lh);
  412.                 }
  413.             }
  414.         } catch (java.lang.Exception e) {
  415.             e.printStackTrace();

  416.             return null;
  417.         }

  418.         int[] aiWeekend = new int[2];
  419.         aiWeekend[1] = org.drip.analytics.date.DateUtil.SUNDAY;
  420.         aiWeekend[0] = org.drip.analytics.date.DateUtil.SATURDAY;

  421.         for (java.util.Map.Entry<java.lang.String, org.drip.analytics.eventday.Locale> me :
  422.             mapHols.entrySet())
  423.             me.getValue().addWeekend (aiWeekend);

  424.         System.out.println ("Loading hols from DB took " + (System.nanoTime() - lStart) * 1.e-06 +
  425.             " m-sec\n");

  426.         return mapHols;
  427.     }

  428.     public static void main (
  429.         final java.lang.String[] astrArgs)
  430.         throws java.lang.Exception
  431.     {
  432.         java.lang.String strConfigFile = "c:\\Lakshmi\\java\\BondAnal\\Config.xml";

  433.         org.drip.analytics.support.CaseInsensitiveTreeMap<org.drip.analytics.eventday.Locale> mapHols =
  434.             LoadHolidayCalendars (strConfigFile);

  435.         for (java.util.Map.Entry<java.lang.String, org.drip.analytics.eventday.Locale> me :
  436.             mapHols.entrySet())
  437.             System.out.println (me.getKey() + "=" + me.getValue());

  438.         System.out.println ("Logger: " + LoggerLocation (strConfigFile));
  439.     }
  440. }