/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  com.l7tech.gateway.common.security.password.SecurePassword
 *  com.l7tech.objectmodel.Entity
 *  com.l7tech.objectmodel.EntityHeader
 *  com.l7tech.objectmodel.FindException
 *  com.l7tech.util.MasterPasswordManager
 *  org.springframework.transaction.annotation.Propagation
 *  org.springframework.transaction.annotation.Transactional
 */
package com.l7tech.server.security.password;

import com.l7tech.gateway.common.security.password.SecurePassword;
import com.l7tech.objectmodel.Entity;
import com.l7tech.objectmodel.EntityHeader;
import com.l7tech.objectmodel.FindException;
import com.l7tech.server.HibernateEntityManager;
import com.l7tech.server.security.password.SecurePasswordManager;
import com.l7tech.server.security.sharedkey.SharedKeyManager;
import com.l7tech.util.MasterPasswordManager;
import java.text.ParseException;
import java.util.concurrent.atomic.AtomicReference;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

@Transactional(propagation=Propagation.SUPPORTS, rollbackFor={Throwable.class})
public class SecurePasswordManagerImpl
extends HibernateEntityManager<SecurePassword, EntityHeader>
implements SecurePasswordManager {
    private final SharedKeyManager m;
    private final AtomicReference<MasterPasswordManager> n = new AtomicReference();

    public SecurePasswordManagerImpl(SharedKeyManager sharedKeyManager) {
        this.m = sharedKeyManager;
    }

    public Class<? extends Entity> getImpClass() {
        return SecurePassword.class;
    }

    @Override
    public String encryptPassword(char[] cArray) throws FindException {
        return this.a().encryptPassword(cArray);
    }

    @Override
    public char[] decryptPassword(String string) throws FindException, ParseException {
        return this.a().decryptPassword(string);
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    private MasterPasswordManager a() throws FindException {
        MasterPasswordManager masterPasswordManager = this.n.get();
        if (masterPasswordManager == null) {
            AtomicReference<MasterPasswordManager> atomicReference = this.n;
            synchronized (atomicReference) {
                masterPasswordManager = this.n.get();
                if (masterPasswordManager == null) {
                    masterPasswordManager = new MasterPasswordManager(this.m.getSharedKey(), true);
                    this.n.set(masterPasswordManager);
                }
            }
        }
        return masterPasswordManager;
    }
}

