/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  com.l7tech.identity.IdentityProvider
 *  com.l7tech.identity.IdentityProviderConfig
 *  com.l7tech.identity.IdentityProviderConfigManager
 *  com.l7tech.identity.InvalidIdProviderCfgException
 *  com.l7tech.objectmodel.EntityHeader
 *  com.l7tech.objectmodel.FindException
 *  com.l7tech.objectmodel.Goid
 *  org.springframework.beans.BeansException
 *  org.springframework.context.ApplicationContext
 *  org.springframework.context.ApplicationContextAware
 *  org.springframework.context.ApplicationEvent
 */
package com.l7tech.server.identity;

import com.l7tech.identity.IdentityProvider;
import com.l7tech.identity.IdentityProviderConfig;
import com.l7tech.identity.IdentityProviderConfigManager;
import com.l7tech.identity.InvalidIdProviderCfgException;
import com.l7tech.objectmodel.EntityHeader;
import com.l7tech.objectmodel.FindException;
import com.l7tech.objectmodel.Goid;
import com.l7tech.server.event.EntityInvalidationEvent;
import com.l7tech.server.identity.IdentityProviderFactorySpi;
import com.l7tech.server.util.PostStartupApplicationListener;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationEvent;

public class IdentityProviderFactory
implements ApplicationContextAware,
PostStartupApplicationListener {
    private static final Logger a;
    private static Map<Goid, IdentityProvider> b;
    private final IdentityProviderConfigManager c;
    private ApplicationContext d;
    private Map<String, IdentityProviderFactorySpi> e;
    private static final String[] f;

    public IdentityProviderFactory(IdentityProviderConfigManager identityProviderConfigManager) {
        this.c = identityProviderConfigManager;
    }

    public Collection<IdentityProvider> findAllIdentityProviders() throws FindException {
        ArrayList<IdentityProvider> arrayList = new ArrayList<IdentityProvider>();
        IdentityProviderConfigManager identityProviderConfigManager = this.a();
        for (EntityHeader entityHeader : identityProviderConfigManager.findAllHeaders()) {
            IdentityProvider identityProvider = this.getProvider(entityHeader.getGoid());
            try {
                if (identityProvider == null) continue;
                arrayList.add(identityProvider);
            }
            catch (FindException findException) {
                throw IdentityProviderFactory.a((Exception)((Object)findException));
            }
        }
        return Collections.unmodifiableList(arrayList);
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    public IdentityProvider getProvider(Goid goid) throws FindException {
        IdentityProvider identityProvider = b.get(goid);
        try {
            if (identityProvider != null) {
                return identityProvider;
            }
        }
        catch (InvalidIdProviderCfgException invalidIdProviderCfgException) {
            throw IdentityProviderFactory.a((Exception)((Object)invalidIdProviderCfgException));
        }
        IdentityProviderFactory identityProviderFactory = this;
        synchronized (identityProviderFactory) {
            identityProvider = b.get(goid);
            if (identityProvider == null) {
                IdentityProviderConfigManager identityProviderConfigManager = this.a();
                IdentityProviderConfig identityProviderConfig = (IdentityProviderConfig)identityProviderConfigManager.findByPrimaryKey(goid);
                if (identityProviderConfig == null) {
                    return null;
                }
                try {
                    identityProvider = this.a(identityProviderConfig, true);
                }
                catch (InvalidIdProviderCfgException invalidIdProviderCfgException) {
                    String string = f[5] + String.valueOf(identityProviderConfig.type());
                    a.log(Level.SEVERE, string, invalidIdProviderCfgException);
                    throw new RuntimeException(string, invalidIdProviderCfgException);
                }
                b.put(goid, identityProvider);
            }
        }
        return identityProvider;
    }

    public void test(IdentityProviderConfig identityProviderConfig, String string, char[] cArray) throws InvalidIdProviderCfgException {
        IdentityProvider identityProvider = this.a(identityProviderConfig, false);
        identityProvider.test(false, string, cArray);
    }

    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.d = applicationContext;
    }

    public void onApplicationEvent(ApplicationEvent applicationEvent) {
        EntityInvalidationEvent entityInvalidationEvent;
        if (applicationEvent instanceof EntityInvalidationEvent && IdentityProviderConfig.class.isAssignableFrom((entityInvalidationEvent = (EntityInvalidationEvent)applicationEvent).getEntityClass())) {
            Goid[] goidArray;
            for (Goid goid : goidArray = entityInvalidationEvent.getEntityIds()) {
                this.a(goid);
                b.remove(goid);
            }
        }
    }

    private IdentityProviderConfigManager a() {
        return this.c;
    }

    private Map<String, IdentityProviderFactorySpi> b() {
        Map map = this.e;
        if (map == null) {
            map = this.e = this.d.getBeansOfType(IdentityProviderFactorySpi.class);
        }
        return map;
    }

    private IdentityProviderFactorySpi a(IdentityProviderConfig identityProviderConfig) throws InvalidIdProviderCfgException {
        String string = identityProviderConfig.type().getClassname();
        IdentityProviderFactorySpi identityProviderFactorySpi = null;
        Map<String, IdentityProviderFactorySpi> map = this.b();
        for (Map.Entry<String, IdentityProviderFactorySpi> entry : map.entrySet()) {
            String string2 = entry.getKey();
            IdentityProviderFactorySpi identityProviderFactorySpi2 = entry.getValue();
            if (!string.equals(identityProviderFactorySpi2.getClassname())) continue;
            String[] stringArray = f;
            a.log(Level.FINE, stringArray[1], new String[]{string2, string});
            identityProviderFactorySpi = identityProviderFactorySpi2;
            break;
        }
        try {
            if (identityProviderFactorySpi == null) {
                throw new InvalidIdProviderCfgException(f[4] + string + f[0]);
            }
        }
        catch (InvalidIdProviderCfgException invalidIdProviderCfgException) {
            throw IdentityProviderFactory.a((Exception)((Object)invalidIdProviderCfgException));
        }
        return identityProviderFactorySpi;
    }

    private IdentityProvider a(IdentityProviderConfig identityProviderConfig, boolean bl2) throws InvalidIdProviderCfgException {
        IdentityProviderFactorySpi identityProviderFactorySpi = this.a(identityProviderConfig);
        try {
            return identityProviderFactorySpi.createIdentityProvider(identityProviderConfig, bl2);
        }
        catch (InvalidIdProviderCfgException invalidIdProviderCfgException) {
            throw invalidIdProviderCfgException;
        }
        catch (Exception exception) {
            throw new InvalidIdProviderCfgException((Throwable)exception);
        }
    }

    /*
     * Enabled aggressive block sorting
     * Enabled unnecessary exception pruning
     * Enabled aggressive exception aggregation
     */
    private void a(Goid goid) {
        IdentityProvider identityProvider = b.get(goid);
        try {
            if (identityProvider == null) return;
            try {
                this.a(identityProvider.getConfig()).destroyIdentityProvider(identityProvider);
                return;
            }
            catch (InvalidIdProviderCfgException invalidIdProviderCfgException) {
                String[] stringArray = f;
                a.warning(stringArray[3] + identityProvider.getConfig().type().getClassname() + stringArray[2]);
                return;
            }
        }
        catch (RuntimeException runtimeException) {
            throw IdentityProviderFactory.a(runtimeException);
        }
    }

    /*
     * Unable to fully structure code
     */
    static {
        block10: {
            block9: {
                var5 = new String[6];
                var3_1 = 0;
                var2_2 = "E\u001897E\b{a\u0013E\u0006S\u000faoGUBF\u0013zpZH\u0007DAsgPX\rD\u00185!\u0014WRKF2&UC\u0010\u0016\u0015lvV\fE\u0011\u001a${\u0014\u000bL\u0002E\u00185!Y\u0014yb\u0013B\rBAso]HBP\u0000vr\\^\u001b\u0016\u0015z&WI\u0011B\u0013z\u007f\u0013E\u0006S\u000faoGUBF\u0013zpZH\u0007DA2";
                var4_3 = "E\u001897E\b{a\u0013E\u0006S\u000faoGUBF\u0013zpZH\u0007DAsgPX\rD\u00185!\u0014WRKF2&UC\u0010\u0016\u0015lvV\fE\u0011\u001a${\u0014\u000bL\u0002E\u00185!Y\u0014yb\u0013B\rBAso]HBP\u0000vr\\^\u001b\u0016\u0015z&WI\u0011B\u0013z\u007f\u0013E\u0006S\u000faoGUBF\u0013zpZH\u0007DA2".length();
                var1_4 = 2;
                var0_5 = -1;
lbl7:
                // 2 sources

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

                while (true) {
                    var5[var3_1++] = IdentityProviderFactory.a(v0, IdentityProviderFactory.a(v2));
                    if ((var0_5 += var1_4) < var4_3) {
                        var1_4 = var2_2.charAt(var0_5);
                        ** continue;
                    }
                    var2_2 = "1I\u0004ir\u0003R\u001dRQc\u007fMXR@\u0010fbLN\u000b\u0006\u0017jd\u0003U\u0016C\u001fq\u007fWERV\u0003j`JX\u0017TQqoSYR\u000161G\u001f\"b\u0003U\u001cO\u0005lwOU\bCQdx\u0003U\u0016C\u001fq\u007fWERE\u0010f~FX\"T\u001es\u007fGY\u0000\u0006\u0006lbK\u001c\u0006_\u0001`6";
                    var4_3 = "1I\u0004ir\u0003R\u001dRQc\u007fMXR@\u0010fbLN\u000b\u0006\u0017jd\u0003U\u0016C\u001fq\u007fWERV\u0003j`JX\u0017TQqoSYR\u000161G\u001f\"b\u0003U\u001cO\u0005lwOU\bCQdx\u0003U\u0016C\u001fq\u007fWERE\u0010f~FX\"T\u001es\u007fGY\u0000\u0006\u0006lbK\u001c\u0006_\u0001`6".length();
                    var1_4 = 51;
                    var0_5 = -1;
lbl22:
                    // 2 sources

                    while (true) {
                        v0 = 82;
                        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++] = IdentityProviderFactory.a(v0, IdentityProviderFactory.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;
            }
        }
        IdentityProviderFactory.f = var5;
        IdentityProviderFactory.a = Logger.getLogger(IdentityProviderFactory.class.getName());
        IdentityProviderFactory.b = new ConcurrentHashMap<Goid, IdentityProvider>();
    }

    private static Exception a(Exception exception) {
        return exception;
    }

    private static char[] a(String string) {
        char[] cArray = string.toCharArray();
        if (cArray.length < 2) {
            cArray = cArray;
            cArray[0] = (char)(cArray[0] ^ 0x6E);
        }
        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 -> 32;
                    case 1 -> 116;
                    case 2 -> 35;
                    case 3 -> 87;
                    case 4 -> 68;
                    case 5 -> 113;
                    default -> 110;
                })));
                ++n3;
                n4 = n4;
            } while (n4 > n3);
        }
        return new String(cArray2).intern();
    }
}

