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.maven.index.examples.indexing;
20  
21  import javax.annotation.PreDestroy;
22  import javax.inject.Singleton;
23  
24  import java.io.IOException;
25  import java.util.LinkedHashMap;
26  import java.util.Map;
27  
28  import org.slf4j.Logger;
29  import org.slf4j.LoggerFactory;
30  import org.springframework.stereotype.Component;
31  
32  /**
33   * This class represents a mapping between repositoryId-s and their respective indexes.
34   *
35   * @author mtodorov
36   */
37  @Singleton
38  @Component
39  public class RepositoryIndexManager {
40  
41      private static final Logger LOGGER = LoggerFactory.getLogger(RepositoryIndexManager.class);
42  
43      /**
44       * K: repositoryId
45       * V: index
46       */
47      private Map<String, RepositoryIndexer> indexes = new LinkedHashMap<>();
48  
49      public RepositoryIndexManager() {
50          // no op
51      }
52  
53      /**
54       * A convenience method for closing the indexes. This method will be called
55       * by the Spring container and it shouldn't be invoked directly.
56       * <p/>
57       * This method is of particular importance, as you should close the indexes properly.
58       */
59      @PreDestroy
60      private void close() {
61          for (String repositoryId : indexes.keySet()) {
62              try {
63                  final RepositoryIndexer repositoryIndexer = indexes.get(repositoryId);
64  
65                  LOGGER.debug("Closing indexer for " + repositoryIndexer.getRepositoryId() + "...");
66  
67                  repositoryIndexer.close();
68  
69                  LOGGER.debug("Closed indexer for " + repositoryIndexer.getRepositoryId() + ".");
70              } catch (IOException e) {
71                  LOGGER.error(e.getMessage(), e);
72              }
73          }
74      }
75  
76      public Map<String, RepositoryIndexer> getIndexes() {
77          return indexes;
78      }
79  
80      public void setIndexes(Map<String, RepositoryIndexer> indexes) {
81          this.indexes = indexes;
82      }
83  
84      public RepositoryIndexer getRepositoryIndex(String repositoryId) {
85          return indexes.get(repositoryId);
86      }
87  
88      public RepositoryIndexer addRepositoryIndex(String repositoryId, RepositoryIndexer value) {
89          return indexes.put(repositoryId, value);
90      }
91  
92      public RepositoryIndexer removeRepositoryIndex(String repositoryId) {
93          return indexes.remove(repositoryId);
94      }
95  }