View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.logging.log4j.core.config.plugins.util;
19  
20  import java.io.Serializable;
21  import java.util.Map;
22  import java.util.Set;
23  import java.util.concurrent.ConcurrentHashMap;
24  import java.util.concurrent.ConcurrentMap;
25  
26  /**
27   * Registry for PluginType maps partitioned by category names.
28   *
29   * @param <T> plugin information object such as PluginType or PluginEntry.
30   */
31  public class PluginRegistry<T extends Serializable> {
32      private final ConcurrentMap<String, ConcurrentMap<String, T>> categories =
33          new ConcurrentHashMap<String, ConcurrentMap<String, T>>();
34  
35      /**
36       * Gets or creates a plugin category if not already available. Category names are case-insensitive. The
37       * ConcurrentMap that is returned should also be treated as a case-insensitive plugin map where names should be
38       * converted to lowercase before retrieval or storage.
39       *
40       * @param category the plugin category to look up or create.
41       * @return the plugin map for the given category name.
42       * @throws IllegalArgumentException if the argument is {@code null}
43       */
44      public ConcurrentMap<String, T> getCategory(final String category) {
45          if (category == null) {
46              throw new IllegalArgumentException("Category name cannot be null.");
47          }
48          final String key = category.toLowerCase();
49          categories.putIfAbsent(key, new ConcurrentHashMap<String, T>());
50          return categories.get(key);
51      }
52  
53      /**
54       * Returns the number of plugin categories currently available. This is primarily useful for serialization.
55       *
56       * @return the number of plugin categories.
57       */
58      public int getCategoryCount() {
59          return categories.size();
60      }
61  
62      /**
63       * Indicates whether or not any plugin categories have been registered. Note that this does not necessarily
64       * indicate if any plugins are registered as categories may be empty.
65       *
66       * @return {@code true} if there any categories registered.
67       */
68      public boolean isEmpty() {
69          return categories.isEmpty();
70      }
71  
72      /**
73       * Resets the registry to an empty state.
74       */
75      public void clear() {
76          categories.clear();
77      }
78  
79      /**
80       * Indicates whether or not the given category name is registered and has plugins in that category.
81       *
82       * @param category the plugin category name to check.
83       * @return {@code true} if the category exists and has plugins registered.
84       * @throws IllegalArgumentException if the argument is {@code null}
85       */
86      public boolean hasCategory(final String category) {
87          if (category == null) {
88              throw new IllegalArgumentException("Category name cannot be null.");
89          }
90          final String key = category.toLowerCase();
91          return categories.containsKey(key) && !categories.get(key).isEmpty();
92      }
93  
94      /**
95       * Gets an entry set for iterating over the registered plugin categories.
96       *
97       * @return an entry set of the registered plugin categories.
98       */
99      public Set<Map.Entry<String, ConcurrentMap<String, T>>> getCategories() {
100         return categories.entrySet();
101     }
102 }