Builder parse()

in apm-agent-plugins/apm-jdbc-plugin/src/main/java/co/elastic/apm/agent/jdbc/helper/ConnectionMetaData.java [99:189]


            Builder parse(String vendorUrl, Builder builder) {
                builder.withPort(1521);

                // Examples:
                // jdbc:oracle:thin:scott/tiger@//myhost:1521/myinstance
                // jdbc:oracle:thin:scott/tiger@127.0.0.1:666:myinstance
                // jdbc:oracle:thin:scott/tiger@localhost:myinstance
                // jdbc:oracle:oci:scott/tiger/@
                // jdbc:oracle:thin:@ldap://ldap.acme.com:7777/sales,cn=OracleContext,dc=com
                // jdbc:oracle:thin:@(DESCRIPTION=(LOAD_BALANCE=on)(ADDRESS=(PROTOCOL=TCP)(HOST=host1)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=service_name)))
                // jdbc:oracle:thin:@(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=cluster_alias)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=service_name)))
                // jdbc:oracle:thin:@(DESCRIPTION=(LOAD_BALANCE=on)(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=host1)(PORT=1521))(ADDRESS=(PROTOCOL=TCP)(HOST=host2)(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME=service_name)))
                int indexOfUserDetailsEnd = vendorUrl.indexOf('@');
                if (indexOfUserDetailsEnd > 0) {
                    if (vendorUrl.length() > indexOfUserDetailsEnd + 1) {
                        vendorUrl = vendorUrl.substring(indexOfUserDetailsEnd + 1).trim();
                    } else {
                        // jdbc:oracle:oci:scott/tiger/@
                        // nothing left to parse
                        return builder;
                    }
                }

                if (vendorUrl.startsWith("(")) {
                    // (DESCRIPTION=(LOAD_BALANCE=on)(ADDRESS=(PROTOCOL=TCP)(HOST=host1)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=service_name)))
                    // (DESCRIPTION=(LOAD_BALANCE=on)(ADDRESS_LIST=(ADDRESS=(PROTOCOL=TCP)(HOST=host1)(PORT=1521))(ADDRESS=(PROTOCOL=TCP)(HOST=host2)(PORT=1521)))(CONNECT_DATA=(SERVICE_NAME=service_name)))
                    try {
                        TreeNode parsedTree = buildOracleTree(vendorUrl);
                        if (parsedTree == null) {
                            logger.warn("Failed to parse Oracle DB address list from: {}", vendorUrl);
                            return builder.withParsingError();
                        } else {
                            traverseOracleTree(vendorUrl, parsedTree, builder);
                        }
                    } catch (Exception e) {
                        logger.warn("Failed to parse oracle description {}", vendorUrl);
                        return builder.withParsingError();
                    }

                } else if (vendorUrl.indexOf('/') >= 0) {
                    // try looking for patterns: host:port/instance or //host:port/instance

                    String authority = vendorUrl;
                    if (authority.startsWith("//")) {
                        authority = vendorUrl.substring(2);
                    }

                    int authorityEnd = authority.indexOf('/');
                    if (authorityEnd >= 0) {
                        if (authorityEnd + 1 < authority.length()) {
                            builder.withInstance(authority.substring(authorityEnd + 1));
                        }
                        authority = authority.substring(0, authorityEnd);

                    }

                    parseAuthority(authority, builder);
                } else {

                    // Thin driver host:port:sid syntax:
                    // myhost:666:instance
                    // myhost:instance
                    // thin:myhost:port:instance
                    if (vendorUrl.startsWith("thin:")) {
                        vendorUrl = vendorUrl.substring("thin:".length());
                    }

                    String[] parts = vendorUrl.split(":");
                    if (parts.length > 0) {
                        builder.withHost(parts[0]);
                    }
                    if (parts.length > 1) {
                        String portOrDb = parts[1];
                        boolean isInt = true;
                        for (char c : portOrDb.toCharArray()) {
                            isInt = isInt && c >= '0' && c <= '9';
                        }
                        if (isInt) {
                            builder.withPort(toNumericPort(vendorUrl, portOrDb));
                        } else {
                            builder.withInstance(portOrDb);
                        }
                    }
                    if (parts.length > 2) {
                        // assume the last item is always the instance name if provided
                        builder.withInstance(parts[2]);
                    }

                }
                return builder;
            }