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

Java tutorial

Introduction

Here is the source code for de.fhg.iais.asc.ASC.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.net.URL;
import java.util.List;

import org.apache.commons.cli.BasicParser;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.slf4j.LoggerFactory;

import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.joran.JoranConfigurator;
import ch.qos.logback.core.joran.spi.JoranException;
import ch.qos.logback.core.util.StatusPrinter;

import com.google.common.base.Strings;

import de.fhg.iais.asc.commons.AscConfiguration;
import de.fhg.iais.asc.concordance.Concordance;
import de.fhg.iais.asc.model.Model;
import de.fhg.iais.asc.transformer.jdom.xslt.impl.AscTransformXSLResolver;
import de.fhg.iais.asc.util.ProviderInfoRequester;
import de.fhg.iais.asc.workflow.ASCWorkflow;
import de.fhg.iais.asc.xslt.binaries.download.Downloader;
import de.fhg.iais.asc.xslt.binaries.scale.conf.ConfBinariesXML;
import de.fhg.iais.asc.xslt.ntv2.converter.ConverterXSLTBridge;
import de.fhg.iais.commons.annotation.UsedBy;
import de.fhg.iais.commons.configuration.FileResourceLocator;
import de.fhg.iais.commons.configuration.IResourceLocator;
import de.fhg.iais.cortex.server.client.RestIngestMiniFactory;

/**
 * Commandline ASC.
 * 
 * @author kst
 */
@UsedBy("main class")
public class ASC {
    private static IResourceLocator HOMEDIR = new FileResourceLocator();
    private static IResourceLocator TRANSFORMATIONSDIR = null;

    /**
     * main method
     * 
     * @param arga
     * @throws ParseException
     */
    public static void main(final String[] arga) throws ParseException {

        Options options = createASCOptions();

        CommandLineParser parser = new BasicParser();
        CommandLine cl = parser.parse(options, arga);

        if (cl.hasOption("help")) {
            new HelpFormatter().printHelp("java -jar asc.jar [OPTIONS]*", options);
            return;
        }

        List<AscConfiguration> configs = readCommandLine(cl);

        LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();

        try {
            JoranConfigurator configurator = new JoranConfigurator();
            configurator.setContext(lc);
            lc.reset();
            configurator.doConfigure(HOMEDIR.getFile("conf/asc/logback.xml"));
        } catch (JoranException je) {
            je.printStackTrace(); // NOSONAR
        }

        StatusPrinter.printInCaseOfErrorsOrWarnings(lc);

        setGlobalProxy(configs);
        initXSLUtilityClasses(configs);

        ASCWorkflow asc = new ASCWorkflow(TRANSFORMATIONSDIR, HOMEDIR);
        asc.run(configs);

        RestIngestMiniFactory.shutdown();
    }

    private static Options createASCOptions() {
        Options options = new Options();
        options.addOption("asc", true, "Konfigurationsdatei des ASC. (Standard: asc.properties)");
        options.addOption("provider", true,
                "Providerspezifische Konfigurationsdatei. Es knnen mehrere Dateien in Anfhrungszeichen und durch Leerstellen getrennt angegeben werden. (Standard: provider.properties)");
        options.addOption("help", false, "Zeigt diese Hilfe an.");
        return options;
    }

    private static void setGlobalProxy(List<AscConfiguration> configs) {
        for (AscConfiguration config : configs) {
            String proxyUrl = config.get(AscConfiguration.PROXY, "");
            try {
                if (!Strings.isNullOrEmpty(proxyUrl)) {
                    URL url = new URL(proxyUrl);
                    String host = url.getHost();
                    String port = String.valueOf(url.getPort());

                    System.setProperty("http.proxyHost", host);
                    System.setProperty("http.proxyPort", port);
                    System.setProperty("https.proxyHost", host);
                    System.setProperty("https.proxyPort", port);
                }
            } catch (Exception e) {
                System.setProperty("http.proxyHost", null);
                System.setProperty("http.proxyPort", null);
                System.setProperty("https.proxyHost", null);
                System.setProperty("https.proxyPort", null);
                continue;
            }
        }
    }

    public static void initXSLUtilityClasses(List<AscConfiguration> configs) {

        for (AscConfiguration config : configs) {
            String transformationsDir = config.get(AscConfiguration.TRANSFORMATIONS_DIR, ".");
            TRANSFORMATIONSDIR = new FileResourceLocator(transformationsDir);
            Concordance.init(TRANSFORMATIONSDIR);
            Downloader.init(ProxyType.DOWNLOADER.createProxiedClient(config));
            AscTransformXSLResolver.init(ProxyType.XSLT_PROCESSOR.createProxiedClient(config));
            ProviderInfoRequester.init(ProxyType.PROVIDERINFOREQUESTER.createProxiedClient(config), config);
            Model.init(TRANSFORMATIONSDIR);
            ConfBinariesXML.init(TRANSFORMATIONSDIR);
            ConverterXSLTBridge.init(TRANSFORMATIONSDIR);
            break;
        }
    }

    /**
     * Read the arguments either from the command line or the properties file.
     * 
     * @param cl
     * @return
     */
    public static List<AscConfiguration> readCommandLine(CommandLine cl) {
        return new AscConfigurationsFromArgs(HOMEDIR, cl).getConfigs();
    }
}