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