view src/main/java/org/icedrobot/ika/plugins/borg/AndroidAssimilator.java @ 12:5fa4fe467471

Refactor framework and plugins.
author Mario Torre <neugens.limasoftware@gmail.com>
date Tue, 29 Mar 2011 23:53:18 +0200
parents b2827d00500a
children
line wrap: on
line source

/*
 * IKA - IcedRobot Kiosk Application
 * Copyright (C) 2011  IcedRobot team
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.icedrobot.ika.plugins.borg;

import org.icedrobot.ika.runtime.scm.Repository;
import org.icedrobot.ika.runtime.scm.GITRepository;
import java.io.File;
import java.io.IOException;
import java.util.Properties;
import java.util.concurrent.CyclicBarrier;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.icedrobot.ika.plugins.IkaPluginException;

/**
 * @author Mario Torre <neugens.limasoftware@gmail.com>
 */
class AndroidAssimilator {

    static String ANDROID_REPOS_CONFIGS =
        "/org/icedrobot/ika/plugins/borg/repositories.properties";

    static String ANDROID_SUBREPOS_CONFIGS =
        "/org/icedrobot/ika/plugins/borg/subrepositories.properties";

    static void assimilate(File base) {

        // contains all the remote repositories that we need to create and
        // jeopardise
        Properties configs = new Properties();
        try {
            configs.load(AndroidAssimilator.class.
                                   getResourceAsStream(ANDROID_REPOS_CONFIGS));

        } catch (IOException ex) {
            throw new IkaPluginException("cannot load configurations", ex);
        }

        // we spawn as many thread as a repositories to clone
        // so all the cloning (and assimilating) operations are
        // actually perfomed in parallel per repository
        CyclicBarrier barrier = new CyclicBarrier(configs.size() + 1);
        for (Object name : configs.keySet()) {

            String[] data = configs.getProperty((String) name).split(",");
            Repository repository = new GITRepository((String) name, base,
                                                       data[0], data[1],
                                                       data[2]);
            
            ClonerTask task = new ClonerTask(repository, barrier);
            Thread thread = new Thread(task, repository.getName());
            thread.setDaemon(true);
            thread.start();
        }

        // wait that all task are terinated before returning
        try {
            barrier.await();
            System.err.println("done!");
        } catch (Throwable ex) {
            Logger.getLogger(ClonerTask.class.getName()).log(Level.SEVERE,
                             ex.getMessage(), ex);
            throw new IkaPluginException(ex.getMessage(), ex);
        }
    }
}