/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  com.l7tech.gateway.common.cluster.ClusterProperty
 *  com.l7tech.gateway.common.cluster.ImmutableClusterProperty
 *  com.l7tech.objectmodel.FindException
 *  com.l7tech.objectmodel.Goid
 *  org.springframework.context.ApplicationEvent
 *  org.springframework.jmx.export.annotation.ManagedAttribute
 *  org.springframework.jmx.export.annotation.ManagedOperation
 *  org.springframework.jmx.export.annotation.ManagedResource
 */
package com.l7tech.server.cluster;

import com.l7tech.gateway.common.cluster.ClusterProperty;
import com.l7tech.gateway.common.cluster.ImmutableClusterProperty;
import com.l7tech.objectmodel.FindException;
import com.l7tech.objectmodel.Goid;
import com.l7tech.server.cluster.ClusterPropertyListener;
import com.l7tech.server.cluster.ClusterPropertyManager;
import com.l7tech.server.event.EntityInvalidationEvent;
import com.l7tech.server.util.PostStartupApplicationListener;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.concurrent.atomic.AtomicReference;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.springframework.context.ApplicationEvent;
import org.springframework.jmx.export.annotation.ManagedAttribute;
import org.springframework.jmx.export.annotation.ManagedOperation;
import org.springframework.jmx.export.annotation.ManagedResource;

@ManagedResource(description="Cluster Property Cache", objectName="l7tech:type=ClusterPropertyCache")
public class ClusterPropertyCache
implements PostStartupApplicationListener {
    private static final Logger a;
    private final AtomicReference<Map<String, ClusterProperty>> b = new AtomicReference();
    private final Object c = new Object();
    private ClusterPropertyManager d;
    private ClusterPropertyListener e;
    private static final String[] f;

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    public void setClusterPropertyManager(ClusterPropertyManager clusterPropertyManager) {
        Object object = this.c;
        synchronized (object) {
            if (clusterPropertyManager == null) {
                String[] stringArray = f;
                throw new IllegalArgumentException(stringArray[9]);
            }
            try {
                if (this.d != null) {
                    throw new IllegalStateException(f[5]);
                }
            }
            catch (FindException findException) {
                throw ClusterPropertyCache.a((Exception)((Object)findException));
            }
        }
        try {
            object = clusterPropertyManager.findAll();
            TreeMap<String, ImmutableClusterProperty> treeMap = new TreeMap<String, ImmutableClusterProperty>(String.CASE_INSENSITIVE_ORDER);
            Iterator iterator = object.iterator();
            while (iterator.hasNext()) {
                ClusterProperty clusterProperty = (ClusterProperty)iterator.next();
                try {
                    if (clusterProperty.getName() == null) continue;
                    treeMap.put(clusterProperty.getName(), new ImmutableClusterProperty(clusterProperty));
                }
                catch (FindException findException) {
                    throw ClusterPropertyCache.a((Exception)((Object)findException));
                }
            }
            this.b.set(Collections.unmodifiableMap(treeMap));
        }
        catch (FindException findException) {
            a.log(Level.WARNING, f[3], findException);
        }
        object = this.c;
        synchronized (object) {
            this.d = clusterPropertyManager;
        }
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    public void setClusterPropertyListener(ClusterPropertyListener clusterPropertyListener) {
        Object object = this.c;
        synchronized (object) {
            if (clusterPropertyListener == null) {
                String[] stringArray = f;
                throw new IllegalArgumentException(stringArray[12]);
            }
            try {
                if (this.e != null) {
                    throw new IllegalStateException(f[8]);
                }
            }
            catch (IllegalArgumentException illegalArgumentException) {
                throw ClusterPropertyCache.a(illegalArgumentException);
            }
            this.e = clusterPropertyListener;
        }
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     * Enabled aggressive block sorting
     * Enabled unnecessary exception pruning
     * Enabled aggressive exception aggregation
     * Converted monitor instructions to comments
     * Lifted jumps to return sites
     */
    public void onApplicationEvent(ApplicationEvent applicationEvent) {
        if (!(applicationEvent instanceof EntityInvalidationEvent)) return;
        EntityInvalidationEvent entityInvalidationEvent = (EntityInvalidationEvent)applicationEvent;
        if (!ClusterProperty.class.equals(entityInvalidationEvent.getEntityClass())) return;
        Goid[] goidArray = this.c;
        // MONITORENTER : this.c
        ClusterPropertyManager clusterPropertyManager = this.d;
        ClusterPropertyListener clusterPropertyListener = this.e;
        // MONITOREXIT : goidArray
        try {
            if (clusterPropertyManager == null) {
                a.warning(f[1]);
                return;
            }
        }
        catch (FindException findException) {
            throw ClusterPropertyCache.a((Exception)((Object)findException));
        }
        goidArray = entityInvalidationEvent.getEntityIds();
        try {
            if (goidArray == null) return;
            if (goidArray.length <= 0) return;
        }
        catch (FindException findException) {
            throw ClusterPropertyCache.a((Exception)((Object)findException));
        }
        Map<String, ClusterProperty> map = this.b.get();
        TreeMap<String, ClusterProperty> treeMap = new TreeMap<String, ClusterProperty>(String.CASE_INSENSITIVE_ORDER);
        try {
            if (map != null) {
                treeMap.putAll(map);
            }
        }
        catch (FindException findException) {
            throw ClusterPropertyCache.a((Exception)((Object)findException));
        }
        ArrayList<ClusterProperty[]> arrayList = new ArrayList<ClusterProperty[]>();
        ArrayList<ClusterProperty> arrayList2 = new ArrayList<ClusterProperty>();
        block14: for (Goid goid : goidArray) {
            try {
                ClusterProperty clusterProperty;
                ClusterProperty clusterProperty2;
                block20: {
                    clusterProperty2 = (ClusterProperty)clusterPropertyManager.findByPrimaryKey(goid);
                    try {
                        if (clusterProperty2 == null || clusterProperty2.getName() == null) break block20;
                    }
                    catch (FindException findException) {
                        throw ClusterPropertyCache.a((Exception)((Object)findException));
                    }
                    String[] stringArray = f;
                    a.log(Level.FINE, stringArray[4], clusterProperty2.getName());
                    clusterProperty2 = new ImmutableClusterProperty(clusterProperty2);
                    clusterProperty = treeMap.put(clusterProperty2.getName(), clusterProperty2);
                    arrayList.add(new ClusterProperty[]{clusterProperty, clusterProperty2});
                    continue;
                }
                if (clusterProperty2 != null) continue;
                clusterProperty = treeMap.values().iterator();
                while (clusterProperty.hasNext()) {
                    ClusterProperty clusterProperty3 = (ClusterProperty)clusterProperty.next();
                    try {
                        if (!clusterProperty3.getGoid().equals((Object)goid)) continue;
                        clusterProperty.remove();
                        arrayList2.add(clusterProperty3);
                        continue block14;
                    }
                    catch (FindException findException) {
                        throw ClusterPropertyCache.a((Exception)((Object)findException));
                    }
                }
            }
            catch (FindException findException) {
                a.log(Level.WARNING, f[2], findException);
            }
        }
        this.b.set(Collections.unmodifiableMap(treeMap));
        for (ClusterProperty[] clusterPropertyArray : arrayList) {
            this.a(clusterPropertyListener, clusterPropertyArray[0], clusterPropertyArray[1]);
        }
        Iterator iterator = arrayList2.iterator();
        while (iterator.hasNext()) {
            ClusterProperty clusterProperty = (ClusterProperty)iterator.next();
            this.a(clusterPropertyListener, clusterProperty);
        }
    }

    public ClusterProperty getCachedEntityByName(String string) {
        ClusterProperty clusterProperty = null;
        Map<String, ClusterProperty> map = this.b.get();
        if (map != null) {
            clusterProperty = map.get(string);
        }
        return clusterProperty;
    }

    @Deprecated
    public ClusterProperty getCachedEntityByName(String string, int n2) {
        ClusterProperty clusterProperty = null;
        Map<String, ClusterProperty> map = this.b.get();
        if (map != null) {
            clusterProperty = map.get(string);
        }
        return clusterProperty;
    }

    @ManagedOperation(description="Get Property Value")
    public String getPropertyValue(String string) {
        String string2 = null;
        ClusterProperty clusterProperty = this.getCachedEntityByName(string);
        if (clusterProperty != null) {
            string2 = clusterProperty.getValue();
        }
        return string2;
    }

    @ManagedAttribute(description="Cached Property Names", currencyTimeLimit=30)
    public Set<String> getPropertyNames() {
        TreeSet<String> treeSet = new TreeSet<String>();
        Map<String, ClusterProperty> map = this.b.get();
        try {
            if (map != null) {
                treeSet.addAll(map.keySet());
            }
        }
        catch (IllegalArgumentException illegalArgumentException) {
            throw ClusterPropertyCache.a(illegalArgumentException);
        }
        return treeSet;
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    public void refreshFromDatabase() {
        ClusterPropertyManager clusterPropertyManager;
        Object object = this.c;
        synchronized (object) {
            clusterPropertyManager = this.d;
        }
        try {
            if (clusterPropertyManager == null) {
                throw new IllegalStateException(f[10]);
            }
        }
        catch (FindException findException) {
            throw ClusterPropertyCache.a((Exception)((Object)findException));
        }
        try {
            object = clusterPropertyManager.findAll();
            TreeMap<String, ImmutableClusterProperty> treeMap = new TreeMap<String, ImmutableClusterProperty>(String.CASE_INSENSITIVE_ORDER);
            Iterator iterator = object.iterator();
            while (iterator.hasNext()) {
                ClusterProperty clusterProperty = (ClusterProperty)iterator.next();
                try {
                    if (clusterProperty.getName() == null) continue;
                    treeMap.put(clusterProperty.getName(), new ImmutableClusterProperty(clusterProperty));
                }
                catch (FindException findException) {
                    throw ClusterPropertyCache.a((Exception)((Object)findException));
                }
            }
            this.b.set(Collections.unmodifiableMap(treeMap));
            String[] stringArray = f;
            a.log(Level.FINE, stringArray[11], treeMap.size());
        }
        catch (FindException findException) {
            a.log(Level.WARNING, f[6], findException);
        }
    }

    private void a(ClusterPropertyListener clusterPropertyListener, ClusterProperty clusterProperty, ClusterProperty clusterProperty2) {
        if (clusterPropertyListener != null) {
            try {
                clusterPropertyListener.clusterPropertyChanged(clusterProperty, clusterProperty2);
            }
            catch (Exception exception) {
                a.log(Level.WARNING, f[7], exception);
            }
        }
    }

    private void a(ClusterPropertyListener clusterPropertyListener, ClusterProperty clusterProperty) {
        if (clusterPropertyListener != null) {
            try {
                clusterPropertyListener.clusterPropertyDeleted(clusterProperty);
            }
            catch (Exception exception) {
                a.log(Level.WARNING, f[0], exception);
            }
        }
    }

    /*
     * Unable to fully structure code
     */
    static {
        block10: {
            block9: {
                var5 = new String[13];
                var3_1 = 0;
                var2_2 = "P)H:wEv35O&sEvg<\u001d%jSpv+X;#Tla JifXgv5I lN*KA ^,jVaweX'wIpjeT'uAhz!\\=jOj3 K,mT$q0Ii`Lq`1X;#Pv|5X;wY$~$S(dEv3,NimOp36X=\"\u001eV7O&q\u0000h|$Y mG$p)H:wEv35O&sEvg< V7O&q\u0000h|$Y mG$p)H:wEv35O&sEvg,X:\u001aC7R9fRpje\u001anx\u0010y4b\u0011ivP`r1X--#p)H:wEvC7R9fRpj\b\\'bGaae\\%qEew<\u001d:fT%1V7O&q\u0000vv#O,pHm}\"\u001d*oUwg OisRkc O=jEw3#O&n\u0000`r1\\+bSa*P)H:wEv35O&sEvg<\u001d%jSpv+X;#Tla JifXgv5I lN*$p)H:wEvC7R9fRpj\tT:wEjv7\u001d(oRar!DipEp2'p)H:wEvC7R9fRpj\b\\'bGaaeP<pT$}*IiaE$}0Q%.P$S'lT$a [;fSl)e^%vSpv7m;lPaa1D\u0004bNet OimOp36X=";
                var4_3 = "P)H:wEv35O&sEvg<\u001d%jSpv+X;#Tla JifXgv5I lN*KA ^,jVaweX'wIpjeT'uAhz!\\=jOj3 K,mT$q0Ii`Lq`1X;#Pv|5X;wY$~$S(dEv3,NimOp36X=\"\u001eV7O&q\u0000h|$Y mG$p)H:wEv35O&sEvg< V7O&q\u0000h|$Y mG$p)H:wEv35O&sEvg,X:\u001aC7R9fRpje\u001anx\u0010y4b\u0011ivP`r1X--#p)H:wEvC7R9fRpj\b\\'bGaae\\%qEew<\u001d:fT%1V7O&q\u0000vv#O,pHm}\"\u001d*oUwg OisRkc O=jEw3#O&n\u0000`r1\\+bSa*P)H:wEv35O&sEvg<\u001d%jSpv+X;#Tla JifXgv5I lN*$p)H:wEvC7R9fRpj\tT:wEjv7\u001d(oRar!DipEp2'p)H:wEvC7R9fRpj\b\\'bGaaeP<pT$}*IiaE$}0Q%.P$S'lT$a [;fSl)e^%vSpv7m;lPaa1D\u0004bNet OimOp36X=".length();
                var1_4 = 42;
                var0_5 = -1;
lbl7:
                // 2 sources

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

                while (true) {
                    var5[var3_1++] = ClusterPropertyCache.a(v0, ClusterPropertyCache.a(v2));
                    if ((var0_5 += var1_4) < var4_3) {
                        var1_4 = var2_2.charAt(var0_5);
                        ** continue;
                    }
                    var2_2 = "i\bs\u0013N{D^\t5\u0002G}_O\bgA[zCK\bg\u0015R(OZ\u000e}\u0004\u000bn^T\u00005\u0005J|MY\ff\u0004\u000b W\u000b\u00105\u0011Yg\\^\u001fa\bN{\u0005(X\u0001`\u0012_m^k\u001fz\u0011NzXB!|\u0012_mB^\u001f5\f^{X\u001b\u0003z\u0015\u000bjI\u001b\u0003`\rG";
                    var4_3 = "i\bs\u0013N{D^\t5\u0002G}_O\bgA[zCK\bg\u0015R(OZ\u000e}\u0004\u000bn^T\u00005\u0005J|MY\ff\u0004\u000b W\u000b\u00105\u0011Yg\\^\u001fa\bN{\u0005(X\u0001`\u0012_m^k\u001fz\u0011NzXB!|\u0012_mB^\u001f5\f^{X\u001b\u0003z\u0015\u000bjI\u001b\u0003`\rG".length();
                    var1_4 = 63;
                    var0_5 = -1;
lbl22:
                    // 2 sources

                    while (true) {
                        v0 = 68;
                        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++] = ClusterPropertyCache.a(v0, ClusterPropertyCache.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;
            }
        }
        ClusterPropertyCache.f = var5;
        ClusterPropertyCache.a = Logger.getLogger(ClusterPropertyCache.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] ^ 0x68);
        }
        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 -> 127;
                    case 1 -> 41;
                    case 2 -> 81;
                    case 3 -> 37;
                    case 4 -> 111;
                    case 5 -> 76;
                    default -> 104;
                })));
                ++n3;
                n4 = n4;
            } while (n4 > n3);
        }
        return new String(cArray2).intern();
    }
}

