View Javadoc
1   /* 
2    * Licensed under the Apache License, Version 2.0 (the "License");
3    * you may not use this file except in compliance with the License.
4    * You may obtain a copy of the License at
5    *
6    * http://www.apache.org/licenses/LICENSE-2.0
7    *
8    * Unless required by applicable law or agreed to in writing, software
9    * distributed under the License is distributed on an "AS IS" BASIS,
10   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11   * See the License for the specific language governing permissions and
12   * limitations under the License.
13   *
14   */
15  package org.esigate.impl;
16  
17  import java.util.LinkedHashMap;
18  import java.util.List;
19  import java.util.Map;
20  import java.util.SortedSet;
21  import java.util.TreeSet;
22  
23  import org.esigate.Driver;
24  
25  /**
26   * This class is used by DriverFactory to hold both the user instances and a list of the global mappings.
27   * 
28   * <p>
29   * This object is replaced atomically in the DriverFactory allowing a synchronization-less update of the configuration.
30   * 
31   * @author Nicolas Richeton
32   */
33  public class IndexedInstances {
34      private final Map<String, Driver> instances;
35      private final Map<UriMapping, String> uriMappings;
36  
37      public IndexedInstances(Map<String, Driver> instances) {
38          this.instances = instances;
39          this.uriMappings = buildUriMappings();
40      }
41  
42      private Map<UriMapping, String> buildUriMappings() {
43          Map<UriMapping, String> result = new LinkedHashMap<>();
44  
45          Map<UriMapping, String> unsortedResult = new LinkedHashMap<>();
46  
47          if (this.instances != null) {
48              for (String instanceId : this.instances.keySet()) {
49                  List<UriMapping> driverMappings = this.instances.get(instanceId).getConfiguration().getUriMappings();
50  
51                  for (UriMapping mapping : driverMappings) {
52                      unsortedResult.put(mapping, instanceId);
53                  }
54              }
55          }
56  
57          // Order according to weight.
58          SortedSet<UriMapping> keys = new TreeSet<>(new UriMappingComparator());
59          keys.addAll(unsortedResult.keySet());
60          for (UriMapping key : keys) {
61              result.put(key, unsortedResult.get(key));
62          }
63  
64          return result;
65      }
66  
67      /**
68       * A map containing all instances with their ids.
69       * 
70       * @return Map content (instanceName, instance)
71       */
72      public Map<String, Driver> getInstances() {
73          return this.instances;
74      }
75  
76      /**
77       * A map containing all URI mappings and the associated driver instance name.
78       * 
79       * @return Map content (mapping, instanceName)
80       */
81      public Map<UriMapping, String> getUrimappings() {
82          return this.uriMappings;
83      }
84  
85  }