|
| 1 | +/* |
| 2 | + * Copyright (c) MuleSoft, Inc. All rights reserved. http://www.mulesoft.com |
| 3 | + * The software in this package is published under the terms of the CPAL v1.0 |
| 4 | + * license, a copy of which has been included with this distribution in the |
| 5 | + * LICENSE.txt file. |
| 6 | + */ |
| 7 | +package org.mule.extension.db.internal.domain.connection.datasource; |
| 8 | + |
| 9 | +import static org.mule.db.commons.internal.domain.connection.ConnectionTracingMetadataUtils.getHostFrom; |
| 10 | +import static org.mule.db.commons.internal.domain.connection.ConnectionTracingMetadataUtils.getProtocolFrom; |
| 11 | +import static org.mule.extension.db.internal.domain.connection.datasource.NoConnectionMetadata.getMetadata; |
| 12 | +import static org.slf4j.LoggerFactory.getLogger; |
| 13 | + |
| 14 | +import static java.util.Optional.empty; |
| 15 | +import static java.util.Optional.ofNullable; |
| 16 | + |
| 17 | +import org.mule.db.commons.internal.domain.connection.DbConnectionTracingMetadata; |
| 18 | + |
| 19 | +import java.sql.Connection; |
| 20 | +import java.sql.DatabaseMetaData; |
| 21 | +import java.sql.SQLException; |
| 22 | +import java.util.Optional; |
| 23 | + |
| 24 | +import org.slf4j.Logger; |
| 25 | + |
| 26 | +/** |
| 27 | + * A {@link DbConnectionTracingMetadata} based on the connection metadata |
| 28 | + */ |
| 29 | +public class ConnectionBasedDbConnectionTracingMetadata implements DbConnectionTracingMetadata { |
| 30 | + |
| 31 | + public static final String UNSET = "unset"; |
| 32 | + private static final Logger LOGGER = getLogger(ConnectionBasedDbConnectionTracingMetadata.class); |
| 33 | + private final Connection connection; |
| 34 | + private DatabaseMetaData metadata; |
| 35 | + |
| 36 | + public ConnectionBasedDbConnectionTracingMetadata(Connection connection) { |
| 37 | + this.connection = connection; |
| 38 | + } |
| 39 | + |
| 40 | + @Override |
| 41 | + public String getDbSystem() { |
| 42 | + try { |
| 43 | + return getMetaData().getDatabaseProductName(); |
| 44 | + } catch (SQLException e) { |
| 45 | + LOGGER.debug("Unable to retrieve connection metadata for tracing.", e); |
| 46 | + } |
| 47 | + |
| 48 | + return UNSET; |
| 49 | + } |
| 50 | + |
| 51 | + private DatabaseMetaData getMetaData() throws SQLException { |
| 52 | + // In case the information cannot be retrieved it will not be present. |
| 53 | + if (metadata == null) { |
| 54 | + try { |
| 55 | + metadata = connection.getMetaData(); |
| 56 | + } catch (Throwable e) { |
| 57 | + LOGGER.debug("Unable to retrieve database metadata from connection", e); |
| 58 | + metadata = getMetadata(); |
| 59 | + } |
| 60 | + |
| 61 | + } |
| 62 | + |
| 63 | + return metadata; |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public String getConnectionString() { |
| 68 | + try { |
| 69 | + return getMetaData().getURL(); |
| 70 | + } catch (SQLException e) { |
| 71 | + LOGGER.debug("Unable to retrieve connection metadata for tracing.", e); |
| 72 | + } |
| 73 | + |
| 74 | + return UNSET; |
| 75 | + } |
| 76 | + |
| 77 | + @Override |
| 78 | + public String getUser() { |
| 79 | + try { |
| 80 | + return getMetaData().getUserName(); |
| 81 | + } catch (SQLException e) { |
| 82 | + LOGGER.debug("Unable to retrieve connection metadata for tracing.", e); |
| 83 | + } |
| 84 | + |
| 85 | + return UNSET; |
| 86 | + } |
| 87 | + |
| 88 | + @Override |
| 89 | + public Optional<String> getPeerName() { |
| 90 | + try { |
| 91 | + return ofNullable(getHostFrom(getMetaData().getURL())); |
| 92 | + } catch (SQLException e) { |
| 93 | + LOGGER.debug("Unable to retrieve connection metadata for tracing.", e); |
| 94 | + } |
| 95 | + |
| 96 | + return empty(); |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public Optional<String> getPeerTransport() { |
| 101 | + try { |
| 102 | + return ofNullable(getProtocolFrom(getMetaData().getURL())); |
| 103 | + } catch (SQLException e) { |
| 104 | + LOGGER.debug("Unable to retrieve connection metadata for tracing.", e); |
| 105 | + } |
| 106 | + |
| 107 | + return empty(); |
| 108 | + } |
| 109 | +} |
0 commit comments