View Javadoc
1   /*
2    * Copyright (C) 2021, Thomas Wolf <thomas.wolf@paranor.ch> and others
3    *
4    * This program and the accompanying materials are made available under the
5    * terms of the Eclipse Distribution License v. 1.0 which is available at
6    * https://www.eclipse.org/org/documents/edl-v10.php.
7    *
8    * SPDX-License-Identifier: BSD-3-Clause
9    */
10  package org.eclipse.jgit.lib;
11  
12  import java.util.Iterator;
13  import java.util.ServiceConfigurationError;
14  import java.util.ServiceLoader;
15  
16  import org.slf4j.Logger;
17  import org.slf4j.LoggerFactory;
18  
19  /**
20   * A {@code GpgSignatureVerifierFactory} creates {@link GpgSignatureVerifier} instances.
21   *
22   * @since 5.11
23   */
24  public abstract class GpgSignatureVerifierFactory {
25  
26  	private static final Logger LOG = LoggerFactory
27  			.getLogger(GpgSignatureVerifierFactory.class);
28  
29  	private static volatile GpgSignatureVerifierFactory defaultFactory = loadDefault();
30  
31  	private static GpgSignatureVerifierFactory loadDefault() {
32  		try {
33  			ServiceLoader<GpgSignatureVerifierFactory> loader = ServiceLoader
34  					.load(GpgSignatureVerifierFactory.class);
35  			Iterator<GpgSignatureVerifierFactory> iter = loader.iterator();
36  			if (iter.hasNext()) {
37  				return iter.next();
38  			}
39  		} catch (ServiceConfigurationError e) {
40  			LOG.error(e.getMessage(), e);
41  		}
42  		return null;
43  	}
44  
45  	/**
46  	 * Retrieves the default factory.
47  	 *
48  	 * @return the default factory or {@code null} if none set
49  	 */
50  	public static GpgSignatureVerifierFactory getDefault() {
51  		return defaultFactory;
52  	}
53  
54  	/**
55  	 * Sets the default factory.
56  	 *
57  	 * @param factory
58  	 *            the new default factory
59  	 */
60  	public static void setDefault(GpgSignatureVerifierFactory factory) {
61  		defaultFactory = factory;
62  	}
63  
64  	/**
65  	 * Creates a new {@link GpgSignatureVerifier}.
66  	 *
67  	 * @return the new {@link GpgSignatureVerifier}
68  	 */
69  	public abstract GpgSignatureVerifier getVerifier();
70  
71  }