001/* 002 * (C) Copyright 2006-2008 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 * bstefanescu 018 */ 019package org.nuxeo.ecm.webengine.loader.store; 020 021import java.io.File; 022import java.io.FileInputStream; 023import java.io.IOException; 024import java.io.InputStream; 025import java.net.URL; 026 027import org.apache.commons.logging.Log; 028import org.apache.commons.logging.LogFactory; 029import org.nuxeo.common.utils.FileUtils; 030 031/** 032 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a> 033 */ 034public class FileResourceStore implements ResourceStore { 035 036 private static final Log log = LogFactory.getLog(FileResourceStore.class); 037 038 protected File root; 039 040 public FileResourceStore(File root) throws IOException { 041 this.root = root.getCanonicalFile(); 042 } 043 044 public File getRoot() { 045 return root; 046 } 047 048 public final File getFile(String name) { 049 return new File(root, name); 050 } 051 052 public boolean exists(String name) { 053 return getFile(name).exists(); 054 } 055 056 public long lastModified(String name) { 057 return getFile(name).lastModified(); 058 } 059 060 public URL getURL(String name) { 061 try { 062 File file = getFile(name); 063 if (file.exists()) { 064 return file.toURI().toURL(); 065 } 066 } catch (IOException e) { 067 log.error("Failed to transform file to URL: " + name, e); 068 } 069 return null; 070 } 071 072 public byte[] getBytes(String name) { 073 InputStream in = getStream(name); 074 if (in != null) { 075 try { 076 return FileUtils.readBytes(in); 077 } catch (IOException e) { 078 log.error("Failed to read file: " + name, e); 079 } 080 } 081 return null; 082 } 083 084 public InputStream getStream(String name) { 085 try { 086 return new FileInputStream(getFile(name)); 087 } catch (IOException e) { 088 } 089 return null; 090 } 091 092 public void remove(String name) { 093 getFile(name).delete(); 094 } 095 096 public void put(String name, byte[] data) throws IOException { 097 FileUtils.writeFile(getFile(name), data); 098 } 099 100 public void put(String name, InputStream data) throws IOException { 101 FileUtils.copyToFile(data, getFile(name)); 102 } 103 104 public String getLocation() { 105 return root.toString(); 106 } 107 108 @Override 109 public boolean equals(Object obj) { 110 if (this == obj) { 111 return true; 112 } 113 if (obj instanceof FileResourceStore) { 114 FileResourceStore store = (FileResourceStore) obj; 115 return store.root.equals(root); 116 } 117 return false; 118 } 119 120 @Override 121 public int hashCode() { 122 return root.hashCode(); 123 } 124 125 @Override 126 public String toString() { 127 return "FileResourceStore: " + root; 128 } 129 130}