summaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit/src/org/eclipse/jgit/lib/Signers.java
blob: 7771b07841c05e9e00c8b7d79e072fd90801accf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
 * Copyright (C) 2024 Thomas Wolf <twolf@apache.org> and others
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Distribution License v. 1.0 which is available at
 * https://www.eclipse.org/org/documents/edl-v10.php.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */
package org.eclipse.jgit.lib;

import java.text.MessageFormat;
import java.util.EnumMap;
import java.util.Map;
import java.util.ServiceConfigurationError;
import java.util.ServiceLoader;
import java.util.concurrent.ConcurrentHashMap;

import org.eclipse.jgit.annotations.NonNull;
import org.eclipse.jgit.internal.JGitText;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Manages the available signers.
 *
 * @since 7.0
 */
public final class Signers {

	private static final Logger LOG = LoggerFactory.getLogger(Signers.class);

	private static final Map<GpgConfig.GpgFormat, SignerFactory> SIGNER_FACTORIES = loadSigners();

	private static final Map<GpgConfig.GpgFormat, Signer> SIGNERS = new ConcurrentHashMap<>();

	private static Map<GpgConfig.GpgFormat, SignerFactory> loadSigners() {
		Map<GpgConfig.GpgFormat, SignerFactory> result = new EnumMap<>(
				GpgConfig.GpgFormat.class);
		try {
			for (SignerFactory factory : ServiceLoader
					.load(SignerFactory.class)) {
				GpgConfig.GpgFormat format = factory.getType();
				SignerFactory existing = result.get(format);
				if (existing != null) {
					LOG.warn("{}", //$NON-NLS-1$
							MessageFormat.format(
									JGitText.get().signatureServiceConflict,
									"SignerFactory", format, //$NON-NLS-1$
									existing.getClass().getCanonicalName(),
									factory.getClass().getCanonicalName()));
				} else {
					result.put(format, factory);
				}
			}
		} catch (ServiceConfigurationError e) {
			LOG.error(e.getMessage(), e);
		}
		return result;
	}

	private Signers() {
		// No instantiation
	}

	/**
	 * Retrieves a {@link Signer} that can produce signatures of the given type
	 * {@code format}.
	 *
	 * @param format
	 *            {@link GpgConfig.GpgFormat} the signer must support
	 * @return a {@link Signer}, or {@code null} if none is available
	 */
	public static Signer get(@NonNull GpgConfig.GpgFormat format) {
		return SIGNERS.computeIfAbsent(format, f -> {
			SignerFactory factory = SIGNER_FACTORIES.get(format);
			if (factory == null) {
				return null;
			}
			return factory.create();
		});
	}

	/**
	 * Sets a specific signer to use for a specific signature type.
	 *
	 * @param format
	 *            signature type to set the {@code signer} for
	 * @param signer
	 *            the {@link Signer} to use for signatures of type
	 *            {@code format}; if {@code null}, a default implementation, if
	 *            available, may be used.
	 */
	public static void set(@NonNull GpgConfig.GpgFormat format, Signer signer) {
		if (signer == null) {
			SIGNERS.remove(format);
		} else {
			SIGNERS.put(format, signer);
		}
	}
}