de.fhg.iais.asc.AscConfigurationsFromArgs.java Source code

Java tutorial

Introduction

Here is the source code for de.fhg.iais.asc.AscConfigurationsFromArgs.java

Source

package de.fhg.iais.asc;

/******************************************************************************
 * Copyright 2011 (c) Fraunhofer IAIS Netmedia  http://www.iais.fraunhofer.de *
 * ************************************************************************** *
 * 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.                                             *
 ******************************************************************************/

import java.io.File;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.filefilter.FileFilterUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.base.Splitter;

import de.fhg.iais.asc.commons.AscConfiguration;
import de.fhg.iais.commons.configuration.IResourceLocator;

class AscConfigurationsFromArgs {
    private static final Logger LOG = LoggerFactory.getLogger(AscConfigurationsFromArgs.class);

    private final IResourceLocator homeDir;
    private final String ascProperties;
    private final List<AscConfiguration> configs = new LinkedList<AscConfiguration>();

    public AscConfigurationsFromArgs(IResourceLocator homeDir, CommandLine cl) {
        this.homeDir = homeDir;
        this.ascProperties = cl.hasOption("asc") ? cl.getOptionValue("asc") : "asc.properties";

        String providerProperties = cl.hasOption("provider") ? cl.getOptionValue("provider")
                : "provider.properties";

        Iterator<String> it = Splitter.on(" ").trimResults().split(providerProperties).iterator();
        while (it.hasNext()) {
            addPath(it.next());
        }
    }

    private void addPath(String path) {
        if (StringUtils.isEmpty(path)) {
            return;
        }

        File file = this.homeDir.getFile(path);

        if (file.isDirectory()) {
            int count = 0;
            // TODO: Check if it is working
            for (File propertyFile : FileUtils.listFiles(file, FileFilterUtils.suffixFileFilter(".properties"),
                    FileFilterUtils.directoryFileFilter())) {
                addSingleFile(propertyFile.getAbsolutePath());
                ++count;
            }

            LOG.info("Provider info: " + path + "/*.properties has " + count + " files");
            if (count == 0) {
                LOG.error("Could not find any provider properties in " + this.homeDir.getFile(path));
            }
        } else {
            LOG.info("Provider info: %", path);
            addSingleFile(path);
        }
    }

    private void addSingleFile(String propertyPath) {
        AscConfiguration config = new AscConfiguration(this.ascProperties, propertyPath);
        this.configs.add(config);
    }

    public List<AscConfiguration> getConfigs() {
        return this.configs;
    }
}