De-couple ServiceLoader calls from class loading
Use the holder pattern to de-couple the loading of super classes from the ServiceLoader calls to set up global instances. This prevents potential lock inversions. Bug: 579550 Change-Id: Ie8284e4d6d680ddd4cc6a486bbefe8ed00266240 Signed-off-by: Thomas Wolf <thomas.wolf@paranor.ch>
This commit is contained in:
parent
7e06e51750
commit
7f9cd7d2ba
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright (C) 2021, Thomas Wolf <thomas.wolf@paranor.ch> and others
|
||||
* Copyright (C) 2021, 2022 Thomas Wolf <thomas.wolf@paranor.ch> 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
|
||||
|
@ -26,6 +26,8 @@ public abstract class GpgSignatureVerifierFactory {
|
|||
private static final Logger LOG = LoggerFactory
|
||||
.getLogger(GpgSignatureVerifierFactory.class);
|
||||
|
||||
private static class DefaultFactory {
|
||||
|
||||
private static volatile GpgSignatureVerifierFactory defaultFactory = loadDefault();
|
||||
|
||||
private static GpgSignatureVerifierFactory loadDefault() {
|
||||
|
@ -42,11 +44,10 @@ private static GpgSignatureVerifierFactory loadDefault() {
|
|||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the default factory.
|
||||
*
|
||||
* @return the default factory or {@code null} if none set
|
||||
*/
|
||||
private DefaultFactory() {
|
||||
// No instantiation
|
||||
}
|
||||
|
||||
public static GpgSignatureVerifierFactory getDefault() {
|
||||
return defaultFactory;
|
||||
}
|
||||
|
@ -60,6 +61,26 @@ public static GpgSignatureVerifierFactory getDefault() {
|
|||
public static void setDefault(GpgSignatureVerifierFactory factory) {
|
||||
defaultFactory = factory;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieves the default factory.
|
||||
*
|
||||
* @return the default factory or {@code null} if none set
|
||||
*/
|
||||
public static GpgSignatureVerifierFactory getDefault() {
|
||||
return DefaultFactory.getDefault();
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the default factory.
|
||||
*
|
||||
* @param factory
|
||||
* the new default factory
|
||||
*/
|
||||
public static void setDefault(GpgSignatureVerifierFactory factory) {
|
||||
DefaultFactory.setDefault(factory);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a new {@link GpgSignatureVerifier}.
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
/*
|
||||
* Copyright (C) 2008, Robin Rosenberg <robin.rosenberg@dewire.com>
|
||||
* Copyright (C) 2008, 2020 Shawn O. Pearce <spearce@spearce.org> and others
|
||||
* Copyright (C) 2008, 2022 Shawn O. Pearce <spearce@spearce.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
|
||||
|
@ -36,17 +36,37 @@
|
|||
*/
|
||||
public abstract class SshSessionFactory {
|
||||
|
||||
private static class DefaultFactory {
|
||||
|
||||
private static volatile SshSessionFactory INSTANCE = loadSshSessionFactory();
|
||||
|
||||
private static SshSessionFactory loadSshSessionFactory() {
|
||||
ServiceLoader<SshSessionFactory> loader = ServiceLoader.load(SshSessionFactory.class);
|
||||
ServiceLoader<SshSessionFactory> loader = ServiceLoader
|
||||
.load(SshSessionFactory.class);
|
||||
Iterator<SshSessionFactory> iter = loader.iterator();
|
||||
if(iter.hasNext()) {
|
||||
if (iter.hasNext()) {
|
||||
return iter.next();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private DefaultFactory() {
|
||||
// No instantiation
|
||||
}
|
||||
|
||||
public static SshSessionFactory getInstance() {
|
||||
return INSTANCE;
|
||||
}
|
||||
|
||||
public static void setInstance(SshSessionFactory newFactory) {
|
||||
if (newFactory != null) {
|
||||
INSTANCE = newFactory;
|
||||
} else {
|
||||
INSTANCE = loadSshSessionFactory();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the currently configured JVM-wide factory.
|
||||
* <p>
|
||||
|
@ -57,7 +77,7 @@ private static SshSessionFactory loadSshSessionFactory() {
|
|||
* @return factory the current factory for this JVM.
|
||||
*/
|
||||
public static SshSessionFactory getInstance() {
|
||||
return INSTANCE;
|
||||
return DefaultFactory.getInstance();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -68,11 +88,7 @@ public static SshSessionFactory getInstance() {
|
|||
* {@code null} the default factory will be restored.
|
||||
*/
|
||||
public static void setInstance(SshSessionFactory newFactory) {
|
||||
if (newFactory != null) {
|
||||
INSTANCE = newFactory;
|
||||
} else {
|
||||
INSTANCE = loadSshSessionFactory();
|
||||
}
|
||||
DefaultFactory.setInstance(newFactory);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in New Issue