protected void init()

in server/container/util/src/main/java/org/apache/james/util/sql/SqlResources.java [148:264]


    protected void init(Document sqlDoc, String sqlDefsSection, Connection conn, Map<String, String> configParameters) throws SQLException {
        // First process the database matcher, to determine the
        // sql statements to use.
        Element dbMatcherElement = (Element) (sqlDoc.getElementsByTagName("dbMatchers").item(0));
        String dbProduct = null;
        if (dbMatcherElement != null) {
            dbProduct = matchDbConnection(conn, dbMatcherElement);
        }

        // Now get the options valid for the database product used.
        Element dbOptionsElement = (Element) (sqlDoc.getElementsByTagName("dbOptions").item(0));
        if (dbOptionsElement != null) {
            // First populate the map with default values
            populateDbOptions("", dbOptionsElement, dbOptions);
            // Now update the map with specific product values
            if (dbProduct != null) {
                populateDbOptions(dbProduct, dbOptionsElement, dbOptions);
            }
        }

        // Now get the section defining sql for the repository required.
        NodeList sections = sqlDoc.getElementsByTagName("sqlDefs");
        int sectionsCount = sections.getLength();
        Element sectionElement = null;
        boolean found = false;
        for (int i = 0; i < sectionsCount; i++) {
            sectionElement = (Element) (sections.item(i));
            String sectionName = sectionElement.getAttribute("name");
            if (sectionName != null && sectionName.equals(sqlDefsSection)) {
                found = true;
                break;
            }

        }
        if (!found) {
            String exceptionBuffer = "Error loading sql definition file. " + "The element named \'" + sqlDefsSection + "\' does not exist.";
            throw new RuntimeException(exceptionBuffer);
        }

        // Get parameters defined within the file as defaults,
        // and use supplied parameters as overrides.
        Map<String, String> parameters = new HashMap<>();
        // First read from the <params> element, if it exists.
        Element parametersElement = (Element) (sectionElement.getElementsByTagName("parameters").item(0));
        if (parametersElement != null) {
            NamedNodeMap params = parametersElement.getAttributes();
            int paramCount = params.getLength();
            for (int i = 0; i < paramCount; i++) {
                Attr param = (Attr) params.item(i);
                String paramName = param.getName();
                String paramValue = param.getValue();
                parameters.put(paramName, paramValue);
            }
        }
        // Then copy in the parameters supplied with the call.
        parameters.putAll(configParameters);

        // 2 maps - one for storing default statements,
        // the other for statements with a "db" attribute matching this
        // connection.
        Map<String, String> defaultSqlStatements = new HashMap<>();
        Map<String, String> dbProductSqlStatements = new HashMap<>();

        // Process each sql statement, replacing string parameters,
        // and adding to the appropriate map..
        NodeList sqlDefs = sectionElement.getElementsByTagName("sql");
        int sqlCount = sqlDefs.getLength();
        for (int i = 0; i < sqlCount; i++) {
            // See if this needs to be processed (is default or product
            // specific)
            Element sqlElement = (Element) (sqlDefs.item(i));
            String sqlDb = sqlElement.getAttribute("db");
            Map<String, String> sqlMap;
            if (sqlDb.equals("")) {
                // default
                sqlMap = defaultSqlStatements;
            } else if (sqlDb.equals(dbProduct)) {
                // Specific to this product
                sqlMap = dbProductSqlStatements;
            } else {
                // for a different product
                continue;
            }

            // Get the key and value for this SQL statement.
            String sqlKey = sqlElement.getAttribute("name");
            if (sqlKey == null) {
                // ignore statements without a "name" attribute.
                continue;
            }
            String sqlString = sqlElement.getFirstChild().getNodeValue();

            // Do parameter replacements for this sql string.
            StringBuilder replaceBuffer = new StringBuilder(64);
            for (Map.Entry<String, String> entry : parameters.entrySet()) {
                replaceBuffer.setLength(0);
                replaceBuffer.append("${").append(entry.getKey()).append("}");
                sqlString = substituteSubString(sqlString, replaceBuffer.toString(), entry.getValue());
            }

            // See if we already have registered a string of this value
            String shared = stringTable.get(sqlString);
            // If not, register it -- we will use it next time
            if (shared == null) {
                stringTable.put(sqlString, sqlString);
            } else {
                sqlString = shared;
            }

            // Add to the sqlMap - either the "default" or the "product" map
            sqlMap.put(sqlKey, sqlString);
        }

        // Copy in default strings, then overwrite product-specific ones.
        sql.putAll(defaultSqlStatements);
        sql.putAll(dbProductSqlStatements);
    }