|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + * |
| 17 | + */ |
| 18 | + |
| 19 | +package org.apache.skywalking.apm.plugin.jdbc.connectionurl.parser; |
| 20 | + |
| 21 | +import org.apache.skywalking.apm.network.trace.component.ComponentsDefine; |
| 22 | +import org.apache.skywalking.apm.plugin.jdbc.trace.ConnectionInfo; |
| 23 | + |
| 24 | +public class DMURLParser extends AbstractURLParser { |
| 25 | + private static final int DEFAULT_PORT = 5236; |
| 26 | + private static final String DB_TYPE = "DM"; |
| 27 | + private static final String URL_PARAMS_HOST_KEY = "host"; |
| 28 | + private static final String URL_PARAMS_PORT_KEY = "port"; |
| 29 | + private static final String URL_PARAMS_SCHEMA_KEY = "schema"; |
| 30 | + |
| 31 | + public DMURLParser(String url) { |
| 32 | + super(url); |
| 33 | + } |
| 34 | + |
| 35 | + @Override |
| 36 | + protected URLLocation fetchDatabaseHostsIndexRange() { |
| 37 | + int hostLabelStartIndex = url.indexOf("//"); |
| 38 | + if (hostLabelStartIndex == -1) { |
| 39 | + return new URLLocation(0, 0); |
| 40 | + } |
| 41 | + int hostLabelEndIndex = url.indexOf("?", hostLabelStartIndex + 2); |
| 42 | + if (hostLabelEndIndex == -1) { |
| 43 | + hostLabelEndIndex = url.length(); |
| 44 | + } |
| 45 | + return new URLLocation(hostLabelStartIndex + 2, hostLabelEndIndex); |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + protected URLLocation fetchDatabaseNameIndexRange() { |
| 50 | + return new URLLocation(0, 0); |
| 51 | + } |
| 52 | + |
| 53 | + @Override |
| 54 | + public ConnectionInfo parse() { |
| 55 | + URLLocation location = fetchDatabaseHostsIndexRange(); |
| 56 | + String hostPortSegment = ""; |
| 57 | + if (location.endIndex() > location.startIndex()) { |
| 58 | + hostPortSegment = url.substring(location.startIndex(), location.endIndex()); |
| 59 | + } |
| 60 | + |
| 61 | + String host = ""; |
| 62 | + String port = ""; |
| 63 | + |
| 64 | + if (!hostPortSegment.isEmpty()) { |
| 65 | + String[] parts = hostPortSegment.split(":"); |
| 66 | + if (parts.length >= 1) { |
| 67 | + host = parts[0]; |
| 68 | + } |
| 69 | + if (parts.length == 2) { |
| 70 | + port = parts[1]; |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + if (host.isEmpty()) { |
| 75 | + host = fetchFromUrlParams(URL_PARAMS_HOST_KEY); |
| 76 | + } |
| 77 | + if (port == null || port.isEmpty()) { |
| 78 | + port = fetchFromUrlParams(URL_PARAMS_PORT_KEY); |
| 79 | + } |
| 80 | + |
| 81 | + if (port == null || port.isEmpty()) { |
| 82 | + port = String.valueOf(DEFAULT_PORT); |
| 83 | + } |
| 84 | + |
| 85 | + String schema = fetchFromUrlParams(URL_PARAMS_SCHEMA_KEY); |
| 86 | + |
| 87 | + return new ConnectionInfo( |
| 88 | + ComponentsDefine.DMDB_JDBC_DRIVER, |
| 89 | + DB_TYPE, |
| 90 | + host, |
| 91 | + Integer.parseInt(port), |
| 92 | + schema == null ? "" : schema |
| 93 | + ); |
| 94 | + } |
| 95 | + |
| 96 | + private String fetchFromUrlParams(String key) { |
| 97 | + int paramIndex = url.indexOf("?"); |
| 98 | + if (paramIndex == -1) { |
| 99 | + return null; |
| 100 | + } |
| 101 | + String[] params = url.substring(paramIndex + 1).split("&"); |
| 102 | + for (String pair : params) { |
| 103 | + if (pair.startsWith(key + "=")) { |
| 104 | + String[] segments = pair.split("=", 2); |
| 105 | + if (segments.length == 2) { |
| 106 | + return segments[1]; |
| 107 | + } |
| 108 | + } |
| 109 | + } |
| 110 | + return null; |
| 111 | + } |
| 112 | +} |
0 commit comments