/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  com.l7tech.common.io.WhirlycacheFactory
 *  com.l7tech.identity.Group
 *  com.l7tech.identity.GroupBean
 *  com.l7tech.identity.GroupManager
 *  com.l7tech.identity.IdentityProvider
 *  com.l7tech.identity.User
 *  com.l7tech.identity.UserBean
 *  com.l7tech.identity.ValidationException
 *  com.l7tech.objectmodel.EntityType
 *  com.l7tech.objectmodel.FindException
 *  com.l7tech.objectmodel.Goid
 *  com.l7tech.objectmodel.IdentityHeader
 *  com.l7tech.util.Either
 *  com.l7tech.util.ExceptionUtils
 *  com.whirlycott.cache.Cache
 */
package com.l7tech.server.admin;

import com.l7tech.common.io.WhirlycacheFactory;
import com.l7tech.identity.Group;
import com.l7tech.identity.GroupBean;
import com.l7tech.identity.GroupManager;
import com.l7tech.identity.IdentityProvider;
import com.l7tech.identity.User;
import com.l7tech.identity.UserBean;
import com.l7tech.identity.ValidationException;
import com.l7tech.objectmodel.EntityType;
import com.l7tech.objectmodel.FindException;
import com.l7tech.objectmodel.Goid;
import com.l7tech.objectmodel.IdentityHeader;
import com.l7tech.server.admin.CacheEntry;
import com.l7tech.util.Either;
import com.l7tech.util.ExceptionUtils;
import com.whirlycott.cache.Cache;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.logging.Level;
import java.util.logging.Logger;

class GroupCache {
    private static final Logger a;
    private final AtomicInteger b = new AtomicInteger();
    private final AtomicInteger c = new AtomicInteger();
    private final Map<Goid, Long> d = new ConcurrentHashMap<Goid, Long>();
    private final Cache e;
    private static final String[] f;

    GroupCache(String string, int n2, int n3, int n4) {
        this.b.set(n3);
        this.c.set(n4);
        this.e = n2 < 1 ? null : WhirlycacheFactory.createCache((String)string, (int)n2, (int)293, (String)WhirlycacheFactory.POLICY_LFU);
    }

    public Set<IdentityHeader> getCachedValidatedGroups(User user, IdentityProvider identityProvider, boolean bl2) throws ValidationException {
        Goid goid = identityProvider.getConfig().getGoid();
        CacheKey cacheKey = new CacheKey(goid, EntityType.USER, user.getId());
        Set<IdentityHeader> set = this.a(cacheKey, user.getLogin(), identityProvider, this.b.get());
        try {
            if (set != null) {
                return set;
            }
        }
        catch (ValidationException validationException) {
            throw GroupCache.a((Exception)((Object)validationException));
        }
        return this.a(user, cacheKey, identityProvider, bl2);
    }

    public Set<IdentityHeader> getCachedGroups(Group group, IdentityProvider identityProvider) throws FindException {
        Goid goid = identityProvider.getConfig().getGoid();
        CacheKey cacheKey = new CacheKey(goid, EntityType.GROUP, group.getId());
        Set<IdentityHeader> set = this.a(cacheKey, group.getName(), identityProvider, this.b.get());
        try {
            if (set != null) {
                return set;
            }
        }
        catch (FindException findException) {
            throw GroupCache.a((Exception)((Object)findException));
        }
        return this.a(group, cacheKey, identityProvider);
    }

    public void invalidate(Goid goid) {
        this.d.put(goid, System.currentTimeMillis());
    }

    void a(int n2) {
        this.b.set(n2);
    }

    private Set<IdentityHeader> a(CacheKey cacheKey, String string, IdentityProvider identityProvider, int n2) {
        if (this.e == null) {
            return null;
        }
        CacheEntry cacheEntry = null;
        Object object = this.e.retrieve((Object)cacheKey);
        if (object != null && object instanceof CacheEntry) {
            cacheEntry = (CacheEntry)object;
        }
        if (cacheEntry == null) {
            return null;
        }
        long l2 = cacheEntry.getTimestamp();
        Long l3 = this.d.get(cacheKey.b);
        if (l2 + (long)n2 > System.currentTimeMillis() && (l3 == null || l3 < l2)) {
            if (a.isLoggable(Level.FINE)) {
                String[] stringArray = f;
                a.log(Level.FINE, stringArray[6], new String[]{string, identityProvider.getConfig().getName()});
            }
            return (Set)cacheEntry.getCachedObject();
        }
        if (a.isLoggable(Level.FINE)) {
            a.log(Level.FINE, f[4], new String[]{string, identityProvider.getConfig().getName()});
        }
        return null;
    }

    private <UT extends User, GT extends Group, GMT extends GroupManager<UT, GT>> Set<IdentityHeader> a(User user, CacheKey cacheKey, IdentityProvider<UT, ?, ?, GMT> identityProvider, boolean bl2) throws ValidationException {
        Set set;
        User user2 = user instanceof UserBean ? identityProvider.getUserManager().reify((UserBean)user) : user;
        try {
            if (!bl2) {
                identityProvider.validate(user2);
            }
        }
        catch (FindException findException) {
            throw GroupCache.a((Exception)((Object)findException));
        }
        GroupManager groupManager = identityProvider.getGroupManager();
        try {
            set = groupManager.getGroupHeaders(user2);
        }
        catch (FindException findException) {
            String[] stringArray = f;
            a.log(Level.WARNING, stringArray[5] + user2.getId() + stringArray[2] + ExceptionUtils.getMessage((Throwable)findException) + stringArray[0], findException);
            throw new ValidationException(stringArray[7] + user2.getId() + stringArray[1]);
        }
        return this.a(cacheKey, identityProvider, Either.left((Object)user2), set);
    }

    private <UT extends User, GT extends Group, GMT extends GroupManager<UT, GT>> Set<IdentityHeader> a(Group group, CacheKey cacheKey, IdentityProvider<UT, ?, ?, GMT> identityProvider) throws FindException {
        Group group2 = group instanceof GroupBean ? identityProvider.getGroupManager().reify((GroupBean)group) : group;
        GroupManager groupManager = identityProvider.getGroupManager();
        Set set = groupManager.getGroupHeadersForNestedGroup(group.getId());
        return this.a(cacheKey, identityProvider, Either.right((Object)group2), set);
    }

    private <UT extends User, GT extends Group, GMT extends GroupManager<UT, ?>> Set<IdentityHeader> a(CacheKey cacheKey, IdentityProvider<UT, ?, ?, GMT> identityProvider, Either<UT, GT> either, Set<IdentityHeader> set) {
        if (set != null) {
            int n2 = this.c.get();
            HashSet<IdentityHeader> hashSet = new HashSet<IdentityHeader>();
            int n3 = 1;
            String string = null;
            string = either.isLeft() ? ((User)either.left()).getLogin() : ((Group)either.right()).getName();
            for (IdentityHeader identityHeader : set) {
                if (n3 > n2) {
                    String[] stringArray = f;
                    a.log(Level.INFO, stringArray[3] + string + stringArray[8] + n2);
                    break;
                }
                hashSet.add(identityHeader);
                ++n3;
            }
            CacheEntry cacheEntry = new CacheEntry(hashSet);
            this.e.store((Object)cacheKey, (Object)cacheEntry);
            if (a.isLoggable(Level.FINE)) {
                a.log(Level.FINE, f[9], new String[]{string, identityProvider.getConfig().getName()});
            }
            return hashSet;
        }
        return null;
    }

    void a() {
        WhirlycacheFactory.shutdown((Cache)this.e);
    }

    /*
     * Unable to fully structure code
     */
    static {
        block10: {
            block9: {
                var5 = new String[10];
                var3_1 = 0;
                var2_2 = "#M\u0002#M\u0012#Ok$YQ#vC&$XP-c\u0006kf)G\u0002;1BM+$\u00049.^Sli\u0006&#NQ?l\n;aML>$\u00168$Y\f+v\f>1\u000b\u0004FG\u0002()N\u0003)|\u0013\"3R\u0003*k\u0011k4XF>+\u00049.^Sl\u007fS6aBPlw\u0017*-N\u0003#jC\u0002%{\u0003n\u007fR6c\u0010\u0003;m\u000f'aYF:e\u000f\"%JW)!A\u00119.Y\u0003-g\u0000.2XJ\"cC,3DV<wC-.Y\u00039w\u00069a\f+G\u0002()N\u0003$m\u0017k'DQlq\u0010.3\u0004D>k\u0016;aP\u00131$\u00059.F\u0003\u0005`3kcP\u00121&!A\u00119.Y\u0003-g\u0000.2XJ\"cC,3DV<wC-.Y\u00039w\u00069a\f";
                var4_3 = "#M\u0002#M\u0012#Ok$YQ#vC&$XP-c\u0006kf)G\u0002;1BM+$\u00049.^Sli\u0006&#NQ?l\n;aML>$\u00168$Y\f+v\f>1\u000b\u0004FG\u0002()N\u0003)|\u0013\"3R\u0003*k\u0011k4XF>+\u00049.^Sl\u007fS6aBPlw\u0017*-N\u0003#jC\u0002%{\u0003n\u007fR6c\u0010\u0003;m\u000f'aYF:e\u000f\"%JW)!A\u00119.Y\u0003-g\u0000.2XJ\"cC,3DV<wC-.Y\u00039w\u00069a\f+G\u0002()N\u0003$m\u0017k'DQlq\u0010.3\u0004D>k\u0016;aP\u00131$\u00059.F\u0003\u0005`3kcP\u00121&!A\u00119.Y\u0003-g\u0000.2XJ\"cC,3DV<wC-.Y\u00039w\u00069a\f".length();
                var1_4 = 2;
                var0_5 = -1;
lbl7:
                // 2 sources

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

                while (true) {
                    var5[var3_1++] = GroupCache.a(v0, GroupCache.a(v2));
                    if ((var0_5 += var1_4) < var4_3) {
                        var1_4 = var2_2.charAt(var0_5);
                        ** continue;
                    }
                    var2_2 = "d\u0004mrLB\u0000Eon\t\u0000+$Vcs\u001cDf&Inc\u001e\u0017c*T,v\u001e\re M|g\u0000\u0017+%K~&\u0019\u0017n1\u000bkt\u0003\u0011{c_<{L\u000becmhVLFprY.";
                    var4_3 = "d\u0004mrLB\u0000Eon\t\u0000+$Vcs\u001cDf&Inc\u001e\u0017c*T,v\u001e\re M|g\u0000\u0017+%K~&\u0019\u0017n1\u000bkt\u0003\u0011{c_<{L\u000becmhVLFprY.".length();
                    var1_4 = 5;
                    var0_5 = -1;
lbl22:
                    // 2 sources

                    while (true) {
                        v0 = 38;
                        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++] = GroupCache.a(v0, GroupCache.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;
            }
        }
        GroupCache.f = var5;
        GroupCache.a = Logger.getLogger(GroupCache.class.getName());
    }

    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] ^ 0x2D);
        }
        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 -> 101;
                    case 1 -> 2;
                    case 2 -> 42;
                    case 3 -> 32;
                    case 4 -> 74;
                    case 5 -> 66;
                    default -> 45;
                })));
                ++n3;
                n4 = n4;
            } while (n4 > n3);
        }
        return new String(cArray2).intern();
    }

    public static class CacheKey {
        private int a = -1;
        private final Goid b;
        private final EntityType c;
        private final String d;

        public CacheKey(Goid goid, EntityType entityType, String string) {
            this.b = goid;
            this.c = entityType;
            this.d = string;
        }

        public boolean equals(Object object) {
            if (this == object) {
                return true;
            }
            if (object == null || this.getClass() != object.getClass()) {
                return false;
            }
            CacheKey cacheKey = (CacheKey)object;
            if (this.b != null ? !this.b.equals((Object)cacheKey.b) : cacheKey.b != null) {
                return false;
            }
            if (this.c != cacheKey.c) {
                return false;
            }
            return !(this.d != null ? !this.d.equals(cacheKey.d) : cacheKey.d != null);
        }

        public int hashCode() {
            if (this.a == -1) {
                int n2 = this.b != null ? this.b.hashCode() : 0;
                n2 = 31 * n2 + (this.c != null ? this.c.hashCode() : 0);
                this.a = n2 = 31 * n2 + (this.d != null ? this.d.hashCode() : 0);
            }
            return this.a;
        }
    }
}

