View Javadoc
1   /*
2    * (c) Copyright 2006-2020 by rapiddweller GmbH & Volker Bergmann. All rights reserved.
3    *
4    * Redistribution and use in source and binary forms, with or without
5    * modification, is permitted under the terms of the
6    * GNU General Public License.
7    *
8    * For redistributing this software or a derivative work under a license other
9    * than the GPL-compatible Free Software License as defined by the Free
10   * Software Foundation or approved by OSI, you must first obtain a commercial
11   * license to this software product from rapiddweller GmbH & Volker Bergmann.
12   *
13   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
14   * WITHOUT A WARRANTY OF ANY KIND. ALL EXPRESS OR IMPLIED CONDITIONS,
15   * REPRESENTATIONS AND WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF
16   * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE
17   * HEREBY EXCLUDED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
18   * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21   * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22   * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24   * POSSIBILITY OF SUCH DAMAGE.
25   */
26  
27  package com.rapiddweller.benerator.main;
28  
29  import com.rapiddweller.benerator.BeneratorConstants;
30  import com.rapiddweller.benerator.BeneratorError;
31  import com.rapiddweller.benerator.BeneratorFactory;
32  import com.rapiddweller.benerator.BeneratorUtil;
33  import com.rapiddweller.benerator.engine.BeneratorContext;
34  import com.rapiddweller.benerator.engine.BeneratorMonitor;
35  import com.rapiddweller.benerator.engine.DescriptorRunner;
36  import com.rapiddweller.common.ArrayUtil;
37  import com.rapiddweller.common.IOUtil;
38  import com.rapiddweller.common.LogCategoriesConstants;
39  import com.rapiddweller.common.log.LoggingInfoPrinter;
40  import com.rapiddweller.common.ui.ConsoleInfoPrinter;
41  import com.rapiddweller.common.ui.InfoPrinter;
42  import com.rapiddweller.common.version.VersionInfo;
43  import com.rapiddweller.contiperf.sensor.MemorySensor;
44  import com.rapiddweller.format.text.KiloFormatter;
45  import com.rapiddweller.jdbacl.DBUtil;
46  import org.apache.logging.log4j.LogManager;
47  import org.apache.logging.log4j.Logger;
48  
49  import java.io.IOException;
50  
51  /**
52   * Parses and executes a benerator setup file.<br/>
53   * <br/>
54   * Created: 14.08.2007 19:14:28
55   *
56   * @author Volker Bergmann
57   */
58  public class Benerator {
59  
60    private static final Logger LOGGER = LogManager.getLogger(Benerator.class);
61  
62    // methods ---------------------------------------------------------------------------------------------------------
63  
64    /**
65     * The entry point of application.
66     *
67     * @param args the input arguments
68     * @throws IOException the io exception
69     */
70    public static void main(String[] args) throws IOException {
71      VersionInfo.getInfo("benerator").verifyDependencies();
72      if (
73          ArrayUtil.contains("--version", args) ||
74              ArrayUtil.contains("-v", args) ||
75              ArrayUtil.contains("-version", args)
76      ) {
77        printVersionInfoAndExit();
78      } else {
79        runFromCommandLine(args);
80      }
81    }
82  
83    private static void runFromCommandLine(String[] args) throws IOException {
84      try {
85        InfoPrinter printer = new LoggingInfoPrinter(LogCategoriesConstants.CONFIG);
86        String filename = (args.length > 0 ? args[0] : "benerator.xml");
87        runFile(filename, printer);
88        DBUtil.assertAllDbResourcesClosed(false);
89      } catch (BeneratorError e) {
90        LOGGER.error(e.getMessage(), e);
91        System.exit(e.getCode());
92      }
93    }
94  
95    /**
96     * Run file.
97     *
98     * @param filename the filename
99     * @param printer  the printer
100    * @throws IOException the io exception
101    */
102   public static void runFile(String filename, InfoPrinter printer) throws IOException {
103     BeneratorMonitor.INSTANCE.reset();
104     MemorySensor memProfiler = MemorySensor.getInstance();
105     memProfiler.reset();
106     if (printer != null) {
107       printer.printLines("Running file " + filename);
108       BeneratorUtil.checkSystem(printer);
109     }
110     BeneratorContext context = BeneratorFactory.getInstance().createContext(IOUtil.getParentUri(filename));
111     DescriptorRunnerriptorRunner.html#DescriptorRunner">DescriptorRunner runner = new DescriptorRunner(filename, context);
112     try {
113       runner.run();
114     } finally {
115       IOUtil.close(runner);
116     }
117     BeneratorUtil.logConfig("Max. committed heap size: " + new KiloFormatter(1024).format(memProfiler.getMaxCommittedHeapSize()) + "B");
118   }
119 
120   private static void printVersionInfoAndExit() {
121     InfoPrinter console = new ConsoleInfoPrinter();
122     BeneratorUtil.printVersionInfo(console);
123     System.exit(BeneratorConstants.EXIT_CODE_NORMAL);
124   }
125 
126 }