| 1 | /** |
|---|
| 2 | * Copyright 2008 ThimbleWare Inc. |
|---|
| 3 | * |
|---|
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
|---|
| 5 | * you may not use this file except in compliance with the License. |
|---|
| 6 | * You may obtain a copy of the License at |
|---|
| 7 | * |
|---|
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
|---|
| 9 | * |
|---|
| 10 | * Unless required by applicable law or agreed to in writing, software |
|---|
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
|---|
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|---|
| 13 | * See the License for the specific language governing permissions and |
|---|
| 14 | * limitations under the License. |
|---|
| 15 | */ |
|---|
| 16 | package com.thimbleware.jmemcached; |
|---|
| 17 | |
|---|
| 18 | import org.apache.commons.cli.*; |
|---|
| 19 | |
|---|
| 20 | import java.net.InetSocketAddress; |
|---|
| 21 | |
|---|
| 22 | |
|---|
| 23 | /** |
|---|
| 24 | * Command line interface to the Java memcache daemon. |
|---|
| 25 | * |
|---|
| 26 | * Arguments in general parallel those of the C implementation. |
|---|
| 27 | */ |
|---|
| 28 | public class Main { |
|---|
| 29 | |
|---|
| 30 | public static void main(String[] args) throws Exception { |
|---|
| 31 | |
|---|
| 32 | // setup command line options |
|---|
| 33 | Options options = new Options(); |
|---|
| 34 | options.addOption("h", "help", false, "print this help screen"); |
|---|
| 35 | options.addOption("i", "idle", true, "disconnect after idle <x> seconds"); |
|---|
| 36 | options.addOption("p", "port", true, "port to listen on"); |
|---|
| 37 | options.addOption("m", "memory", true, "max memory to use in MB"); |
|---|
| 38 | options.addOption("c", "ceiling", true, "ceiling memory to use in MB"); |
|---|
| 39 | options.addOption("l", "listen", true, "Address to listen on"); |
|---|
| 40 | options.addOption("V", false, "Show version number"); |
|---|
| 41 | options.addOption("v", false, "verbose (show commands)"); |
|---|
| 42 | |
|---|
| 43 | // read command line options |
|---|
| 44 | CommandLineParser parser = new PosixParser(); |
|---|
| 45 | CommandLine cmdline = parser.parse(options, args); |
|---|
| 46 | |
|---|
| 47 | if (cmdline.hasOption("help") || cmdline.hasOption("h")) { |
|---|
| 48 | System.out.println("Memcached Version " + MemCacheDaemon.memcachedVersion); |
|---|
| 49 | System.out.println("http://thimbleware.com/projects/memcached\n"); |
|---|
| 50 | |
|---|
| 51 | HelpFormatter formatter = new HelpFormatter(); |
|---|
| 52 | formatter.printHelp("java -jar memcached.jar", options); |
|---|
| 53 | return; |
|---|
| 54 | } |
|---|
| 55 | |
|---|
| 56 | if (cmdline.hasOption("V")) { |
|---|
| 57 | System.out.println("Memcached Version " + MemCacheDaemon.memcachedVersion); |
|---|
| 58 | return; |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | int port = 11211; |
|---|
| 62 | if (cmdline.hasOption("p")) { |
|---|
| 63 | port = Integer.parseInt(cmdline.getOptionValue("p")); |
|---|
| 64 | } else if (cmdline.hasOption("port")) { |
|---|
| 65 | port = Integer.parseInt(cmdline.getOptionValue("port")); |
|---|
| 66 | } |
|---|
| 67 | |
|---|
| 68 | InetSocketAddress addr = new InetSocketAddress(port); |
|---|
| 69 | if (cmdline.hasOption("l")) { |
|---|
| 70 | addr = new InetSocketAddress(cmdline.getOptionValue("l"), port); |
|---|
| 71 | } else if (cmdline.hasOption("listen")) { |
|---|
| 72 | addr = new InetSocketAddress(cmdline.getOptionValue("listen"), port); |
|---|
| 73 | } |
|---|
| 74 | |
|---|
| 75 | int max_size = 10000; |
|---|
| 76 | if (cmdline.hasOption("s")) { |
|---|
| 77 | max_size = Integer.parseInt(cmdline.getOptionValue("s")); |
|---|
| 78 | System.out.println("Setting max elements to " + String.valueOf(max_size)); |
|---|
| 79 | } else if (cmdline.hasOption("size")) { |
|---|
| 80 | max_size = Integer.parseInt(cmdline.getOptionValue("size")); |
|---|
| 81 | System.out.println("Setting max elements to " + String.valueOf(max_size)); |
|---|
| 82 | } |
|---|
| 83 | |
|---|
| 84 | int idle = -1; |
|---|
| 85 | if (cmdline.hasOption("i")) { |
|---|
| 86 | idle = Integer.parseInt(cmdline.getOptionValue("i")); |
|---|
| 87 | } else if (cmdline.hasOption("idle")) { |
|---|
| 88 | idle = Integer.parseInt(cmdline.getOptionValue("idle")); |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | |
|---|
| 92 | boolean verbose = false; |
|---|
| 93 | if (cmdline.hasOption("v")) { |
|---|
| 94 | verbose = true; |
|---|
| 95 | } |
|---|
| 96 | |
|---|
| 97 | long ceiling; |
|---|
| 98 | if (cmdline.hasOption("c")) { |
|---|
| 99 | ceiling = Integer.parseInt(cmdline.getOptionValue("c")) * 1024000; |
|---|
| 100 | System.out.println("Setting ceiling memory size to " + String.valueOf(ceiling) + " bytes"); |
|---|
| 101 | } else if (cmdline.hasOption("ceiling")) { |
|---|
| 102 | ceiling = Integer.parseInt(cmdline.getOptionValue("ceiling")) * 1024000; |
|---|
| 103 | System.out.println("Setting ceiling memory size to " + String.valueOf(ceiling) + " bytes"); |
|---|
| 104 | } else { |
|---|
| 105 | ceiling = 1024000; |
|---|
| 106 | System.out.println("Setting ceiling memory size to JVM limit of " + String.valueOf(ceiling) + " bytes"); |
|---|
| 107 | } |
|---|
| 108 | |
|---|
| 109 | long maxBytes; |
|---|
| 110 | if (cmdline.hasOption("m")) { |
|---|
| 111 | maxBytes = Integer.parseInt(cmdline.getOptionValue("m")) * 1024000; |
|---|
| 112 | System.out.println("Setting max memory size to " + String.valueOf(maxBytes) + " bytes"); |
|---|
| 113 | } else if (cmdline.hasOption("memory")) { |
|---|
| 114 | maxBytes = Integer.parseInt(cmdline.getOptionValue("memory")) * 1024000; |
|---|
| 115 | System.out.println("Setting max memory size to " + String.valueOf(maxBytes) + " bytes"); |
|---|
| 116 | } else { |
|---|
| 117 | maxBytes = Runtime.getRuntime().maxMemory(); |
|---|
| 118 | System.out.println("Setting max memory size to JVM limit of " + String.valueOf(maxBytes) + " bytes"); |
|---|
| 119 | } |
|---|
| 120 | |
|---|
| 121 | if (maxBytes > Runtime.getRuntime().maxMemory()) { |
|---|
| 122 | System.out.println("ERROR : JVM heap size is not big enough. use '-Xmx" + String.valueOf(maxBytes / 1024000) + "m' java argument before the '-jar' option."); |
|---|
| 123 | return; |
|---|
| 124 | } |
|---|
| 125 | |
|---|
| 126 | // create daemon and start it |
|---|
| 127 | MemCacheDaemon daemon = new MemCacheDaemon(); |
|---|
| 128 | LRUCacheStorageDelegate cacheStorage = new LRUCacheStorageDelegate(max_size, maxBytes, 1024000); |
|---|
| 129 | daemon.setCache(new Cache(cacheStorage)); |
|---|
| 130 | daemon.setAddr(addr); |
|---|
| 131 | daemon.setIdleTime(idle); |
|---|
| 132 | daemon.setPort(port); |
|---|
| 133 | daemon.setVerbose(verbose); |
|---|
| 134 | daemon.start(); |
|---|
| 135 | } |
|---|
| 136 | |
|---|
| 137 | } |
|---|