OpenOffice2PdfCaller.java 18 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.aemfd.docmanager.Document
 *  com.adobe.native2pdf.bmc.ResultStruct
 *  com.adobe.native2pdf.xml.FiletypeSettings
 *  com.adobe.native2pdf.xml.FiletypeSettings$Settings
 *  com.adobe.native2pdf.xml.SecuritySettings
 *  com.adobe.native2pdf.xml.SecuritySettings$Settings
 *  com.adobe.pdfg.common.FileUtilities
 *  com.adobe.pdfg.common.Guid
 *  com.adobe.pdfg.common.JobConfiguration
 *  com.adobe.pdfg.common.StreamGobbler
 *  com.adobe.pdfg.common.Utils
 *  com.adobe.pdfg.exception.ConversionException
 *  com.adobe.pdfg.exception.ErrorCode
 *  com.adobe.pdfg.exception.FileFormatNotSupportedException
 *  com.adobe.pdfg.exception.InvalidParameterException
 *  com.adobe.pdfg.logging.PDFGLogger
 *  com.adobe.service.ConnectionFactory
 */
package com.adobe.pdfg.impl;

import com.adobe.aemfd.docmanager.Document;
import com.adobe.native2pdf.bmc.ResultStruct;
import com.adobe.native2pdf.xml.FiletypeSettings;
import com.adobe.native2pdf.xml.SecuritySettings;
import com.adobe.pdfg.callbacks.OpenOfficeToPDFTransactionCallback;
import com.adobe.pdfg.common.FileUtilities;
import com.adobe.pdfg.common.Guid;
import com.adobe.pdfg.common.JobConfiguration;
import com.adobe.pdfg.common.StreamGobbler;
import com.adobe.pdfg.common.Utils;
import com.adobe.pdfg.exception.ConversionException;
import com.adobe.pdfg.exception.ErrorCode;
import com.adobe.pdfg.exception.FileFormatNotSupportedException;
import com.adobe.pdfg.exception.InvalidParameterException;
import com.adobe.pdfg.impl.BMCCaller;
import com.adobe.pdfg.impl.GeneratePDFServiceImpl;
import com.adobe.pdfg.impl.GeneratePDFUtil;
import com.adobe.pdfg.logging.PDFGLogger;
import com.adobe.pdfg.postprocess.PostProcessFileInfo;
import com.adobe.pdfg.transaction.TransactionCallback;
import com.adobe.service.ConnectionFactory;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.Serializable;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Semaphore;
import org.omg.CORBA.COMM_FAILURE;

public class OpenOffice2PdfCaller
extends BMCCaller {
    static final long serialVersionUID = 2301;
    static int oo_pool_size = 1;
    private static Semaphore ooConversionLock = null;
    private static String m_osname = System.getProperty("os.name").toLowerCase();
    private String OPENOFFICE_IN_FILE_NAME = "openoffice-in";

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     * Unable to fully structure code
     * Enabled aggressive block sorting
     * Enabled unnecessary exception pruning
     * Enabled aggressive exception aggregation
     * Converted monitor instructions to comments
     * Lifted jumps to return sites
     */
    public Map createPDF(GeneratePDFServiceImpl coreSvcImpl, Document sourceFileDoc, String inputFileName, String fileTypeSettings, String pdfSettings, String securitySettings, Document settingsDoc, String jobConfigurationString, Document xmpDoc, String[] openOfficeData) throws InvalidParameterException, ConversionException, FileFormatNotSupportedException {
        debugMsgsLogged = false;
        jobIdentityId = (String)Utils.threadLocalValue.get();
        debugMsgs = new StringBuilder();
        debugMsgs.append("\nInside openOffice2PdfCaller.createPDF for job=" + jobIdentityId);
        xmpFile = null;
        success = true;
        waitStartTime = 0;
        waitEndTime = 0;
        if (sourceFileDoc == null) {
            throw new InvalidParameterException(11025);
        }
        errorCode = -1;
        startTime = System.currentTimeMillis();
        this.pdfgLogger.info("001-001", new Object[]{inputFileName});
        this.pdfgLogger.info("001-024", new Object[]{inputFileName, new Date(startTime), jobIdentityId});
        this.pdfgLogger.info("001-016", new Object[]{inputFileName, "OpenOffice to PDF"});
        extension = inputFileName.substring(inputFileName.lastIndexOf("."), inputFileName.length());
        pdfgTmpDir = null;
        try {
            pdfgTmpDir = FileUtilities.createGuidDir((String)new Guid().toString());
        }
        catch (IOException e2) {
            throw new ConversionException(1000, (Throwable)e2);
        }
        sourceFile = new File(pdfgTmpDir, this.OPENOFFICE_IN_FILE_NAME + extension);
        try {
            sourceFileDoc.copyToFile(sourceFile);
        }
        catch (IOException e3) {
            throw new ConversionException(1000, (Throwable)e3);
        }
        if (sourceFile.length() == 0) {
            throw new ConversionException(11019);
        }
        errorCode = this.initializeConfiguration(coreSvcImpl, settingsDoc, jobConfigurationString, errorCode);
        debugMsgs.append("\nafter calling initializeConfiguration() for job=" + jobIdentityId);
        sourcePathOrURL = null;
        try {
            sourcePathOrURL = sourceFile.getCanonicalPath();
        }
        catch (IOException e1) {
            throw new ConversionException(9001, (Throwable)e1);
        }
        this.pdfgLogger.info("001-022", new Object[]{inputFileName, this.config.getSecuritySettings().getName()});
        this.pdfgLogger.info("001-023", new Object[]{inputFileName, this.config.getFiletypeSettings().getName()});
        jobOptionFile = null;
        suffix = ".joboptions";
        errorCode = 1002;
        jobOptionFile = this.validateConfigAndWriteJobOptions(coreSvcImpl, this.config, this.jobOptionName, ".joboptions", false, this.appConfig, pdfgTmpDir);
        this.jobOptionName = this.getJobOptionNameFromFile(jobOptionFile, ".joboptions");
        debugMsgs.append("\nafter calling getJobOptionNameFromFile() for job=" + jobIdentityId);
        destinationPath = null;
        try {
            try {
                path = System.getenv("OpenOffice_PATH");
                v0 = bOpenOfficePathSet = path != null && "".equals(path.trim()) == false;
                if (!bOpenOfficePathSet) {
                    throw new ConversionException(11030);
                }
                usersMap = coreSvcImpl.getUserAccountsMap();
                if ((OpenOffice2PdfCaller.m_osname.contains("2008") || OpenOffice2PdfCaller.m_osname.contains("vista") || OpenOffice2PdfCaller.m_osname.contains("windows 7")) && (usersMap == null || usersMap.isEmpty())) {
                    this.pdfgLogger.info("Conversion failed as no user is defined.");
                    throw new ConversionException(80036);
                }
                results = null;
                byteArrayOutputStream = new ByteArrayOutputStream();
                objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
                objectOutputStream.writeObject(openOfficeData);
                parentDir = pdfgTmpDir.getParent();
                destinationPath = parentDir + File.separator + new Guid().toString() + ".pdf";
                if (!OpenOffice2PdfCaller.m_osname.contains("win")) {
                    this.changeTmpDirPermissions(parentDir);
                }
                debugMsgs.append("\nbefore calling OpenOfficeToPDFTransactionCallback.initConnectionFactory() for job=" + jobIdentityId);
                transactionCallback = new OpenOfficeToPDFTransactionCallback(coreSvcImpl.getOpenOfficeToPdfFactory());
                debugMsgs.append("\nafter calling OpenOfficeToPDFTransactionCallback.initConnectionFactory() for job=" + jobIdentityId);
                transactionCallback.initOpenOfficeToPdfFactory(usersMap);
                transactionCallback.setTimeoutSeconds(this.timeoutSeconds);
                transactionCallback.setOpenOfficeData(byteArrayOutputStream.toByteArray());
                transactionCallback.setSourcePath(sourcePathOrURL);
                transactionCallback.setDestinationPath(destinationPath);
                OpenOffice2PdfCaller.oo_pool_size = transactionCallback.getPoolSize();
                if (OpenOffice2PdfCaller.ooConversionLock == null) {
                    var38_42 = OpenOffice2PdfCaller.class;
                    // MONITORENTER : com.adobe.pdfg.impl.OpenOffice2PdfCaller.class
                    if (OpenOffice2PdfCaller.ooConversionLock == null) {
                        OpenOffice2PdfCaller.ooConversionLock = new Semaphore(OpenOffice2PdfCaller.oo_pool_size, true);
                        debugMsgs.append("\nafter creating semaphore for OpenOffice for job=" + jobIdentityId);
                    }
                    // MONITOREXIT : var38_42
                }
                debugMsgs.append("\nTrying to acquire semaphore lock for OpenOffice for job=" + jobIdentityId);
                waitStartTime = System.currentTimeMillis();
                OpenOffice2PdfCaller.ooConversionLock.acquire();
                waitEndTime = System.currentTimeMillis();
                debugMsgs.append("\nAcquired semaphore lock for OpenOffice for job=" + jobIdentityId);
                try {
                    try {
                        debugMsgs.append("\nAbout to call invokeInSMT() for job=" + jobIdentityId);
                        this.pdfgLogger.debug(debugMsgs.toString());
                        debugMsgsLogged = true;
                        results = this.invokeInSMT(transactionCallback, coreSvcImpl);
                        debugMsgs = new StringBuilder();
                        debugMsgsLogged = false;
                        debugMsgs.append("\nAfter Calline invokeInSMT() for job=" + jobIdentityId);
                    }
                    catch (COMM_FAILURE cf) {
                        this.pdfgLogger.trace(cf.getMessage(), null, (Throwable)cf);
                        endTime = System.currentTimeMillis();
                        if ((long)this.timeoutSeconds >= (endTime - startTime) / 1000) throw new ConversionException(1000);
                        throw new ConversionException(ErrorCode.JOB_TIMEOUT_ERROR);
                    }
                    var42_44 = null;
                    debugMsgs.append("\nBefore releasing semaphore lock for job=" + jobIdentityId);
                    OpenOffice2PdfCaller.ooConversionLock.release();
                    debugMsgs.append("\nAfter releasing semaphore lock for job=" + jobIdentityId);
                }
                catch (Throwable var41_48) {
                    var42_45 = null;
                    debugMsgs.append("\nBefore releasing semaphore lock for job=" + jobIdentityId);
                    OpenOffice2PdfCaller.ooConversionLock.release();
                    debugMsgs.append("\nAfter releasing semaphore lock for job=" + jobIdentityId);
                    throw var41_48;
                }
                debugMsgs.append("\nBefore calling checkResult() for job=" + jobIdentityId);
                errorCode = this.checkResult(results, errorCode);
                debugMsgs.append("\nAfter calling checkResult(). errorCode=" + errorCode + " for job=" + jobIdentityId);
                if (xmpDoc != null) {
                    xmpFile = new File(pdfgTmpDir, this.OPENOFFICE_IN_FILE_NAME + ".xmp");
                    xmpDoc.copyToFile(xmpFile);
                }
                errorCode = this.setDistillerParams(coreSvcImpl, "OpenOffice", errorCode);
                debugMsgs.append("\nAfter calling setDistillerParams(). errorCode=" + errorCode + " for job=" + jobIdentityId);
                filePaths = new PostProcessFileInfo();
                filePaths.pdfFilePath = destinationPath;
                debugMsgs.append("\nBefore calling doPostProcess() for job=" + jobIdentityId);
                this.pdfgLogger.debug(debugMsgs.toString());
                debugMsgsLogged = true;
                errorCode = this.doPostProcess("OpenOffice", "OpenOffice", xmpFile, destinationPath, coreSvcImpl, jobOptionFile, errorCode, filePaths, false, false, null, 1, GeneratePDFUtil.applyWaterMark());
                debugMsgs = new StringBuilder();
                debugMsgsLogged = false;
                debugMsgs.append("\nAfter calling doPostProcess() for job=" + jobIdentityId);
                convertedDoc = filePaths.postProcessedDoc;
                pdfFile = null;
                if (convertedDoc == null) {
                    pdfFile = new File(filePaths.pdfFilePath);
                }
                fileName = inputFileName.substring(0, inputFileName.lastIndexOf(".") + 1) + "pdf";
                if (pdfFile != null && pdfFile.exists() && pdfFile.length() > 0) {
                    convertedDoc = new Document(pdfFile, false);
                }
                if (convertedDoc == null) throw new ConversionException(1000);
                convertedDoc.setAttribute("file", (Object)fileName);
                map = new HashMap<String, Document>();
                map.put("ConvertedDoc", convertedDoc);
                debugMsgs.append("\nconvertedDoc is not null. Returning from OpenOffice2PdfCaller.createPDF() for job=" + jobIdentityId);
                var43_51 = map;
                var45_52 = null;
                if (pdfgTmpDir != null) {
                    GeneratePDFServiceImpl.deleteSubFiles(pdfgTmpDir);
                }
                if (jobOptionFile != null) {
                    try {
                        jobOptionFile.delete();
                        jobOptionFile = null;
                    }
                    catch (Exception t) {
                        this.pdfgLogger.trace("001-007", null, (Throwable)t);
                    }
                }
                endTime = System.currentTimeMillis();
                if (success) {
                    this.pdfgLogger.info("001-027", new Object[]{inputFileName});
                } else {
                    this.pdfgLogger.info("001-028", new Object[]{inputFileName});
                }
                this.pdfgLogger.info("001-025", new Object[]{inputFileName, new Date(endTime), jobIdentityId});
                this.pdfgLogger.info("001-030", new Object[]{inputFileName, waitEndTime - waitStartTime, jobIdentityId});
                this.pdfgLogger.info("001-026", new Object[]{inputFileName, endTime - startTime - (waitEndTime - waitStartTime), jobIdentityId});
                if (debugMsgsLogged != false) return var43_51;
                this.pdfgLogger.debug(debugMsgs.toString());
                return var43_51;
            }
            catch (ConversionException e) {
                this.pdfgLogger.trace(e.getMessage(), null, (Throwable)e);
                success = false;
                throw e;
            }
            catch (InvalidParameterException e) {
                this.pdfgLogger.trace(e.getMessage(), null, (Throwable)e);
                success = false;
                throw e;
            }
            catch (FileFormatNotSupportedException e) {
                this.pdfgLogger.trace(e.getMessage(), null, (Throwable)e);
                success = false;
                throw e;
            }
            catch (Exception e) {
                success = false;
                throw new ConversionException(1000, (Throwable)e);
            }
        }
        catch (Throwable var44_58) {
            block42 : {
                var45_53 = null;
                if (pdfgTmpDir != null) {
                    GeneratePDFServiceImpl.deleteSubFiles(pdfgTmpDir);
                }
                if (jobOptionFile != null) {
                    ** try [egrp 8[TRYBLOCK] [18 : 1765->1777)] { 
lbl190: // 1 sources:
                    jobOptionFile.delete();
                    jobOptionFile = null;
                    break block42;
lbl193: // 1 sources:
                    catch (Exception t) {
                        this.pdfgLogger.trace("001-007", null, (Throwable)t);
                    }
                }
            }
            endTime = System.currentTimeMillis();
            if (success) {
                this.pdfgLogger.info("001-027", new Object[]{inputFileName});
            } else {
                this.pdfgLogger.info("001-028", new Object[]{inputFileName});
            }
            this.pdfgLogger.info("001-025", new Object[]{inputFileName, new Date(endTime), jobIdentityId});
            this.pdfgLogger.info("001-030", new Object[]{inputFileName, waitEndTime - waitStartTime, jobIdentityId});
            this.pdfgLogger.info("001-026", new Object[]{inputFileName, endTime - startTime - (waitEndTime - waitStartTime), jobIdentityId});
            if (debugMsgsLogged != false) throw var44_58;
            this.pdfgLogger.debug(debugMsgs.toString());
            throw var44_58;
        }
    }

    private void changeTmpDirPermissions(String parentDir) throws ConversionException {
        try {
            this.pdfgLogger.info("Changing the permission of dir " + parentDir);
            String[] cmd = new String[]{"chmod", "-R", "a+wx", parentDir};
            this.runCmd(cmd);
        }
        catch (Exception e) {
            throw new ConversionException(17002, parentDir, (Throwable)e);
        }
    }

    private int runCmd(String[] cmd) {
        int retVal;
        Process proc = null;
        try {
            proc = Runtime.getRuntime().exec(cmd);
        }
        catch (IOException e) {
            this.pdfgLogger.trace(e.getMessage(), null, (Throwable)e);
            return -1;
        }
        try {
            proc.getOutputStream().close();
        }
        catch (IOException e) {
            this.pdfgLogger.trace(e.getMessage(), null, (Throwable)e);
        }
        String cmdLine = Utils.unsplit((String[])cmd);
        StreamGobbler errorGobbler = new StreamGobbler("error stream consumer for cmd: " + cmdLine, proc.getErrorStream(), (OutputStream)System.err);
        errorGobbler.start();
        StreamGobbler outputGobbler = new StreamGobbler("output stream consumer for cmd: " + cmdLine, proc.getInputStream(), null);
        outputGobbler.start();
        do {
            try {
                retVal = proc.waitFor();
                break;
            }
            catch (InterruptedException e) {
                this.pdfgLogger.trace(e.getMessage(), null, (Throwable)e);
                continue;
            }
            break;
        } while (true);
        errorGobbler.collectThread();
        outputGobbler.collectThread();
        return retVal;
    }

    protected boolean checkLinearizationForPostProcessing() {
        return true;
    }
}