View Javadoc
1   /*
2    * Copyright 2016 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  
17  package org.openehealth.ipf.commons.ihe.fhir;
18  
19  import ca.uhn.fhir.rest.server.RestfulServer;
20  import org.slf4j.Logger;
21  import org.slf4j.LoggerFactory;
22  
23  import java.util.HashSet;
24  import java.util.Map;
25  import java.util.Set;
26  import java.util.concurrent.ConcurrentHashMap;
27  
28  /**
29   * Default implementation of {@link FhirRegistry}
30   *
31   * @author Christian Ohr
32   * @since 3.2
33   */
34  public class DefaultFhirRegistry implements FhirRegistry {
35  
36      private static final Logger LOG = LoggerFactory.getLogger(DefaultFhirRegistry.class);
37  
38      private static Map<String, FhirRegistry> registries = new ConcurrentHashMap<>();
39  
40      private final Set<Object> resourceProviders;
41      private final Set<RestfulServer> servlets;
42  
43      private DefaultFhirRegistry() {
44          resourceProviders = new HashSet<>();
45          servlets = new HashSet<>();
46      }
47  
48      /**
49       * Lookup or create a new FHIR registry if none exists with the given (servlet) name
50       */
51      public static FhirRegistry getFhirRegistry(String name) {
52          return registries.computeIfAbsent(name, s -> new DefaultFhirRegistry());
53      }
54  
55      /**
56       * Removes the FHIR registry with the given (servlet) name
57       */
58      public static FhirRegistry removeFhirRegistry(String name) {
59          return registries.remove(name);
60      }
61  
62      @Override
63      public void register(Object resourceProvider) {
64          resourceProviders.add(resourceProvider);
65          for (RestfulServer servlet : servlets) {
66              servlet.registerProvider(resourceProvider);
67          }
68      }
69  
70      @Override
71      public void unregister(Object resourceProvider) throws Exception {
72          resourceProviders.remove(resourceProvider);
73          for (RestfulServer provider : servlets) {
74              provider.unregisterProvider(resourceProvider);
75          }
76      }
77  
78      @Override
79      public void register(RestfulServer servlet) {
80          LOG.debug("Registering FHIR servlet with name {}. Providers registered so far: {}",
81                  servlet.getServletName(), resourceProviders.size());
82          servlets.add(servlet);
83          servlet.registerProviders(resourceProviders);
84      }
85  
86      @Override
87      public void unregister(RestfulServer servlet) throws Exception {
88          LOG.debug("Unregistering FHIR Servlet with name {} and {} connected providers",
89                  servlet.getServletName(), resourceProviders.size());
90          servlets.remove(servlet);
91          servlet.unregisterProviders(resourceProviders);
92      }
93  
94      @Override
95      public boolean hasServlet(String servletName) {
96          return servlets.stream().anyMatch(p -> p.getServletName().equals(servletName));
97      }
98  }