001/*
002 * (C) Copyright 2006-2016 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 *     Thierry Delprat
018 *     Stephane Lacoin
019 *     Florent Guillaume
020 */
021package org.nuxeo.ecm.core.convert.extension;
022
023import java.io.File;
024import java.io.IOException;
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;
030import org.nuxeo.ecm.core.api.NuxeoException;
031
032/**
033 * XMap Descriptor for the {@link org.nuxeo.ecm.core.convert.api.ConversionService} configuration.
034 */
035@XObject("configuration")
036public class GlobalConfigDescriptor {
037
038    public static final boolean DEFAULT_CACHE_ENABLED = true;
039
040    public static final long DEFAULT_GC_INTERVAL_IN_MIN = 10;
041
042    public static final int DEFAULT_DISK_CACHE_IN_KB = 10 * 1024;
043
044    public static final String DEFAULT_CACHING_DIRECTORY = "convertcache";
045
046    @XNode("enableCache")
047    protected Boolean enableCache;
048
049    public boolean isCacheEnabled() {
050        return enableCache == null ? DEFAULT_CACHE_ENABLED : enableCache.booleanValue();
051    }
052
053    @XNode("cachingDirectory")
054    protected String cachingDirectory;
055
056    public String getCachingDirectory() {
057        return cachingDirectory == null ? getDefaultCachingDirectory() : cachingDirectory;
058    }
059
060    protected String getDefaultCachingDirectory() {
061        File cache = new File(Environment.getDefault().getData(), DEFAULT_CACHING_DIRECTORY);
062        return cache.getAbsolutePath();
063    }
064
065    /** @since 9.1 */
066    public void clearCachingDirectory() {
067        File cache = new File(getCachingDirectory());
068        if (cache.exists()) {
069            try {
070                FileUtils.deleteDirectory(cache);
071            } catch (IOException e) {
072                throw new NuxeoException("Cannot create cache dir " + cache, e);
073            }
074        }
075        cache.mkdirs();
076    }
077
078    protected Long GCInterval;
079
080    @XNode("gcInterval")
081    public void setGCInterval(long value) {
082        GCInterval = value == 0 ? null : Long.valueOf(value);
083    }
084
085    public long getGCInterval() {
086        return GCInterval == null ? DEFAULT_GC_INTERVAL_IN_MIN : GCInterval.longValue();
087    }
088
089    protected Integer diskCacheSize;
090
091    @XNode("diskCacheSize")
092    public void setDiskCacheSize(int size) {
093        diskCacheSize = size == 0 ? null : Integer.valueOf(size);
094    }
095
096    public int getDiskCacheSize() {
097        return diskCacheSize == null ? DEFAULT_DISK_CACHE_IN_KB : diskCacheSize.intValue();
098    }
099
100    public void update(GlobalConfigDescriptor other) {
101        if (other.enableCache != null) {
102            enableCache = other.enableCache;
103        }
104        if (other.GCInterval != null) {
105            GCInterval = other.GCInterval;
106        }
107        if (other.diskCacheSize != null) {
108            diskCacheSize = other.diskCacheSize;
109        }
110        if (other.cachingDirectory != null) {
111            cachingDirectory = other.cachingDirectory;
112        }
113    }
114
115}