Skip to content

Commit 60c39f3

Browse files
authored
Added TenantDataSourceConfig annotation to add possibility exclude datasource to be used by multi-tenancy. (#1380)
1 parent a6f29a6 commit 60c39f3

File tree

2 files changed

+57
-1
lines changed

2 files changed

+57
-1
lines changed

grails-datastore-core/src/main/groovy/org/grails/datastore/mapping/core/connections/ConnectionSourcesSupport.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@
22

33
import org.grails.datastore.mapping.config.Entity;
44
import org.grails.datastore.mapping.model.PersistentEntity;
5+
import org.grails.datastore.mapping.multitenancy.TenantDataSourceConfig;
56
import org.springframework.util.ClassUtils;
67

8+
import java.util.Arrays;
79
import java.util.Collections;
810
import 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
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright 2017 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.grails.datastore.mapping.multitenancy;
17+
18+
import java.lang.annotation.*;
19+
20+
/**
21+
* <p>An annotation that adds possibility to configure whether a domain should be excluded from particular
22+
* data source(s). For example:</p>
23+
*
24+
* <pre>
25+
* {@literal @}TenantDataSourceConfig(dataSourcesToExclude=["adminDataSource"])
26+
* class Animal implements MultiTenant<Animal> {
27+
* ...
28+
* }
29+
* </pre>
30+
*/
31+
@Target(ElementType.TYPE)
32+
@Retention(RetentionPolicy.RUNTIME)
33+
@Documented
34+
public @interface TenantDataSourceConfig {
35+
String[] dataSourcesToExclude() default "";
36+
}

0 commit comments

Comments
 (0)