/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  com.l7tech.identity.ldap.LdapUrlBasedIdentityProviderConfig
 */
package com.l7tech.server.identity.ldap;

import com.l7tech.identity.ldap.LdapUrlBasedIdentityProviderConfig;
import com.l7tech.server.ServerConfig;
import com.l7tech.server.identity.ldap.LdapUrlProvider;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.logging.Level;
import java.util.logging.Logger;

public class LdapUrlProviderImpl
implements LdapUrlProvider {
    private static final Logger a;
    private static final Long b;
    private final ReentrantReadWriteLock c = new ReentrantReadWriteLock();
    private final LdapUrlBasedIdentityProviderConfig d;
    private final String[] e;
    private final ServerConfig f;
    private final Long[] g;
    private String h;
    private static final String[] i;

    public LdapUrlProviderImpl(LdapUrlBasedIdentityProviderConfig ldapUrlBasedIdentityProviderConfig, ServerConfig serverConfig) {
        this.d = ldapUrlBasedIdentityProviderConfig;
        this.e = ldapUrlBasedIdentityProviderConfig.getLdapUrl();
        this.f = serverConfig;
        if (this.e != null) {
            this.g = new Long[this.e.length];
            this.h = this.e[0];
        } else {
            this.g = new Long[0];
        }
    }

    @Override
    public String getLastWorkingLdapUrl() {
        ReentrantReadWriteLock.ReadLock readLock = this.c.readLock();
        try {
            readLock.lockInterruptibly();
            String string = this.h;
            return string;
        }
        catch (InterruptedException interruptedException) {
            throw new RuntimeException(interruptedException);
        }
        finally {
            readLock.unlock();
        }
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    @Override
    public String markCurrentUrlFailureAndGetFirstAvailableOne(String string) {
        ReentrantReadWriteLock.WriteLock writeLock = this.c.writeLock();
        try {
            writeLock.lockInterruptibly();
        }
        catch (InterruptedException interruptedException) {
            throw new RuntimeException(interruptedException);
        }
        try {
            int n2;
            String[] stringArray;
            Long l2 = this.d.getReconnectTimeout();
            if (l2 == null) {
                try {
                    l2 = Long.parseLong(this.f.getProperty(LdapUrlBasedIdentityProviderConfig.PROP_LDAP_RECONNECT_TIMEOUT));
                }
                catch (NumberFormatException numberFormatException) {
                    stringArray = i;
                    a.log(Level.WARNING, LdapUrlBasedIdentityProviderConfig.PROP_LDAP_RECONNECT_TIMEOUT + stringArray[6], numberFormatException);
                    l2 = b;
                }
            }
            if (string != this.h) {
                String string2 = this.h;
                return string2;
            }
            if (string != null) {
                int n3 = 0;
                n2 = 0;
                while (true) {
                    block22: {
                        try {
                            if (n2 >= this.e.length) break;
                            if (this.e[n2] != string) break block22;
                        }
                        catch (InterruptedException interruptedException) {
                            throw LdapUrlProviderImpl.a(interruptedException);
                        }
                        n3 = n2;
                        this.g[n2] = System.currentTimeMillis();
                        String string3 = this.e[n2];
                        long l3 = l2 / 1000L;
                        stringArray = i;
                        a.info(stringArray[4] + l3 + stringArray[5] + string3);
                    }
                    ++n2;
                }
                try {
                    if (n3 > this.e.length - 1) {
                        throw new RuntimeException(i[3]);
                    }
                }
                catch (InterruptedException interruptedException) {
                    throw LdapUrlProviderImpl.a(interruptedException);
                }
            }
            for (int i2 = 0; i2 < this.e.length; ++i2) {
                n2 = 0;
                if (this.g[i2] == null) {
                    n2 = 1;
                    stringArray = i;
                    a.fine(stringArray[7] + this.e[i2]);
                } else {
                    long l4 = System.currentTimeMillis() - this.g[i2];
                    if (l4 > l2) {
                        n2 = 1;
                        this.g[i2] = null;
                        stringArray = i;
                        a.fine(stringArray[1] + this.e[i2]);
                    }
                }
                if (n2 == 0) continue;
                stringArray = i;
                a.info(stringArray[2] + this.e[i2]);
                String string4 = this.h = this.e[i2];
                return string4;
            }
            a.fine(i[0]);
            this.h = null;
            String string5 = null;
            return string5;
        }
        finally {
            writeLock.unlock();
        }
    }

    /*
     * Unable to fully structure code
     */
    static {
        block10: {
            block9: {
                var5 = new String[8];
                var3_1 = 0;
                var2_2 = "\u0013\rZ)\f+O\"AC{\f<\u000e3\u0013S)\u0002#O1\nZ`\u0013;K6O<\u001e\u0005Wy@\u001a|\u001eA^h\u0013oL7\u0004X)\u0002#O1\nZ`\u0013;K6AZf\u000e(\u000e7\u000fY|\u0007'\u0000r5Dp\t!Ir\bB)\u0001(O;\u000f\f)\"\u0006\u0013O`\u000e(\u000e&\u000e\u0016{\u0005,A$\u0004D)\u0015<G<\u0006\u0016}\b&]r\u0014DeZo\u0018\"\u0000Ez\u0005+\u000e3AC{\fo@=\u0015\u0016`\u000eoB;\u0012B\u001a\u0010\rWj\u000b#G!\u0015_g\u0007o[ \r\u0016o\u000f=\u000e<\u0004N}@\u000br\u0012Sj\u000f!J!A\f)";
                var4_3 = "\u0013\rZ)\f+O\"AC{\f<\u000e3\u0013S)\u0002#O1\nZ`\u0013;K6O<\u001e\u0005Wy@\u001a|\u001eA^h\u0013oL7\u0004X)\u0002#O1\nZ`\u0013;K6AZf\u000e(\u000e7\u000fY|\u0007'\u0000r5Dp\t!Ir\bB)\u0001(O;\u000f\f)\"\u0006\u0013O`\u000e(\u000e&\u000e\u0016{\u0005,A$\u0004D)\u0015<G<\u0006\u0016}\b&]r\u0014DeZo\u0018\"\u0000Ez\u0005+\u000e3AC{\fo@=\u0015\u0016`\u000eoB;\u0012B\u001a\u0010\rWj\u000b#G!\u0015_g\u0007o[ \r\u0016o\u000f=\u000e<\u0004N}@\u000br\u0012Sj\u000f!J!A\f)".length();
                var1_4 = 30;
                var0_5 = -1;
lbl7:
                // 2 sources

                while (true) {
                    v0 = 73;
                    v1 = ++var0_5;
                    v2 = var2_2.substring(v1, v1 + var1_4);
                    v3 = -1;
                    break block9;
                    break;
                }
lbl13:
                // 1 sources

                while (true) {
                    var5[var3_1++] = LdapUrlProviderImpl.a(v0, LdapUrlProviderImpl.a(v2));
                    if ((var0_5 += var1_4) < var4_3) {
                        var1_4 = var2_2.charAt(var0_5);
                        ** continue;
                    }
                    var2_2 = "m\u000e[y\u000f5C9\u0007\tx\u0010$\u0011.\u0011Gp\u00167D?\u001bM6\u000f\"^=\u001b[z\u0006~\u00118\r@x\u0018pU(\u0018Hc\u0013$\u001d\u0019\fP6\n\"]m\u0010Fb_?_m\u001cEw\u001c;]$\r]6\u00065Em";
                    var4_3 = "m\u000e[y\u000f5C9\u0007\tx\u0010$\u0011.\u0011Gp\u00167D?\u001bM6\u000f\"^=\u001b[z\u0006~\u00118\r@x\u0018pU(\u0018Hc\u0013$\u001d\u0019\fP6\n\"]m\u0010Fb_?_m\u001cEw\u001c;]$\r]6\u00065Em".length();
                    var1_4 = 48;
                    var0_5 = -1;
lbl22:
                    // 2 sources

                    while (true) {
                        v0 = 86;
                        v4 = ++var0_5;
                        v2 = var2_2.substring(v4, v4 + var1_4);
                        v3 = 0;
                        break block9;
                        break;
                    }
                    break;
                }
lbl28:
                // 1 sources

                while (true) {
                    var5[var3_1++] = LdapUrlProviderImpl.a(v0, LdapUrlProviderImpl.a(v2));
                    if ((var0_5 += var1_4) < var4_3) {
                        var1_4 = var2_2.charAt(var0_5);
                        ** continue;
                    }
                    break block10;
                    break;
                }
            }
            switch (v3) {
                default: {
                    ** continue;
                }
                ** case 0:
lbl39:
                // 1 sources

                ** continue;
            }
        }
        LdapUrlProviderImpl.i = var5;
        LdapUrlProviderImpl.a = Logger.getLogger(LdapUrlProviderImpl.class.getName());
        LdapUrlProviderImpl.b = 60000L;
    }

    private static InterruptedException a(InterruptedException interruptedException) {
        return interruptedException;
    }

    private static char[] a(String string) {
        char[] cArray = string.toCharArray();
        if (cArray.length < 2) {
            cArray = cArray;
            cArray[0] = (char)(cArray[0] ^ 0x67);
        }
        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 -> 27;
                    case 1 -> 40;
                    case 2 -> 127;
                    case 3 -> 64;
                    case 4 -> 41;
                    case 5 -> 6;
                    default -> 103;
                })));
                ++n3;
                n4 = n4;
            } while (n4 > n3);
        }
        return new String(cArray2).intern();
    }
}

