001/*
002 * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 *     Nuxeo - initial API and implementation
011 *
012 */
013package org.nuxeo.ecm.core.convert.extension;
014
015import java.io.File;
016import java.io.IOException;
017import java.io.Serializable;
018
019import org.apache.commons.io.FileUtils;
020import org.nuxeo.common.Environment;
021import org.nuxeo.common.xmap.annotation.XNode;
022import org.nuxeo.common.xmap.annotation.XObject;
023
024/**
025 * XMap Descriptor for the {@link org.nuxeo.ecm.core.convert.api.ConversionService} configuration.
026 *
027 * @author tiry
028 */
029@XObject("configuration")
030public class GlobalConfigDescriptor implements Serializable {
031
032    public static final long DEFAULT_GC_INTERVAL_IN_MIN = 10;
033
034    public static final int DEFAULT_DISK_CACHE_IN_KB = 10 * 1024;
035
036    private static final String CACHING_DIRECTORY = "convertcache";
037
038    private static final long serialVersionUID = 1L;
039
040    protected long GCInterval = DEFAULT_GC_INTERVAL_IN_MIN;
041
042    protected int diskCacheSize = DEFAULT_DISK_CACHE_IN_KB;
043
044    @XNode("enableCache")
045    protected boolean enableCache = true;
046
047    @XNode("cachingDirectory")
048    protected String cachingDirectory = defaultCachingDirectory().getAbsolutePath();
049
050    public long getGCInterval() {
051        return GCInterval;
052    }
053
054    @XNode("gcInterval")
055    public void setGCInterval(long value) {
056        GCInterval = value == 0 ? DEFAULT_GC_INTERVAL_IN_MIN : value;
057    }
058
059    public int getDiskCacheSize() {
060        return diskCacheSize;
061    }
062
063    @XNode("diskCacheSize")
064    public void setDiskCacheSize(int size) {
065        diskCacheSize = size == 0 ? DEFAULT_DISK_CACHE_IN_KB : size;
066    }
067
068    public boolean isCacheEnabled() {
069        return enableCache;
070    }
071
072    public void update(GlobalConfigDescriptor other) {
073        if (other.GCInterval != DEFAULT_GC_INTERVAL_IN_MIN) {
074            GCInterval = other.GCInterval;
075        }
076        if (other.diskCacheSize != DEFAULT_GC_INTERVAL_IN_MIN) {
077            diskCacheSize = other.diskCacheSize;
078        }
079
080        if (other.cachingDirectory != defaultCachingDirectory().getAbsolutePath()) {
081            cachingDirectory = other.cachingDirectory;
082        }
083
084        enableCache = other.enableCache;
085    }
086
087    protected File defaultCachingDirectory() {
088        File data = new File(Environment.getDefault().getData(), CACHING_DIRECTORY);
089        if (data.exists()) {
090            try {
091                FileUtils.deleteDirectory(data);
092            } catch (IOException cause) {
093                throw new RuntimeException("Cannot create cache dir " + data, cause);
094            }
095        }
096        data.mkdirs();
097        return data.getAbsoluteFile();
098    }
099
100    public String getCachingDirectory() {
101        return cachingDirectory;
102    }
103
104}