View Javadoc
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.shardingsphere.data.pipeline.api.type;
19  
20  import lombok.AccessLevel;
21  import lombok.EqualsAndHashCode;
22  import lombok.Getter;
23  import org.apache.shardingsphere.data.pipeline.api.PipelineDataSourceConfiguration;
24  import org.apache.shardingsphere.data.pipeline.spi.JdbcQueryPropertiesExtension;
25  import org.apache.shardingsphere.database.connector.core.jdbcurl.appender.JdbcUrlAppender;
26  import org.apache.shardingsphere.database.connector.core.jdbcurl.parser.StandardJdbcUrlParser;
27  import org.apache.shardingsphere.database.connector.core.spi.DatabaseTypedSPILoader;
28  import org.apache.shardingsphere.database.connector.core.type.DatabaseType;
29  import org.apache.shardingsphere.database.connector.core.type.DatabaseTypeFactory;
30  import org.apache.shardingsphere.infra.datasource.pool.props.domain.DataSourcePoolProperties;
31  import org.apache.shardingsphere.infra.util.yaml.YamlEngine;
32  import org.apache.shardingsphere.infra.yaml.config.swapper.resource.YamlDataSourceConfigurationSwapper;
33  
34  import java.util.Arrays;
35  import java.util.HashMap;
36  import java.util.Map;
37  import java.util.Optional;
38  import java.util.Properties;
39  
40  /**
41   * Pipeline data source configuration for standard JDBC.
42   */
43  @EqualsAndHashCode(of = "parameter")
44  @Getter
45  public final class StandardPipelineDataSourceConfiguration implements PipelineDataSourceConfiguration {
46      
47      public static final String TYPE = "JDBC";
48      
49      private static final String DATA_SOURCE_CLASS_NAME = "dataSourceClassName";
50      
51      private final String parameter;
52      
53      private final DatabaseType databaseType;
54      
55      private final String url;
56      
57      private final String username;
58      
59      private final String password;
60      
61      @Getter(AccessLevel.NONE)
62      private final DataSourcePoolProperties dataSourcePoolProps;
63      
64      @SuppressWarnings("unchecked")
65      public StandardPipelineDataSourceConfiguration(final String poolYamlContent) {
66          this(YamlEngine.unmarshal(poolYamlContent, Map.class));
67      }
68      
69      public StandardPipelineDataSourceConfiguration(final Map<String, Object> poolProps) {
70          parameter = YamlEngine.marshal(poolProps);
71          Map<String, Object> newPoolProps = new HashMap<>(poolProps);
72          for (String each : Arrays.asList("minPoolSize", "minimumIdle")) {
73              newPoolProps.put(each, "1");
74          }
75          if (newPoolProps.containsKey("jdbcUrl")) {
76              newPoolProps.put("url", newPoolProps.get("jdbcUrl"));
77              newPoolProps.remove("jdbcUrl");
78          }
79          databaseType = DatabaseTypeFactory.get(String.valueOf(newPoolProps.get("url")));
80          newPoolProps.remove(DATA_SOURCE_CLASS_NAME);
81          newPoolProps.put(DATA_SOURCE_CLASS_NAME, "com.zaxxer.hikari.HikariDataSource");
82          appendJdbcQueryProperties(newPoolProps);
83          url = String.valueOf(newPoolProps.get("url"));
84          username = String.valueOf(newPoolProps.get("username"));
85          password = String.valueOf(newPoolProps.get("password"));
86          dataSourcePoolProps = new YamlDataSourceConfigurationSwapper().swapToDataSourcePoolProperties(newPoolProps);
87      }
88      
89      private void appendJdbcQueryProperties(final Map<String, Object> poolProps) {
90          Optional<JdbcQueryPropertiesExtension> extension = DatabaseTypedSPILoader.findService(JdbcQueryPropertiesExtension.class, databaseType);
91          if (!extension.isPresent()) {
92              return;
93          }
94          String jdbcUrl = String.valueOf(poolProps.get("url"));
95          Properties queryProps = new StandardJdbcUrlParser().parseQueryProperties(jdbcUrl.contains("?") ? jdbcUrl.substring(jdbcUrl.indexOf("?") + 1) : "");
96          extension.get().extendQueryProperties(queryProps);
97          String url = new JdbcUrlAppender().appendQueryProperties(jdbcUrl, queryProps);
98          poolProps.put("url", url);
99      }
100     
101     @Override
102     public Object getDataSourceConfiguration() {
103         return dataSourcePoolProps;
104     }
105     
106     @Override
107     public String getType() {
108         return TYPE;
109     }
110 }