Skip to content

Commit 4df4995

Browse files
authored
Merge pull request apache#26 from SphereEx/pingan-multi-schema
Pick: jdbc support multi database configuration
2 parents aa17f64 + c6c4f50 commit 4df4995

File tree

22 files changed

+1196
-5
lines changed

22 files changed

+1196
-5
lines changed

infra/url/core/src/main/java/org/apache/shardingsphere/infra/url/core/ShardingSphereURLLoadEngine.java

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,18 @@
1717

1818
package org.apache.shardingsphere.infra.url.core;
1919

20+
import com.sphereex.dbplusengine.SphereEx;
21+
import com.sphereex.dbplusengine.infra.url.spi.ShardingSphereURLDirectoryLoader;
22+
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
2023
import org.apache.shardingsphere.infra.url.core.arg.URLArgumentLineRender;
24+
import org.apache.shardingsphere.infra.url.core.arg.URLArgumentPlaceholderType;
2125
import org.apache.shardingsphere.infra.url.core.arg.URLArgumentPlaceholderTypeFactory;
22-
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
2326
import org.apache.shardingsphere.infra.url.spi.ShardingSphereURLLoader;
2427

2528
import java.util.Arrays;
2629
import java.util.Collection;
30+
import java.util.Map;
31+
import java.util.stream.Collectors;
2732

2833
/**
2934
* ShardingSphere URL load engine.
@@ -34,9 +39,14 @@ public final class ShardingSphereURLLoadEngine {
3439

3540
private final ShardingSphereURLLoader urlLoader;
3641

42+
@SphereEx
43+
@SuppressWarnings("rawtypes")
44+
private final ShardingSphereURLDirectoryLoader urlDirectoryLoader;
45+
3746
public ShardingSphereURLLoadEngine(final ShardingSphereURL url) {
3847
this.url = url;
3948
urlLoader = TypedSPILoader.getService(ShardingSphereURLLoader.class, url.getSourceType());
49+
urlDirectoryLoader = TypedSPILoader.getService(ShardingSphereURLDirectoryLoader.class, url.getSourceType());
4050
}
4151

4252
/**
@@ -48,4 +58,29 @@ public byte[] loadContent() {
4858
Collection<String> lines = Arrays.asList(urlLoader.load(url.getConfigurationSubject(), url.getQueryProps()).split(System.lineSeparator()));
4959
return URLArgumentLineRender.render(lines, URLArgumentPlaceholderTypeFactory.valueOf(url.getQueryProps()));
5060
}
61+
62+
/**
63+
* Is directory or not.
64+
*
65+
* @return directory or not
66+
*/
67+
@SphereEx
68+
public boolean isDirectory() {
69+
return urlDirectoryLoader.isDirectory(url.getConfigurationSubject(), url.getQueryProps());
70+
}
71+
72+
/**
73+
* Load configuration contents.
74+
*
75+
* @return loaded contents
76+
*/
77+
@SuppressWarnings("unchecked")
78+
@SphereEx
79+
public Map<String, byte[]> loadContents() {
80+
URLArgumentPlaceholderType placeholderType = URLArgumentPlaceholderTypeFactory.valueOf(url.getQueryProps());
81+
Collection<?> allConfigurationSubjects = urlDirectoryLoader.getAllConfigurationSubjects(url.getConfigurationSubject(), url.getQueryProps());
82+
return allConfigurationSubjects.stream().collect(Collectors.toMap(Object::toString,
83+
each -> URLArgumentLineRender.render(
84+
Arrays.asList(urlDirectoryLoader.loadIndicate(url.getConfigurationSubject(), url.getQueryProps(), urlLoader, each).split(System.lineSeparator())), placeholderType)));
85+
}
5186
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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 com.sphereex.dbplusengine.infra.url.spi;
19+
20+
import org.apache.shardingsphere.infra.spi.annotation.SingletonSPI;
21+
import org.apache.shardingsphere.infra.spi.type.typed.TypedSPI;
22+
import org.apache.shardingsphere.infra.url.spi.ShardingSphereURLLoader;
23+
24+
import java.util.Collection;
25+
import java.util.Properties;
26+
27+
/**
28+
* ShardingSphere URL directory loader.
29+
*
30+
* @param <T> type of indicated subject
31+
*/
32+
@SingletonSPI
33+
public interface ShardingSphereURLDirectoryLoader<T> extends TypedSPI {
34+
35+
/**
36+
* Is directory or not.
37+
*
38+
* @param configurationSubject configuration subject
39+
* @param queryProps query properties
40+
* @return directory or not
41+
*/
42+
boolean isDirectory(String configurationSubject, Properties queryProps);
43+
44+
/**
45+
* Get all configuration subjects.
46+
*
47+
* @param configurationSubject configuration subject
48+
* @param queryProps query properties
49+
* @return all configuration subjects
50+
*/
51+
Collection<T> getAllConfigurationSubjects(String configurationSubject, Properties queryProps);
52+
53+
/**
54+
* Load indicate content.
55+
*
56+
* @param configurationSubject configuration subject
57+
* @param queryProps query properties
58+
* @param urlLoader URL loader
59+
* @param indicatedSubject indicated subject
60+
* @return loaded content
61+
*/
62+
String loadIndicate(String configurationSubject, Properties queryProps, ShardingSphereURLLoader urlLoader, T indicatedSubject);
63+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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 com.sphereex.dbplusengine.infra.url.absolutepath;
19+
20+
import com.sphereex.dbplusengine.infra.url.spi.ShardingSphereURLDirectoryLoader;
21+
import org.apache.shardingsphere.infra.url.spi.ShardingSphereURLLoader;
22+
23+
import java.io.File;
24+
import java.util.Arrays;
25+
import java.util.Collection;
26+
import java.util.Collections;
27+
import java.util.Properties;
28+
import java.util.stream.Collectors;
29+
30+
/**
31+
* Absolute path URL directory loader.
32+
*/
33+
public final class AbsolutePathURLDirectoryLoader implements ShardingSphereURLDirectoryLoader<String> {
34+
35+
@Override
36+
public boolean isDirectory(final String configurationSubject, final Properties queryProps) {
37+
return getAbsoluteFile(configurationSubject).isDirectory();
38+
}
39+
40+
@Override
41+
public Collection<String> getAllConfigurationSubjects(final String configurationSubject, final Properties queryProps) {
42+
File[] files = getAbsoluteFile(configurationSubject).listFiles();
43+
return null == files ? Collections.emptyList() : Arrays.stream(files).map(File::getName).filter(each -> !each.startsWith(".")).collect(Collectors.toList());
44+
}
45+
46+
private File getAbsoluteFile(final String configurationSubject) {
47+
return new File(configurationSubject);
48+
}
49+
50+
@Override
51+
public String loadIndicate(final String configurationSubject, final Properties queryProps, final ShardingSphereURLLoader urlLoader, final String indicatedSubject) {
52+
return urlLoader.load(configurationSubject + File.separator + indicatedSubject, queryProps);
53+
}
54+
55+
@Override
56+
public String getType() {
57+
return "absolutepath:";
58+
}
59+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
com.sphereex.dbplusengine.infra.url.absolutepath.AbsolutePathURLDirectoryLoader
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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 com.sphereex.dbplusengine.infra.url.absolutepath;
19+
20+
import org.apache.shardingsphere.infra.url.absolutepath.AbsolutePathURLLoader;
21+
import org.junit.jupiter.api.Test;
22+
23+
import java.util.Collection;
24+
import java.util.Objects;
25+
import java.util.Properties;
26+
27+
import static org.hamcrest.CoreMatchers.is;
28+
import static org.hamcrest.MatcherAssert.assertThat;
29+
import static org.junit.jupiter.api.Assertions.assertFalse;
30+
import static org.junit.jupiter.api.Assertions.assertTrue;
31+
32+
class AbsolutePathURLDirectoryLoaderTest {
33+
34+
private final AbsolutePathURLDirectoryLoader loader = new AbsolutePathURLDirectoryLoader();
35+
36+
@Test
37+
void assertIsDirectory() {
38+
assertTrue(loader.isDirectory(getConfigurationPath(), new Properties()));
39+
}
40+
41+
@Test
42+
void assertGetAllConfigurationSubjects() {
43+
Collection<String> actual = loader.getAllConfigurationSubjects(getConfigurationPath(), new Properties());
44+
assertThat(actual.size(), is(1));
45+
assertThat(actual.iterator().next(), is("fixture.yaml"));
46+
}
47+
48+
@Test
49+
void assertLoadIndicate() {
50+
String actual = loader.loadIndicate(getConfigurationPath(), new Properties(), new AbsolutePathURLLoader(), "fixture.yaml");
51+
assertFalse(actual.isEmpty());
52+
}
53+
54+
private String getConfigurationPath() {
55+
return Objects.requireNonNull(Thread.currentThread().getContextClassLoader().getResource("config/absolutepath/")).getPath();
56+
}
57+
}

infra/url/type/classpath/pom.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,13 @@
3232
<artifactId>shardingsphere-infra-url-spi</artifactId>
3333
<version>${project.version}</version>
3434
</dependency>
35+
36+
<!-- SPEX ADDED: BEGIN -->
37+
<dependency>
38+
<groupId>org.apache.shardingsphere</groupId>
39+
<artifactId>shardingsphere-infra-util</artifactId>
40+
<version>${project.version}</version>
41+
</dependency>
42+
<!-- SPEX ADDED: END -->
3543
</dependencies>
3644
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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 com.sphereex.dbplusengine.infra.url.classpath;
19+
20+
import com.sphereex.dbplusengine.infra.url.spi.ShardingSphereURLDirectoryLoader;
21+
import org.apache.commons.lang3.StringUtils;
22+
import org.apache.shardingsphere.infra.url.spi.ShardingSphereURLLoader;
23+
import org.apache.shardingsphere.infra.util.directory.ClasspathResourceDirectoryReader;
24+
25+
import java.io.File;
26+
import java.util.Collection;
27+
import java.util.Properties;
28+
import java.util.stream.Collectors;
29+
import java.util.stream.Stream;
30+
31+
/**
32+
* Class path URL directory loader.
33+
*/
34+
public final class ClassPathURLDirectoryLoader implements ShardingSphereURLDirectoryLoader<String> {
35+
36+
@Override
37+
public boolean isDirectory(final String configurationSubject, final Properties queryProps) {
38+
return ClasspathResourceDirectoryReader.isDirectory(configurationSubject);
39+
}
40+
41+
@Override
42+
public Collection<String> getAllConfigurationSubjects(final String configurationSubject, final Properties queryProps) {
43+
try (Stream<String> resourceNameStream = ClasspathResourceDirectoryReader.read(configurationSubject)) {
44+
return resourceNameStream.map(resourceName -> StringUtils.removeStart(resourceName, configurationSubject + "/")).collect(Collectors.toList());
45+
}
46+
}
47+
48+
@Override
49+
public String loadIndicate(final String configurationSubject, final Properties queryProps, final ShardingSphereURLLoader urlLoader, final String indicatedSubject) {
50+
return urlLoader.load(configurationSubject + File.separator + indicatedSubject, queryProps);
51+
}
52+
53+
@Override
54+
public String getType() {
55+
return "classpath:";
56+
}
57+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
com.sphereex.dbplusengine.infra.url.classpath.ClassPathURLDirectoryLoader

0 commit comments

Comments
 (0)