001/* 002 * (C) Copyright 2006-2008 Nuxeo SAS (http://nuxeo.com/) and contributors. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the GNU Lesser General Public License 006 * (LGPL) version 2.1 which accompanies this distribution, and is available at 007 * http://www.gnu.org/licenses/lgpl.html 008 * 009 * This library is distributed in the hope that it will be useful, 010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 012 * Lesser General Public License for more details. 013 * 014 * Contributors: 015 * bstefanescu 016 */ 017package org.nuxeo.ecm.webengine.loader.store; 018 019import java.io.ByteArrayInputStream; 020import java.io.IOException; 021import java.io.InputStream; 022import java.net.URL; 023import java.util.HashMap; 024import java.util.Map; 025 026import org.nuxeo.common.utils.FileUtils; 027 028/** 029 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a> 030 */ 031public class MemoryStore implements ResourceStore { 032 033 protected final Map<String, byte[]> store; 034 035 protected String location; 036 037 public MemoryStore() { 038 this(new HashMap<String, byte[]>()); 039 } 040 041 public MemoryStore(Map<String, byte[]> store) { 042 this.store = store; 043 this.location = "java:" + System.identityHashCode(this); 044 } 045 046 public boolean exists(String name) { 047 return store.containsKey(name); 048 } 049 050 public byte[] getBytes(String name) { 051 return store.get(name); 052 } 053 054 public InputStream getStream(String name) { 055 byte[] data = store.get(name); 056 return data == null ? null : new ByteArrayInputStream(data); 057 } 058 059 public URL getURL(String name) { // TODO not yet implemented 060 return null; 061 } 062 063 public long lastModified(String name) { 064 return 0; 065 } 066 067 public void put(String name, InputStream data) throws IOException { 068 store.put(name, FileUtils.readBytes(data)); 069 } 070 071 public void put(String name, byte[] data) throws IOException { 072 store.put(name, data); 073 } 074 075 public void remove(String name) { 076 store.remove(name); 077 } 078 079 public String getLocation() { 080 return "java"; 081 } 082 083 @Override 084 public String toString() { 085 return getLocation(); 086 } 087}