|
| 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 | +package org.apache.dolphinscheduler.plugin.datasource.azuresql; |
| 19 | + |
| 20 | +import org.apache.dolphinscheduler.plugin.datasource.api.client.CommonDataSourceClient; |
| 21 | +import org.apache.dolphinscheduler.plugin.datasource.azuresql.param.AzureSQLAuthMode; |
| 22 | +import org.apache.dolphinscheduler.plugin.datasource.azuresql.param.AzureSQLConnectionParam; |
| 23 | +import org.apache.dolphinscheduler.plugin.datasource.azuresql.param.AzureSQLDataSourceProcessor; |
| 24 | +import org.apache.dolphinscheduler.spi.datasource.BaseConnectionParam; |
| 25 | +import org.apache.dolphinscheduler.spi.enums.DbType; |
| 26 | + |
| 27 | +import java.sql.Connection; |
| 28 | +import java.sql.SQLException; |
| 29 | +import java.sql.Statement; |
| 30 | +import java.util.concurrent.TimeUnit; |
| 31 | + |
| 32 | +import org.slf4j.Logger; |
| 33 | +import org.slf4j.LoggerFactory; |
| 34 | + |
| 35 | +import com.google.common.base.Stopwatch; |
| 36 | + |
| 37 | +public class AzureSQLDataSourceClient extends CommonDataSourceClient { |
| 38 | + |
| 39 | + private static final Logger logger = LoggerFactory.getLogger(AzureSQLDataSourceClient.class); |
| 40 | + |
| 41 | + public AzureSQLDataSourceClient(BaseConnectionParam baseConnectionParam, DbType dbType) { |
| 42 | + super(baseConnectionParam, dbType); |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + public Connection getConnection() { |
| 47 | + AzureSQLConnectionParam connectionParam = (AzureSQLConnectionParam) this.baseConnectionParam; |
| 48 | + if (!connectionParam.getMode().equals(AzureSQLAuthMode.ACCESSTOKEN)) { |
| 49 | + return super.getConnection(); |
| 50 | + } |
| 51 | + return AzureSQLDataSourceProcessor.tokenGetConnection(connectionParam); |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public void checkClient() { |
| 56 | + |
| 57 | + AzureSQLConnectionParam connectionParam = (AzureSQLConnectionParam) this.baseConnectionParam; |
| 58 | + Stopwatch stopwatch = Stopwatch.createStarted(); |
| 59 | + String validationQuery = this.baseConnectionParam.getValidationQuery(); |
| 60 | + if (!connectionParam.getMode().equals(AzureSQLAuthMode.ACCESSTOKEN)) { |
| 61 | + // Checking data source client |
| 62 | + try { |
| 63 | + this.jdbcTemplate.execute(validationQuery); |
| 64 | + } catch (Exception e) { |
| 65 | + throw new RuntimeException("JDBC connect failed", e); |
| 66 | + } finally { |
| 67 | + logger.info("Time to execute check jdbc client with sql {} for {} ms ", |
| 68 | + this.baseConnectionParam.getValidationQuery(), stopwatch.elapsed(TimeUnit.MILLISECONDS)); |
| 69 | + } |
| 70 | + } else { |
| 71 | + try (Statement statement = getConnection().createStatement()) { |
| 72 | + if (!statement.execute(validationQuery)) { |
| 73 | + throw new SQLException("execute check azure sql token client failed : " + validationQuery); |
| 74 | + } |
| 75 | + } catch (SQLException e) { |
| 76 | + throw new RuntimeException(e); |
| 77 | + } finally { |
| 78 | + logger.info("Time to execute check azure sql token client with sql {} for {} ms ", |
| 79 | + this.baseConnectionParam.getValidationQuery(), stopwatch.elapsed(TimeUnit.MILLISECONDS)); |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | +} |
0 commit comments