001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.servicemix.store.memory;
018
019 import java.io.IOException;
020 import java.util.concurrent.ConcurrentHashMap;
021 import java.util.concurrent.ConcurrentMap;
022
023 import org.apache.commons.logging.Log;
024 import org.apache.commons.logging.LogFactory;
025 import org.apache.servicemix.id.IdGenerator;
026
027 /**
028 * {@link MemoryStore} which removes entries from the store after the specified timeout
029 * to free memory.
030 */
031 public class TimeoutMemoryStore extends MemoryStore {
032
033 private static final Log LOG = LogFactory.getLog(TimeoutMemoryStore.class);
034 private ConcurrentMap<String, Entry> datas = new ConcurrentHashMap<String, Entry>();
035 private final long timeout;
036
037 protected TimeoutMemoryStore(IdGenerator idGenerator, long timeout) {
038 super(idGenerator);
039 this.timeout = timeout;
040 }
041
042 /**
043 * {@inheritDoc}
044 */
045 public void store(String id, Object data) throws IOException {
046 LOG.debug("Storing object with id: " + id);
047 datas.put(id, new Entry(data));
048 }
049
050 /**
051 * {@inheritDoc}
052 *
053 * Before attempting to load the object, all data older than the specified timeout will first be
054 * removed from the store.
055 */
056 public Object load(String id) throws IOException {
057 evict();
058 LOG.debug("Loading object with id:" + id);
059 Entry entry = datas.remove(id);
060 return entry == null ? null : entry.data;
061 }
062
063 private void evict() {
064 long now = System.currentTimeMillis();
065 for (String key : datas.keySet()) {
066 long age = now - datas.get(key).time;
067 if (age > timeout) {
068 LOG.debug("Removing object with id " + key + " from store after " + age + " ms");
069 datas.remove(key);
070 }
071 }
072 }
073
074 /*
075 * A single store entry
076 */
077 private final class Entry {
078 final long time = System.currentTimeMillis();
079 final Object data;
080
081 private Entry(Object data) {
082 this.data = data;
083 }
084 }
085 }