/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  com.l7tech.gateway.common.cluster.ServiceUsage
 *  com.l7tech.gateway.common.service.ServiceStatistics
 *  com.l7tech.objectmodel.FindException
 *  com.l7tech.objectmodel.Goid
 *  com.l7tech.util.Config
 *  com.l7tech.util.ExceptionUtils
 *  org.hibernate.FlushMode
 *  org.hibernate.HibernateException
 *  org.hibernate.Session
 *  org.hibernate.query.Query
 *  org.springframework.beans.factory.annotation.Autowired
 *  org.springframework.context.ApplicationContext
 *  org.springframework.context.ApplicationEvent
 *  org.springframework.dao.DataAccessException
 *  org.springframework.orm.hibernate5.HibernateCallback
 *  org.springframework.orm.hibernate5.support.HibernateDaoSupport
 *  org.springframework.transaction.PlatformTransactionManager
 *  org.springframework.transaction.annotation.Propagation
 *  org.springframework.transaction.annotation.Transactional
 */
package com.l7tech.server.cluster;

import com.l7tech.gateway.common.cluster.ServiceUsage;
import com.l7tech.gateway.common.service.ServiceStatistics;
import com.l7tech.objectmodel.FindException;
import com.l7tech.objectmodel.Goid;
import com.l7tech.server.HibernateEntityManager;
import com.l7tech.server.cluster.ServiceUsageManager;
import com.l7tech.server.cluster.aa;
import com.l7tech.server.cluster.q;
import com.l7tech.server.cluster.r;
import com.l7tech.server.event.system.ReadyForMessages;
import com.l7tech.server.event.system.Stopping;
import com.l7tech.server.service.ServiceCache;
import com.l7tech.server.util.ManagedTimer;
import com.l7tech.server.util.PostStartupApplicationListener;
import com.l7tech.util.Config;
import com.l7tech.util.ExceptionUtils;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.TimerTask;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Supplier;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.hibernate.FlushMode;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.query.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationEvent;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.hibernate5.HibernateCallback;
import org.springframework.orm.hibernate5.support.HibernateDaoSupport;
import org.springframework.transaction.PlatformTransactionManager;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

public class MysqlServiceUsageManager
extends HibernateDaoSupport
implements ServiceUsageManager,
PropertyChangeListener,
PostStartupApplicationListener {
    private static final Logger a;
    private static final int b = 300000;
    private static final int c = 4000;
    private static final int d = 86400000;
    private static final int e = 8053;
    private static final String f;
    static final String g;
    static final String h;
    static final String i;
    private static final String j;
    private static final String k;
    private static final String l;
    static final int m = 0;
    private final String n;
    private final AtomicBoolean o = new AtomicBoolean();
    private Supplier<ServiceCache> p;
    private final Config q;
    private aa r;
    private final ApplicationContext s;
    private ManagedTimer t;
    private PlatformTransactionManager u;
    private static final String[] v;

    @Autowired
    public MysqlServiceUsageManager(ApplicationContext applicationContext, Config config, String string) {
        this.n = string;
        this.q = config;
        this.s = applicationContext;
        this.c();
        this.p = new q(this, applicationContext);
        String[] stringArray = v;
        this.t = (ManagedTimer)applicationContext.getBean(stringArray[17], ManagedTimer.class);
        this.u = (PlatformTransactionManager)applicationContext.getBean(stringArray[19], PlatformTransactionManager.class);
        this.r = new aa(this, applicationContext, this.getHibernateTemplate(), this.u, string);
    }

    /*
     * Loose catch block
     */
    @Override
    @Transactional(propagation=Propagation.REQUIRED, rollbackFor={Throwable.class}, readOnly=true)
    public Collection<ServiceUsage> getAll() throws FindException {
        HashSet<ServiceUsage> hashSet;
        FlushMode flushMode;
        Session session;
        block12: {
            HashSet<ServiceUsage> hashSet2;
            session = null;
            flushMode = null;
            session = HibernateEntityManager.useCurrentOrOpenNewSession(this.getSessionFactory());
            flushMode = session.getHibernateFlushMode();
            session.setHibernateFlushMode(FlushMode.MANUAL);
            List list = session.createQuery(k).list();
            hashSet = hashSet2 = new HashSet<ServiceUsage>(list);
            if (session == null) break block12;
            try {
                block14: {
                    if (flushMode == null) break block12;
                    break block14;
                    catch (HibernateException hibernateException) {
                        throw MysqlServiceUsageManager.a(hibernateException);
                    }
                }
                session.setHibernateFlushMode(flushMode);
            }
            catch (HibernateException hibernateException) {
                throw MysqlServiceUsageManager.a(hibernateException);
            }
        }
        HibernateEntityManager.closeNewSession(session);
        return hashSet;
        catch (HibernateException hibernateException) {
            try {
                throw new FindException(v[6], (Throwable)hibernateException);
            }
            catch (Throwable throwable) {
                block13: {
                    try {
                        try {
                            if (session == null || flushMode == null) break block13;
                        }
                        catch (HibernateException hibernateException2) {
                            throw MysqlServiceUsageManager.a(hibernateException2);
                        }
                        session.setHibernateFlushMode(flushMode);
                    }
                    catch (HibernateException hibernateException3) {
                        throw MysqlServiceUsageManager.a(hibernateException3);
                    }
                }
                HibernateEntityManager.closeNewSession(session);
                throw throwable;
            }
        }
    }

    /*
     * Loose catch block
     */
    @Override
    @Transactional(propagation=Propagation.REQUIRED, rollbackFor={Throwable.class}, readOnly=true)
    public ServiceUsage[] findByNode(String string) throws FindException {
        ServiceUsage[] serviceUsageArray;
        FlushMode flushMode;
        Session session;
        block12: {
            session = null;
            flushMode = null;
            session = HibernateEntityManager.useCurrentOrOpenNewSession(this.getSessionFactory());
            flushMode = session.getHibernateFlushMode();
            session.setHibernateFlushMode(FlushMode.MANUAL);
            Query query = session.createQuery(i);
            String[] stringArray = v;
            query.setParameter(stringArray[9], (Object)string);
            List list = query.list();
            serviceUsageArray = list.toArray(new ServiceUsage[list.size()]);
            if (session == null) break block12;
            try {
                block14: {
                    if (flushMode == null) break block12;
                    break block14;
                    catch (HibernateException hibernateException) {
                        throw MysqlServiceUsageManager.a(hibernateException);
                    }
                }
                session.setHibernateFlushMode(flushMode);
            }
            catch (HibernateException hibernateException) {
                throw MysqlServiceUsageManager.a(hibernateException);
            }
        }
        HibernateEntityManager.closeNewSession(session);
        return serviceUsageArray;
        catch (HibernateException hibernateException) {
            try {
                throw new FindException(v[14] + string, (Throwable)hibernateException);
            }
            catch (Throwable throwable) {
                block13: {
                    try {
                        try {
                            if (session == null || flushMode == null) break block13;
                        }
                        catch (HibernateException hibernateException2) {
                            throw MysqlServiceUsageManager.a(hibernateException2);
                        }
                        session.setHibernateFlushMode(flushMode);
                    }
                    catch (HibernateException hibernateException3) {
                        throw MysqlServiceUsageManager.a(hibernateException3);
                    }
                }
                HibernateEntityManager.closeNewSession(session);
                throw throwable;
            }
        }
    }

    /*
     * Loose catch block
     */
    @Override
    @Transactional(propagation=Propagation.REQUIRED, rollbackFor={Throwable.class}, readOnly=true)
    public ServiceUsage[] findByServiceGoid(Goid goid) throws FindException {
        ServiceUsage[] serviceUsageArray;
        FlushMode flushMode;
        Session session;
        block12: {
            session = null;
            flushMode = null;
            session = HibernateEntityManager.useCurrentOrOpenNewSession(this.getSessionFactory());
            flushMode = session.getHibernateFlushMode();
            session.setHibernateFlushMode(FlushMode.MANUAL);
            Query query = session.createQuery(l);
            String[] stringArray = v;
            query.setParameter(stringArray[15], (Object)goid);
            List list = query.list();
            serviceUsageArray = list.toArray(new ServiceUsage[list.size()]);
            if (session == null) break block12;
            try {
                block14: {
                    if (flushMode == null) break block12;
                    break block14;
                    catch (HibernateException hibernateException) {
                        throw MysqlServiceUsageManager.a(hibernateException);
                    }
                }
                session.setHibernateFlushMode(flushMode);
            }
            catch (HibernateException hibernateException) {
                throw MysqlServiceUsageManager.a(hibernateException);
            }
        }
        HibernateEntityManager.closeNewSession(session);
        return serviceUsageArray;
        catch (HibernateException hibernateException) {
            try {
                throw new FindException(v[12] + String.valueOf(goid), (Throwable)hibernateException);
            }
            catch (Throwable throwable) {
                block13: {
                    try {
                        try {
                            if (session == null || flushMode == null) break block13;
                        }
                        catch (HibernateException hibernateException2) {
                            throw MysqlServiceUsageManager.a(hibernateException2);
                        }
                        session.setHibernateFlushMode(flushMode);
                    }
                    catch (HibernateException hibernateException3) {
                        throw MysqlServiceUsageManager.a(hibernateException3);
                    }
                }
                HibernateEntityManager.closeNewSession(session);
                throw throwable;
            }
        }
    }

    @Override
    @Transactional(propagation=Propagation.REQUIRED, rollbackFor={Throwable.class})
    public void record(ServiceUsage serviceUsage) {
        try {
            this.getHibernateTemplate().saveOrUpdate((Object)serviceUsage);
        }
        catch (DataAccessException dataAccessException) {
            a.log(Level.SEVERE, v[0], ExceptionUtils.getDebugException((Throwable)dataAccessException));
        }
    }

    @Override
    @Transactional(propagation=Propagation.REQUIRED, rollbackFor={Throwable.class})
    public void clear(String string) {
        try {
            this.getHibernateTemplate().execute((HibernateCallback)new r(this, string));
        }
        catch (DataAccessException dataAccessException) {
            a.log(Level.SEVERE, v[2], ExceptionUtils.getDebugException((Throwable)dataAccessException));
        }
    }

    public List<ServiceUsage> getServiceUsageFromCache(String string) {
        Collection<ServiceStatistics> collection;
        ArrayList<ServiceUsage> arrayList;
        block5: {
            arrayList = new ArrayList<ServiceUsage>();
            collection = this.p.get().getAllServiceStatistics();
            try {
                try {
                    if (collection != null && !collection.isEmpty()) break block5;
                }
                catch (HibernateException hibernateException) {
                    throw MysqlServiceUsageManager.a(hibernateException);
                }
                return arrayList;
            }
            catch (HibernateException hibernateException) {
                throw MysqlServiceUsageManager.a(hibernateException);
            }
        }
        for (ServiceStatistics serviceStatistics : collection) {
            arrayList.add(ServiceUsage.fromStat((ServiceStatistics)serviceStatistics, (String)string));
        }
        return arrayList;
    }

    void a(Supplier<ServiceCache> supplier) {
        this.p = supplier;
    }

    @Override
    public boolean isEnabled() {
        return this.o.get();
    }

    @Override
    public void propertyChange(PropertyChangeEvent propertyChangeEvent) {
        try {
            if (v[8].equals(propertyChangeEvent.getPropertyName())) {
                this.a();
            }
        }
        catch (HibernateException hibernateException) {
            throw MysqlServiceUsageManager.a(hibernateException);
        }
    }

    public void onApplicationEvent(ApplicationEvent applicationEvent) {
        block6: {
            block5: {
                try {
                    if (!(applicationEvent instanceof ReadyForMessages)) break block5;
                    this.a();
                    break block6;
                }
                catch (HibernateException hibernateException) {
                    throw MysqlServiceUsageManager.a(hibernateException);
                }
            }
            try {
                if (applicationEvent instanceof Stopping) {
                    this.b();
                }
            }
            catch (HibernateException hibernateException) {
                throw MysqlServiceUsageManager.a(hibernateException);
            }
        }
    }

    private void a() {
        block11: {
            block10: {
                String[] stringArray = v;
                long l2 = this.q.getTimeUnitProperty(stringArray[8], 300000L);
                try {
                    if (l2 == 0L) {
                        this.b();
                        return;
                    }
                }
                catch (HibernateException hibernateException) {
                    throw MysqlServiceUsageManager.a(hibernateException);
                }
                try {
                    if (this.r != null) {
                        this.r.cancel();
                    }
                }
                catch (HibernateException hibernateException) {
                    throw MysqlServiceUsageManager.a(hibernateException);
                }
                try {
                    try {
                        this.r = new aa(this, this.s, this.getHibernateTemplate(), this.u, this.n);
                        if (l2 < 4000L || l2 > 86400000L) break block10;
                    }
                    catch (HibernateException hibernateException) {
                        throw MysqlServiceUsageManager.a(hibernateException);
                    }
                    this.t.schedule((TimerTask)this.r, 8053L, l2);
                    break block11;
                }
                catch (HibernateException hibernateException) {
                    throw MysqlServiceUsageManager.a(hibernateException);
                }
            }
            a.log(Level.WARNING, v[4]);
            this.t.schedule((TimerTask)this.r, 8053L, 4000L);
        }
        this.o.set(true);
    }

    private void b() {
        block4: {
            block5: {
                try {
                    try {
                        if (!this.o.get()) break block4;
                        if (this.r == null) break block5;
                    }
                    catch (HibernateException hibernateException) {
                        throw MysqlServiceUsageManager.a(hibernateException);
                    }
                    this.r.cancel();
                }
                catch (HibernateException hibernateException) {
                    throw MysqlServiceUsageManager.a(hibernateException);
                }
            }
            this.r = new aa(this, this.s, this.getHibernateTemplate(), this.u, this.n);
            this.t.schedule((TimerTask)this.r, 100L);
            this.o.set(false);
        }
    }

    private void c() {
        block3: {
            block2: {
                long l2 = this.q.getTimeUnitProperty(v[20], 300000L);
                try {
                    if (l2 != 0L) break block2;
                    this.o.set(false);
                    break block3;
                }
                catch (HibernateException hibernateException) {
                    throw MysqlServiceUsageManager.a(hibernateException);
                }
            }
            this.o.set(true);
        }
    }

    /*
     * Unable to fully structure code
     */
    static {
        block10: {
            block9: {
                var5 = new String[21];
                var3_1 = 0;
                var2_2 = "}LOZE\u001c@QW\u001aDD_ALG\u001aEDNXW@_\u0016TOOYF%\u001etrssy\u000eMFH@H_KaVIWFY\u0000PL^SHX\u000e\u0003\u0003\u0000XNXKWG.}LOZE\u001c@QW\u001aUMYOL\u0003_NHOZWM]\u0016RY\\HJYS\u0001I]_D_\u0016SYMQQ^E\u001cxqu{\u0001OKLUSUDc[MB]S\u0001u`\u001e`vwro\u000eewMLWMUJ\u001eSHYQY\\JZ\u001a@@P[[\u0003MWR\u001cKPW_DDX\u000eXLH\u0016RY\\HJYStOOYF\u0014CQXOJFsXUY\\HBV\u0018\u0001i]WM]\u0016LU@WNO[\u0001JORV_\u0016NZ\u000e\n\u0003ISBS@ZP\u0014\rMFH@H_KaVIWFY(}LOZE\u001c@QW\u001aDDH\\WFLS\u0001OKLUSUD\u001c[MB]S\u0001NK]LHRR\u001cxqu{\u0001OKLUSUDc[MB]S\u0001u`\u001e`vwro\u000e\u001bMFH@H_KkP[QD\u0012[NG[BDu@JFH@@P\u0006PL^SHX6\u001ebi\u0016RY\\HJYS~I]_D_\u0016vtklf\u001aEDNXW@_iTOOYF\u0014XNXKWG\u001a\u000b\u0001\u0006@QG__E\tMFH@H_KWG:}LOZER\tJ\u0003HSUNG[U_\u0016RY\\HJYS\u0001I]_D_\u0016SYMQQ^E\u0001ZAL\u0003ISSJG]F\u001aQNUJ\u0004\u0003\u0006PL^SHX4}LOZER\tJ\u0003HSUNG[U_\u0016RY\\HJYS\u0001I]_D_\u0016SYMQQ^E\u0001ZAL\u0003TYEYgZ\u0019\u001a\tMFH@H_KWG\u001d\u001etrssy\u000eMFH@H_KWG\u001a\u000b\u0001\u0006][QL_BYGZ\u0016SBTWFYJ|BY]FNAKM^bHQKL\fzfvsuy\u000exqu{\u0001";
                var4_3 = "}LOZE\u001c@QW\u001aDD_ALG\u001aEDNXW@_\u0016TOOYF%\u001etrssy\u000eMFH@H_KaVIWFY\u0000PL^SHX\u000e\u0003\u0003\u0000XNXKWG.}LOZE\u001c@QW\u001aUMYOL\u0003_NHOZWM]\u0016RY\\HJYS\u0001I]_D_\u0016SYMQQ^E\u001cxqu{\u0001OKLUSUDc[MB]S\u0001u`\u001e`vwro\u000eewMLWMUJ\u001eSHYQY\\JZ\u001a@@P[[\u0003MWR\u001cKPW_DDX\u000eXLH\u0016RY\\HJYStOOYF\u0014CQXOJFsXUY\\HBV\u0018\u0001i]WM]\u0016LU@WNO[\u0001JORV_\u0016NZ\u000e\n\u0003ISBS@ZP\u0014\rMFH@H_KaVIWFY(}LOZE\u001c@QW\u001aDDH\\WFLS\u0001OKLUSUD\u001c[MB]S\u0001NK]LHRR\u001cxqu{\u0001OKLUSUDc[MB]S\u0001u`\u001e`vwro\u000e\u001bMFH@H_KkP[QD\u0012[NG[BDu@JFH@@P\u0006PL^SHX6\u001ebi\u0016RY\\HJYS~I]_D_\u0016vtklf\u001aEDNXW@_iTOOYF\u0014XNXKWG\u001a\u000b\u0001\u0006@QG__E\tMFH@H_KWG:}LOZER\tJ\u0003HSUNG[U_\u0016RY\\HJYS\u0001I]_D_\u0016SYMQQ^E\u0001ZAL\u0003ISSJG]F\u001aQNUJ\u0004\u0003\u0006PL^SHX4}LOZER\tJ\u0003HSUNG[U_\u0016RY\\HJYS\u0001I]_D_\u0016SYMQQ^E\u0001ZAL\u0003TYEYgZ\u0019\u001a\tMFH@H_KWG\u001d\u001etrssy\u000eMFH@H_KWG\u001a\u000b\u0001\u0006][QL_BYGZ\u0016SBTWFYJ|BY]FNAKM^bHQKL\fzfvsuy\u000exqu{\u0001".length();
                var1_4 = 30;
                var0_5 = -1;
lbl7:
                // 2 sources

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

                while (true) {
                    var5[var3_1++] = MysqlServiceUsageManager.a(v0, MysqlServiceUsageManager.a(v2));
                    if ((var0_5 += var1_4) < var4_3) {
                        var1_4 = var2_2.charAt(var0_5);
                        ** continue;
                    }
                    var2_2 = "\u001e\u0005\u000f\f\u0006\t\u0019\u001e\u001e\u0001\f8\t\u0014\u000b\u0010\u000b\u0010\u001b\u0019\u0012\u001c\u0014\u001c\u000b\u001f?\u0004\u000f\u0005\u0010F\u000f\u001a\u0013\u000f\u0016\u0010!\u0014\u001e\u0012\u001c\u0014\u0014\u0004";
                    var4_3 = "\u001e\u0005\u000f\f\u0006\t\u0019\u001e\u001e\u0001\f8\t\u0014\u000b\u0010\u000b\u0010\u001b\u0019\u0012\u001c\u0014\u001c\u000b\u001f?\u0004\u000f\u0005\u0010F\u000f\u001a\u0013\u000f\u0016\u0010!\u0014\u001e\u0012\u001c\u0014\u0014\u0004".length();
                    var1_4 = 18;
                    var0_5 = -1;
lbl22:
                    // 2 sources

                    while (true) {
                        v0 = 9;
                        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++] = MysqlServiceUsageManager.a(v0, MysqlServiceUsageManager.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;
            }
        }
        MysqlServiceUsageManager.v = var5;
        MysqlServiceUsageManager.f = MysqlServiceUsageManager.v[5];
        MysqlServiceUsageManager.g = MysqlServiceUsageManager.v[13];
        MysqlServiceUsageManager.h = MysqlServiceUsageManager.v[11];
        MysqlServiceUsageManager.a = Logger.getLogger(MysqlServiceUsageManager.class.getName());
        var0_6 = MysqlServiceUsageManager.v;
        MysqlServiceUsageManager.i = var0_6[7] + ServiceUsage.class.getName() + var0_6[1];
        MysqlServiceUsageManager.j = var0_6[18] + ServiceUsage.class.getName() + var0_6[10];
        MysqlServiceUsageManager.k = var0_6[3] + ServiceUsage.class.getName();
        MysqlServiceUsageManager.l = var0_6[3] + ServiceUsage.class.getName() + var0_6[16];
    }

    private static HibernateException a(HibernateException hibernateException) {
        return hibernateException;
    }

    private static char[] a(String string) {
        char[] cArray = string.toCharArray();
        if (cArray.length < 2) {
            cArray = cArray;
            cArray[0] = (char)(cArray[0] ^ 0x73);
        }
        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 -> 99;
                    case 1 -> 126;
                    case 2 -> 103;
                    case 3 -> 107;
                    case 4 -> 124;
                    case 5 -> 97;
                    default -> 115;
                })));
                ++n3;
                n4 = n4;
            } while (n4 > n3);
        }
        return new String(cArray2).intern();
    }
}

