![JAR search and dependency download from the Maven repository](/logo.png)
com.kosprov.jargon2.internal.discovery.Jargon2BackendDiscovery Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of jargon2-api Show documentation
Show all versions of jargon2-api Show documentation
Fluent Java API for Argon2 password hashing
package com.kosprov.jargon2.internal.discovery;
import com.kosprov.jargon2.spi.Jargon2Backend;
import java.util.HashSet;
import java.util.ServiceLoader;
import java.util.Set;
/**
* Singleton object that searches for {@link Jargon2Backend} implementations.
*
*
* It checks the value of the -Dcom.kosprov.jargon2.spi.backend system property for the
* class name of the Jargon2Backend implementation. This implementation must has a no-args constructor.
*
*
*
* Then, it checks for service providers registered with Java's {@link ServiceLoader} mechanism.
* See {@link Jargon2Backend} documentation for more details.
*
*
*
* The previous 2 steps combined should output only 1 backend.
*
*
* @see Jargon2Backend
*/
public enum Jargon2BackendDiscovery {
INSTANCE;
private static final String JARGON2_BACKEND_SYSTEM_PROP_NAME = "com.kosprov.jargon2.spi.backend";
private volatile Jargon2Backend backend;
/**
* Get the single instance of the {@link Jargon2Backend}
*
* @return The discovered backend instance
*/
public Jargon2Backend getJargon2Backend() {
if (backend == null) {
synchronized (this) {
if (backend == null) {
Set backendsFound = new HashSet<>();
String backendClassName = System.getProperty(JARGON2_BACKEND_SYSTEM_PROP_NAME);
if (backendClassName != null && !"".equals(backendClassName.trim())) {
try {
backendsFound.add((Jargon2Backend) Class.forName(backendClassName).newInstance());
} catch (Exception e) {
throw new Jargon2BackendDiscoveryException("Could not create Jargon2Backend instance from class " + backendClassName, e);
}
}
ServiceLoader loader = ServiceLoader.load(Jargon2Backend.class);
for (Jargon2Backend loadedBackend : loader) {
backendsFound.add(loadedBackend);
}
if (backendsFound.size() == 1) {
backend = backendsFound.iterator().next(); // All good
} else if (backendsFound.size() > 1) {
StringBuilder sb = new StringBuilder();
sb.append('[');
for (Jargon2Backend b : backendsFound) {
sb.append(b.getClass().getName()).append(", ");
}
sb.setLength(sb.length() - 2);
sb.append(']');
throw new Jargon2BackendDiscoveryException("Found more than one Jargon2Backends: " + sb.toString());
} else {
throw new Jargon2BackendDiscoveryException("Could not find appropriate jargon2Backend. Use either a service provider or define its class with -D" + JARGON2_BACKEND_SYSTEM_PROP_NAME);
}
}
}
}
return backend;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy