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.builders;
20
21 import java.util.Objects;
22
23 import org.apache.commons.lang3.StringUtils;
24 import org.apache.rat.ConfigurationException;
25 import org.apache.rat.analysis.matchers.SPDXMatcherFactory;
26 import org.apache.rat.config.parameters.MatcherBuilder;
27
28 /**
29 * A builder for SPDX matchers.
30 * <p>
31 * Uses a {@code ThreadLocal} factory so that each thread in a parallel Maven
32 * build gets its own {@link SPDXMatcherFactory} instance with isolated match
33 * state. This prevents two threads scanning different documents from
34 * cross-contaminating each other's SPDX identifier results.
35 * </p>
36 */
37 @MatcherBuilder(SPDXMatcherFactory.Match.class)
38 public class SpdxBuilder extends AbstractBuilder {
39
40 /**
41 * Per-thread SPDXMatcherFactory. Each thread gets its own factory with
42 * its own matcher map and per-document match state ({@code lastMatch},
43 * {@code checked}).
44 */
45 private static final ThreadLocal<SPDXMatcherFactory> FACTORY = ThreadLocal.withInitial(SPDXMatcherFactory::newInstance);
46
47 /** The SPDX name */
48 private String name;
49
50 /**
51 * Sets the name for the SPDX matcher. This is the same as the identifier in the SPDX license list.
52 *
53 * @param name The text that follows the colon ':' in the SPDX tag.
54 * @return this builder.
55 * @see <a href="https://spdx.org/licenses/">SPDX license list</a>
56 */
57 public SpdxBuilder setName(final String name) {
58 Objects.requireNonNull(name, "SPDX name must not be null");
59 this.name = name;
60 super.setId("SPDX:" + name);
61 return this;
62 }
63
64 /**
65 * Set the id for the matcher.
66 *
67 * @param id the id to use.
68 * @return this builder.
69 */
70 @Override
71 public AbstractBuilder setId(final String id) {
72 if (StringUtils.isNotBlank(id)) {
73 throw new ConfigurationException("'id' is not supported for SPDX matchers. "
74 + "SPXD matchers always have 'SPDX:<name>' as their id");
75 }
76 return this;
77 }
78
79 @Override
80 public SPDXMatcherFactory.Match build() {
81 return FACTORY.get().create(name);
82 }
83
84 @Override
85 public String toString() {
86 return "SpdxBuilder: " + name;
87 }
88 }