TempFileManagerImpl.java 3.21 KB
/*
 * Decompiled with CFR 0_118.
 * 
 * Could not load the following classes:
 *  com.adobe.aemds.bedrock.CoreConfigService
 *  com.adobe.aemds.bedrock.internal.Utilities
 *  com.adobe.service.ConnectionFactory
 *  com.adobe.service.DataManager
 *  com.adobe.service.DataManagerHelper
 *  com.adobe.service.InvalidSourceException
 *  org.apache.commons.io.FileUtils
 *  org.apache.felix.scr.annotations.Component
 *  org.apache.felix.scr.annotations.Reference
 *  org.apache.felix.scr.annotations.Service
 */
package com.adobe.aemfd.docmanager.impl;

import com.adobe.aemds.bedrock.CoreConfigService;
import com.adobe.aemds.bedrock.internal.Utilities;
import com.adobe.aemfd.docmanager.TempFileManager;
import com.adobe.service.ConnectionFactory;
import com.adobe.service.DataManager;
import com.adobe.service.DataManagerHelper;
import com.adobe.service.InvalidSourceException;
import java.io.File;
import java.io.IOException;
import javax.naming.NameNotFoundException;
import org.apache.commons.io.FileUtils;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.Service;

@Component
@Service(value={TempFileManager.class})
public class TempFileManagerImpl
implements TempFileManager {
    @Reference
    private CoreConfigService coreConfigService;
    private static final String TEMP_FILE_DIR_NAME = "tfmgr";
    private static final String TEMP_FILE_NAME_PREFIX = "tfm";
    private File tempDirectory;

    public File getTempFile() throws IOException {
        return this.getTempFile("tfm", null);
    }

    private synchronized void createTempDirectory() throws IOException {
        if (this.tempDirectory == null) {
            this.tempDirectory = new File(this.coreConfigService.getServerTempDir(), "tfmgr");
            FileUtils.forceMkdir((File)this.tempDirectory);
        }
    }

    public File getTempFile(String prefix, String suffix) throws IOException {
        this.createTempDirectory();
        return File.createTempFile(prefix, suffix, this.tempDirectory);
    }

    public DataManager getDataManager() {
        ConnectionFactory cf;
        try {
            cf = Utilities.serviceLookup((String)"DataManagerService");
        }
        catch (NameNotFoundException e) {
            throw new IllegalStateException("DataManager service not found in registry!", e);
        }
        return DataManagerHelper.narrow((Object)cf.getConnection());
    }

    public File getTransactionBoundTempFile() throws IOException {
        DataManager dm = this.getDataManager();
        String fp = dm.getTempFileName(true);
        File res = new File(fp);
        try {
            dm.manageTempFile(fp);
        }
        catch (InvalidSourceException e) {
            FileUtils.deleteQuietly((File)res);
            throw new IOException("Error registering temp file for auto-cleanup", (Throwable)e);
        }
        return res;
    }

    protected void bindCoreConfigService(CoreConfigService coreConfigService) {
        this.coreConfigService = coreConfigService;
    }

    protected void unbindCoreConfigService(CoreConfigService coreConfigService) {
        if (this.coreConfigService == coreConfigService) {
            this.coreConfigService = null;
        }
    }
}