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

Java tutorial

Introduction

Here is the source code for com.stehno.sanctuary.core.remote.InMemoryRemoteStore.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 java.io.File;
import java.util.*;

/**
 * Remote store that simply stores file references in memory. This remote store is not
 * useful for normal operations and simply exists to provide an active remote store
 * implementation for testing and prototyping.
 *
 * This should NOT be used in any production environment.
 *
 * @author cjstehno
 */
public class InMemoryRemoteStore implements RemoteStore {

    private static final Log log = LogFactory.getLog(InMemoryRemoteStore.class);
    private final Map<String, Set<String>> stores = new HashMap<String, Set<String>>();

    public void init() {
        if (log.isDebugEnabled())
            log.debug("Initialized");
    }

    public void destroy() {
        if (log.isDebugEnabled())
            log.debug("Destroyed");
    }

    public Collection<String> getStoredPaths(File rootDir) {
        return Collections.unmodifiableCollection(stores.get(extractRoot(rootDir)));
    }

    @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 String root = extractRoot(rootDirectory);
        Set<String> store = stores.get(root);
        if (store != null) {
            final String storeFile = extractStoreFile(file, root);
            if (log.isDebugEnabled())
                log.debug("Deleted[" + root + "]: " + storeFile);

            store.remove(storeFile);
        }
    }

    private String extractRoot(File rootDirectory) {
        return rootDirectory.toURI().toString();
    }

    private String extractStoreFile(File file, String root) {
        return file.toURI().toString().replace(root, "");
    }

    private void storeFile(final File rootDirectory, final File file, final String action) {
        final String root = extractRoot(rootDirectory);
        Set<String> store = stores.get(root);
        if (store == null) {
            store = new HashSet<String>();
            stores.put(root, store);
        }

        final String storeFile = extractStoreFile(file, root);
        if (log.isDebugEnabled())
            log.debug(action + "[" + root + "]: " + storeFile);

        store.add(storeFile);
    }
}