DataManagerImpl.java 9.53 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.jmx.statistics.CountStatistic
 *  org.apache.commons.codec.binary.Hex
 *  org.apache.commons.io.FileUtils
 *  org.apache.commons.lang.Validate
 *  org.apache.commons.lang.math.NumberUtils
 *  org.slf4j.Logger
 *  org.slf4j.LoggerFactory
 */
package com.adobe.aemds.datamanager.impl;

import com.adobe.CORBA.ServantBase;
import com.adobe.aemds.bedrock.CoreConfigService;
import com.adobe.aemds.datamanager.impl.DataManagerManagedConnection;
import com.adobe.aemds.datamanager.impl.DataManagerService;
import com.adobe.aemds.datamanager.impl.FileDataBufferImpl;
import com.adobe.aemds.datamanager.impl.UrlUtil;
import com.adobe.jmx.statistics.CountStatistic;
import com.adobe.service.DataManager;
import com.adobe.service.DataManagerOperations;
import com.adobe.service.FileDataBuffer;
import com.adobe.service.FileDataBufferHelper;
import com.adobe.service.InvalidSourceException;
import java.io.File;
import java.io.IOException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.commons.codec.binary.Hex;
import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.Validate;
import org.apache.commons.lang.math.NumberUtils;
import org.omg.CORBA.Object;
import org.omg.CORBA.UNKNOWN;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DataManagerImpl
extends ServantBase
implements DataManagerOperations {
    private static final String TRANSACTION_MAX_INLINE_SIZE_KEY = "transactionMaxInlineSize";
    private static final byte[] EMPTY_BYTES = new byte[0];
    private static final String DATAMAN_DIR_PREFIX = "datam";
    private static final Logger logger = LoggerFactory.getLogger(DataManager.class);
    private static SecureRandom _random;
    private final List<File> tmpFileList;
    private final DataManagerManagedConnection resource;
    private CoreConfigService coreConfigService;
    private File temporaryDirectory;
    private byte[] _invocationContext = null;
    private final Map<String, java.lang.Object> sharedData;

    DataManagerImpl(DataManagerManagedConnection resource) {
        this.resource = resource;
        this.tmpFileList = Collections.synchronizedList(new ArrayList());
        this.sharedData = Collections.synchronizedMap(new HashMap());
        resource.connect(this);
    }

    private synchronized void createTempDirectory() throws IOException {
        if (this.temporaryDirectory == null) {
            String DMPrefix = "datam";
            String dirName = DMPrefix + DataManagerImpl.getRandomFileName();
            this.temporaryDirectory = new File(this.coreConfigService.getServerTempDir(), dirName);
            FileUtils.forceMkdir((File)this.temporaryDirectory);
        }
    }

    /*
     * WARNING - Removed try catching itself - possible behaviour change.
     */
    public void cleanup() {
        try {
            try {
                if (this.temporaryDirectory != null) {
                    FileUtils.deleteQuietly((File)this.temporaryDirectory);
                }
                for (File file : this.tmpFileList) {
                    if (file.delete()) continue;
                    file.deleteOnExit();
                }
                java.lang.Object var4_4 = null;
                this.tmpFileList.clear();
            }
            catch (Exception e) {
                logger.error("Error cleaning up Data Manager files", (Throwable)e);
                java.lang.Object var4_5 = null;
                this.tmpFileList.clear();
            }
        }
        catch (Throwable var3_7) {
            java.lang.Object var4_6 = null;
            this.tmpFileList.clear();
            throw var3_7;
        }
    }

    public String getTempFileName(boolean create) {
        try {
            boolean result;
            this.createTempDirectory();
            File tempFile = create ? this.createTempFile() : new File(this.temporaryDirectory, DataManagerImpl.getRandomFileName());
            String retVal = tempFile.getAbsolutePath();
            if (!create || !(result = tempFile.createNewFile())) {
                // empty if block
            }
            this.resource.getService().filesAllocated.increment();
            return retVal;
        }
        catch (Exception e) {
            logger.error("Error retrieving or creating a Data Manager temporary file", (Throwable)e);
            throw new UNKNOWN("Exception in getTempFileName. Exception: " + e.toString());
        }
    }

    public void manageTempFile(String filePath) throws InvalidSourceException {
        block5 : {
            this.assertNotNull(filePath, "Null filePath arg.");
            try {
                this.createTempDirectory();
                File tempFile = new File(filePath);
                if (tempFile.exists()) {
                    String parentDir = tempFile.getParent();
                    if (parentDir == null || !parentDir.equals(this.temporaryDirectory.getCanonicalPath())) {
                        this.tmpFileList.add(new File(filePath));
                    }
                    break block5;
                }
                throw new InvalidSourceException("File " + filePath + " does not exist");
            }
            catch (InvalidSourceException e) {
                logger.error("Temp file to manage does not exist at path " + filePath, (Throwable)e);
                throw e;
            }
            catch (Exception e) {
                logger.error("Error trying to manage file " + filePath, (Throwable)e);
                throw new InvalidSourceException("Exception in ManageTempFile: " + e.toString());
            }
        }
    }

    public FileDataBuffer createFileDataBuffer(String filePath) throws InvalidSourceException {
        this.assertNotNull(filePath, "Null filePath arg in createFileDataBuffer");
        try {
            File tmpFile = new File(filePath);
            if (tmpFile.exists() && tmpFile.canRead()) {
                FileDataBufferImpl dbImpl = new FileDataBufferImpl(filePath, this.resource);
                this.resource.getService().createdFromFile.increment();
                return FileDataBufferHelper.narrow(dbImpl._this());
            }
        }
        catch (Exception e) {
            logger.error("Error creating FileDataBuffer for file " + filePath, (Throwable)e);
        }
        throw new InvalidSourceException("Cannot open " + filePath + " for reading");
    }

    public FileDataBuffer createFileDataBufferFromUrl(String urlStr) throws InvalidSourceException {
        return UrlUtil.createFileDataBufferFromUrl(urlStr, this);
    }

    public byte[] getInvocationContext() {
        return this._invocationContext;
    }

    public void setInvocationContext(byte[] invocationContext) {
        this._invocationContext = invocationContext;
    }

    public int getDefaultMaxInlineSize() {
        return 70000;
    }

    public int getTransactionMaxInlineSize() {
        return NumberUtils.toInt((String)this.get("transactionMaxInlineSize"), (int)this.getDefaultMaxInlineSize());
    }

    public void setTransactionMaxInlineSize(int size) {
        this.setTransactionPropertyLong("transactionMaxInlineSize", size);
    }

    public synchronized byte[] getTransactionPropertyBytes(String name) {
        if (this.sharedData == null) {
            return EMPTY_BYTES;
        }
        java.lang.Object o = this.sharedData.get(name);
        if (o == null || !(o instanceof byte[])) {
            return EMPTY_BYTES;
        }
        return (byte[])o;
    }

    public int getTransactionPropertyLong(String name) {
        return NumberUtils.toInt((String)this.get(name), (int)-1);
    }

    public synchronized String getTransactionPropertyString(String name) {
        String result = this.get(name);
        return result != null ? result : "";
    }

    public synchronized void setTransactionPropertyBytes(String name, byte[] data) {
        Validate.notNull((java.lang.Object)data, (String)"Data cannot be null.");
        this.sharedData.put(name, data);
    }

    public synchronized void setTransactionPropertyLong(String name, int data) {
        this.sharedData.put(name, Integer.toString(data));
    }

    public synchronized void setTransactionPropertyString(String name, String data) {
        Validate.notNull((java.lang.Object)data, (String)"Data cannot be null.");
        this.sharedData.put(name, data);
    }

    void setCoreConfigService(CoreConfigService coreConfigService) {
        this.coreConfigService = coreConfigService;
    }

    private String get(String key) {
        return this.sharedData != null ? (String)this.sharedData.get(key) : null;
    }

    private void assertNotNull(java.lang.Object o, String msg) throws InvalidSourceException {
        if (o == null) {
            throw new InvalidSourceException(msg);
        }
    }

    private File createTempFile() throws IOException {
        return File.createTempFile("datam", null, this.temporaryDirectory);
    }

    private static String getRandomFileName() {
        byte[] bytes = new byte[16];
        DataManagerImpl.getSecureRandom().nextBytes(bytes);
        return new String(Hex.encodeHex((byte[])bytes));
    }

    private static SecureRandom getSecureRandom() {
        if (_random == null) {
            try {
                _random = SecureRandom.getInstance("SHA1PRNG");
                return _random;
            }
            catch (NoSuchAlgorithmException e) {
                throw new RuntimeException(e);
            }
        }
        return _random;
    }
}