/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  com.l7tech.gateway.common.licensing.LicenseDocument
 *  com.l7tech.objectmodel.ObjectModelException
 *  com.l7tech.util.ConfigFactory
 *  com.l7tech.util.ExceptionUtils
 *  com.l7tech.util.HexUtils
 *  org.apache.commons.lang3.StringUtils
 *  org.springframework.context.ApplicationEvent
 */
package com.l7tech.server.licensing;

import com.l7tech.gateway.common.licensing.LicenseDocument;
import com.l7tech.objectmodel.ObjectModelException;
import com.l7tech.server.event.AdminInfo;
import com.l7tech.server.event.system.Initialized;
import com.l7tech.server.licensing.LicenseDocumentManager;
import com.l7tech.server.licensing.c;
import com.l7tech.server.util.PostStartupApplicationListener;
import com.l7tech.util.ConfigFactory;
import com.l7tech.util.ExceptionUtils;
import com.l7tech.util.HexUtils;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.logging.Logger;
import java.util.zip.GZIPInputStream;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.ApplicationEvent;

public class BootstrapLicenseService
implements PostStartupApplicationListener {
    private final String a;
    private final String b;
    String c;
    private LicenseDocumentManager d;
    private final Logger e;
    private static final String[] f;

    public BootstrapLicenseService(LicenseDocumentManager licenseDocumentManager) {
        String string;
        block4: {
            block3: {
                this.e = Logger.getLogger(this.getClass().getName());
                this.d = licenseDocumentManager;
                String[] stringArray = f;
                this.a = ConfigFactory.getProperty((String)stringArray[9]);
                this.b = ConfigFactory.getProperty((String)stringArray[1]);
                if (this.b != null) {
                    try {
                        if (this.b.trim().length() <= 0) break block3;
                        string = System.getenv(this.b);
                        break block4;
                    }
                    catch (IllegalStateException illegalStateException) {
                        throw BootstrapLicenseService.a(illegalStateException);
                    }
                }
            }
            string = null;
        }
        this.c = string;
    }

    public void onApplicationEvent(ApplicationEvent applicationEvent) {
        try {
            if (applicationEvent instanceof Initialized) {
                this.a();
            }
        }
        catch (IllegalStateException illegalStateException) {
            throw BootstrapLicenseService.a(illegalStateException);
        }
    }

    private void a() {
        block23: {
            ArrayList<LicenseDocument> arrayList;
            block21: {
                try {
                    if (!this.d.findAll().isEmpty()) {
                        this.e.fine(f[12]);
                        return;
                    }
                }
                catch (IllegalStateException illegalStateException) {
                    throw BootstrapLicenseService.a(illegalStateException);
                }
                catch (ObjectModelException objectModelException) {
                    String[] stringArray = f;
                    throw new IllegalStateException(stringArray[8] + ExceptionUtils.getMessageWithCause((Throwable)objectModelException), objectModelException);
                }
                arrayList = new ArrayList<LicenseDocument>();
                try {
                    String[] stringArray;
                    Object object;
                    block22: {
                        block20: {
                            try {
                                if (!ConfigFactory.getBooleanProperty((String)f[6], (boolean)false) || !StringUtils.isNotBlank((CharSequence)this.c)) break block20;
                            }
                            catch (ObjectModelException objectModelException) {
                                throw BootstrapLicenseService.a((Exception)((Object)objectModelException));
                            }
                            object = HexUtils.decodeBase64((String)this.c, (boolean)true);
                            stringArray = f;
                            this.e.info(stringArray[7] + this.b + stringArray[4]);
                            arrayList.add(this.a(new GZIPInputStream(new ByteArrayInputStream((byte[])object))));
                        }
                        if (this.a == null) break block21;
                        object = new File(this.a);
                        try {
                            if (!((File)object).exists() || !((File)object).isDirectory()) break block22;
                        }
                        catch (ObjectModelException objectModelException) {
                            throw BootstrapLicenseService.a((Exception)((Object)objectModelException));
                        }
                        File[] fileArray = ((File)object).listFiles();
                        try {
                            if (fileArray == null || fileArray.length <= 0) break block21;
                        }
                        catch (ObjectModelException objectModelException) {
                            throw BootstrapLicenseService.a((Exception)((Object)objectModelException));
                        }
                        for (File file : fileArray) {
                            try {
                                if (file.isFile()) {
                                    this.e.info(f[2] + file.getCanonicalPath());
                                    arrayList.add(this.a(new FileInputStream(file)));
                                    continue;
                                }
                            }
                            catch (ObjectModelException objectModelException) {
                                throw BootstrapLicenseService.a((Exception)((Object)objectModelException));
                            }
                            stringArray = f;
                            this.e.info(stringArray[0] + file.getPath());
                        }
                        break block21;
                    }
                    stringArray = f;
                    this.e.info(stringArray[11] + ((File)object).getPath());
                }
                catch (Exception exception) {
                    String[] stringArray = f;
                    throw new IllegalStateException(stringArray[5] + ExceptionUtils.getMessageWithCause((Throwable)exception), exception);
                }
            }
            try {
                try {
                    if (!arrayList.isEmpty() || !ConfigFactory.getBooleanProperty((String)f[3], (boolean)false)) break block23;
                }
                catch (ObjectModelException objectModelException) {
                    throw BootstrapLicenseService.a((Exception)((Object)objectModelException));
                }
                throw new IllegalStateException(f[10]);
            }
            catch (ObjectModelException objectModelException) {
                throw BootstrapLicenseService.a((Exception)((Object)objectModelException));
            }
        }
    }

    private LicenseDocument a(InputStream inputStream) throws Exception {
        try {
            LicenseDocument licenseDocument = AdminInfo.find(false).wrapCallable(new c(this, inputStream)).call();
            return licenseDocument;
        }
        finally {
            inputStream.close();
        }
    }

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

    /*
     * Unable to fully structure code
     */
    static {
        block10: {
            block9: {
                var5 = new String[13];
                var3_1 = 0;
                var2_2 = "{7\u001f,\\|\u001c\b0\u0019=Hp\u0016O|\u0010.CtB\b\u001eJ3\u0019(_m\nI,X9BoVD5\u00159Bj\u001d\u0006*\u0017.Ex\u001aD9\u0019a2\u0005(Mu\u0014A2\u0011|@p\u001bM2\u00059\f\u007f\nG1L|\u0019J3\u0019(_m\nI,X0Ez\u001dF/\u0013r^|\t]5\u00049\u0015\b9\u0018*Ek\u0017F1\u00132X9\u000eI.\u001f=Nu\u001d\u0019n=\u001f0\fm\u0017\b5\u0018/Xx\u0014D|\u001a5O|\u0016[9L|\u001cJ3\u0019(_m\nI,X9BoVD5\u00159Bj\u001d\u00069\u0018=Nu\u001d\u0018a2\u0005(Mu\u0014A2\u0011|@p\u001bM2\u00059\f\u007f\nG1V\u0019}2\u0017>@|X\\3V0Cx\u001c\b0\u001f?Iw\u000bM/L|\u0018J3\u0019(_m\nI,X:Cu\u001cM.X0Ez\u001dF/\u0013\u001bf3V0Ez\u001dF/\u0013|@v\u0019L9\u0012r\f\\\u0000A(\u001f2K7";
                var4_3 = "{7\u001f,\\|\u001c\b0\u0019=Hp\u0016O|\u0010.CtB\b\u001eJ3\u0019(_m\nI,X9BoVD5\u00159Bj\u001d\u0006*\u0017.Ex\u001aD9\u0019a2\u0005(Mu\u0014A2\u0011|@p\u001bM2\u00059\f\u007f\nG1L|\u0019J3\u0019(_m\nI,X0Ez\u001dF/\u0013r^|\t]5\u00049\u0015\b9\u0018*Ek\u0017F1\u00132X9\u000eI.\u001f=Nu\u001d\u0019n=\u001f0\fm\u0017\b5\u0018/Xx\u0014D|\u001a5O|\u0016[9L|\u001cJ3\u0019(_m\nI,X9BoVD5\u00159Bj\u001d\u00069\u0018=Nu\u001d\u0018a2\u0005(Mu\u0014A2\u0011|@p\u001bM2\u00059\f\u007f\nG1V\u0019}2\u0017>@|X\\3V0Cx\u001c\b0\u001f?Iw\u000bM/L|\u0018J3\u0019(_m\nI,X:Cu\u001cM.X0Ez\u001dF/\u0013\u001bf3V0Ez\u001dF/\u0013|@v\u0019L9\u0012r\f\\\u0000A(\u001f2K7".length();
                var1_4 = 22;
                var0_5 = -1;
lbl7:
                // 2 sources

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

                while (true) {
                    var5[var3_1++] = BootstrapLicenseService.a(v0, BootstrapLicenseService.a(v2));
                    if ((var0_5 += var1_4) < var4_3) {
                        var1_4 = var2_2.charAt(var0_5);
                        ** continue;
                    }
                    var2_2 = "\u0001Pp\\'\u000fxm]zK,\u001fi\"Kj\u0019'\u0013im_|L'\u0018'm$\u0001Pp\\'\u000fxmX\u007fK,\u001dy4\u0019zW:\b|!Uv]g\\N&PcI \u0012zc";
                    var4_3 = "\u0001Pp\\'\u000fxm]zK,\u001fi\"Kj\u0019'\u0013im_|L'\u0018'm$\u0001Pp\\'\u000fxmX\u007fK,\u001dy4\u0019zW:\b|!Uv]g\\N&PcI \u0012zc".length();
                    var1_4 = 29;
                    var0_5 = -1;
lbl22:
                    // 2 sources

                    while (true) {
                        v0 = 13;
                        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++] = BootstrapLicenseService.a(v0, BootstrapLicenseService.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;
            }
        }
        BootstrapLicenseService.f = var5;
    }

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

