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.EqualsAndHashCode;
21  import lombok.Getter;
22  import org.apache.shardingsphere.data.pipeline.api.PipelineDataSourceConfiguration;
23  import org.apache.shardingsphere.data.pipeline.spi.JdbcQueryPropertiesExtension;
24  import org.apache.shardingsphere.infra.database.core.connector.url.JdbcUrlAppender;
25  import org.apache.shardingsphere.infra.database.core.connector.url.StandardJdbcUrlParser;
26  import org.apache.shardingsphere.infra.database.core.spi.DatabaseTypedSPILoader;
27  import org.apache.shardingsphere.infra.database.core.type.DatabaseType;
28  import org.apache.shardingsphere.infra.database.core.type.DatabaseTypeFactory;
29  import org.apache.shardingsphere.infra.datasource.pool.props.domain.DataSourcePoolProperties;
30  import org.apache.shardingsphere.infra.util.yaml.YamlEngine;
31  import org.apache.shardingsphere.infra.yaml.config.swapper.resource.YamlDataSourceConfigurationSwapper;
32  
33  import java.util.Arrays;
34  import java.util.HashMap;
35  import java.util.LinkedHashMap;
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  public final class StandardPipelineDataSourceConfiguration implements PipelineDataSourceConfiguration {
45      
46      public static final String TYPE = "JDBC";
47      
48      private static final String DATA_SOURCE_CLASS_NAME = "dataSourceClassName";
49      
50      @Getter
51      private final String parameter;
52      
53      private final DataSourcePoolProperties dataSourcePoolProps;
54      
55      @Getter
56      private final DatabaseType databaseType;
57      
58      @Getter
59      private final String url;
60      
61      @Getter
62      private final String username;
63      
64      @Getter
65      private final String password;
66      
67      @SuppressWarnings("unchecked")
68      public StandardPipelineDataSourceConfiguration(final String param) {
69          this(param, YamlEngine.unmarshal(param, Map.class));
70      }
71      
72      public StandardPipelineDataSourceConfiguration(final Map<String, Object> poolProps) {
73          this(YamlEngine.marshal(poolProps), new HashMap<>(poolProps));
74      }
75      
76      private StandardPipelineDataSourceConfiguration(final String param, final Map<String, Object> poolProps) {
77          parameter = param;
78          for (String each : Arrays.asList("minPoolSize", "minimumIdle")) {
79              poolProps.put(each, "1");
80          }
81          if (poolProps.containsKey("jdbcUrl")) {
82              poolProps.put("url", poolProps.get("jdbcUrl"));
83              poolProps.remove("jdbcUrl");
84          }
85          poolProps.remove(DATA_SOURCE_CLASS_NAME);
86          databaseType = DatabaseTypeFactory.get(String.valueOf(poolProps.get("url")));
87          poolProps.put(DATA_SOURCE_CLASS_NAME, "com.zaxxer.hikari.HikariDataSource");
88          appendJdbcQueryProperties(databaseType, poolProps);
89          username = String.valueOf(poolProps.get("username"));
90          password = String.valueOf(poolProps.get("password"));
91          url = String.valueOf(poolProps.get("url"));
92          dataSourcePoolProps = new YamlDataSourceConfigurationSwapper().swapToDataSourcePoolProperties(poolProps);
93      }
94      
95      public StandardPipelineDataSourceConfiguration(final String jdbcUrl, final String username, final String password) {
96          this(wrapParameter(jdbcUrl, username, password));
97      }
98      
99      private static Map<String, Object> wrapParameter(final String jdbcUrl, final String username, final String password) {
100         Map<String, Object> result = new LinkedHashMap<>(3, 1F);
101         // Reference ConnectionPropertySynonyms
102         result.put("url", jdbcUrl);
103         result.put("username", username);
104         result.put("password", password);
105         return result;
106     }
107     
108     private void appendJdbcQueryProperties(final DatabaseType databaseType, final Map<String, Object> poolProps) {
109         Optional<JdbcQueryPropertiesExtension> extension = DatabaseTypedSPILoader.findService(JdbcQueryPropertiesExtension.class, databaseType);
110         if (!extension.isPresent()) {
111             return;
112         }
113         String jdbcUrl = String.valueOf(poolProps.get("url"));
114         Properties queryProps = new StandardJdbcUrlParser().parseQueryProperties(jdbcUrl.contains("?") ? jdbcUrl.substring(jdbcUrl.indexOf("?") + 1) : "");
115         extension.get().extendQueryProperties(queryProps);
116         String url = new JdbcUrlAppender().appendQueryProperties(jdbcUrl, queryProps);
117         poolProps.put("url", url);
118     }
119     
120     @Override
121     public String getType() {
122         return TYPE;
123     }
124     
125     @Override
126     public Object getDataSourceConfiguration() {
127         return dataSourcePoolProps;
128     }
129 }