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