com.stehno.sanctuary.core.scan.DefaultDirectoryScanner.java Source code

Java tutorial

Introduction

Here is the source code for com.stehno.sanctuary.core.scan.DefaultDirectoryScanner.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.scan;

import com.stehno.sanctuary.core.ChangeSet;
import com.stehno.sanctuary.core.FileStatus;
import com.stehno.sanctuary.core.local.LocalStore;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.util.Assert;

import java.io.File;
import java.util.LinkedList;
import java.util.Queue;

/**
 * Default implementation of the DirectoryScanner.
 * 
 * @author cjstehno
 */
public class DefaultDirectoryScanner implements DirectoryScanner {

    private static final Log log = LogFactory.getLog(DefaultDirectoryScanner.class);
    private final LocalStore localStore;

    /**
     * Creates an instance of the default Directory scanner with the given local store.
     *
     * @param localStore the local store to be used
     */
    public DefaultDirectoryScanner(LocalStore localStore) {
        this.localStore = localStore;

        if (log.isDebugEnabled())
            log.debug("Instantiated");
    }

    /**
     * Scans the given directory for changes. The directory passed in will be used as the root
     * of the changeset and the stored files.
     * 
     * @param directory
     * @return a populated change set.
     */
    @Override
    public ChangeSet scanDirectory(final File directory) {
        Assert.isTrue(directory != null && directory.isDirectory(), "A non-null directory must be specified.");

        if (log.isDebugEnabled())
            log.debug("Scanning: " + directory);

        final ChangeSet changeSet = new ChangeSet(directory);

        final Queue<File> directories = new LinkedList<File>();
        directories.add(directory);

        while (!directories.isEmpty()) {
            final File scanningDir = directories.poll();

            for (final File item : scanningDir.listFiles()) {
                if (item.isDirectory()) {
                    directories.add(item);
                } else {
                    changeSet.addFileStatus(localStore.fileStatus(item), item);
                }
            }
        }

        // figure out the deleted files
        for (final String path : localStore.listFilePaths()) {
            final File file = new File(path);
            if (!file.exists()) {
                changeSet.addFileStatus(FileStatus.DELETED, file);
            }
        }

        return changeSet;
    }
}