de.prozesskraft.pkraft.Createmap.java Source code

Java tutorial

Introduction

Here is the source code for de.prozesskraft.pkraft.Createmap.java

Source

package de.prozesskraft.pkraft;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

import javax.xml.bind.JAXBException;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.GnuParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.Option;
import org.apache.commons.cli.OptionBuilder;
import org.apache.commons.cli.Options;
import org.ini4j.Ini;
import org.ini4j.InvalidFileFormatException;

import de.prozesskraft.commons.*;

public class Createmap {

    /*----------------------------
      structure
    ----------------------------*/
    static CommandLine commandline;
    static String author = "alexander.vogel@prozesskraft.de";
    static String version = "[% version %]";
    static String date = "[% date %]";
    static Ini ini;

    /*----------------------------
      constructors
    ----------------------------*/
    //   public static void main(String[] args) throws SAXException, IOException, ParserConfigurationException
    public static void main(String[] args) throws org.apache.commons.cli.ParseException, IOException {

        //      try
        //      {
        //         if (args.length != 3)
        //         {
        //            System.out.println("Please specify processdefinition file (xml) and an outputfilename");
        //         }
        //         
        //      }
        //      catch (ArrayIndexOutOfBoundsException e)
        //      {
        //         System.out.println("***ArrayIndexOutOfBoundsException: Please specify processdefinition.xml, openoffice_template.od*, newfile_for_processdefinitions.odt\n" + e.toString());
        //      }

        /*----------------------------
          get options from ini-file
        ----------------------------*/
        File inifile = new java.io.File(
                WhereAmI.getInstallDirectoryAbsolutePath(Createmap.class) + "/" + "../etc/pkraft-createmap.ini");

        if (inifile.exists()) {
            try {
                ini = new Ini(inifile);
            } catch (InvalidFileFormatException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            } catch (IOException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        } else {
            System.err.println("ini file does not exist: " + inifile.getAbsolutePath());
            System.exit(1);
        }

        /*----------------------------
          create boolean options
        ----------------------------*/
        Option ohelp = new Option("help", "print this message");

        /*----------------------------
          create argument options
        ----------------------------*/
        Option ooutput = OptionBuilder.withArgName("FILE").hasArg()
                .withDescription("[mandatory; default: process.dot] file for generated map.")
                //            .isRequired()
                .create("output");

        Option odefinition = OptionBuilder.withArgName("FILE").hasArg()
                .withDescription("[mandatory] process definition file.")
                //            .isRequired()
                .create("definition");

        /*----------------------------
          create options object
        ----------------------------*/
        Options options = new Options();

        options.addOption(ohelp);
        options.addOption(ooutput);
        options.addOption(odefinition);

        /*----------------------------
          create the parser
        ----------------------------*/
        CommandLineParser parser = new GnuParser();
        try {
            // parse the command line arguments
            commandline = parser.parse(options, args);

        } catch (Exception exp) {
            // oops, something went wrong
            System.err.println("Parsing failed. Reason: " + exp.getMessage());
            exiter();
        }

        /*----------------------------
          usage/help
        ----------------------------*/
        if (commandline.hasOption("help")) {
            HelpFormatter formatter = new HelpFormatter();
            formatter.printHelp("createmap", options);
            System.out.println("");
            System.out.println("author: " + author + " | version: " + version + " | date: " + date);
            System.exit(0);
        }

        /*----------------------------
          ueberpruefen ob eine schlechte kombination von parametern angegeben wurde
        ----------------------------*/
        if (!(commandline.hasOption("definition"))) {
            System.err.println("option -definition is mandatory.");
            exiter();
        }

        if (!(commandline.hasOption("output"))) {
            System.err.println("option -output is mandatory.");
            exiter();
        }

        /*----------------------------
          die lizenz ueberpruefen und ggf abbrechen
        ----------------------------*/

        // check for valid license
        ArrayList<String> allPortAtHost = new ArrayList<String>();
        allPortAtHost.add(ini.get("license-server", "license-server-1"));
        allPortAtHost.add(ini.get("license-server", "license-server-2"));
        allPortAtHost.add(ini.get("license-server", "license-server-3"));

        MyLicense lic = new MyLicense(allPortAtHost, "1", "user-edition", "0.1");

        // lizenz-logging ausgeben
        for (String actLine : (ArrayList<String>) lic.getLog()) {
            System.err.println(actLine);
        }

        // abbruch, wenn lizenz nicht valide
        if (!lic.isValid()) {
            System.exit(1);
        }

        /*----------------------------
          die eigentliche business logic
        ----------------------------*/
        Process p1 = new Process();
        java.io.File output = new java.io.File(commandline.getOptionValue("output"));

        if (output.exists()) {
            System.err.println("warn: already exists: " + output.getCanonicalPath());
            exiter();
        }

        p1.setInfilexml(commandline.getOptionValue("definition"));
        System.err.println("info: reading process definition " + commandline.getOptionValue("definition"));

        // dummy process
        Process p2 = null;

        try {
            p2 = p1.readXml();
        } catch (JAXBException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            System.err.println("error");
            exiter();
        }

        // den wrapper process generieren
        ArrayList<String> dot = p2.getProcessAsDotGraph();

        // dot file rausschreiben
        writeFile.writeFile(output, dot);
    }

    private static void exiter() {
        System.out.println("try -help for help.");
        System.exit(1);
    }

}