/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  com.l7tech.common.http.HttpMethod
 *  com.l7tech.gateway.common.service.PublishedService
 *  com.l7tech.gateway.common.service.PublishedService$WsdlStrategy
 *  com.l7tech.gateway.common.service.ServiceDocument
 *  com.l7tech.gateway.common.service.ServiceDocumentWsdlStrategy
 *  com.l7tech.gateway.common.service.ServiceTemplate
 *  com.l7tech.objectmodel.FindException
 *  com.l7tech.objectmodel.Goid
 *  com.l7tech.objectmodel.ObjectModelException
 *  com.l7tech.objectmodel.PersistentEntity
 *  com.l7tech.policy.Policy
 *  com.l7tech.util.ConfigFactory
 *  com.l7tech.util.ExceptionUtils
 *  org.jetbrains.annotations.NotNull
 *  org.springframework.context.ApplicationEvent
 */
package com.l7tech.server.service;

import com.l7tech.common.http.HttpMethod;
import com.l7tech.gateway.common.service.PublishedService;
import com.l7tech.gateway.common.service.ServiceDocument;
import com.l7tech.gateway.common.service.ServiceDocumentWsdlStrategy;
import com.l7tech.gateway.common.service.ServiceTemplate;
import com.l7tech.objectmodel.FindException;
import com.l7tech.objectmodel.Goid;
import com.l7tech.objectmodel.ObjectModelException;
import com.l7tech.objectmodel.PersistentEntity;
import com.l7tech.policy.Policy;
import com.l7tech.server.cluster.ClusterMaster;
import com.l7tech.server.event.AdminInfo;
import com.l7tech.server.event.system.LicenseChangeEvent;
import com.l7tech.server.event.system.Started;
import com.l7tech.server.folder.FolderManager;
import com.l7tech.server.policy.PolicyVersionManager;
import com.l7tech.server.policy.module.AssertionModuleRegistrationEvent;
import com.l7tech.server.service.ServiceDocumentManager;
import com.l7tech.server.service.ServiceManager;
import com.l7tech.server.service.ServiceTemplateManager;
import com.l7tech.server.service.af;
import com.l7tech.server.util.PostStartupApplicationListener;
import com.l7tech.util.ConfigFactory;
import com.l7tech.util.ExceptionUtils;
import java.io.IOException;
import java.util.Collection;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.logging.Logger;
import org.jetbrains.annotations.NotNull;
import org.springframework.context.ApplicationEvent;

public class ServiceTemplateBootstrapService
implements PostStartupApplicationListener {
    private static final Logger a;
    private static final Map<String, Goid> b;
    private static final AtomicBoolean c;
    private static String d;
    private final FolderManager e;
    private final ServiceManager f;
    private final ServiceDocumentManager g;
    private final PolicyVersionManager h;
    private final ServiceTemplateManager i;
    private final ClusterMaster j;
    private static final String[] k;

    static void a() {
        d = ConfigFactory.getProperty((String)k[1]);
    }

    public ServiceTemplateBootstrapService(FolderManager folderManager, ServiceManager serviceManager, ServiceDocumentManager serviceDocumentManager, PolicyVersionManager policyVersionManager, ServiceTemplateManager serviceTemplateManager, ClusterMaster clusterMaster) {
        this.e = folderManager;
        this.f = serviceManager;
        this.g = serviceDocumentManager;
        this.h = policyVersionManager;
        this.i = serviceTemplateManager;
        this.j = clusterMaster;
        this.init();
    }

    /*
     * Enabled force condition propagation
     * Lifted jumps to return sites
     */
    protected void init() {
        try {
            if (d == null) return;
            c.set(true);
            for (PublishedService publishedService : this.f.findAll()) {
                try {
                    if (!publishedService.isInternal()) continue;
                    b.put(publishedService.getName(), publishedService.getGoid());
                    a.info(publishedService.getName() + k[6]);
                }
                catch (FindException findException) {
                    throw ServiceTemplateBootstrapService.a((Exception)((Object)findException));
                    return;
                }
            }
        }
        catch (FindException findException) {
            a.warning(k[7] + ExceptionUtils.getMessageWithCause((Throwable)findException));
        }
    }

    public void onApplicationEvent(ApplicationEvent applicationEvent) {
        block7: {
            try {
                block6: {
                    try {
                        try {
                            if (applicationEvent instanceof LicenseChangeEvent || applicationEvent instanceof Started) break block6;
                        }
                        catch (IllegalArgumentException illegalArgumentException) {
                            throw ServiceTemplateBootstrapService.a(illegalArgumentException);
                        }
                        if (!(applicationEvent instanceof AssertionModuleRegistrationEvent)) break block7;
                    }
                    catch (IllegalArgumentException illegalArgumentException) {
                        throw ServiceTemplateBootstrapService.a(illegalArgumentException);
                    }
                }
                this.installBootstrapServices();
            }
            catch (IllegalArgumentException illegalArgumentException) {
                throw ServiceTemplateBootstrapService.a(illegalArgumentException);
            }
        }
    }

    protected void installBootstrapServices() {
        try {
            AdminInfo.find(false).wrapCallable(new af(this)).call();
        }
        catch (Exception exception) {
            a.warning(ExceptionUtils.getMessageWithCause((Throwable)exception));
        }
    }

    private Goid a(@NotNull ServiceTemplate serviceTemplate) {
        try {
            if (serviceTemplate == null) {
                ServiceTemplateBootstrapService.a(0);
            }
        }
        catch (ObjectModelException objectModelException) {
            throw ServiceTemplateBootstrapService.a((Exception)((Object)objectModelException));
        }
        try {
            UUID uUID;
            Policy policy;
            String[] stringArray;
            PublishedService publishedService;
            block16: {
                publishedService = new PublishedService();
                try {
                    publishedService.setFolder(this.e.findRootFolder());
                    publishedService.setName(serviceTemplate.getName());
                    publishedService.getPolicy().setXml(serviceTemplate.getDefaultPolicyXml());
                    publishedService.setRoutingUri(serviceTemplate.getDefaultUriPrefix());
                    publishedService.setSoap(serviceTemplate.isSoap());
                    if (!serviceTemplate.isSoap()) {
                        publishedService.setHttpMethods(EnumSet.of(HttpMethod.POST, HttpMethod.GET, HttpMethod.PUT, HttpMethod.DELETE));
                    }
                }
                catch (ObjectModelException objectModelException) {
                    throw ServiceTemplateBootstrapService.a((Exception)((Object)objectModelException));
                }
                publishedService.setInternal(true);
                publishedService.parseWsdlStrategy((PublishedService.WsdlStrategy)new ServiceDocumentWsdlStrategy((Collection)serviceTemplate.getServiceDocuments()));
                publishedService.setWsdlUrl(serviceTemplate.getServiceDescriptorUrl());
                publishedService.setWsdlXml(serviceTemplate.getServiceDescriptorXml());
                publishedService.setDisabled(false);
                stringArray = k;
                a.fine(stringArray[3]);
                policy = publishedService.getPolicy();
                try {
                    if (policy == null || policy.getGuid() != null) break block16;
                }
                catch (ObjectModelException objectModelException) {
                    throw ServiceTemplateBootstrapService.a((Exception)((Object)objectModelException));
                }
                uUID = UUID.randomUUID();
                policy.setGuid(uUID.toString());
            }
            publishedService.setTracingEnabled(false);
            uUID = this.f.save((PersistentEntity)publishedService);
            try {
                if (policy != null) {
                    this.h.checkpointPolicy(policy, true, true);
                }
            }
            catch (ObjectModelException objectModelException) {
                throw ServiceTemplateBootstrapService.a((Exception)((Object)objectModelException));
            }
            if (serviceTemplate.isSoap()) {
                Collection<ServiceDocument> collection = this.g.findByServiceId((Goid)uUID);
                for (ServiceDocument serviceDocument : collection) {
                    this.g.delete((PersistentEntity)serviceDocument);
                }
                for (ServiceDocument serviceDocument : serviceTemplate.getServiceDocuments()) {
                    serviceDocument.setGoid(ServiceDocument.DEFAULT_GOID);
                    serviceDocument.setServiceId((Goid)uUID);
                    this.g.save((PersistentEntity)serviceDocument);
                }
            }
            this.f.createRoles((PersistentEntity)publishedService);
            stringArray = k;
            a.info(stringArray[0] + serviceTemplate.getName() + stringArray[2]);
            return publishedService.getGoid();
        }
        catch (ObjectModelException | IOException throwable) {
            String[] stringArray = k;
            a.warning(stringArray[12] + serviceTemplate.getName() + stringArray[10] + throwable.getMessage());
            return null;
        }
    }

    /*
     * Unable to fully structure code
     */
    static {
        block10: {
            block9: {
                var5 = new String[13];
                var3_1 = 0;
                var2_2 = "+}]c`R!\u000e3Zc`J)\u0001v\t\u0019\u0000|FraH2\u0003c\u0007`}P$\u0007a\u0007uwN6\u000bpLu\bBp[csH%\u0006\u001b1r_o|[`\fv^&BI\"\u000ezZnwX\u0013\u0007a_oqY9\u0001|D)~\u000b4\u0007pA)aY2\u0014v[)aY2\u0014zJc=o%\u0010e@ewh%\u000fcEgfY\u0002\r|]ufN!\u0012@LtdU#\u0007\u0016\u000b}ZrsP,6vDv~]4\u0007@LtdU#\u0007!N3`hfY2\frE&aY2\u0014zJc2],\u0010vHbk\u001c%\u001azZra\u0018'a[i`\u001c&\u000b}Mo|[`\u0011v[p{_%\u0011)\t\b\u0016vDv~]4\u0007>#aNs\u007fY.\u00163Oi`\u001c\u0000,|]HgP,BcHtsQ%\u0016v[&5\u00193E3F`2\u00193L6Z&\u007fI3\u00163Gif\u001c\"\u00073Gs~P\u000bBpHsaY$BqP<";
                var4_3 = "+}]c`R!\u000e3Zc`J)\u0001v\t\u0019\u0000|FraH2\u0003c\u0007`}P$\u0007a\u0007uwN6\u000bpLu\bBp[csH%\u0006\u001b1r_o|[`\fv^&BI\"\u000ezZnwX\u0013\u0007a_oqY9\u0001|D)~\u000b4\u0007pA)aY2\u0014v[)aY2\u0014zJc=o%\u0010e@ewh%\u000fcEgfY\u0002\r|]ufN!\u0012@LtdU#\u0007\u0016\u000b}ZrsP,6vDv~]4\u0007@LtdU#\u0007!N3`hfY2\frE&aY2\u0014zJc2],\u0010vHbk\u001c%\u001azZra\u0018'a[i`\u001c&\u000b}Mo|[`\u0011v[p{_%\u0011)\t\b\u0016vDv~]4\u0007>#aNs\u007fY.\u00163Oi`\u001c\u0000,|]HgP,BcHtsQ%\u0016v[&5\u00193E3F`2\u00193L6Z&\u007fI3\u00163Gif\u001c\"\u00073Gs~P\u000bBpHsaY$BqP<".length();
                var1_4 = 17;
                var0_5 = -1;
lbl7:
                // 2 sources

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

                while (true) {
                    var5[var3_1++] = ServiceTemplateBootstrapService.a(v0, ServiceTemplateBootstrapService.a(v2));
                    if ((var0_5 += var1_4) < var4_3) {
                        var1_4 = var2_2.charAt(var0_5);
                        ** continue;
                    }
                    var2_2 = "c\u001f%\u0011\u0002+Q`\u0000d\u0003\u001e3Gd\u0002d\u0016\u0014-Uh\u0013/\u0016 D\u00028\n\u0003\u007f@s\u0015+\u0011\u00181D!\u0019$\u0011\u0014-M`\u001cj\u0016\u0014-Uh\u0013/E";
                    var4_3 = "c\u001f%\u0011\u0002+Q`\u0000d\u0003\u001e3Gd\u0002d\u0016\u0014-Uh\u0013/\u0016 D\u00028\n\u0003\u007f@s\u0015+\u0011\u00181D!\u0019$\u0011\u0014-M`\u001cj\u0016\u0014-Uh\u0013/E".length();
                    var1_4 = 25;
                    var0_5 = -1;
lbl22:
                    // 2 sources

                    while (true) {
                        v0 = 81;
                        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++] = ServiceTemplateBootstrapService.a(v0, ServiceTemplateBootstrapService.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;
            }
        }
        ServiceTemplateBootstrapService.k = var5;
        ServiceTemplateBootstrapService.a = Logger.getLogger(ServiceTemplateBootstrapService.class.getName());
        ServiceTemplateBootstrapService.b = new HashMap<String, Goid>();
        ServiceTemplateBootstrapService.c = new AtomicBoolean();
        ServiceTemplateBootstrapService.d = ConfigFactory.getProperty((String)ServiceTemplateBootstrapService.k[11]);
    }

    private static /* synthetic */ void a(int n2) {
        String[] stringArray = k;
        throw new IllegalArgumentException(String.format(stringArray[9], stringArray[8], stringArray[4], stringArray[5]));
    }

    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] ^ 0x72);
        }
        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 -> 80;
                    case 1 -> 33;
                    case 2 -> 27;
                    case 3 -> 52;
                    case 4 -> 32;
                    case 5 -> 14;
                    default -> 114;
                })));
                ++n3;
                n4 = n4;
            } while (n4 > n3);
        }
        return new String(cArray2).intern();
    }
}

