ImpersonatedConnectionManager.java 8.89 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.service;

import com.adobe.service.ConnectionResource;
import com.adobe.service.ServiceAPI;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;

/*
 * This class specifies class file version 49.0 but uses Java 6 signatures.  Assumed Java 6.
 */
public class ImpersonatedConnectionManager {
    private static Map<ServiceAPI, List<Credential>> credentialMap = new HashMap<ServiceAPI, List<Credential>>();
    private static Map<ServiceAPI, Boolean> connectedServiceMap = Collections.synchronizedMap(new HashMap());
    private static Map<ServiceAPI, Map<ConnectionResource, Credential>> serviceConnectionResourceMap = Collections.synchronizedMap(new HashMap());
    private static final Logger logger = LoggerFactory.getLogger(ImpersonatedConnectionManager.class);

    private ImpersonatedConnectionManager() {
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    public static Credential acquireCredential(ServiceAPI svc, ConnectionResource cr) {
        if (svc == null || cr == null) {
            return null;
        }
        Map<ServiceAPI, List<Credential>> map = credentialMap;
        synchronized (map) {
            List<Credential> credentialWallet = credentialMap.get(svc);
            if (credentialWallet == null) {
                return null;
            }
            Credential cred = ImpersonatedConnectionManager.getNextFreeCredential(credentialWallet);
            if (cred == null) {
                return null;
            }
            Map<ConnectionResource, Credential> connectionResourceMap = serviceConnectionResourceMap.get(svc);
            if (connectionResourceMap == null) {
                connectionResourceMap = new HashMap<ConnectionResource, Credential>();
                serviceConnectionResourceMap.put(svc, connectionResourceMap);
            }
            connectionResourceMap.put(cr, cred);
            logger.info("Service " + svc + ": Resource " + cr + " acquired credential " + cred.user);
            return cred;
        }
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    public static void releaseCredential(ServiceAPI svc, ConnectionResource cr) {
        if (svc == null || cr == null) {
            return;
        }
        Map<ServiceAPI, List<Credential>> map = credentialMap;
        synchronized (map) {
            List<Credential> credentialWallet = credentialMap.get(svc);
            if (credentialWallet == null) {
                return;
            }
            Map<ConnectionResource, Credential> connectionResourceMap = serviceConnectionResourceMap.get(svc);
            if (connectionResourceMap == null) {
                return;
            }
            Credential cred = connectionResourceMap.remove(cr);
            if (cred == null) {
                return;
            }
            logger.info("Service " + svc + ": Resource " + cr + " returned credential " + cred.user);
            ImpersonatedConnectionManager.returnFreeCredential(credentialWallet, cred);
        }
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    public static void addCredentials(ServiceAPI svc, Map<String, String> newCredentials) {
        if (svc == null || newCredentials == null) {
            return;
        }
        boolean bModifyServicePool = false;
        Map<ServiceAPI, List<Credential>> map = credentialMap;
        synchronized (map) {
            List<Credential> credentialWallet = credentialMap.get(svc);
            if (credentialWallet == null) {
                if (connectedServiceMap.containsKey(svc)) {
                    logger.warn("Service {0}: Cannot accept impersonation credentials because there may already be running instance(s).", (Object)svc.getName());
                    return;
                }
                bModifyServicePool = true;
                credentialWallet = new ArrayList<Credential>();
                credentialMap.put(svc, credentialWallet);
            }
            for (Map.Entry<String, String> entry : newCredentials.entrySet()) {
                boolean isInUse;
                if (entry.getKey() == null || entry.getValue() == null) continue;
                Credential cred = new Credential(entry.getKey(), entry.getValue());
                Map<ConnectionResource, Credential> connectionResourceMap = serviceConnectionResourceMap.get(svc);
                boolean bl = isInUse = connectionResourceMap == null ? false : connectionResourceMap.containsValue(cred);
                if (!credentialWallet.contains(cred) && !isInUse) {
                    if (bModifyServicePool) {
                        credentialWallet.add(cred);
                        continue;
                    }
                    logger.warn("Service {0}: Rejecting additional impersonation credential '{1}' as pool size has already been fixed.", (Object)svc.getName(), (Object)cred.user);
                    continue;
                }
                if (credentialWallet.contains(cred)) {
                    int index = credentialWallet.indexOf(cred);
                    Credential savedCredential = credentialWallet.get(index);
                    if (savedCredential.password.equals(cred.password)) continue;
                    savedCredential.password = cred.password;
                    logger.info("Service {0}: Accepting new password for user {1}", (Object)svc.getName(), (Object)cred.user);
                    continue;
                }
                ImpersonatedConnectionManager.updateInUseCredential(svc, cred);
            }
            if (bModifyServicePool && svc.getPoolMax() > credentialWallet.size()) {
                logger.warn("Service {0}: Reducing maximum pool size from {1} to {2} to match number of impersonation credentials.", new Object[]{svc.getName(), svc.getPoolMax(), credentialWallet.size()});
                svc.setPoolMax(credentialWallet.size());
            }
        }
    }

    private static void updateInUseCredential(ServiceAPI svc, Credential cred) {
        Map<ConnectionResource, Credential> connectionResourceMap = serviceConnectionResourceMap.get(svc);
        if (connectionResourceMap != null) {
            for (Map.Entry<ConnectionResource, Credential> entry : connectionResourceMap.entrySet()) {
                Credential inUseCred = entry.getValue();
                if (!inUseCred.equals(cred)) continue;
                if (inUseCred.password.equals(cred.password)) break;
                inUseCred.password = cred.password;
                entry.setValue(inUseCred);
                logger.info("Service {0}: Accepting new password for user {1}", (Object)svc.getName(), (Object)cred.user);
                break;
            }
        }
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    public static void removeCredentials(ServiceAPI svc) {
        if (svc == null) {
            return;
        }
        Map<ServiceAPI, List<Credential>> map = credentialMap;
        synchronized (map) {
            credentialMap.remove(svc);
        }
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    public static boolean isImpersonatedConnection(ServiceAPI svc) {
        if (svc == null) {
            return false;
        }
        Map<ServiceAPI, List<Credential>> map = credentialMap;
        synchronized (map) {
            if (!connectedServiceMap.containsKey(svc)) {
                connectedServiceMap.put(svc, Boolean.TRUE);
            }
            return credentialMap.containsKey(svc);
        }
    }

    private static Credential getNextFreeCredential(List<Credential> credentialWallet) {
        Credential cred = null;
        if (credentialWallet != null && credentialWallet.size() > 0) {
            cred = credentialWallet.remove(0);
        }
        return cred;
    }

    private static void returnFreeCredential(List<Credential> credentialWallet, Credential cred) {
        if (credentialWallet != null && cred != null) {
            credentialWallet.add(cred);
        }
    }

    public static class Credential {
        String user;
        String password;

        Credential(String str1, String str2) {
            this.user = str1;
            this.password = str2;
        }

        public boolean equals(Object cred) {
            return cred instanceof Credential && this.isEquivalent(this.user, ((Credential)cred).user, false);
        }

        public int hashCode() {
            return this.user.hashCode();
        }

        private boolean isEquivalent(String s1, String s2, boolean bIgnoreCase) {
            if (s1 == null && s2 == null) {
                return true;
            }
            if (s1 == null || s2 == null) {
                return false;
            }
            return bIgnoreCase ? s1.equalsIgnoreCase(s2) : s1.equals(s2);
        }
    }

}