/*-
 * ~~~~~~licensing~~~~~~
 * keystore-management
 * ==========
 * Copyright (C) 2020 - 2025 EmDev LLC
 * ==========
 * You may not use this file except in accordance with the License Terms of the Copyright
 * Holder located at: https://entaxy.ru/eula . All copyrights, all intellectual property
 * rights to the Software and any copies are the property of the Copyright Holder. Unless
 * it is explicitly allowed the Copyright Holder, the User is prohibited from using the
 * Software for commercial purposes to provide services to third parties.
 * 
 * The Copyright Holder hereby declares that the Software is provided on an "AS IS".
 * Under no circumstances does the Copyright Holder guarantee or promise that the
 * Software provided by him will be suitable or not suitable for the specific purposes
 * of the User, that the Software will meet all commercial and personal subjective
 * expectations of the User, that the Software will work properly, without technical
 * errors, quickly and uninterruptedly.
 * 
 * Under no circumstances shall the Copyright Holder or its Affiliates is not liable
 * to the User for any direct or indirect losses of the User, his expenses or actual
 * damage, including, downtime; loss of bussines; lost profit; lost earnings; loss
 * or damage to data, property, etc.
 * ~~~~~~/licensing~~~~~~
 */
package ru.entaxy.security.keystore.management.impl;

import javax.management.NotCompliantMBeanException;
import javax.naming.OperationNotSupportedException;

import ru.entaxy.esb.platform.base.management.core.Qualifier;
import ru.entaxy.esb.platform.base.management.core.api.EntaxyRuntimeTyped;
import ru.entaxy.platform.core.management.object.ObjectMBean;
import ru.entaxy.platform.core.management.object.factory.AbstractObjectMBean;
import ru.entaxy.platform.objects.runtime.EntaxyRuntimeObject;
import ru.entaxy.security.keystore.management.KeystoreMBean;
import ru.entaxy.security.keystore.management.KeystoresMBean;
import ru.entaxy.security.keystore.runtime.KeystoreManager;

import java.util.List;

@EntaxyRuntimeTyped(name = KeystoresMBean.KEYSTORE_RUNTIME_TYPE)
@ObjectMBean(subQualifierName = KeystoreMBean.SUBQUALIFIER_KEY)
public class KeystoreMBeanImpl<T extends KeystoreMBean> extends AbstractObjectMBean<T>
        implements KeystoreMBean {

    protected KeystoreManager keystoreManager;

    public KeystoreMBeanImpl(EntaxyRuntimeObject entaxyRuntimeObject)
            throws NotCompliantMBeanException {
        super((Class<T>) KeystoreMBean.class, entaxyRuntimeObject);
    }

    public KeystoreMBeanImpl(EntaxyRuntimeObject entaxyRuntimeObject, KeystoreManager keystoreManager)
            throws NotCompliantMBeanException {
        super((Class<T>) KeystoreMBean.class, entaxyRuntimeObject);
        this.keystoreManager = keystoreManager;
    }

    protected KeystoreMBeanImpl(Class<T> mbeanInterface, EntaxyRuntimeObject entaxyRuntimeObject)
            throws NotCompliantMBeanException {
        super(mbeanInterface, entaxyRuntimeObject);
    }

    protected KeystoreMBeanImpl(Class<T> mbeanInterface, EntaxyRuntimeObject entaxyRuntimeObject,
                                KeystoreManager keystoreManager)
            throws NotCompliantMBeanException {
        super(mbeanInterface, entaxyRuntimeObject);
        this.keystoreManager = keystoreManager;
    }

    @Override
    public Qualifier getJMXQualifier() {
        return KeystoresMBean.QUALIFIER.qualifier(KeystoreMBean.SUBQUALIFIER_KEY, getObjectId());
    }

    @Override
    public String getJMXSubQualifier() {
        return KeystoresMBean.SUBQUALIFIER.concat(",").concat(KeystoreMBean.SUBQUALIFIER_KEY).concat("=")
                .concat(getObjectId());
    }

    @Override
    public List<String> listAliases() throws Exception {
        return keystoreManager.getKeystoreAliases(getObjectId());
    }

    @Override
    public void addKey(String alias, String keyContent) throws Exception {
        throw new OperationNotSupportedException("Add key operation is not supported for jks keystore");
    }

    @Override
    public void removeKey(String alias) throws Exception {
        throw new OperationNotSupportedException("Remove key operation is not supported for jks keystore");
    }
}
