com.stehno.sanctuary.core.remote.FileSystemRemoteStore.java Source code

Java tutorial

Introduction

Here is the source code for com.stehno.sanctuary.core.remote.FileSystemRemoteStore.java

Source

/*
 * Copyright (c) 2011 Christopher J. Stehno
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *        http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.stehno.sanctuary.core.remote;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.vfs2.*;
import org.springframework.util.Assert;

import java.io.File;

/**
 * @author cjstehno
 */
public class FileSystemRemoteStore implements RemoteStore {
    // TODO: uses VFS for additional features, ensure they can be accessed (different filesystem types)
    // TODO: consider backing up last version of file on update or delete

    private static final Log log = LogFactory.getLog(FileSystemRemoteStore.class);
    private final File remoteDir;
    private FileSystemManager fileSystemManager;
    private FileObject remoteRoot;

    public FileSystemRemoteStore(final File remoteDir) {
        // TODO: maybe just do the init stuff here and get rid of this
        this.remoteDir = remoteDir;
    }

    @Override
    public void init() {
        Assert.isTrue(remoteDir.exists(), "Remote directory does not exist.");
        Assert.isTrue(remoteDir.canRead(), "Remote directory is not readable.");
        Assert.isTrue(remoteDir.canWrite(), "Remote directory is not writable.");

        try {
            this.fileSystemManager = VFS.getManager();
            this.remoteRoot = fileSystemManager.toFileObject(remoteDir);

        } catch (FileSystemException e) {
            log.fatal("Unable to retrieve filesystem manager: " + e.getMessage(), e);
        }

        if (log.isInfoEnabled())
            log.info("Initialized for: " + remoteRoot);
    }

    @Override
    public void destroy() {
        if (remoteRoot != null) {
            try {
                remoteRoot.close();
            } catch (FileSystemException e) {
                /* ignore */
            }
        }

        if (log.isInfoEnabled())
            log.info("Destroyed");
    }

    @Override
    public void addFile(File rootDirectory, File file) {
        storeFile(rootDirectory, file, "Added");
    }

    @Override
    public void updateFile(File rootDirectory, File file) {
        storeFile(rootDirectory, file, "Updated");
    }

    @Override
    public void deleteFile(File rootDirectory, File file) {
        final FileObject remoteFile;
        try {
            remoteFile = findRemoteFile(rootDirectory, file);
            remoteFile.delete();

            if (log.isDebugEnabled())
                log.debug("Deleted: " + remoteFile);

        } catch (FileSystemException e) {
            // FIXME: error
            e.printStackTrace();
        }
    }

    private FileObject findRemoteFile(File rootDirectory, File file) throws FileSystemException {
        final String localRoot = rootDirectory.toURI().toString();
        final String localFile = file.toURI().toString();
        final String relativePath = localFile.replace(localRoot, "");

        return remoteRoot.resolveFile(relativePath);
    }

    private void storeFile(File rootDirectory, File file, String action) {
        try {
            FileObject remoteFile = findRemoteFile(rootDirectory, file);

            // TODO: if this fails, it could be a partial copy, save original?
            remoteFile.copyFrom(fileSystemManager.toFileObject(file), new AllFileSelector());

            if (log.isDebugEnabled())
                log.debug(action + ": " + remoteFile);

        } catch (FileSystemException e) {
            // FIXME: error
            e.printStackTrace();
        }
    }
}