Java tutorial
/* * Copyright (c) Sergiu Giurgiu 2012. * * This file is part of TVMan. * * TVMan is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * TVMan is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with TVMan. If not, see <http://www.gnu.org/licenses/>. */ package com.zergiu.tvman; 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.apache.commons.cli.ParseException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * @author sergiu * */ public class TVManOptions { private static final TVManOptions instance = new TVManOptions(); private static Logger log = LoggerFactory.getLogger(TVManOptions.class); private int port = 8080; private String host = "localhost"; private int maxThreads = 250; private int minThreads = 10; private int maxIdleTime = 300000; private boolean ssl = false; private boolean useIO = false;//use NIO by default private String keystorePath = "keystore"; private String keystorePassword = "password"; private String keystoreManagerPassword = "password"; private String trustStorePath = "keystore"; private String trustStorePassword = "password"; private String dbLocation = System.getProperty("user.home") + System.getProperty("file.separator") + ".tvman"; private boolean debug = false; private boolean quiet = false; private boolean detailedDump = false; private int acceptors = 2; private int lowResourcesMaxIdleTime = 5000; private boolean statsOn = false; private int lowResourcesConnections = 20000; private boolean showSystemTray = true; private TVManOptions() { } public static TVManOptions getInstance() { return instance; } public void parseCommandLine(String[] args) { CommandLine line = parseCommandLineArguments(args); setOptions(line); } /** * @param line */ private void setOptions(CommandLine line) { try { if (line.hasOption("port")) { port = Integer.parseInt(line.getOptionValue("port")); log.info("Using port value " + port); } } catch (NumberFormatException ignored) { } if (line.hasOption("host")) { host = line.getOptionValue("host"); log.info("Using host value " + host); } try { if (line.hasOption("maxThreads")) { maxThreads = Integer.parseInt(line.getOptionValue("maxThreads")); log.info("Using maxThreads value " + maxThreads); } } catch (NumberFormatException ignored) { } try { if (line.hasOption("maxIdleTime")) { maxIdleTime = Integer.parseInt(line.getOptionValue("maxIdleTime")); log.info("Using maxIdleTime value " + maxIdleTime); } } catch (NumberFormatException ignored) { } ssl = line.hasOption("ssl"); log.info("Using ssl " + ssl); useIO = line.hasOption("useIO"); log.info("Using IO " + useIO); if (line.hasOption("keystorePassword")) { keystorePassword = line.getOptionValue("keystorePassword"); log.info("Using keystorePassword value " + keystorePassword); } if (line.hasOption("keystorePath")) { keystorePath = line.getOptionValue("keystorePath"); log.info("Using keystorePath value " + keystorePath); } if (line.hasOption("keystoreManagerPassword")) { keystoreManagerPassword = line.getOptionValue("keystoreManagerPassword"); log.info("Using keystoreManagerPassword value " + keystoreManagerPassword); } if (line.hasOption("trustStorePassword")) { trustStorePassword = line.getOptionValue("trustStorePassword"); log.info("Using trustStorePassword value " + trustStorePassword); } if (line.hasOption("trustStorePath")) { trustStorePath = line.getOptionValue("trustStorePath"); log.info("Using trustStorePath value " + trustStorePath); } if (line.hasOption("db")) { dbLocation = line.getOptionValue("db"); log.info("Using db value " + dbLocation); } debug = line.hasOption("debug"); quiet = line.hasOption("quiet"); setShowSystemTray(!line.hasOption("noSystemTray")); } private CommandLine parseCommandLineArguments(String[] args) { Options options = createOptions(); CommandLineParser parser = new GnuParser(); CommandLine line = null; try { // parse the command line arguments line = parser.parse(options, args, true); } catch (ParseException exp) { System.err.println("Parsing options failed. Reason: " + exp.getMessage()); showHelp(options); System.exit(-1); } if (line.hasOption("help") || line.hasOption("?")) { showHelp(options); System.exit(0); } return line; } private static void showHelp(Options options) { HelpFormatter helpFormatter = new HelpFormatter(); StringBuilder footer = new StringBuilder(); footer.append( "If you want to use a ssl connection, you should generate a keystore using the 'keytool' tool that comes with the JRE.\n"); footer.append("Example: keytool -keystore keystore -alias tvman -genkey -keyalg RSA\n"); helpFormatter.printHelp(160, "tvman", null, options, footer.toString(), true); } @SuppressWarnings("static-access") private static Options createOptions() { Options options = new Options(); Option help = new Option("help", "print help information"); Option help2 = new Option("?", "print help information"); Option debug = new Option("debug", "print debugging information"); Option quiet = new Option("quiet", "be quiet"); Option dbLoc = OptionBuilder.withArgName("folder").hasArg() .withDescription("Where to store and look for the database").create("db"); Option host = OptionBuilder.withArgName("host|ip").hasArg() .withDescription( "Specifies the host to listen to (default localhost, use 0.0.0.0 for all interfaces)") .create("host"); Option port = OptionBuilder.withArgName("port").hasArg() .withDescription("Specifies the port to listen on (default 8080)").create("port"); Option maxThreads = OptionBuilder.withArgName("number").hasArg() .withDescription("The maximum number of threads to spawn at a time (default 20)") .create("maxThreads"); Option maxIdleTime = OptionBuilder.withArgName("number").hasArg() .withDescription("Set the maximum Idle time for a connection in milliseconds (default 30000)") .create("maxIdleTime"); Option useIO = new Option("useIO", "Tells the application to use blocking IO instead of Non-Blocking IO (the default)"); Option ssl = new Option("ssl", "If true start an SSL connection. You may want to specify the port as well to something more appropriate (e.g. 8443) and use https:// when accessing the application."); Option keystorePath = OptionBuilder.withArgName("path").hasArg() .withDescription("Specifies the path to the keystore (default ./keystore)").create("keystorePath"); Option keystorePassword = OptionBuilder.withArgName("password").hasArg().withDescription( "Specify the keystore password (default 'password'). You can use the jetty password obfuscation utility to obfuscate it http://wiki.eclipse.org/Jetty/Howto/Secure_Passwords") .create("keystorePassword"); Option keystoreManagerPassword = OptionBuilder.withArgName("password").hasArg().withDescription( "Specify the keystore manager password (default 'password'). You can use the jetty password obfuscation utility to obfuscate it http://wiki.eclipse.org/Jetty/Howto/Secure_Passwords") .create("keystoreManagerPassword"); Option trustStorePath = OptionBuilder.withArgName("path").hasArg() .withDescription("Specifies the path to the trust store (default <keystorePath>") .create("trustStorePath"); Option trustStorePassword = OptionBuilder.withArgName("password").hasArg().withDescription( "Specify the trust store password (default 'password'). You can use the jetty password obfuscation utility to obfuscate it http://wiki.eclipse.org/Jetty/Howto/Secure_Passwords") .create("trustStorePassword"); Option noSystemTray = new Option("noSystemTray", "Dont show system tray icon"); options.addOption(help); options.addOption(help2); options.addOption(debug); options.addOption(quiet); options.addOption(dbLoc); options.addOption(host); options.addOption(port); options.addOption(maxThreads); options.addOption(maxIdleTime); options.addOption(useIO); options.addOption(ssl); options.addOption(keystorePath); options.addOption(keystorePassword); options.addOption(keystoreManagerPassword); options.addOption(trustStorePath); options.addOption(trustStorePassword); options.addOption(noSystemTray); return options; } /** * @return the port */ public int getPort() { return port; } /** * @param port the port to set */ public void setPort(int port) { this.port = port; } /** * @return the host */ public String getHost() { return host; } /** * @param host the host to set */ public void setHost(String host) { this.host = host; } /** * @return the maxThreads */ public int getMaxThreads() { return maxThreads; } /** * @param maxThreads the maxThreads to set */ public void setMaxThreads(int maxThreads) { this.maxThreads = maxThreads; } /** * @return the maxIdleTime */ public int getMaxIdleTime() { return maxIdleTime; } /** * @param maxIdleTime the maxIdleTime to set */ public void setMaxIdleTime(int maxIdleTime) { this.maxIdleTime = maxIdleTime; } /** * @return the ssl */ public boolean isSsl() { return ssl; } /** * @param ssl the ssl to set */ public void setSsl(boolean ssl) { this.ssl = ssl; } /** * @return the useIO */ public boolean isUseIO() { return useIO; } /** * @param useIO the useIO to set */ public void setUseIO(boolean useIO) { this.useIO = useIO; } /** * @return the keystorePath */ public String getKeystorePath() { return keystorePath; } /** * @param keystorePath the keystorePath to set */ public void setKeystorePath(String keystorePath) { this.keystorePath = keystorePath; } /** * @return the keystorePassword */ public String getKeystorePassword() { return keystorePassword; } /** * @param keystorePassword the keystorePassword to set */ public void setKeystorePassword(String keystorePassword) { this.keystorePassword = keystorePassword; } /** * @return the keystoreManagerPassword */ public String getKeystoreManagerPassword() { return keystoreManagerPassword; } /** * @param keystoreManagerPassword the keystoreManagerPassword to set */ public void setKeystoreManagerPassword(String keystoreManagerPassword) { this.keystoreManagerPassword = keystoreManagerPassword; } /** * @return the trustStorePath */ public String getTrustStorePath() { return trustStorePath; } /** * @param trustStorePath the trustStorePath to set */ public void setTrustStorePath(String trustStorePath) { this.trustStorePath = trustStorePath; } /** * @return the trustStorePassword */ public String getTrustStorePassword() { return trustStorePassword; } /** * @param trustStorePassword the trustStorePassword to set */ public void setTrustStorePassword(String trustStorePassword) { this.trustStorePassword = trustStorePassword; } /** * @return the dbLocation */ public String getDbLocation() { return dbLocation; } /** * @param dbLocation the dbLocation to set */ public void setDbLocation(String dbLocation) { this.dbLocation = dbLocation; } /** * @return the debug */ public boolean isDebug() { return debug; } /** * @param debug the debug to set */ public void setDebug(boolean debug) { this.debug = debug; } /** * @return the quiet */ public boolean isQuiet() { return quiet; } /** * @param quiet the quiet to set */ public void setQuiet(boolean quiet) { this.quiet = quiet; } public int getMinThreads() { return minThreads; } public void setMinThreads(int minThreads) { this.minThreads = minThreads; } public boolean isDetailedDump() { return detailedDump; } public void setDetailedDump(boolean detailedDump) { this.detailedDump = detailedDump; } public int getAcceptors() { return acceptors; } public void setAcceptors(int acceptors) { this.acceptors = acceptors; } public int getLowResourcesMaxIdleTime() { return lowResourcesMaxIdleTime; } public void setLowResourcesMaxIdleTime(int lowResourcesMaxIdleTime) { this.lowResourcesMaxIdleTime = lowResourcesMaxIdleTime; } public boolean isStatsOn() { return statsOn; } public void setStatsOn(boolean statsOn) { this.statsOn = statsOn; } public int getLowResourcesConnections() { return lowResourcesConnections; } public void setLowResourcesConnections(int lowResourcesConnections) { this.lowResourcesConnections = lowResourcesConnections; } public boolean isShowSystemTray() { return showSystemTray; } public void setShowSystemTray(boolean showSystemTray) { this.showSystemTray = showSystemTray; } }