ResourcePeerImpl.java 3.62 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  javax.transaction.Synchronization
 *  javax.transaction.SystemException
 *  javax.transaction.Transaction
 *  javax.transaction.TransactionManager
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.service;

import com.adobe.aemds.bedrock.internal.OSGiUtils;
import com.adobe.service.Resource;
import com.adobe.service.ResourcePeer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.transaction.Synchronization;
import javax.transaction.SystemException;
import javax.transaction.Transaction;
import javax.transaction.TransactionManager;
import java.lang.reflect.UndeclaredThrowableException;

class ResourcePeerImpl
extends ResourcePeer
implements Synchronization {
    private final Logger logger = LoggerFactory.getLogger(ResourcePeerImpl.class);
    private Transaction savedTx;
    private boolean didRollback = false;

    ResourcePeerImpl(Resource resource) {
        super(resource);
    }

    private static TransactionManager getTransactionManager() {
        return OSGiUtils.getTransactionManager();
    }

    public void resumeTx() {
        if (this.getSavedTx() == null) {
            return;
        }
        TransactionManager tm = ResourcePeerImpl.getTransactionManager();
        try {
            Transaction trans = tm.getTransaction();
            if (trans != null) {
                throw new IllegalStateException("Unexpected Transaction " + (Object)trans);
            }
            tm.resume(this.getSavedTx());
            trans = tm.getTransaction();
            if (trans != this.getSavedTx()) {
                throw new IllegalStateException("Transaction not resumed " + (Object)trans);
            }
        }
        catch (Exception e) {
            this.logger.error("Unexpected exception while resuming transaction", (Throwable)e);
            throw new UndeclaredThrowableException(e);
        }
    }

    public void suspendTx() {
        if (this.getSavedTx() == null) {
            return;
        }
        TransactionManager tm = ResourcePeerImpl.getTransactionManager();
        try {
            Transaction trans = tm.getTransaction();
            if (trans != this.getSavedTx()) {
                throw new IllegalStateException("Wrong transaction " + (Object)trans);
            }
            tm.suspend();
            trans = tm.getTransaction();
            if (trans != null) {
                throw new IllegalStateException("Not suspended " + (Object)trans);
            }
        }
        catch (SystemException e) {
            this.logger.error("Unexpected exception while suspending transaction", (Throwable)e);
            throw new UndeclaredThrowableException((Throwable)e, "System exception");
        }
    }

    void setSavedTx(Transaction tx) {
        this.savedTx = tx;
    }

    Transaction getSavedTx() {
        return this.savedTx;
    }

    public void afterCompletion(int status) {
        if (status == 4) {
            this.invokeRollback();
        } else if (!this.didRollback) {
            this.invokeCommit();
        }
        this.didRollback = false;
    }

    public void beforeCompletion() {
        try {
            this.invokePrepare();
        }
        catch (Resource.Rollback e) {
            this.logger.warn("Rollback error encountered while preparing resource for tx completion", (Throwable)e);
            this.didRollback = true;
            this.invokeRollback();
        }
    }

    public static class Factory
    implements ResourcePeer.Factory {
        public ResourcePeer create(Resource resource) {
            return new ResourcePeerImpl(resource);
        }
    }

}