22
33import org .grails .datastore .mapping .config .Entity ;
44import org .grails .datastore .mapping .model .PersistentEntity ;
5+ import org .grails .datastore .mapping .multitenancy .TenantDataSourceConfig ;
56import org .springframework .util .ClassUtils ;
67
8+ import java .util .Arrays ;
79import java .util .Collections ;
810import java .util .List ;
911
@@ -56,7 +58,7 @@ public static List<String> getConnectionSourceNames(PersistentEntity entity) {
5658 public static boolean usesConnectionSource (PersistentEntity entity , String connectionSourceName ) {
5759 Class [] interfaces = ClassUtils .getAllInterfacesForClass (entity .getJavaClass ());
5860 if (isMultiTenant (interfaces )) {
59- return true ;
61+ return ! isMultiTenantExcludedDataSource ( entity , connectionSourceName ) ;
6062 }
6163 else {
6264 List <String > names = getConnectionSourceNames (entity );
@@ -74,4 +76,22 @@ protected static boolean isMultiTenant(Class[] interfaces) {
7476 }
7577 return false ;
7678 }
79+
80+ /**
81+ * Returns whether the given entity should be excluded from given connection source name or not.
82+ * It can be configured over {@link TenantDataSourceConfig} annotation
83+ *
84+ * @param entity The name of the multi tenant entity
85+ * @param connectionSourceName The connection source name to check
86+ * @return Whether the given connection should be excluded
87+ */
88+ protected static boolean isMultiTenantExcludedDataSource (PersistentEntity entity , String connectionSourceName ) {
89+ TenantDataSourceConfig tdsc = (TenantDataSourceConfig ) entity .getJavaClass ().getAnnotation (TenantDataSourceConfig .class );
90+ boolean result = false ;
91+ if ( null != tdsc ) {
92+ final String [] dataSourcesToExclude = tdsc .dataSourcesToExclude ();
93+ result = Arrays .asList (dataSourcesToExclude ).contains (connectionSourceName );
94+ }
95+ return result ;
96+ }
7797}
0 commit comments