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 *     Gagnavarslan ehf
018 */
019package org.nuxeo.ecm.webdav.backend;
020
021import java.util.HashMap;
022import java.util.Map;
023import java.util.concurrent.ConcurrentHashMap;
024
025import org.nuxeo.ecm.core.api.CoreSession;
026import org.nuxeo.ecm.core.api.DocumentModel;
027import org.nuxeo.ecm.core.api.DocumentNotFoundException;
028import org.nuxeo.ecm.core.api.IdRef;
029
030/**
031 * @deprecated since 10.2, unused
032 */
033@Deprecated
034public class PathCache {
035
036    private static final long FOLDER_LIFE_TIME = 30 * 60 * 1000;
037
038    private static final long FILE_LIFE_TIME = 1 * 60 * 1000;
039
040    private int maxSize;
041
042    private Map<String, Value> pathToUuidCache = new ConcurrentHashMap<String, Value>();
043
044    private CoreSession session;
045
046    public PathCache(CoreSession session, int maxSize) {
047        this.session = session;
048        this.maxSize = maxSize;
049    }
050
051    public void put(String path, DocumentModel model) {
052        if (model == null) {
053            return;
054        }
055        if (pathToUuidCache.size() >= maxSize) {
056            clean();
057        }
058        pathToUuidCache.put(path, new Value(System.currentTimeMillis()
059                + (model.isFolder() ? FOLDER_LIFE_TIME : FILE_LIFE_TIME), model.getId()));
060    }
061
062    public DocumentModel get(String path) {
063        Value value = pathToUuidCache.get(path);
064        if (value == null) {
065            return null;
066        }
067
068        if (value.getExpiredTime() < System.currentTimeMillis()) {
069            pathToUuidCache.remove(path);
070            return null;
071        }
072        String uuid = value.getValue();
073        DocumentModel model = null;
074        try {
075            model = session.getDocument(new IdRef(uuid));
076        } catch (DocumentNotFoundException e) {
077            // do nothing
078        }
079        if (model == null) {
080            pathToUuidCache.remove(path);
081        }
082
083        return model;
084    }
085
086    public void remove(String path) {
087        Map<String, Value> cacheCopy = new HashMap<String, Value>(pathToUuidCache);
088        for (String key : cacheCopy.keySet()) {
089            if (key.startsWith(path)) {
090                pathToUuidCache.remove(key);
091            }
092        }
093    }
094
095    private int clean() {
096        Map<String, Value> tmpMap = new HashMap<String, Value>(pathToUuidCache);
097        for (String key : tmpMap.keySet()) {
098            if ((pathToUuidCache.get(key).getExpiredTime()) < System.currentTimeMillis()) {
099                remove(key);
100            }
101        }
102        return pathToUuidCache.size();
103    }
104
105    private class Value {
106        private long expiredTime;
107
108        private String value;
109
110        private Value(long expiredTime, String value) {
111            this.expiredTime = expiredTime;
112            this.value = value;
113        }
114
115        public long getExpiredTime() {
116            return expiredTime;
117        }
118
119        @SuppressWarnings("unused")
120        public void setExpiredTime(long expiredTime) {
121            this.expiredTime = expiredTime;
122        }
123
124        public String getValue() {
125            return value;
126        }
127
128        @SuppressWarnings("unused")
129        public void setValue(String value) {
130            this.value = value;
131        }
132    }
133
134}