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