ar.com.zauber.garfio.config.impl.PropertiesConfiguration.java Source code

Java tutorial

Introduction

Here is the source code for ar.com.zauber.garfio.config.impl.PropertiesConfiguration.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.config.impl;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import java.util.Properties;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.Validate;

import ar.com.zauber.garfio.modules.GarfioModule;
import ar.com.zauber.garfio.modules.model.TrackerSession;
import ar.com.zauber.garfio.modules.services.CommentFormatter;
import ar.com.zauber.garfio.modules.services.NotificationAddressProvider;
import ar.com.zauber.garfio.services.Configuration;
import ar.com.zauber.garfio.services.LogParserFactory;
import ar.com.zauber.garfio.services.impl.SimpleLogParserFactory;
import ar.com.zauber.garfio.services.impl.SvnCommentFormatter;
import ar.com.zauber.garfio.services.impl.SvnCommitCommand;

/**
 * TODO Descripcion de la clase. Los comenterios van en castellano.
 * 
 * 
 * @author Juan F. Codagnone
 * @since Oct 7, 2007
 */
public class PropertiesConfiguration extends AbstractConfiguration {
    private final Properties config;
    private final TrackerSession trackerSession;
    private final CommentFormatter commentFormatter;
    private final LogParserFactory parserFactory;
    private GarfioModule garfioModule;
    /** property */
    public static final String GARFIO_MODULE_CLASS = "garfio.module.class";
    /** property */
    public static final String GARFIO_REPO_NAME = "garfio.repo.name";
    /** property */
    public static final String GARFIO_CLASSIC_USERS = "garfio.classic.users";
    /** property */
    public static final String GARFIO_TEMPLATE = "garfio.template";

    /** constructor @throws Exception on error */
    public PropertiesConfiguration(final String filename, final String username, final String revision)
            throws Exception {
        this(getProperties(filename), username, revision);
    }

    /** constructor @throws Exception on error */
    public PropertiesConfiguration(final Properties properties, final String username, final String revision)
            throws Exception {

        Validate.notNull(properties);
        Validate.notEmpty(username);

        this.config = properties;

        final String moduleClassName = config.getProperty(GARFIO_MODULE_CLASS);
        Validate.isTrue(!StringUtils.isBlank(moduleClassName), "You must set property " + GARFIO_MODULE_CLASS);

        try {
            final Class<GarfioModule> gafioModuleClass = (Class<GarfioModule>) Class.forName(moduleClassName);

            garfioModule = gafioModuleClass.newInstance();
        } catch (ClassNotFoundException e) {
            throw new IllegalArgumentException("the class specified in the " + "property " + GARFIO_MODULE_CLASS
                    + " was not found `" + moduleClassName + "'");
        }

        final String classicUsers = config.getProperty(GARFIO_CLASSIC_USERS, "");
        final String template = config.getProperty(GARFIO_TEMPLATE);
        final Reader reader;
        if (template == null) {
            reader = new InputStreamReader(PropertiesConfiguration.class.getClassLoader()
                    .getResourceAsStream("ar/com/zauber/garfio/templates/comment.vm"));
        } else {
            reader = new InputStreamReader(new FileInputStream(template));
        }
        Validate.notNull(reader);
        commentFormatter = new SvnCommentFormatter(reader, new SvnCommitCommand(revision, getRepositoryName()));

        trackerSession = garfioModule.getTrackerSession(commentFormatter, username, config);
        Validate.notNull(trackerSession);

        parserFactory = new SimpleLogParserFactory(classicUsers, trackerSession);
    }

    /** @see Configuration#getTrackerSession() */
    public final TrackerSession getTrackerSession() {
        return trackerSession;
    }

    /** @see Configuration#getCommentTemplate() */
    public final Reader getCommentTemplate() {
        return getTemplateReader(config.getProperty(GARFIO_TEMPLATE));
    }

    /** @see Configuration#getRepositoryName() */
    public final String getRepositoryName() {
        final String reponame = config.getProperty(GARFIO_REPO_NAME);
        Validate.notEmpty(reponame, "property " + GARFIO_REPO_NAME + " must be specified");
        return reponame;
    }

    /**
     * @param filename
     * @throws  IOException on error
     */
    private static Properties getProperties(final String filename) throws IOException {
        Validate.notEmpty(filename);
        final Properties config = new Properties();
        config.load(new FileInputStream(filename));
        return config;
    }

    /** @see Configuration#getNoticationAddressProvider() */
    public final NotificationAddressProvider getNoticationAddressProvider() {
        return garfioModule.getNotificationAddressProvider(trackerSession);
    }

    /** @see Configuration#getCommentFormatter() */
    public final CommentFormatter getCommentFormatter() {
        return commentFormatter;
    }

    /** @see Configuration#getParserFactory() */
    public final LogParserFactory getParserFactory() {
        return parserFactory;
    }
}