/rz-srv-summr/src/com/readrz/summr/Program.java

https://github.com/akuz/readrz-public · Java · 129 lines · 109 code · 18 blank · 2 comment · 34 complexity · 5cc9499c60c64a619910c12c685efc72 MD5 · raw file

  1. package com.readrz.summr;
  2. import java.util.logging.Level;
  3. import java.util.logging.Logger;
  4. import me.akuz.core.StringUtils;
  5. import me.akuz.core.gson.GsonSerializers;
  6. import me.akuz.core.logs.LogUtils;
  7. public class Program {
  8. public static void main(String[] args) throws Exception {
  9. String usageString =
  10. "ARGUMENTS:\n" +
  11. " -mongoServer string : Mongo server\n" +
  12. " -mongoPort int : Mongo server port\n" +
  13. " -mongoDb int : Mongo database name\n" +
  14. " -stopWordsFile string : File with stop words not to use\n" +
  15. " -liveFreqMs int : Frequency of checking for live requests\n" +
  16. " [ -threadCount int ] : Number of threads to use (default 3)\n" +
  17. " [ -logLevel ] : Java logging level (default INFO)\n";
  18. String mongoServer = null;
  19. Integer mongoPort = null;
  20. String mongoDb = null;
  21. String stopWordsFile = null;
  22. Integer liveFreqMs = null;
  23. Integer threadCount = 3;
  24. String logLevelStr = "INFO";
  25. try {
  26. if (args != null) {
  27. for (int i=0; i < args.length; i++) {
  28. if ("-mongoServer".equals(args[i])) {
  29. if (i+1 < args.length) {
  30. mongoServer = StringUtils.unquote(args[i+1]);
  31. i++;
  32. }
  33. } else if ("-mongoPort".equals(args[i])) {
  34. if (i+1 < args.length) {
  35. mongoPort = Integer.parseInt(StringUtils.unquote(args[i+1]));
  36. i++;
  37. }
  38. } else if ("-mongoDb".equals(args[i])) {
  39. if (i+1 < args.length) {
  40. mongoDb = StringUtils.unquote(args[i+1]);
  41. i++;
  42. }
  43. } else if ("-threadCount".equals(args[i])) {
  44. if (i+1 < args.length) {
  45. threadCount = Integer.parseInt(StringUtils.unquote(args[i+1]));
  46. i++;
  47. }
  48. } else if ("-stopWordsFile".equals(args[i])) {
  49. if (i+1 < args.length) {
  50. stopWordsFile = StringUtils.unquote(args[i+1]);
  51. i++;
  52. }
  53. } else if ("-liveFreqMs".equals(args[i])) {
  54. if (i+1 < args.length) {
  55. liveFreqMs = Integer.parseInt(StringUtils.unquote(args[i+1]));
  56. i++;
  57. }
  58. } else if ("-logLevel".equals(args[i])) {
  59. if (i+1 < args.length) {
  60. logLevelStr = StringUtils.unquote(args[i+1]);
  61. i++;
  62. }
  63. }
  64. }
  65. }
  66. if (mongoServer == null) {
  67. throw new IllegalArgumentException("Mongo server not specified");
  68. }
  69. if (mongoPort == null) {
  70. throw new IllegalArgumentException("Mongo port not specified");
  71. }
  72. if (mongoDb == null) {
  73. throw new IllegalArgumentException("Mongo db not specified");
  74. }
  75. if (stopWordsFile == null) {
  76. throw new IllegalArgumentException("Stop words file not specified");
  77. }
  78. if (liveFreqMs == null) {
  79. throw new IllegalArgumentException("Live freq not specified");
  80. }
  81. if (liveFreqMs < 0) {
  82. throw new IllegalArgumentException("Live freq should be >= 0");
  83. }
  84. } catch (Exception e) {
  85. System.out.println("******** Arguments Error ********");
  86. System.out.println(e.toString());
  87. System.out.println("******** Correct Usage ********");
  88. System.out.println(usageString);
  89. return;
  90. }
  91. // configure logging
  92. Level logLevel = Level.parse(logLevelStr);
  93. LogUtils.configure(logLevel);
  94. Logger log = LogUtils.getLogger(Program.class.getName());
  95. // create program options
  96. ProgramOptions options = new ProgramOptions(
  97. stopWordsFile,
  98. liveFreqMs,
  99. threadCount,
  100. logLevelStr);
  101. log.info("OPTIONS: \n" + GsonSerializers.NoHtmlEscapingPretty.toJson(options));
  102. log.info("STARTING...");
  103. ProgramLogic logic = new ProgramLogic();
  104. logic.execute(
  105. mongoServer,
  106. mongoPort,
  107. mongoDb,
  108. options);
  109. log.info("DONE.");
  110. }
  111. }