Java tutorial
/* * The MIT License (MIT) * * Copyright (c) 2013 Technologiya * * Permission is hereby granted, free of charge, to any person obtaining a copy of * this software and associated documentation files (the "Software"), to deal in * the Software without restriction, including without limitation the rights to * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of * the Software, and to permit persons to whom the Software is furnished to do so, * subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ package ru.taximaxim.dbreplicator2; import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.HelpFormatter; import org.apache.log4j.Logger; import org.apache.log4j.PropertyConfigurator; import org.hibernate.cfg.Configuration; import ru.taximaxim.dbreplicator2.cli.AbstractCommandLineParser; import ru.taximaxim.dbreplicator2.utils.Core; /** * @author TaxiMaxim * */ public final class Application extends AbstractCommandLineParser { private static final Logger LOG = Logger.getLogger(Core.class); /** * ?? ? ?. */ private Application() { setOption("h", "help", true, "", 0, false, null); setOption("i", "init", false, "? ? dbreplicator2", 0, false, null); setOption("u", "update", true, " ? dbreplicator2 ? ", 1, false, "file"); setOption("c", "conf", true, " . ?? hibernate.cfg.xml", 1, false, "file"); setOption("l", "log4j", true, " ? ?. ?? log4j.properties", 1, false, "file"); setOption("s", "start", false, "? dbreplicator2", 0, false, null); } @Override protected void processingCmd(CommandLine commandLine) { boolean hasOption = false; LOG.info("? dbreplicator2..."); // log4j String fLog4j = "log4j.properties"; if (commandLine.hasOption('l')) { String[] arguments = commandLine.getOptionValues('l'); // ? fLog4j = arguments[0]; hasOption = true; } PropertyConfigurator.configureAndWatch(fLog4j); String configurationName = null; if (commandLine.hasOption('c')) { String[] arguments = commandLine.getOptionValues('c'); configurationName = arguments[0]; hasOption = true; } boolean hibernateHbm2ddlAuto = false; if (commandLine.hasOption('i')) { hibernateHbm2ddlAuto = true; hasOption = true; } String hibernateHbm2ddlImportFiles = null; if (commandLine.hasOption('u')) { String[] arguments = commandLine.getOptionValues('u'); hibernateHbm2ddlImportFiles = arguments[0]; hasOption = true; } boolean coreGetTasksPoolStart = false; if (commandLine.hasOption('s')) { // ? // ? ?? ? ? // ? . // 1. ? ?? ?. // 2. ? ? ? ? . coreGetTasksPoolStart = true; hasOption = true; } if (commandLine.hasOption('h') || !hasOption) { if (!hasOption) { LOG.error( "??? , ? ?? [-h] [--help]"); } HelpFormatter formatter = new HelpFormatter(); formatter.printHelp("java dbreplicator2.jar", getOptions()); } if (hasOption) { if (hibernateHbm2ddlAuto && hibernateHbm2ddlImportFiles != null) { start(configurationName, hibernateHbm2ddlAuto, hibernateHbm2ddlImportFiles, coreGetTasksPoolStart); } else if (coreGetTasksPoolStart) { start(configurationName, hibernateHbm2ddlAuto, hibernateHbm2ddlImportFiles, coreGetTasksPoolStart); } else if (hibernateHbm2ddlImportFiles != null) { start(configurationName, hibernateHbm2ddlAuto, hibernateHbm2ddlImportFiles, coreGetTasksPoolStart); } } } protected void start(String configurationName, boolean hibernateHbm2ddlAuto, String hibernateHbm2ddlImportFiles, boolean coreGetTasksPoolStart) { // Hibernate Configuration configuration; // ? configuration = Core.getConfiguration(configurationName); if (hibernateHbm2ddlAuto) { // ? configuration.setProperty("hibernate.hbm2ddl.auto", "create"); } if (hibernateHbm2ddlImportFiles != null) { // ? ? ? configuration.setProperty("hibernate.hbm2ddl.import_files", hibernateHbm2ddlImportFiles); } Core.getSessionFactory(configuration); if (coreGetTasksPoolStart) { Core.getTasksPool().start(); } } /** * * * @param args */ public static void main(String[] args) { new Application().parserCommandLine(args); } }