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.driver.api;
19  
20  import com.google.common.base.Strings;
21  import lombok.AccessLevel;
22  import lombok.NoArgsConstructor;
23  import org.apache.shardingsphere.driver.jdbc.core.datasource.ShardingSphereDataSource;
24  import org.apache.shardingsphere.infra.config.mode.ModeConfiguration;
25  import org.apache.shardingsphere.infra.config.rule.RuleConfiguration;
26  import org.apache.shardingsphere.database.connector.core.DefaultDatabase;
27  
28  import javax.sql.DataSource;
29  import java.sql.SQLException;
30  import java.util.Collection;
31  import java.util.Collections;
32  import java.util.LinkedHashMap;
33  import java.util.LinkedList;
34  import java.util.Map;
35  import java.util.Properties;
36  
37  /**
38   * ShardingSphere data source factory.
39   */
40  @NoArgsConstructor(access = AccessLevel.PRIVATE)
41  public final class ShardingSphereDataSourceFactory {
42      
43      /**
44       * Create ShardingSphere data source.
45       *
46       * @param modeConfig mode configuration
47       * @return ShardingSphere data source
48       * @throws SQLException SQL exception
49       */
50      public static DataSource createDataSource(final ModeConfiguration modeConfig) throws SQLException {
51          return createDataSource(DefaultDatabase.LOGIC_NAME, modeConfig);
52      }
53      
54      /**
55       * Create ShardingSphere data source.
56       *
57       * @param databaseName database name
58       * @param modeConfig mode configuration
59       * @return ShardingSphere data source
60       * @throws SQLException SQL exception
61       */
62      public static DataSource createDataSource(final String databaseName, final ModeConfiguration modeConfig) throws SQLException {
63          return new ShardingSphereDataSource(getDatabaseName(databaseName), modeConfig);
64      }
65      
66      /**
67       * Create ShardingSphere data source.
68       *
69       * @param modeConfig mode configuration
70       * @param dataSourceMap data source map
71       * @param configs rule configurations
72       * @param props properties for data source
73       * @return ShardingSphere data source
74       * @throws SQLException SQL exception
75       */
76      public static DataSource createDataSource(final ModeConfiguration modeConfig,
77                                                final Map<String, DataSource> dataSourceMap, final Collection<RuleConfiguration> configs, final Properties props) throws SQLException {
78          return createDataSource(DefaultDatabase.LOGIC_NAME, modeConfig, dataSourceMap, configs, props);
79      }
80      
81      /**
82       * Create ShardingSphere data source.
83       *
84       * @param databaseName database name
85       * @param modeConfig mode configuration
86       * @param dataSourceMap data source map
87       * @param configs rule configurations
88       * @param props properties for data source
89       * @return ShardingSphere data source
90       * @throws SQLException SQL exception
91       */
92      public static DataSource createDataSource(final String databaseName, final ModeConfiguration modeConfig,
93                                                final Map<String, DataSource> dataSourceMap, final Collection<RuleConfiguration> configs, final Properties props) throws SQLException {
94          return new ShardingSphereDataSource(getDatabaseName(databaseName),
95                  modeConfig, null == dataSourceMap ? new LinkedHashMap<>() : dataSourceMap, null == configs ? new LinkedList<>() : configs, props);
96      }
97      
98      /**
99       * Create ShardingSphere data source.
100      *
101      * @param databaseName database name
102      * @param modeConfig mode configuration
103      * @param dataSource data source
104      * @param configs rule configurations
105      * @param props properties for data source
106      * @return ShardingSphere data source
107      * @throws SQLException SQL exception
108      */
109     public static DataSource createDataSource(final String databaseName, final ModeConfiguration modeConfig,
110                                               final DataSource dataSource, final Collection<RuleConfiguration> configs, final Properties props) throws SQLException {
111         return createDataSource(databaseName, modeConfig, Collections.singletonMap(getDatabaseName(databaseName), dataSource), configs, props);
112     }
113     
114     /**
115      * Create ShardingSphere data source.
116      *
117      * @param modeConfig mode configuration
118      * @param dataSource data source
119      * @param configs rule configurations
120      * @param props properties for data source
121      * @return ShardingSphere data source
122      * @throws SQLException SQL exception
123      */
124     public static DataSource createDataSource(final ModeConfiguration modeConfig,
125                                               final DataSource dataSource, final Collection<RuleConfiguration> configs, final Properties props) throws SQLException {
126         return createDataSource(modeConfig, Collections.singletonMap(DefaultDatabase.LOGIC_NAME, dataSource), configs, props);
127     }
128     
129     /**
130      * Create ShardingSphere data source.
131      *
132      * @param dataSourceMap data source map
133      * @param configs rule configurations
134      * @param props properties for data source
135      * @return ShardingSphere data source
136      * @throws SQLException SQL exception
137      */
138     public static DataSource createDataSource(final Map<String, DataSource> dataSourceMap, final Collection<RuleConfiguration> configs, final Properties props) throws SQLException {
139         return createDataSource((ModeConfiguration) null, dataSourceMap, configs, props);
140     }
141     
142     /**
143      * Create ShardingSphere data source.
144      *
145      * @param databaseName database name
146      * @param dataSourceMap data source map
147      * @param configs rule configurations
148      * @param props properties for data source
149      * @return ShardingSphere data source
150      * @throws SQLException SQL exception
151      */
152     public static DataSource createDataSource(final String databaseName,
153                                               final Map<String, DataSource> dataSourceMap, final Collection<RuleConfiguration> configs, final Properties props) throws SQLException {
154         return createDataSource(databaseName, null, dataSourceMap, configs, props);
155     }
156     
157     /**
158      * Create ShardingSphere data source.
159      *
160      * @param dataSource data source
161      * @param configs rule configurations
162      * @param props properties for data source
163      * @return ShardingSphere data source
164      * @throws SQLException SQL exception
165      */
166     public static DataSource createDataSource(final DataSource dataSource, final Collection<RuleConfiguration> configs, final Properties props) throws SQLException {
167         return createDataSource((ModeConfiguration) null, dataSource, configs, props);
168     }
169     
170     /**
171      * Create ShardingSphere data source.
172      *
173      * @param databaseName database name
174      * @param dataSource data source
175      * @param configs rule configurations
176      * @param props properties for data source
177      * @return ShardingSphere data source
178      * @throws SQLException SQL exception
179      */
180     public static DataSource createDataSource(final String databaseName, final DataSource dataSource, final Collection<RuleConfiguration> configs, final Properties props) throws SQLException {
181         return createDataSource(databaseName, null, dataSource, configs, props);
182     }
183     
184     private static String getDatabaseName(final String databaseName) {
185         return Strings.isNullOrEmpty(databaseName) ? DefaultDatabase.LOGIC_NAME : databaseName;
186     }
187 }