Skip to content

Commit

Permalink
Refactor ShardingSphereDriverUtils (#30269)
Browse files Browse the repository at this point in the history
  • Loading branch information
terrymanu authored Feb 24, 2024
1 parent e122a38 commit 4a201a0
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public Optional<ContextManager> getContextManager() {
if (isEnhancedForProxy) {
return Optional.ofNullable(ProxyContext.getInstance().getContextManager());
}
Optional<Map<String, ShardingSphereDataSource>> dataSourceMap = ShardingSphereDriverUtils.getShardingSphereDataSources();
Optional<Map<String, ShardingSphereDataSource>> dataSourceMap = ShardingSphereDriverUtils.findShardingSphereDataSources();
if (dataSourceMap.isPresent() && !dataSourceMap.get().isEmpty()) {
return Optional.ofNullable(AgentReflectionUtils.getFieldValue(dataSourceMap.get().values().iterator().next(), "contextManager"));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,27 +39,27 @@
public final class ShardingSphereDriverUtils {

/**
* Get ShardingSphere data sources.
* Find ShardingSphere data sources.
*
* @return got data source
* @return found data source
*/
public static Optional<Map<String, ShardingSphereDataSource>> getShardingSphereDataSources() {
Optional<ShardingSphereDriver> driver = getShardingSphereDriver();
if (driver.isPresent()) {
DriverDataSourceCache dataSourceCache = AgentReflectionUtils.getFieldValue(driver.get(), "dataSourceCache");
Map<String, DataSource> dataSourceMap = AgentReflectionUtils.getFieldValue(dataSourceCache, "dataSourceMap");
Map<String, ShardingSphereDataSource> result = new LinkedHashMap<>();
for (Entry<String, DataSource> entry : dataSourceMap.entrySet()) {
if (entry.getValue() instanceof ShardingSphereDataSource) {
result.put(entry.getKey(), (ShardingSphereDataSource) entry.getValue());
}
public static Optional<Map<String, ShardingSphereDataSource>> findShardingSphereDataSources() {
Optional<ShardingSphereDriver> driver = findShardingSphereDriver();
if (!driver.isPresent()) {
return Optional.empty();
}
DriverDataSourceCache dataSourceCache = AgentReflectionUtils.getFieldValue(driver.get(), "dataSourceCache");
Map<String, DataSource> dataSourceMap = AgentReflectionUtils.getFieldValue(dataSourceCache, "dataSourceMap");
Map<String, ShardingSphereDataSource> result = new LinkedHashMap<>();
for (Entry<String, DataSource> entry : dataSourceMap.entrySet()) {
if (entry.getValue() instanceof ShardingSphereDataSource) {
result.put(entry.getKey(), (ShardingSphereDataSource) entry.getValue());
}
return Optional.of(result);
}
return Optional.empty();
return Optional.of(result);
}

private static Optional<ShardingSphereDriver> getShardingSphereDriver() {
private static Optional<ShardingSphereDriver> findShardingSphereDriver() {
Enumeration<Driver> driverEnumeration = DriverManager.getDrivers();
while (driverEnumeration.hasMoreElements()) {
Driver driver = driverEnumeration.nextElement();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public final class JDBCMetaDataInfoExporter implements MetricsExporter {

@Override
public Optional<GaugeMetricFamilyMetricsCollector> export(final String pluginType) {
Optional<Map<String, ShardingSphereDataSource>> dataSourceMap = ShardingSphereDriverUtils.getShardingSphereDataSources();
Optional<Map<String, ShardingSphereDataSource>> dataSourceMap = ShardingSphereDriverUtils.findShardingSphereDataSources();
if (!dataSourceMap.isPresent()) {
return Optional.empty();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public final class JDBCStateExporter implements MetricsExporter {

@Override
public Optional<GaugeMetricFamilyMetricsCollector> export(final String pluginType) {
Optional<Map<String, ShardingSphereDataSource>> dataSourceMap = ShardingSphereDriverUtils.getShardingSphereDataSources();
Optional<Map<String, ShardingSphereDataSource>> dataSourceMap = ShardingSphereDriverUtils.findShardingSphereDataSources();
if (!dataSourceMap.isPresent()) {
return Optional.empty();
}
Expand Down

0 comments on commit 4a201a0

Please sign in to comment.