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.rat.configuration;
20  
21  import java.lang.reflect.InvocationTargetException;
22  import java.util.Collection;
23  import java.util.Collections;
24  import java.util.HashMap;
25  import java.util.Map;
26  import java.util.Objects;
27  
28  import org.apache.commons.lang3.StringUtils;
29  import org.apache.commons.text.WordUtils;
30  import org.apache.rat.ConfigurationException;
31  import org.apache.rat.Defaults;
32  import org.apache.rat.configuration.builders.AbstractBuilder;
33  
34  /**
35   * A class to track the Matcher Builders as they are defined. Matchers may be defined in multiple configuration files.
36   * This class tracks them so that they can be referenced across the configuration files.
37   */
38  public final class MatcherBuilderTracker {
39  
40      /** The instance of the BuildTracker. */
41      private static MatcherBuilderTracker instance;
42  
43      /** Map of matcher name to the class of the Builder */
44      private final Map<String, Class<? extends AbstractBuilder>> matcherBuilders;
45  
46      /**
47       * Gets the instance of the MatcherBuilderTracker.
48       * @return the instance of the MatcherBuilderTracker.
49       */
50      public  static synchronized MatcherBuilderTracker instance() {
51          if (instance == null) {
52              instance = new MatcherBuilderTracker();
53              Defaults.init();
54          }
55          return instance;
56      }
57  
58      /**
59       * Adds a builder to the tracker.
60       * If the {@code name} is null then the builder class name simple is used with the "Builder" suffix removed.
61       * @param className the Class name for the builder.
62       * @param name the short name for the builder.
63       */
64      public static void addBuilder(final String className, final String name) {
65          instance().addBuilderImpl(className, name);
66      }
67  
68      /**
69       * Get the matching builder for the name.
70       * @param name The name of the builder.
71       * @return the builder for that name. (not null)
72       */
73      public static AbstractBuilder getMatcherBuilder(final String name) {
74          Class<? extends AbstractBuilder> clazz = instance().matcherBuilders.get(name);
75          if (clazz == null) {
76              StringBuilder sb = new StringBuilder(System.lineSeparator()).append("Valid builders").append(System.lineSeparator());
77              instance().matcherBuilders.keySet().forEach(x -> sb.append(x).append(System.lineSeparator()));
78              sb.append("ERROR MSG").append(System.lineSeparator());
79              throw new ConfigurationException(sb.append("No matcher builder named ").append(name).toString());
80          }
81          try {
82              return clazz.getConstructor().newInstance();
83          } catch (NoSuchMethodException | SecurityException | InstantiationException | IllegalAccessException
84                  | IllegalArgumentException | InvocationTargetException e) {
85              throw new ConfigurationException(
86                      String.format("Can not instantiate matcher builder named %s (%s)", name, clazz.getName()), e);
87          }
88      }
89  
90      private MatcherBuilderTracker() {
91          matcherBuilders = new HashMap<>();
92      }
93  
94      /**
95       * Gets a collection of classes that are recognized as builders.
96       * @return the collection of builder classes
97       */
98      public Collection<Class<? extends AbstractBuilder>> getClasses() {
99          return Collections.unmodifiableCollection(matcherBuilders.values());
100     }
101 
102     private void addBuilderImpl(final String className, final String name) {
103         Objects.requireNonNull(className, "className may not be null");
104         Class<?> clazz;
105         try {
106             clazz = getClass().getClassLoader().loadClass(className);
107         } catch (ClassNotFoundException e) {
108             throw new ConfigurationException(e);
109         }
110         if (AbstractBuilder.class.isAssignableFrom(clazz)) {
111             @SuppressWarnings("unchecked")
112             Class<? extends AbstractBuilder> candidate = (Class<? extends AbstractBuilder>) clazz;
113             String workingName = name;
114             if (StringUtils.isBlank(workingName)) {
115                 workingName = candidate.getSimpleName();
116                 if (!workingName.endsWith("Builder")) {
117                     throw new ConfigurationException(
118                             "name is required, or " + candidate.getName() + " must end with 'Builder'");
119                 }
120                 workingName = workingName.substring(0, workingName.lastIndexOf("Builder"));
121                 if (StringUtils.isBlank(workingName)) {
122                     throw new ConfigurationException("Last segment of " + candidate.getName()
123                             + " may not be 'Builder', but must end in 'Builder'");
124                 }
125                 workingName = WordUtils.uncapitalize(workingName);
126             }
127             matcherBuilders.put(workingName, candidate);
128         } else {
129             throw new ConfigurationException("Class " + clazz.getName() + " does not extend " + AbstractBuilder.class);
130         }
131     }
132 }