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:bs@nuxeo.com">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    @Override
054    public boolean exists(String name) {
055        return getFile(name).exists();
056    }
057
058    @Override
059    public long lastModified(String name) {
060        return getFile(name).lastModified();
061    }
062
063    @Override
064    public URL getURL(String name) {
065        try {
066            File file = getFile(name);
067            if (file.exists()) {
068                return file.toURI().toURL();
069            }
070        } catch (IOException e) {
071            log.error("Failed to transform file to URL: " + name, e);
072        }
073        return null;
074    }
075
076    @Override
077    public byte[] getBytes(String name) {
078        try (InputStream in = getStream(name)) {
079            if (in != null) {
080                return IOUtils.toByteArray(in);
081            }
082        } catch (IOException e) {
083            log.error("Failed to read file: " + name, e);
084        }
085        return null;
086    }
087
088    @Override
089    public InputStream getStream(String name) {
090        try {
091            return new FileInputStream(getFile(name));
092        } catch (IOException e) {
093        }
094        return null;
095    }
096
097    @Override
098    public void remove(String name) {
099        getFile(name).delete();
100    }
101
102    @Override
103    public void put(String name, byte[] data) throws IOException {
104        FileUtils.writeByteArrayToFile(getFile(name), data);
105    }
106
107    @Override
108    public void put(String name, InputStream data) throws IOException {
109        FileUtils.copyInputStreamToFile(data, getFile(name));
110    }
111
112    @Override
113    public String getLocation() {
114        return root.toString();
115    }
116
117    @Override
118    public boolean equals(Object obj) {
119        if (this == obj) {
120            return true;
121        }
122        if (obj instanceof FileResourceStore) {
123            FileResourceStore store = (FileResourceStore) obj;
124            return store.root.equals(root);
125        }
126        return false;
127    }
128
129    @Override
130    public int hashCode() {
131        return root.hashCode();
132    }
133
134    @Override
135    public String toString() {
136        return "FileResourceStore: " + root;
137    }
138
139}