View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.syncope.core.persistence.jpa;
20  
21  import java.util.Comparator;
22  import org.apache.syncope.common.keymaster.client.api.DomainWatcher;
23  import org.apache.syncope.common.keymaster.client.api.model.Domain;
24  import org.apache.syncope.core.persistence.api.DomainHolder;
25  import org.apache.syncope.core.persistence.api.DomainRegistry;
26  import org.apache.syncope.core.persistence.api.SyncopeCoreLoader;
27  import org.apache.syncope.core.spring.ApplicationContextProvider;
28  import org.slf4j.Logger;
29  import org.slf4j.LoggerFactory;
30  import org.springframework.aop.support.AopUtils;
31  import org.springframework.context.ConfigurableApplicationContext;
32  
33  public class RuntimeDomainLoader implements DomainWatcher {
34  
35      protected static final Logger LOG = LoggerFactory.getLogger(RuntimeDomainLoader.class);
36  
37      protected final DomainHolder domainHolder;
38  
39      protected final DomainRegistry domainRegistry;
40  
41      public RuntimeDomainLoader(
42              final DomainHolder domainHolder,
43              final DomainRegistry domainRegistry,
44              final ConfigurableApplicationContext ctx) {
45  
46          this.domainHolder = domainHolder;
47          this.domainRegistry = domainRegistry;
48  
49          // only needed by ZookeeperDomainOps' early init on afterPropertiesSet
50          if (ApplicationContextProvider.getApplicationContext() == null) {
51              ApplicationContextProvider.setApplicationContext(ctx);
52          }
53      }
54  
55      @Override
56      public void added(final Domain domain) {
57          if (domainHolder.getDomains().containsKey(domain.getKey())) {
58              LOG.debug("Domain {} already inited, skipping", domain.getKey());
59          } else {
60              LOG.info("Domain {} registration", domain.getKey());
61  
62              domainRegistry.register(domain);
63  
64              ApplicationContextProvider.getBeanFactory().getBeansOfType(SyncopeCoreLoader.class).values().
65                      stream().sorted(Comparator.comparing(SyncopeCoreLoader::getOrder)).
66                      forEachOrdered(loader -> {
67                          String loaderName = AopUtils.getTargetClass(loader).getName();
68  
69                          LOG.debug("[{}] Starting on domain '{}'", loaderName, domain);
70                          loader.load(domain.getKey(), domainHolder.getDomains().get(domain.getKey()));
71                          LOG.debug("[{}] Completed on domain '{}'", loaderName, domain);
72                      });
73  
74              LOG.info("Domain {} successfully deployed", domain.getKey());
75          }
76      }
77  
78      @Override
79      public void removed(final String domain) {
80          if (domainHolder.getDomains().containsKey(domain)) {
81              LOG.info("Domain {} unregistration", domain);
82  
83              ApplicationContextProvider.getBeanFactory().getBeansOfType(SyncopeCoreLoader.class).values().
84                      stream().sorted(Comparator.comparing(SyncopeCoreLoader::getOrder).reversed()).
85                      forEachOrdered(loader -> {
86                          String loaderName = AopUtils.getTargetClass(loader).getName();
87  
88                          LOG.debug("[{}] Starting dispose on domain '{}'", loaderName, domain);
89                          loader.unload(domain);
90                          LOG.debug("[{}] Dispose completed on domain '{}'", loaderName, domain);
91                      });
92  
93              domainRegistry.unregister(domain);
94  
95              domainHolder.getDomains().remove(domain);
96  
97              LOG.info("Domain {} successfully undeployed", domain);
98          } else {
99              LOG.debug("Domain {} not inited, skipping", domain);
100         }
101     }
102 }