com.stehno.sanctuary.core.Sanctuary.java Source code

Java tutorial

Introduction

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

import com.stehno.sanctuary.core.archive.DefaultFileArchiver;
import com.stehno.sanctuary.core.local.JdbcLocalStore;
import com.stehno.sanctuary.core.scan.DirectoryScanner;
import com.stehno.sanctuary.core.archive.FileArchiver;
import com.stehno.sanctuary.core.local.LocalStore;
import com.stehno.sanctuary.core.remote.RemoteStore;
import com.stehno.sanctuary.core.remote.S3RemoteStore;
import com.stehno.sanctuary.core.scan.DefaultDirectoryScanner;
import org.apache.commons.dbcp.cpdsadapter.DriverAdapterCPDS;
import org.apache.commons.dbcp.datasources.SharedPoolDataSource;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;
import java.io.File;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * this is a temporary testing/demo runner class. this class will eventually be removed.
 * @author cjstehno
 */
public class Sanctuary {

    private static final Log log = LogFactory.getLog(Sanctuary.class);
    private static final File WORKING_DIR = new File("C:/someworking/dir");

    public static void main(String[] args) throws Exception {
        log.info("Starting run...");

        JdbcTemplate jdbcTemplate = new JdbcTemplate(createDataSource());

        // FIXME: AndroidLocalStore
        LocalStore localStore = new JdbcLocalStore(jdbcTemplate);
        localStore.init();

        // FIXME: FileSystemRemoteStore, S3RemoteStore, AndroidS3LocalStore?
        // FIXME: remote keys
        RemoteStore remoteStore = new S3RemoteStore("yourkey", "yourotherkey");
        remoteStore.init();

        DirectoryScanner scanner = new DefaultDirectoryScanner(localStore);

        ExecutorService executor = new ThreadPoolExecutor(2, 2, 1, TimeUnit.MINUTES,
                new LinkedBlockingQueue<Runnable>());
        FileArchiver archiver = new DefaultFileArchiver(executor, localStore, remoteStore);

        ChangeSet changeSet = scanner.scanDirectory(WORKING_DIR);

        // FIXME: allow review of changeset here (confirm/cancel)
        log.info("Changes: " + changeSet);

        MessageSet messageSet = archiver.archiveChanges(changeSet);

        // FIXME: allow review of messages here
        log.info("Messages" + messageSet);

        remoteStore.destroy();
        localStore.destroy();
    }

    private static DataSource createDataSource() throws ClassNotFoundException {
        SharedPoolDataSource poolDataSource = new SharedPoolDataSource();

        DriverAdapterCPDS cpds = new DriverAdapterCPDS();
        cpds.setDriver("org.h2.Driver");
        cpds.setUrl("jdbc:h2:/home/cjstehno/h2/sanctuary");
        cpds.setUser("sa");
        cpds.setPassword("");

        poolDataSource.setConnectionPoolDataSource(cpds);
        poolDataSource.setMaxActive(10);
        poolDataSource.setMaxIdle(5);
        poolDataSource.setMinEvictableIdleTimeMillis(1000);
        poolDataSource.setTestWhileIdle(true);
        poolDataSource.setValidationQuery("select 1");

        return poolDataSource;
    }
}