/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  com.l7tech.common.io.WhirlycacheFactory
 *  com.l7tech.util.CausedIOException
 *  com.l7tech.util.ResourceUtils
 *  com.whirlycott.cache.Cache
 */
package com.l7tech.server.url;

import com.l7tech.common.io.WhirlycacheFactory;
import com.l7tech.server.identity.ldap.LdapUtils;
import com.l7tech.server.url.AbstractUrlObjectCache;
import com.l7tech.util.CausedIOException;
import com.l7tech.util.ResourceUtils;
import com.whirlycott.cache.Cache;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.regex.Matcher;
import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.PartialResultException;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;

public class LdapUrlObjectCache<PT>
extends AbstractUrlObjectCache<LdapCacheEntry<PT>> {
    private final String c;
    private final String d;
    private final long e;
    private final long f;
    private final boolean g;
    private final Cache h = WhirlycacheFactory.createCache((String)(this.getClass().getSimpleName() + j[2]), (int)100, (int)1800, (String)WhirlycacheFactory.POLICY_LRU);
    private final Lock i = new ReentrantLock();
    private static final String[] j;

    public LdapUrlObjectCache(String string, long l2, AbstractUrlObjectCache.WaitMode waitMode, String string2, String string3, long l3, long l4, boolean bl2) {
        super(string, l2, waitMode);
        this.c = string2;
        this.d = string3;
        this.e = l3;
        this.f = l4;
        this.g = bl2;
    }

    @Override
    protected Lock getReadLock() {
        return this.i;
    }

    @Override
    protected Lock getWriteLock() {
        return this.i;
    }

    @Override
    protected AbstractUrlObjectCache.AbstractCacheEntry<LdapCacheEntry<PT>> cacheGet(String string) {
        return (AbstractUrlObjectCache.AbstractCacheEntry)this.h.retrieve((Object)string);
    }

    @Override
    protected void cachePut(String string, AbstractUrlObjectCache.AbstractCacheEntry abstractCacheEntry) {
        this.h.store((Object)string, (Object)abstractCacheEntry);
    }

    @Override
    protected AbstractUrlObjectCache.AbstractCacheEntry<LdapCacheEntry<PT>> cacheRemove(String string) {
        return (AbstractUrlObjectCache.AbstractCacheEntry)this.h.remove((Object)string);
    }

    @Override
    protected Iterator<AbstractUrlObjectCache.AbstractCacheEntry<LdapCacheEntry<PT>>> cacheIterator() {
        return null;
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     * Loose catch block
     * Enabled force condition propagation
     * Lifted jumps to return sites
     */
    @Override
    protected AbstractUrlObjectCache.DatedUserObject<LdapCacheEntry<PT>> doGet(String string, String string2, long l2, String string3) throws IOException {
        AbstractUrlObjectCache.DatedUserObject datedUserObject;
        Object object;
        String string4 = new URI(string).getQuery();
        if (string4 != null) {
            block10: {
                object = LdapUtils.LDAP_URL_QUERY_PATTERN.matcher(string4);
                if (!((Matcher)object).matches()) throw new IOException(j[0] + string);
                try {
                    if (((Matcher)object).groupCount() != 0) break block10;
                    throw new IOException(j[0] + string);
                    catch (NamingException namingException) {
                        throw LdapUrlObjectCache.a(namingException);
                    }
                }
                catch (NamingException namingException) {
                    throw LdapUrlObjectCache.a(namingException);
                }
            }
            string4 = ((Matcher)object).group(1);
        }
        object = null;
        try {
            object = LdapUtils.getLdapContext(string, this.c, this.d, this.e, this.f);
            Attributes attributes = object.getAttributes("");
            datedUserObject = new AbstractUrlObjectCache.DatedUserObject(new LdapCacheEntry(attributes, string4, this.g), Long.toString(System.currentTimeMillis()), null);
        }
        catch (Throwable throwable) {
            try {
                ResourceUtils.closeQuietly((Context)object);
                throw throwable;
            }
            catch (URISyntaxException | NamingException exception) {
                throw new CausedIOException(j[1], (Throwable)exception);
            }
        }
        ResourceUtils.closeQuietly((Context)object);
        return datedUserObject;
    }

    private static NamingException a(NamingException namingException) {
        return namingException;
    }

    /*
     * Enabled aggressive block sorting
     */
    static {
        String[] stringArray = new String[3];
        int n2 = 0;
        String string = "mX\u0002H\u0004\u0002\u0013E]N_\u001e\u0015\fY\u0019\u001dZ\u0019\u0019\u0010G\u0019\u0007@K<:aiN{9<D\u0000\u001fcV\u001bB\u000f\u001eYT\u0019\u000b]\u001f\u0011\u001cLP\u001dFK<:aiNM\u0004\u001e\nEA\u001a\u0006\u000eZ\u000fM\u0003\u0015";
        int n3 = "mX\u0002H\u0004\u0002\u0013E]N_\u001e\u0015\fY\u0019\u001dZ\u0019\u0019\u0010G\u0019\u0007@K<:aiN{9<D\u0000\u001fcV\u001bB\u000f\u001eYT\u0019\u000b]\u001f\u0011\u001cLP\u001dFK<:aiNM\u0004\u001e\nEA\u001a\u0006\u000eZ\u000fM\u0003\u0015".length();
        int n4 = 36;
        int n5 = -1;
        while (true) {
            int n6 = ++n5;
            stringArray[n2++] = LdapUrlObjectCache.b(74, LdapUrlObjectCache.b(string.substring(n6, n6 + n4)));
            if ((n5 += n4) >= n3) {
                j = stringArray;
                return;
            }
            n4 = string.charAt(n5);
        }
    }

    private static char[] b(String string) {
        char[] cArray = string.toCharArray();
        if (cArray.length < 2) {
            cArray = cArray;
            cArray[0] = (char)(cArray[0] ^ 0x34);
        }
        return cArray;
    }

    /*
     * Handled impossible loop by duplicating code
     * Enabled aggressive block sorting
     */
    private static String b(int n2, char[] cArray) {
        char[] cArray2;
        block11: {
            int n3;
            int n4;
            int n5;
            block10: {
                n5 = n2;
                cArray2 = cArray;
                n4 = cArray.length;
                n3 = 0;
                if (!true) break block10;
                n4 = n4;
                if (n4 <= n3) break block11;
            }
            do {
                n5 = n5;
                cArray2 = cArray2;
                int n6 = n3;
                cArray2[n6] = (char)(cArray2[n6] ^ (n5 ^ (switch (n3 % 7) {
                    case 0 -> 106;
                    case 1 -> 115;
                    case 2 -> 36;
                    case 3 -> 100;
                    case 4 -> 33;
                    case 5 -> 58;
                    default -> 52;
                })));
                ++n3;
                n4 = n4;
            } while (n4 > n3);
        }
        return new String(cArray2).intern();
    }

    public static class LdapCacheEntry<PT> {
        private final String a;
        private final List<Object> b;
        private final Attributes c;
        private PT d;
        private static final String e = LdapCacheEntry.a(95, LdapCacheEntry.a("_\u0018\u0011tlO\\"));

        private LdapCacheEntry(Attributes attributes, String string, boolean bl2) throws NamingException {
            this.c = attributes;
            this.a = string;
            if (string == null) {
                this.b = Collections.emptyList();
            } else {
                String string2;
                String string3;
                try {
                    string3 = this.a;
                    string2 = bl2 ? e : "";
                }
                catch (PartialResultException partialResultException) {
                    throw LdapCacheEntry.a(partialResultException);
                }
                String string4 = string3 + string2;
                Attribute attribute = attributes.get(string4);
                ArrayList arrayList = new ArrayList();
                try {
                    NamingEnumeration<?> namingEnumeration = attribute.getAll();
                    while (namingEnumeration.hasMore()) {
                        Object obj = namingEnumeration.next();
                        arrayList.add(obj);
                    }
                }
                catch (PartialResultException partialResultException) {
                    LdapUtils.handlePartialResultException(partialResultException);
                }
                this.b = Collections.unmodifiableList(arrayList);
            }
        }

        public String getInterestingAttributeName() {
            return this.a;
        }

        public List<Object> getInterestingAttributeValues() {
            return this.b;
        }

        public Attributes getAttributes() {
            return this.c;
        }

        public synchronized PT getPayload() {
            return this.d;
        }

        public synchronized void setPayload(PT PT) {
            this.d = PT;
        }

        private static PartialResultException a(PartialResultException partialResultException) {
            return partialResultException;
        }

        private static char[] a(String string) {
            char[] cArray = string.toCharArray();
            if (cArray.length < 2) {
                cArray = cArray;
                cArray[0] = (char)(cArray[0] ^ 0x7A);
            }
            return cArray;
        }

        /*
         * Handled impossible loop by duplicating code
         * Enabled aggressive block sorting
         */
        private static String a(int n2, char[] cArray) {
            char[] cArray2;
            block11: {
                int n3;
                int n4;
                int n5;
                block10: {
                    n5 = n2;
                    cArray2 = cArray;
                    n4 = cArray.length;
                    n3 = 0;
                    if (!true) break block10;
                    n4 = n4;
                    if (n4 <= n3) break block11;
                }
                do {
                    n5 = n5;
                    cArray2 = cArray2;
                    int n6 = n3;
                    cArray2[n6] = (char)(cArray2[n6] ^ (n5 ^ (switch (n3 % 7) {
                        case 0 -> 59;
                        case 1 -> 37;
                        case 2 -> 39;
                        case 3 -> 69;
                        case 4 -> 82;
                        case 5 -> 98;
                        default -> 122;
                    })));
                    ++n3;
                    n4 = n4;
                } while (n4 > n3);
            }
            return new String(cArray2).intern();
        }
    }
}

