001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one
003 *  or more contributor license agreements.  See the NOTICE file
004 *  distributed with this work for additional information
005 *  regarding copyright ownership.  The ASF licenses this file
006 *  to you under the Apache License, Version 2.0 (the
007 *  "License"); you may not use this file except in compliance
008 *  with the License.  You may obtain a copy of the License at
009 *
010 *        http://www.apache.org/licenses/LICENSE-2.0
011 *
012 *  Unless required by applicable law or agreed to in writing,
013 *  software distributed under the License is distributed on an
014 *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015 *  KIND, either express or implied.  See the License for the
016 *  specific language governing permissions and limitations
017 *  under the License.
018 */
019
020package org.apache.isis.core.commons.resource;
021
022import java.io.File;
023import java.io.FileInputStream;
024import java.io.FileNotFoundException;
025import java.io.FileOutputStream;
026import java.io.InputStream;
027import java.io.OutputStream;
028
029public class ResourceStreamSourceFileSystem extends ResourceStreamSourceAbstract {
030
031    /**
032     * Factory method to guard against nulls
033     */
034    public static ResourceStreamSource create(final String directory2) {
035        return directory2 != null ? new ResourceStreamSourceFileSystem(directory2) : null;
036    }
037
038    private final String directory;
039
040    public ResourceStreamSourceFileSystem(final String directory) {
041        this.directory = directory;
042    }
043
044    @Override
045    protected InputStream doReadResource(final String resourcePath) throws FileNotFoundException {
046        final File file = new File(directory, resourcePath);
047        return new FileInputStream(file);
048    }
049
050    @Override
051    public OutputStream writeResource(final String resourcePath) {
052        final File file = new File(directory, resourcePath);
053        try {
054            return new FileOutputStream(file);
055        } catch (final FileNotFoundException e) {
056            return null;
057        }
058    }
059
060    @Override
061    public String getName() {
062        return "file system (directory '" + directory + "')";
063    }
064
065}