View Javadoc
1   package org.apache.maven.index.examples.indexing;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import org.slf4j.Logger;
23  import org.slf4j.LoggerFactory;
24  import org.springframework.stereotype.Component;
25  
26  import javax.annotation.PreDestroy;
27  import javax.inject.Singleton;
28  import java.io.IOException;
29  import java.util.LinkedHashMap;
30  import java.util.Map;
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  
42      private static final Logger LOGGER = LoggerFactory.getLogger( RepositoryIndexManager.class );
43  
44      /**
45       * K: repositoryId
46       * V: index
47       */
48      private Map<String, RepositoryIndexer> indexes = new LinkedHashMap<>();
49  
50  
51      public RepositoryIndexManager()
52      {
53          // no op
54      }
55  
56      /**
57       * A convenience method for closing the indexes. This method will be called
58       * by the Spring container and it shouldn't be invoked directly.
59       * <p/>
60       * This method is of particular importance, as you should close the indexes properly.
61       */
62      @PreDestroy
63      private void close()
64      {
65          for ( String repositoryId : indexes.keySet() )
66          {
67              try
68              {
69                  final RepositoryIndexer repositoryIndexer = indexes.get( repositoryId );
70  
71                  LOGGER.debug( "Closing indexer for " + repositoryIndexer.getRepositoryId() + "..." );
72  
73                  repositoryIndexer.close();
74  
75                  LOGGER.debug( "Closed indexer for " + repositoryIndexer.getRepositoryId() + "." );
76              }
77              catch ( IOException e )
78              {
79                  LOGGER.error( e.getMessage(), e );
80              }
81          }
82      }
83  
84      public Map<String, RepositoryIndexer> getIndexes()
85      {
86          return indexes;
87      }
88  
89      public void setIndexes( Map<String, RepositoryIndexer> indexes )
90      {
91          this.indexes = indexes;
92      }
93  
94      public RepositoryIndexer getRepositoryIndex( String repositoryId )
95      {
96          return indexes.get( repositoryId );
97      }
98  
99      public RepositoryIndexer addRepositoryIndex( String repositoryId, RepositoryIndexer value )
100     {
101         return indexes.put( repositoryId, value );
102     }
103 
104     public RepositoryIndexer removeRepositoryIndex( String repositoryId )
105     {
106         return indexes.remove( repositoryId );
107     }
108 
109 }