Java tutorial
/* * 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.archive; import com.stehno.sanctuary.core.ChangeSet; import com.stehno.sanctuary.core.FileStatus; import com.stehno.sanctuary.core.MessageSet; import com.stehno.sanctuary.core.local.LocalStore; import com.stehno.sanctuary.core.remote.RemoteStore; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import java.io.File; import java.util.LinkedList; import java.util.Queue; import java.util.concurrent.ExecutorService; import java.util.concurrent.Future; /** * @author cjstehno */ public class DefaultFileArchiver implements FileArchiver { private static final Log log = LogFactory.getLog(DefaultFileArchiver.class); private final LocalStore localStore; private final RemoteStore remoteStore; private final ExecutorService executor; private long collectionWaitTime = 100; public DefaultFileArchiver(ExecutorService executor, LocalStore localStore, RemoteStore remoteStore) { this.executor = executor; this.localStore = localStore; this.remoteStore = remoteStore; if (log.isDebugEnabled()) log.debug("Instatiated"); } @Override public MessageSet archiveChanges(final ChangeSet changeSet) { if (log.isDebugEnabled()) log.debug("Archiving changes for " + changeSet.getRootDirectory() + "..."); final MessageSet messageSet = new MessageSet(changeSet.getRootDirectory().getPath()); final Queue<Future> futures = new LinkedList<Future>(); int count = 0; for (final File file : changeSet.listFiles(FileStatus.NEW)) { futures.add(executor.submit(new Runnable() { @Override public void run() { try { remoteStore.addFile(changeSet.getRootDirectory(), file); localStore.storeFile(file); messageSet.addMessage(file.getPath(), "Added successfully"); } catch (Exception ex) { messageSet.addError(file.getPath(), "Add failed: " + ex.getMessage()); } } })); count++; } if (log.isDebugEnabled()) log.debug("Scheduled Adds: " + count); count = 0; for (final File file : changeSet.listFiles(FileStatus.MODIFIED)) { futures.add(executor.submit(new Runnable() { @Override public void run() { try { remoteStore.updateFile(changeSet.getRootDirectory(), file); localStore.storeFile(file); messageSet.addMessage(file.getPath(), "Updated successfully"); } catch (Exception ex) { messageSet.addError(file.getPath(), "Update failed: " + ex.getMessage()); } } })); count++; } if (log.isDebugEnabled()) log.debug("Scheduled Updates: " + count); count = 0; for (final File file : changeSet.listFiles(FileStatus.DELETED)) { futures.add(executor.submit(new Runnable() { @Override public void run() { try { remoteStore.deleteFile(changeSet.getRootDirectory(), file); localStore.removeFile(file); messageSet.addMessage(file.getPath(), "Deleted successfully"); } catch (Exception ex) { messageSet.addError(file.getPath(), "Delete failed: " + ex.getMessage()); } } })); count++; } if (log.isDebugEnabled()) log.debug("Scheduled Deletes: " + count); do { while (!futures.isEmpty()) { if (futures.peek().isDone()) { futures.poll(); } } if (collectionWaitTime > 0) { try { Thread.sleep(100); } catch (InterruptedException ie) { } } } while (!futures.isEmpty()); return messageSet; } }