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.Map;
021    import java.util.concurrent.ConcurrentHashMap;
022    
023    import org.apache.commons.logging.Log;
024    import org.apache.commons.logging.LogFactory;
025    import org.apache.servicemix.id.IdGenerator;
026    import org.apache.servicemix.store.Store;
027    
028    /**
029     * A simple memory store implementation based on a simple map.
030     * This store is neither clusterable, nor persistent, nor transactional.
031     * 
032     * @author gnodet
033     */
034    public class MemoryStore implements Store {
035    
036        private static final Log LOG = LogFactory.getLog(MemoryStore.class);
037    
038        private Map<String, Object> datas = new ConcurrentHashMap<String, Object>();
039    
040        private IdGenerator idGenerator;
041    
042        public MemoryStore(IdGenerator idGenerator) {
043            this.idGenerator = idGenerator;
044        }
045    
046        public boolean hasFeature(String name) {
047            return false;
048        }
049    
050        public void store(String id, Object data) throws IOException {
051            LOG.debug("Storing object with id: " + id);
052            datas.put(id, data);
053        }
054    
055        public String store(Object data) throws IOException {
056            String id = idGenerator.generateId();
057            store(id, data);
058            return id;
059        }
060    
061        public Object load(String id) throws IOException {
062            LOG.debug("Loading object with id: " + id);
063            return datas.remove(id);
064        }
065    
066    }