/src/main/org/quickcached/QuickCached.java

http://quickcached.googlecode.com/ · Java · 142 lines · 101 code · 26 blank · 15 comment · 27 complexity · 084a30338f1b58b911567c68791c79cb MD5 · raw file

  1. package org.quickcached;
  2. import org.quickserver.net.*;
  3. import org.quickserver.net.server.*;
  4. import java.io.*;
  5. import java.lang.management.ManagementFactory;
  6. import java.util.Map;
  7. import org.apache.log4j.xml.DOMConfigurator;
  8. public class QuickCached {
  9. public static final String app_version = "1.1.0";
  10. public static String version = "1.1.0";
  11. public static boolean DEBUG = false;
  12. private static final int KEY_MAX_LENGTH = 250;
  13. private static QuickServer quickcached;
  14. public static void main(String args[]) throws Exception {
  15. int i = 0;
  16. String arg = null;
  17. String value = null;
  18. if(args.length!=0) {
  19. while (i < args.length) {
  20. arg = args[i];
  21. i++;
  22. if(arg.startsWith("-")==false) continue;
  23. if (arg.equals("-v") || arg.equals("-vv")) {
  24. SetupLoggingHook.setMakeLogFile(true);
  25. File log = new File("./log/");
  26. if(log.canRead()==false) {
  27. boolean flag = log.mkdirs();
  28. if(flag==false) {
  29. System.out.println("Unable to create log folder!");
  30. }
  31. }
  32. DOMConfigurator.configure("conf/log4j_debug.xml");
  33. } else {
  34. DOMConfigurator.configure("conf/log4j.xml");
  35. }
  36. if (arg.equals("-vv")) {
  37. DEBUG = true;
  38. }
  39. }
  40. } else {
  41. DOMConfigurator.configure("conf/log4j.xml");
  42. }
  43. String confFile = "conf" + File.separator + "QuickCached.xml";
  44. Object config[] = new Object[]{confFile};
  45. quickcached = new QuickServer();
  46. quickcached.initService(config);
  47. Map configMap = quickcached.getConfig().getApplicationConfiguration();
  48. version = (String) configMap.get("MEMCACHED_VERSION_TO_SHOW");
  49. if(version==null) {
  50. version = "1.4.6";
  51. }
  52. //CLI
  53. //-l <ip_addr>
  54. //Listen on <ip_addr>; default to INDRR_ANY.
  55. //This is an important option to consider as there is no other way to secure the installation.
  56. //Binding to an internal or firewalled network interface is suggested
  57. //-c <num>
  58. //Use <num> max simultaneous connections; the default is 1024.
  59. //-p <num>
  60. //Listen on TCP port <num>, the default is port 11211.
  61. //-v
  62. //Be verbose during the event loop; print out errors and warnings.
  63. //-vv
  64. //Be even more verbose; same as -v but also print client commands and responses.
  65. i = 0;
  66. while (i < args.length) {
  67. arg = args[i];
  68. i++;
  69. if(i < args.length) value = args[i];
  70. if(arg.startsWith("-")==false) continue;
  71. if (arg.equals("-l")) {
  72. quickcached.setBindAddr(value);
  73. } else if (arg.equals("-p")) {
  74. quickcached.setPort(Integer.parseInt(value));
  75. } else if (arg.equals("-c")) {
  76. quickcached.setMaxConnection(Integer.parseInt(value));
  77. } else if (arg.equals("-h")) {
  78. printHelp();
  79. return;
  80. } else if (arg.equals("-v") || arg.equals("-vv")) {
  81. //nothing here
  82. } else {
  83. //print help - TODO
  84. System.out.println("Error: Bad argument passed - " + arg);
  85. printHelp();
  86. return;
  87. }
  88. }
  89. try {
  90. if (quickcached != null) {
  91. quickcached.startServer();
  92. }
  93. } catch (AppException e) {
  94. System.out.println("Error starting server : " + e);
  95. e.printStackTrace();
  96. }
  97. }
  98. public static String getPID() {
  99. String pid = ManagementFactory.getRuntimeMXBean().getName();
  100. int i = pid.indexOf("@");
  101. pid = pid.substring(0, i);
  102. return pid;
  103. }
  104. public static int getPort() {
  105. return quickcached.getPort();
  106. }
  107. public static void printHelp() {
  108. System.out.println("QuickCached " + version);
  109. System.out.println("-p <num> TCP port number to listen on (default: 11211)");
  110. System.out.println("-l <ip_addr> interface to listen on (default: INADDR_ANY, all addresses)");
  111. System.out.println("-c <num> max simultaneous connections");
  112. System.out.println("-v verbose (print errors/warnings while in event loop). Creates logs in log folder");
  113. System.out.println("-vv very verbose (also print client commands/reponses). Debut Mode");
  114. System.out.println("-h print this help and exit");
  115. }
  116. }