Java tutorial
/* * Mail Attachment Gateway * Copyright (c) 2016-2017 Carsten Rambow * mailto:developer AT elomagic DOT 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. */ package de.elomagic.mag; import java.io.IOException; import java.io.InputStream; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardCopyOption; import org.apache.camel.builder.RouteBuilder; import org.apache.camel.main.Main; import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.DefaultParser; import org.apache.commons.cli.HelpFormatter; import org.apache.commons.cli.Option; import org.apache.commons.cli.Options; import org.apache.commons.cli.ParseException; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; /** * * @author Carsten Rambow */ public class MAG { private static final Logger LOGGER = LogManager.getLogger(MAG.class); private final Options options; private final Option optionConfig; private final Option optionEncrypt; private final Option optionHelp; private final Main main = new Main(); public MAG() { options = new Options(); options.addOption(optionConfig = Option.builder("c").longOpt("config").hasArg().argName("file").desc( "Alternative configuration file. Default: [WORKING-HOME]/conf/configuration-defaults.properties") .build()); options.addOption(optionEncrypt = Option.builder("e").longOpt("encrypt") .desc("Encrypt passwords in the given configuration file").build()); options.addOption(optionHelp = Option.builder("h").longOpt("help") .desc("Show all available command line arguments.").build()); } /** * @param args the command line arguments */ public static void main(String[] args) { System.out.println(ApplicationProperties.getDisplayNameVersion()); MAG main = new MAG(); main.run(args); } private void run(String[] args) { Path configurationFile = Paths.get(System.getProperty("user.dir"), "conf", "configuration.properties"); DefaultParser parser = new DefaultParser(); try { CommandLine commandLine = parser.parse(options, args); if (commandLine.hasOption(optionHelp.getOpt())) { printHelp(); } else if (commandLine.hasOption(optionEncrypt.getOpt())) { if (commandLine.hasOption(optionConfig.getOpt())) { String file = commandLine.getOptionValue(optionConfig.getOpt()); configurationFile = Paths.get(file); } encryptConfiguration(configurationFile); } else { boolean initCreateOfConfig = false; if (commandLine.hasOption(optionConfig.getOpt())) { String file = commandLine.getOptionValue(optionConfig.getOpt()); configurationFile = Paths.get(file); LOGGER.info("Using configuration file \"" + file + "\"."); } else if (Files.notExists(configurationFile)) { System.out.println("Creating default configuration file \"" + configurationFile + "\"."); try (InputStream in = getClass().getResourceAsStream(Configuration.DEFAULT_PROPERTIES_FILE)) { Files.createDirectories(configurationFile.getParent()); Files.copy(in, configurationFile, StandardCopyOption.REPLACE_EXISTING); } System.out .println("Configure file \"" + configurationFile + "\" as your needs and start again."); initCreateOfConfig = true; } if (!initCreateOfConfig) { Configuration.loadConfiguration(configurationFile); System.out.println("Use ctrl + c to terminate MAG...."); createCamelMain().run(); System.out.println("MAG was terminated...."); } } } catch (IOException e) { LOGGER.error(e.getMessage(), e); } catch (ParseException e) { LOGGER.error("Parameter syntax error.", e); printHelp(); } catch (Exception e) { LOGGER.error(e.getMessage(), e); } } public void start() throws Exception { createCamelMain().start(); } public void stop() throws Exception { main.stop(); } private Main createCamelMain() { RouteBuilder routeBuilder = RouteBuilderFactory.createRoute(main.getOrCreateCamelContext()); main.addRouteBuilder(routeBuilder); return main; } private void printHelp() { HelpFormatter formatter = new HelpFormatter(); formatter.setWidth(160); formatter.printHelp("mag", ApplicationProperties.getDisplayNameVersion(), options, ""); } private void encryptConfiguration(Path file) throws IOException { System.out.println("Encrypting passwords in the configuration file \"" + file + "\"..."); Configuration.encryptConfiguration(file); } }