|
1 | 1 | package grails.plugin.multitenant.core
|
2 | 2 |
|
3 |
| -import org.apache.log4j.Logger |
4 |
| - |
5 | 3 | import com.infusion.util.event.groovy.GroovyEventBroker
|
6 | 4 | import com.infusion.util.domain.event.HibernateEvent
|
7 |
| - |
8 |
| -import net.sf.ehcache.Cache |
9 |
| -import net.sf.ehcache.Element |
10 |
| -import net.sf.ehcache.CacheManager |
11 |
| - |
12 | 5 | import org.springframework.context.ApplicationContext
|
13 | 6 | import org.springframework.context.ApplicationContextAware
|
14 |
| - |
| 7 | +import org.apache.log4j.Logger |
| 8 | +import net.sf.ehcache.CacheManager |
| 9 | +import net.sf.ehcache.Cache |
| 10 | +import net.sf.ehcache.Element |
15 | 11 | import org.codehaus.groovy.grails.commons.ConfigurationHolder
|
16 | 12 |
|
17 | 13 | /**
|
18 | 14 | * Implementation that looks up tenantIds from a local table DomainTenantMap that stores domain name to tenantId mappings
|
19 |
| - * in support of the single or multi-tenant mode. The data will either be looked up from a configuration database on the |
| 15 | + * in support of the single or multi-tenant mode. The data will either be looked up from a configuration database on the |
20 | 16 | * default datastore.
|
21 | 17 | */
|
22 |
| -public class DomainNameDatabaseTenantResolver extends BaseDomainNameTenantResolver implements ApplicationContextAware { |
23 |
| - |
24 |
| - /** This is a logger for logging status and error messages. */ |
25 |
| - private static Logger log = Logger.getLogger(DomainNameDatabaseTenantResolver.class) |
| 18 | +public class DomainNameDatabaseTenantResolver extends BaseDomainNameTenantResolver implements ApplicationContextAware |
| 19 | +{ |
| 20 | + /** This is a logger for logging status and error messages. */ |
| 21 | + private static Logger log = Logger.getLogger(DomainNameDatabaseTenantResolver.class) |
26 | 22 |
|
27 |
| - /** Used for listening to save events for DomainTenantMap domain class */ |
28 |
| - GroovyEventBroker eventBroker |
| 23 | + /** |
| 24 | + * Used for listening to save events for DomainTenantMap domain class |
| 25 | + */ |
| 26 | + GroovyEventBroker eventBroker |
| 27 | + /** |
| 28 | + * This is the injected context that is used to look up the DomainTenantMap Bean. |
| 29 | + */ |
| 30 | + ApplicationContext applicationContext |
| 31 | + /** |
| 32 | + * This will initialize the data for the application by loading the domain name tenant map data. This data is loaded |
| 33 | + * from tenant 0 which could either be a separate configuration database or the default data source as defined |
| 34 | + * in the data sources config file. This insures we don't end up with cyclic dependencies and that we don't have to store |
| 35 | + * the configuration data with each client. |
| 36 | + */ |
| 37 | + void initialize() |
| 38 | + { |
| 39 | + log.info "Initializing Domain Name Map for Multi Tenant support" |
| 40 | + loadDomainTenantMap() |
| 41 | + } |
29 | 42 |
|
30 |
| - /** This is the injected context that is used to look up the DomainTenantMap Bean. */ |
31 |
| - ApplicationContext applicationContext |
32 |
| - |
33 |
| - /** |
34 |
| - * This will initialize the data for the application by loading the domain name tenant map data. This data is loaded |
35 |
| - * from tenant 0 which could either be a separate configuration database or the default data source as defined |
36 |
| - * in the data sources config file. This insures we don't end up with cyclic dependencies and that we don't have to store |
37 |
| - * the configuration data with each client. |
38 |
| - */ |
39 |
| - void initialize() { |
40 |
| - log.info "Initializing Domain Name Map for Multi Tenant support" |
41 |
| - loadDomainTenantMap() |
| 43 | + /** |
| 44 | + * This will load the hosts data which contains the map between domain and tenant. It will also load the database |
| 45 | + * objects in to another cache that can be accessed to retrieve additional data such as name etc. |
| 46 | + */ |
| 47 | + void loadDomainTenantMap() |
| 48 | + { |
| 49 | + hosts.clear() |
| 50 | + Cache tenantDataCache = CacheManager.getInstance()?.getCache(TENANT_DATA_CACHE_NAME) |
| 51 | + if (tenantDataCache == null) |
| 52 | + { |
| 53 | + // This insures the cache has no limit and items are never removed from the cache. |
| 54 | + CacheManager.getInstance().addCache(new Cache(TENANT_DATA_CACHE_NAME, 1000, true, true, 120, 120)) |
| 55 | + tenantDataCache = CacheManager.getInstance()?.getCache(TENANT_DATA_CACHE_NAME) |
42 | 56 | }
|
43 |
| - |
44 |
| - /** |
45 |
| - * This will load the hosts data which contains the map between domain and tenant. It will also load the database |
46 |
| - * objects in to another cache that can be accessed to retrieve additional data such as name etc. |
47 |
| - */ |
48 |
| - void loadDomainTenantMap() { |
49 |
| - |
50 |
| - hosts.clear() |
51 |
| - Cache tenantDataCache = CacheManager.getInstance()?.getCache(TENANT_DATA_CACHE_NAME) |
52 |
| - if (tenantDataCache == null) { |
53 |
| - // This insures the cache has no limit and items are never removed from the cache. |
54 |
| - CacheManager.getInstance().addCache(new Cache(TENANT_DATA_CACHE_NAME, 1000, true, true, 120, 120)) |
55 |
| - tenantDataCache = CacheManager.getInstance()?.getCache(TENANT_DATA_CACHE_NAME) |
56 |
| - } else { |
57 |
| - tenantDataCache.removeAll() |
58 |
| - } |
59 |
| - |
60 |
| - // See if there is a custom data source bean name |
61 |
| - def domainTenantBeanName = ConfigurationHolder.config.tenant.domainTenantBeanName |
62 |
| - if (domainTenantBeanName == null || domainTenantBeanName?.size() == 0) { |
63 |
| - domainTenantBeanName = "tenant.DomainTenantMap" |
64 |
| - } |
65 |
| - |
66 |
| - // Load the tenant information for the domain to tenant id from the database. |
67 |
| - def list = applicationContext.getBean(domainTenantBeanName).list() |
68 |
| - log.info "Loading " + list?.size() + " Domain Name to Mapped Tenant Id entries from the database object " + domainTenantBeanName |
69 |
| - list.each { map -> |
70 |
| - if (log.isDebugEnabled()) { |
71 |
| - log.debug "Domain->Tenant: ${map.domainName}->${map.mappedTenantId}" |
72 |
| - } |
73 |
| - hosts.put(map.domainName, map.mappedTenantId) |
74 |
| - tenantDataCache.put(new Element(map.mappedTenantId, map)) |
75 |
| - } |
| 57 | + else |
| 58 | + { |
| 59 | + tenantDataCache.removeAll() |
| 60 | + } |
| 61 | + // See if there is a custom data source bean name |
| 62 | + def domainTenantBeanName = ConfigurationHolder.config.tenant.domainTenantBeanName |
| 63 | + if (domainTenantBeanName == null || domainTenantBeanName?.size() == 0) |
| 64 | + { |
| 65 | + domainTenantBeanName = "tenant.DomainTenantMap" |
| 66 | + } |
| 67 | + // Load the tenant information for the domain to tenant id from the database. |
| 68 | + def list = applicationContext.getBean(domainTenantBeanName).list() |
| 69 | + log.info "Loading " + list?.size() + " Domain Name to Mapped Tenant Id entries from the database object " + domainTenantBeanName |
| 70 | + list.each {map -> |
| 71 | + if (log.isDebugEnabled()) |
| 72 | + { |
| 73 | + log.debug "Domain->Tenant: ${map.domainName}->${map.mappedTenantId}" |
| 74 | + } |
| 75 | + hosts.put(map.domainName, map.mappedTenantId) |
| 76 | + tenantDataCache.put(new Element(map.mappedTenantId, map)) |
76 | 77 | }
|
77 |
| - /** |
78 |
| - * The event broker listens for every time a record is saved, then calls a refresh on the |
79 |
| - * list of hosts whenever the DomainTenantMap object is changed via a save or update. |
80 |
| - */ |
81 |
| - void setEventBroker(GroovyEventBroker inEventBroker) { |
82 |
| - if (inEventBroker != null) { |
83 |
| - inEventBroker.subscribe("hibernate.${HibernateEvent.saveOrUpdate}.DomainTenantMap") { event, broker -> |
84 |
| - log.info "DomainTenantMap was changed via a save or update. Reloading the Domain to Tenant information from the database." |
85 |
| - this.initialize() |
86 |
| - } |
87 |
| - } |
| 78 | + } |
| 79 | + /** |
| 80 | + * The event broker listens for every time a record is saved, then calls a refresh on the |
| 81 | + * list of hosts whenever the DomainTenantMap object is changed via a save or update. |
| 82 | + */ |
| 83 | + void setEventBroker(GroovyEventBroker inEventBroker) |
| 84 | + { |
| 85 | + if (inEventBroker != null) |
| 86 | + { |
| 87 | + inEventBroker.subscribe("hibernate.${HibernateEvent.saveOrUpdate}.DomainTenantMap") { |
| 88 | + event, broker -> |
| 89 | + log.info "DomainTenantMap was changed via a save or update. Reloading the Domain to Tenant information from the database." |
| 90 | + this.initialize() |
| 91 | + } |
88 | 92 | }
|
| 93 | + } |
89 | 94 | }
|
0 commit comments