1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.hadoop.hbase.rest;
22
23 import org.apache.commons.cli.CommandLine;
24 import org.apache.commons.cli.HelpFormatter;
25 import org.apache.commons.cli.Options;
26 import org.apache.commons.cli.PosixParser;
27 import org.apache.commons.cli.ParseException;
28
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31 import org.apache.hadoop.conf.Configuration;
32 import org.apache.hadoop.hbase.HBaseConfiguration;
33 import org.apache.hadoop.hbase.rest.filter.GzipFilter;
34 import org.apache.hadoop.hbase.util.VersionInfo;
35
36 import java.util.List;
37 import java.util.ArrayList;
38
39 import org.mortbay.jetty.Server;
40 import org.mortbay.jetty.servlet.Context;
41 import org.mortbay.jetty.servlet.ServletHolder;
42
43 import com.sun.jersey.spi.container.servlet.ServletContainer;
44
45
46
47
48
49
50
51
52
53
54 public class Main implements Constants {
55
56 private static void printUsageAndExit(Options options, int exitCode) {
57 HelpFormatter formatter = new HelpFormatter();
58 formatter.printHelp("bin/hbase rest start", "", options,
59 "\nTo run the REST server as a daemon, execute " +
60 "bin/hbase-daemon.sh start|stop rest [-p <port>] [-ro]\n", true);
61 System.exit(exitCode);
62 }
63
64
65
66
67
68
69 public static void main(String[] args) throws Exception {
70 Log LOG = LogFactory.getLog("RESTServer");
71
72 VersionInfo.logVersion();
73 Configuration conf = HBaseConfiguration.create();
74 RESTServlet servlet = RESTServlet.getInstance(conf);
75
76 Options options = new Options();
77 options.addOption("p", "port", true, "Port to bind to [default: 8080]");
78 options.addOption("ro", "readonly", false, "Respond only to GET HTTP " +
79 "method requests [default: false]");
80
81 CommandLine commandLine = null;
82 try {
83 commandLine = new PosixParser().parse(options, args);
84 } catch (ParseException e) {
85 LOG.error("Could not parse: ", e);
86 printUsageAndExit(options, -1);
87 }
88
89
90 if (commandLine != null && commandLine.hasOption("port")) {
91 String val = commandLine.getOptionValue("port");
92 servlet.getConfiguration()
93 .setInt("hbase.rest.port", Integer.valueOf(val));
94 LOG.debug("port set to " + val);
95 }
96
97
98 if (commandLine != null && commandLine.hasOption("readonly")) {
99 servlet.getConfiguration().setBoolean("hbase.rest.readonly", true);
100 LOG.debug("readonly set to true");
101 }
102
103 @SuppressWarnings("unchecked")
104 List<String> remainingArgs = commandLine != null ?
105 commandLine.getArgList() : new ArrayList<String>();
106 if (remainingArgs.size() != 1) {
107 printUsageAndExit(options, 1);
108 }
109
110 String command = remainingArgs.get(0);
111 if ("start".equals(command)) {
112
113 } else if ("stop".equals(command)) {
114 System.exit(1);
115 } else {
116 printUsageAndExit(options, 1);
117 }
118
119
120 ServletHolder sh = new ServletHolder(ServletContainer.class);
121 sh.setInitParameter(
122 "com.sun.jersey.config.property.resourceConfigClass",
123 ResourceConfig.class.getCanonicalName());
124 sh.setInitParameter("com.sun.jersey.config.property.packages",
125 "jetty");
126
127
128
129 int port = servlet.getConfiguration().getInt("hbase.rest.port", 8080);
130
131 Server server = new Server(port);
132 server.setSendServerVersion(false);
133 server.setSendDateHeader(false);
134 server.setStopAtShutdown(true);
135
136 Context context = new Context(server, "/", Context.SESSIONS);
137 context.addServlet(sh, "/*");
138 context.addFilter(GzipFilter.class, "/*", 0);
139
140 server.start();
141 server.join();
142 }
143 }