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    
018    package org.apache.commons.jci.stores;
019    
020    import java.util.ArrayList;
021    import java.util.HashMap;
022    import java.util.List;
023    import java.util.Map;
024    import org.apache.commons.logging.Log;
025    import org.apache.commons.logging.LogFactory;
026    
027    
028    /**
029     * Store just in memory
030     * 
031     * @author tcurdt
032     */
033    public final class MemoryResourceStore implements ResourceStore {
034    
035        private final Log log = LogFactory.getLog(MemoryResourceStore.class);
036    
037        private final Map<String, byte[]> store = new HashMap<String, byte[]>();
038    
039        public byte[] read( final String pResourceName ) {
040            log.debug("reading resource " + pResourceName);
041            return store.get(pResourceName);
042        }
043    
044        public void write( final String pResourceName, final byte[] pData ) {
045            log.debug("writing resource " + pResourceName + "(" + pData.length + ")");
046            store.put(pResourceName, pData);
047        }
048    
049        public void remove( final String pResourceName ) {
050            log.debug("removing resource " + pResourceName);
051            store.remove(pResourceName);
052        }
053    
054        /**
055         * @deprecated
056         */
057        @Deprecated
058        public String[] list() {
059            if (store == null) {
060                return new String[0];
061            }
062            final List<String> names = new ArrayList<String>();
063            
064            for (String name : store.keySet()) {
065                names.add(name);
066            }
067    
068            return names.toArray(new String[store.size()]);
069        }
070        
071        @Override
072        public String toString() {
073            return this.getClass().getName() + store.toString();
074        }
075    }