001/*
002 * (C) Copyright 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Nuxeo - initial API and implementation
018 *
019 */
020package org.nuxeo.ecm.core.convert.extension;
021
022import java.io.File;
023import java.io.IOException;
024import java.io.Serializable;
025
026import org.apache.commons.io.FileUtils;
027import org.nuxeo.common.Environment;
028import org.nuxeo.common.xmap.annotation.XNode;
029import org.nuxeo.common.xmap.annotation.XObject;
030
031/**
032 * XMap Descriptor for the {@link org.nuxeo.ecm.core.convert.api.ConversionService} configuration.
033 *
034 * @author tiry
035 */
036@XObject("configuration")
037public class GlobalConfigDescriptor implements Serializable {
038
039    public static final long DEFAULT_GC_INTERVAL_IN_MIN = 10;
040
041    public static final int DEFAULT_DISK_CACHE_IN_KB = 10 * 1024;
042
043    private static final String CACHING_DIRECTORY = "convertcache";
044
045    private static final long serialVersionUID = 1L;
046
047    protected long GCInterval = DEFAULT_GC_INTERVAL_IN_MIN;
048
049    protected int diskCacheSize = DEFAULT_DISK_CACHE_IN_KB;
050
051    @XNode("enableCache")
052    protected boolean enableCache = true;
053
054    @XNode("cachingDirectory")
055    protected String cachingDirectory = defaultCachingDirectory().getAbsolutePath();
056
057    public long getGCInterval() {
058        return GCInterval;
059    }
060
061    @XNode("gcInterval")
062    public void setGCInterval(long value) {
063        GCInterval = value == 0 ? DEFAULT_GC_INTERVAL_IN_MIN : value;
064    }
065
066    public int getDiskCacheSize() {
067        return diskCacheSize;
068    }
069
070    @XNode("diskCacheSize")
071    public void setDiskCacheSize(int size) {
072        diskCacheSize = size == 0 ? DEFAULT_DISK_CACHE_IN_KB : size;
073    }
074
075    public boolean isCacheEnabled() {
076        return enableCache;
077    }
078
079    public void update(GlobalConfigDescriptor other) {
080        if (other.GCInterval != DEFAULT_GC_INTERVAL_IN_MIN) {
081            GCInterval = other.GCInterval;
082        }
083        if (other.diskCacheSize != DEFAULT_GC_INTERVAL_IN_MIN) {
084            diskCacheSize = other.diskCacheSize;
085        }
086
087        if (other.cachingDirectory != defaultCachingDirectory().getAbsolutePath()) {
088            cachingDirectory = other.cachingDirectory;
089        }
090
091        enableCache = other.enableCache;
092    }
093
094    protected File defaultCachingDirectory() {
095        File data = new File(Environment.getDefault().getData(), CACHING_DIRECTORY);
096        if (data.exists()) {
097            try {
098                FileUtils.deleteDirectory(data);
099            } catch (IOException cause) {
100                throw new RuntimeException("Cannot create cache dir " + data, cause);
101            }
102        }
103        data.mkdirs();
104        return data.getAbsoluteFile();
105    }
106
107    public String getCachingDirectory() {
108        return cachingDirectory;
109    }
110
111}