ar.com.zauber.garfio.Main.java Source code

Java tutorial

Introduction

Here is the source code for ar.com.zauber.garfio.Main.java

Source

/**
 * Copyright (c) 2007-2009 Zauber S.A. <http://www.zauber.com.ar/>
 *
 * 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 ar.com.zauber.garfio;

import java.io.StringWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.codehaus.plexus.util.IOUtil;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import ar.com.zauber.garfio.config.impl.PropertiesConfiguration;
import ar.com.zauber.garfio.remoting.GarfioService;
import ar.com.zauber.garfio.services.Configuration;
import ar.com.zauber.garfio.services.ConfigurationDAO;
import ar.com.zauber.garfio.services.ErrorNotificator;
import ar.com.zauber.garfio.services.impl.DefaultGarfioService;
import ar.com.zauber.garfio.services.impl.InMemoryConfigurationDAO;

/**
 * Program to run from an svn hook.
 * 
 * Sincronize svn commits logs with MantisBT's issues. 
 * 
 * @author Fernando Zunino
 * @author Juan F. Codagnone
 * @since 09/02/2007
 */
public final class Main {
    /** hide constructor */
    private Main() {
        // utilidad
    }

    /**
     * @param args arguments.
     * @throws Exception on error 
     */
    public static void main(final String[] args) throws Exception {
        if (args.length < 3) {
            System.err.printf("Usage: %s revision username config.properties [dry-run]", Main.class);
            System.exit(1);
        }
        final String revision = args[0];
        final String username = args[1];
        final String config = args[2];
        final boolean isDryRun = args.length >= 4 && "dry-run".equals(args[3]);

        final PropertiesConfiguration configuration = getConfiguration(config, username, revision);
        final GarfioService garfioService = new DefaultGarfioService(getConfigurationDAO(configuration),
                getErrorNotificator());

        final StringWriter writer = new StringWriter();
        IOUtil.copy(System.in, writer);

        if (isDryRun) {
            final List<String> errors = garfioService.dryrun(configuration.getRepositoryName(), username,
                    writer.toString());

            for (String error : errors) {
                System.err.println(error);
            }

            System.exit(errors.isEmpty() ? 0 : 1);
        } else {
            garfioService.run(configuration.getRepositoryName(), revision, args[1], writer.toString());
        }
    }

    /** @return the error notification object */
    private static ErrorNotificator getErrorNotificator() {
        final ApplicationContext ctx = new ClassPathXmlApplicationContext(
                "ar/com/zauber/garfio/config/applicationContext.xml");

        final ErrorNotificator errorNotification = (ErrorNotificator) ctx.getBean("errorNotification");
        return errorNotification;
    }

    /** crea el {@link ConfigurationDAO} en base a las propiedades 
     * @throws Exception on error */
    private static ConfigurationDAO getConfigurationDAO(final PropertiesConfiguration configuration)
            throws Exception {
        final String id = configuration.getRepositoryName();
        final Map<String, Configuration> map = new HashMap<String, Configuration>();
        map.put(id, configuration);

        return new InMemoryConfigurationDAO(map);
    }

    /** @throws Exception on error  */
    private static PropertiesConfiguration getConfiguration(final String properties, final String username,
            final String revision) throws Exception {
        return new PropertiesConfiguration(properties, username, revision);
    }
}