001/*
002 * The contents of this file are subject to the terms of the Common Development and
003 * Distribution License (the License). You may not use this file except in compliance with the
004 * License.
005 *
006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
007 * specific language governing permission and limitations under the License.
008 *
009 * When distributing Covered Software, include this CDDL Header Notice in each file and include
010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
011 * Header, with the fields enclosed by brackets [] replaced by your own identifying
012 * information: "Portions copyright [year] [name of copyright owner]".
013 *
014 * Copyright 2015 ForgeRock AS.
015 */
016
017package org.forgerock.audit.providers;
018
019import java.util.HashMap;
020import java.util.Map;
021
022import org.forgerock.audit.secure.KeyStoreHandler;
023
024/**
025 * Default implementation of {@link KeyStoreHandlerProvider}.
026 * <p>
027 * Multiple KeyStoreHandler instances can be provided, identified by a name.
028 */
029public class DefaultKeyStoreHandlerProvider implements KeyStoreHandlerProvider {
030
031    /** Named secure storage instances availables. */
032    private final Map<String, KeyStoreHandler> keyStoreHandlers;
033
034    /** Creates a initially empty provider. */
035    public DefaultKeyStoreHandlerProvider() {
036        this.keyStoreHandlers = new HashMap<>();
037    }
038
039    /**
040     * Creates a provider with some {@link KeystoreHandler} instances.
041     *
042     * @param handlers
043     *          The storage instances to use in the provider.
044     */
045    public DefaultKeyStoreHandlerProvider(Map<String, KeyStoreHandler> handlers) {
046        this.keyStoreHandlers = handlers;
047    }
048
049    /**
050     * Register a storage with the given name.
051     *
052     * @param name
053     *          Name associated to the {@link KeystoreHandler} instance.
054     * @param handler
055     *          The storage instance.
056     */
057    public void registerKeyStoreHandler(String name, KeyStoreHandler handler) {
058        keyStoreHandlers.put(name, handler);
059    }
060
061    @Override
062    public KeyStoreHandler getKeystoreHandler(String name) {
063        return keyStoreHandlers.get(name);
064    }
065
066}