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