1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
36
37
38 public final class MatcherBuilderTracker {
39
40
41 private static MatcherBuilderTracker instance;
42
43
44 private final Map<String, Class<? extends AbstractBuilder>> matcherBuilders;
45
46
47
48
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
60
61
62
63
64 public static void addBuilder(final String className, final String name) {
65 instance().addBuilderImpl(className, name);
66 }
67
68
69
70
71
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
96
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 }