Skip to content

Commit

Permalink
Added TenantDataSourceConfig annotation to add possibility exclude da…
Browse files Browse the repository at this point in the history
…tasource to be used by multi-tenancy.
  • Loading branch information
jhron authored and puneetbehl committed Mar 5, 2024
1 parent a6f29a6 commit 7de2b06
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@

import org.grails.datastore.mapping.config.Entity;
import org.grails.datastore.mapping.model.PersistentEntity;
import org.grails.datastore.mapping.multitenancy.TenantDataSourceConfig;
import org.springframework.util.ClassUtils;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

Expand Down Expand Up @@ -56,7 +58,7 @@ public static List<String> getConnectionSourceNames(PersistentEntity entity) {
public static boolean usesConnectionSource(PersistentEntity entity, String connectionSourceName) {
Class[] interfaces = ClassUtils.getAllInterfacesForClass(entity.getJavaClass());
if(isMultiTenant(interfaces)) {
return true;
return !isMultiTenantExcludedDataSource( entity, connectionSourceName );
}
else {
List<String> names = getConnectionSourceNames(entity);
Expand All @@ -74,4 +76,22 @@ protected static boolean isMultiTenant(Class[] interfaces) {
}
return false;
}

/**
* Returns whether the given entity should be excluded from given connection source name or not.
* It can be configured over {@link TenantDataSourceConfig} annotation
*
* @param entity The name of the multi tenant entity
* @param connectionSourceName The connection source name to check
* @return Whether the given connection should be excluded
*/
protected static boolean isMultiTenantExcludedDataSource(PersistentEntity entity, String connectionSourceName) {
TenantDataSourceConfig tdsc = (TenantDataSourceConfig) entity.getJavaClass().getAnnotation(TenantDataSourceConfig.class);
boolean result = false;
if ( null != tdsc ) {
final String[] dataSourcesToExclude = tdsc.dataSourcesToExclude();
result = Arrays.asList(dataSourcesToExclude).contains(connectionSourceName);
}
return result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Copyright 2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http:https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.grails.datastore.mapping.multitenancy;

import java.lang.annotation.*;

/**
* <p>An annotation that adds possibility to configure whether a domain should be excluded from particular
* data source(s). For example:</p>
*
* <pre>
* {@literal @}TenantDataSourceConfig(dataSourcesToExclude=["adminDataSource"])
* class Animal implements MultiTenant<Animal> {
* ...
* }
* </pre>
*/
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface TenantDataSourceConfig {
String[] dataSourcesToExclude() default "";
}

0 comments on commit 7de2b06

Please sign in to comment.