Skip to content

Commit

Permalink
minor code cleanup in multitenancy package
Browse files Browse the repository at this point in the history
  • Loading branch information
clean-coder committed Apr 12, 2024
1 parent d5fb5e4 commit 11a94f4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,18 @@ public AbstractSchemaMultiTenantConnectionProvider() {
public Connection getConnection(String tenantIdentifier) throws SQLException {
Connection connection = super.getConnection(tenantIdentifier);

String schema = Objects.equals(tenantIdentifier, DEFAULT_TENANT_ID) ? tenantIdentifier
: MessageFormat.format("okr_{0}", tenantIdentifier);

String schema = convertTenantIdToSchemaName(tenantIdentifier);
logger.debug("Setting schema to {}", schema);
connection.createStatement().execute(String.format("SET SCHEMA '%s';", schema));

connection.createStatement().execute(String.format("SET SCHEMA '%s';", schema));
return connection;
}

private static String convertTenantIdToSchemaName(String tenantIdentifier) {
return Objects.equals(tenantIdentifier, DEFAULT_TENANT_ID) ? tenantIdentifier
: MessageFormat.format("okr_{0}", tenantIdentifier);
}

@Override
protected ConnectionProvider getAnyConnectionProvider() {
return getConnectionProvider(DEFAULT_TENANT_ID);
Expand Down Expand Up @@ -82,16 +85,20 @@ private Properties getHibernatePropertiesForTenantIdentifier(String tenantIdenti
}

private ConnectionProvider initConnectionProvider(Properties hibernateProperties) {
Map<String, Object> configProperties = new HashMap<>();
for (String key : hibernateProperties.stringPropertyNames()) {
String value = hibernateProperties.getProperty(key);
configProperties.put(key, value);
}

Map<String, Object> configProperties = convertPropertiesToMap(hibernateProperties);
DriverManagerConnectionProviderImpl connectionProvider = new DriverManagerConnectionProviderImpl();
connectionProvider.configure(configProperties);
return connectionProvider;
}

private static Map<String, Object> convertPropertiesToMap(Properties properties) {
Map<String, Object> configProperties = new HashMap<>();
for (String key : properties.stringPropertyNames()) {
String value = properties.getProperty(key);
configProperties.put(key, value);
}
return configProperties;
}

protected abstract String getHibernatePropertiesFilePaths();
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,20 @@ public TenantConfigProvider(final @Value("${okr.tenant-ids}") String[] tenantIds
Environment env) {
this.jwkSetUriTemplate = jwkSetUriTemplate;
this.env = env;

for (String tenantId : tenantIds) {
tenantConfigs.put(tenantId,
new TenantConfig(tenantId, getOkrChampionEmailsFromTenant(tenantId),
jwkSetUriTemplate.replace("{tenantid}", tenantId),
frontendClientIssuerUrl.replace("{tenantid}", tenantId),
frontendClientId.replace("{tenantid}", tenantId), this.readDataSourceConfig(tenantId)));
createTenantConfig(jwkSetUriTemplate, frontendClientIssuerUrl, frontendClientId, tenantId));
}
}

private TenantConfig createTenantConfig(String jwkSetUriTemplate, String frontendClientIssuerUrl,
String frontendClientId, String tenantId) {
return new TenantConfig(tenantId, getOkrChampionEmailsFromTenant(tenantId),
jwkSetUriTemplate.replace("{tenantid}", tenantId),
frontendClientIssuerUrl.replace("{tenantid}", tenantId),
frontendClientId.replace("{tenantid}", tenantId), this.readDataSourceConfig(tenantId));
}

private String[] getOkrChampionEmailsFromTenant(String tenantId) {
return Arrays.stream(env.getProperty(MessageFormat.format("okr.tenants.{0}.user.champion.emails", tenantId), "")
.split(EMAIL_DELIMITER)).map(String::trim).toArray(String[]::new);
Expand Down

0 comments on commit 11a94f4

Please sign in to comment.