net.java.truecommons.key.spec.prompting.SharedKeyManager Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of truecommons-key-spec Show documentation
Show all versions of truecommons-key-spec Show documentation
Specifies a pluggable API for key management.
The newest version!
/*
* Copyright (C) 2005-2015 Schlichtherle IT Services.
* All rights reserved. Use is subject to license terms.
*/
package net.java.truecommons.key.spec.prompting;
import net.java.truecommons.shed.Option;
import net.java.truecommons.shed.UniqueObject;
import javax.annotation.concurrent.ThreadSafe;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;
import static net.java.truecommons.shed.Option.apply;
/**
* @param the type of the prompting keys.
* @since TrueCommons 2.2
* @author Christian Schlichtherle
*/
@SuppressWarnings("LoopStatementThatDoesntLoop")
@ThreadSafe
final class SharedKeyManager>
extends UniqueObject {
private final Map> providers = new HashMap<>();
private Option> get(URI uri) {
return apply(providers.get(uri));
}
private Option> put(URI uri, SharedKeyProvider p) {
return apply(providers.put(uri, p));
}
private Option> remove(URI uri) {
return apply(providers.remove(uri));
}
synchronized SharedKeyProvider provider(final URI uri) {
for (final SharedKeyProvider p : get(uri))
return p;
final SharedKeyProvider p = new SharedKeyProvider();
put(uri, p);
p.link();
return p;
}
synchronized void release(final URI uri) {
for (final SharedKeyProvider p : get(uri))
p.release();
}
synchronized void link(final URI originUri, final URI targetUri) {
for (final SharedKeyProvider originProvider : get(originUri)) {
for (final SharedKeyProvider targetProvider : put(targetUri, originProvider)) {
if (targetProvider == originProvider)
return;
targetProvider.unlink();
}
originProvider.link();
}
}
synchronized void unlink(final URI uri) {
for (final SharedKeyProvider p : remove(uri))
p.unlink();
}
}