/*
 * Decompiled with CFR 0.152.
 * 
 * Could not load the following classes:
 *  javax.jms.Destination
 *  javax.jms.JMSException
 *  javax.jms.MessageConsumer
 *  javax.jms.MessageProducer
 *  javax.jms.Queue
 *  javax.jms.Session
 */
package com.l7tech.server.transport.jms2;

import com.l7tech.server.transport.jms.JmsBag;
import com.l7tech.server.transport.jms.JmsConfigException;
import com.l7tech.server.transport.jms.JmsRuntimeException;
import com.l7tech.server.transport.jms.JmsUtil;
import com.l7tech.server.transport.jms2.JmsEndpointConfig;
import com.l7tech.server.transport.jms2.JmsResourceManager;
import com.l7tech.server.transport.jms2.SessionHolder;
import com.l7tech.server.transport.jms2.i;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
import javax.jms.Queue;
import javax.jms.Session;
import javax.naming.Context;
import javax.naming.NamingException;

public abstract class SessionHolderBase
implements SessionHolder {
    private final Logger a = Logger.getLogger(SessionHolderBase.class.getName());
    protected final AtomicInteger referenceCount = new AtomicInteger(0);
    protected final JmsBag bag;
    protected final String name;
    protected final int endpointVersion;
    protected final int connectionVersion;
    protected final JmsEndpointConfig endpointConfig;
    private final long b = System.currentTimeMillis();
    private final AtomicLong c = new AtomicLong(this.b);
    private final Object d = new Object();
    private static final String[] g;

    public SessionHolderBase(JmsEndpointConfig jmsEndpointConfig, JmsBag jmsBag) {
        this.connectionVersion = jmsEndpointConfig.getConnection().getVersion();
        this.endpointConfig = jmsEndpointConfig;
        this.bag = jmsBag;
        this.name = jmsEndpointConfig.getJmsEndpointKey().toString();
        this.endpointVersion = jmsEndpointConfig.getEndpoint().getVersion();
    }

    protected JmsBag makeJmsBag() throws Exception {
        Session session = null;
        MessageConsumer messageConsumer = null;
        MessageProducer messageProducer = null;
        String string = this.endpointConfig.getEndpoint().getDestinationName();
        try {
            session = this.endpointConfig.getEndpoint().isMessageSource() ? this.bag.getConnection().createSession(this.endpointConfig.isTransactional(), 2) : this.bag.getConnection().createSession(false, 2);
            if (this.endpointConfig.getEndpoint().isMessageSource()) {
                Destination destination = JmsUtil.cast(this.bag.getJndiContext().lookup(string), Destination.class);
                messageConsumer = JmsUtil.createMessageConsumer(session, destination);
                Queue queue = this.getFailureQueue(this.bag.getJndiContext());
                if (queue != null) {
                    messageProducer = JmsUtil.createMessageProducer(session, (Destination)queue);
                }
            } else {
                Destination destination = JmsUtil.cast(this.bag.getJndiContext().lookup(string), Destination.class);
                messageProducer = JmsUtil.createMessageProducer(session, destination);
            }
            return new JmsBag(this.bag.getJndiContext(), this.bag.getConnectionFactory(), this.bag.getConnection(), session, messageConsumer, messageProducer, this);
        }
        catch (Exception exception) {
            String[] stringArray;
            try {
                if (messageProducer != null) {
                    messageProducer.close();
                }
            }
            catch (JMSException jMSException) {
                stringArray = g;
                this.a.log(Level.WARNING, stringArray[3] + string);
            }
            try {
                if (messageConsumer != null) {
                    messageConsumer.close();
                }
            }
            catch (JMSException jMSException) {
                stringArray = g;
                this.a.log(Level.WARNING, stringArray[1] + string);
            }
            try {
                if (session != null) {
                    session.close();
                }
            }
            catch (JMSException jMSException) {
                this.a.log(Level.WARNING, g[0]);
            }
            throw exception;
        }
    }

    @Override
    public String getName() {
        return this.name;
    }

    @Override
    public int getEndpointVersion() {
        return this.endpointVersion;
    }

    @Override
    public int getConnectionVersion() {
        return this.connectionVersion;
    }

    @Override
    public void touch() {
        this.c.set(System.currentTimeMillis());
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    @Override
    public void doWithJmsResources(JmsResourceManager.JmsResourceCallback jmsResourceCallback) throws JMSException, JmsRuntimeException, NamingException {
        JmsBag jmsBag = null;
        try {
            try {
                jmsBag = this.borrowJmsBag();
            }
            catch (NamingException namingException) {
                throw namingException;
            }
            catch (Throwable throwable) {
                throw new JmsRuntimeException(throwable);
            }
            jmsResourceCallback.doWork(jmsBag, new i(this));
        }
        finally {
            this.returnJmsBag(jmsBag);
        }
    }

    protected Queue getFailureQueue(Context context) throws NamingException, JmsConfigException, JMSException, JmsRuntimeException {
        block2: {
            try {
                if (!this.endpointConfig.isTransactional() || this.endpointConfig.getEndpoint().getFailureDestinationName() == null) break block2;
            }
            catch (NamingException namingException) {
                throw SessionHolderBase.a(namingException);
            }
            this.a.finest(g[4]);
            String string = this.endpointConfig.getEndpoint().getFailureDestinationName();
            return JmsUtil.cast(context.lookup(string), Queue.class);
        }
        return null;
    }

    @Override
    public boolean ref() {
        return this.referenceCount.getAndIncrement() > 0;
    }

    @Override
    public void unRef() {
        int n2 = this.referenceCount.decrementAndGet();
        if (n2 <= 0 || this.endpointConfig.getEndpoint().isMessageSource()) {
            this.close();
        }
    }

    @Override
    public int refCount() {
        return this.referenceCount.get();
    }

    @Override
    public long getCreatedTime() {
        return this.b;
    }

    @Override
    public AtomicLong getLastAccessTime() {
        return this.c;
    }

    public String toString() {
        return String.format(g[2], this.name, this.connectionVersion, this.endpointVersion);
    }

    private static NamingException a(NamingException namingException) {
        return namingException;
    }

    /*
     * Unable to fully structure code
     */
    static {
        block10: {
            block9: {
                var5 = new String[5];
                var3_1 = 0;
                var2_2 = "K\u001eb7\u00143\bj\u001f#6\u00149[{PI\u0018+v[{\u0003p<\u00178.K\u001eb7\u00143\bj\u001f#6\u00149[{P`:\u0016%]s\u0015qu\u001e9Z>\u0004k0X2Mm\u0004j;\u0019\"Aq\u001e9u\"T=Pu\u001b9Fp\u0015`!\u00119F>X&&Qz\bh\u0015q&\u00119F>Upo]%";
                var4_3 = "K\u001eb7\u00143\bj\u001f#6\u00149[{PI\u0018+v[{\u0003p<\u00178.K\u001eb7\u00143\bj\u001f#6\u00149[{P`:\u0016%]s\u0015qu\u001e9Z>\u0004k0X2Mm\u0004j;\u0019\"Aq\u001e9u\"T=Pu\u001b9Fp\u0015`!\u00119F>X&&Qz\bh\u0015q&\u00119F>Upo]%".length();
                var1_4 = 27;
                var0_5 = -1;
lbl7:
                // 2 sources

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

                while (true) {
                    var5[var3_1++] = SessionHolderBase.a(v0, SessionHolderBase.a(v2));
                    if ((var0_5 += var1_4) < var4_3) {
                        var1_4 = var2_2.charAt(var0_5);
                        ** continue;
                    }
                    var2_2 = "g2N\u001b8\u001f$F3\u000f\u001a8\u0015wW|_\u000b;\u001eqQ9]Y2\u0015v\u0012(G\u001ct\u001eaA(F\u00175\u000em]2\u0015Y\u0018u9[\r=\u0014c\u00122J\u000et<e[0Z\u000b1+qW)J";
                    var4_3 = "g2N\u001b8\u001f$F3\u000f\u001a8\u0015wW|_\u000b;\u001eqQ9]Y2\u0015v\u0012(G\u001ct\u001eaA(F\u00175\u000em]2\u0015Y\u0018u9[\r=\u0014c\u00122J\u000et<e[0Z\u000b1+qW)J".length();
                    var1_4 = 46;
                    var0_5 = -1;
lbl22:
                    // 2 sources

                    while (true) {
                        v0 = 24;
                        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++] = SessionHolderBase.a(v0, SessionHolderBase.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;
            }
        }
        SessionHolderBase.g = var5;
    }

    private static char[] a(String string) {
        char[] cArray = string.toCharArray();
        if (cArray.length < 2) {
            cArray = cArray;
            cArray[0] = (char)(cArray[0] ^ 0x1C);
        }
        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 -> 42;
                    case 1 -> 68;
                    case 2 -> 55;
                    case 3 -> 97;
                    case 4 -> 76;
                    case 5 -> 98;
                    default -> 28;
                })));
                ++n3;
                n4 = n4;
            } while (n4 > n3);
        }
        return new String(cArray2).intern();
    }
}

