3 # Licensed to the Apache Software Foundation (ASF) under one
4 # or more contributor license agreements. See the NOTICE file
5 # distributed with this work for additional information
6 # regarding copyright ownership. The ASF licenses this file
7 # to you under the Apache License, Version 2.0 (the
8 # "License"); you may not use this file except in compliance
9 # with the License. You may obtain a copy of the License at
11 # http://www.apache.org/licenses/LICENSE-2.0
13 # Unless required by applicable law or agreed to in writing, software
14 # distributed under the License is distributed on an "AS IS" BASIS,
15 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 # See the License for the specific language governing permissions and
17 # limitations under the License.
19 # File passed to org.jruby.Main by bin/hbase. Pollutes jirb with hbase imports
20 # and hbase commands and then loads jirb. Outputs a banner that tells user
21 # where to find help, shell version, and loads up a custom hirb.
23 # In noninteractive mode, runs commands from stdin until completion or an error.
24 # On success will exit with status 0, on any problem will exit non-zero. Callers
25 # should only rely on "not equal to 0", because the current error exit code of 1
26 # will likely be updated to diffentiate e.g. invalid commands, incorrect args,
29 # TODO: Interrupt a table creation or a connection to a bad master. Currently
30 # has to time out. Below we've set down the retries for rpc and hbase but
31 # still can be annoying (And there seem to be times when we'll retry for
33 # TODO: Add support for listing and manipulating catalog tables, etc.
34 # TODO: Encoding; need to know how to go from ruby String to UTF-8 bytes
36 # Run the java magic include and import basic HBase types that will help ease
40 # Some goodies for hirb. Should these be left up to the user's discretion?
41 require 'irb/completion'
44 # Add the directory names in hbase.jruby.sources commandline option
45 # to the ruby load path so I can load up my HBase ruby modules
46 sources = java.lang.System.getProperty('hbase.ruby.sources')
47 $LOAD_PATH.unshift Pathname.new(sources)
50 # FIXME: Switch args processing to getopt
52 # See if there are args for this shell. If any, read and then strip from ARGV
53 # so they don't go through to irb. Output shell 'usage' if user types '--help'
54 cmdline_help = <<HERE # HERE document output as shell usage
55 Usage: shell [OPTIONS] [SCRIPTFILE [ARGUMENTS]]
57 --format=OPTION Formatter for outputting results.
58 Valid options are: console, html.
61 -d | --debug Set DEBUG log levels.
62 -h | --help This help.
63 -n | --noninteractive Do not run within an IRB session
64 and exit with non-zero status on
70 log_level = org.apache.log4j.Level::ERROR
74 if arg =~ /^--format=(.+)/i
76 if format =~ /^html$/i
77 raise NoMethodError.new("Not yet implemented")
78 elsif format =~ /^console$/i
81 raise ArgumentError.new("Unsupported format " + arg)
84 elsif arg == '-h' || arg == '--help'
87 elsif arg == '-d' || arg == '--debug'
88 log_level = org.apache.log4j.Level::DEBUG
92 puts "Setting DEBUG log level..."
93 elsif arg == '-n' || arg == '--noninteractive'
97 # Presume it a script. Save it off for running later below
98 # after we've set up some environment.
101 # Presume that any other args are meant for the script.
106 # Delete all processed args
107 found.each { |arg| ARGV.delete(arg) }
108 # Make sure debug flag gets back to IRB
113 # Set logging level to avoid verboseness
114 org.apache.log4j.Logger.getLogger("org.apache.zookeeper").setLevel(log_level)
115 org.apache.log4j.Logger.getLogger("org.apache.hadoop.hbase").setLevel(log_level)
117 # Require HBase now after setting log levels
118 require 'hbase_constants'
124 require 'shell/formatter'
126 # Setup the HBase module. Create a configuration.
127 @hbase = Hbase::Hbase.new
130 @shell = Shell::Shell.new(@hbase, interactive)
131 @shell.debug = @shell_debug
133 # Add commands to this namespace
134 # TODO avoid polluting main namespace by using a binding
135 @shell.export_commands(self)
138 def help(command = nil)
142 # Backwards compatibility method
144 @shell.help_group('tools')
151 conf.back_trace_limit = 0
152 log_level = org.apache.log4j.Level::ERROR
155 conf.back_trace_limit = 100
156 log_level = org.apache.log4j.Level::DEBUG
158 org.apache.log4j.Logger.getLogger("org.apache.zookeeper").setLevel(log_level)
159 org.apache.log4j.Logger.getLogger("org.apache.hadoop.hbase").setLevel(log_level)
164 puts "Debug mode is #{@shell_debug ? 'ON' : 'OFF'}\n\n"
168 # Include hbase constants
169 include HBaseConstants
171 # If script2run, try running it. If we're in interactive mode, will go on to run the shell unless
172 # script calls 'exit' or 'exit 0' or 'exit errcode'.
173 load(script2run) if script2run
176 # Output a banner message that tells users where to go for help
183 def self.start(ap_path = nil)
184 $0 = File::basename(ap_path, ".rb") if ap_path
187 @CONF[:IRB_NAME] = 'hbase'
188 @CONF[:AP_NAME] = 'hbase'
189 @CONF[:BACK_TRACE_LIMIT] = 0 unless $fullBackTrace
192 hirb = HIRB.new(nil, @CONF[:SCRIPT])
197 @CONF[:IRB_RC].call(hirb.context) if @CONF[:IRB_RC]
198 @CONF[:MAIN_CONTEXT] = hirb.context
209 # Noninteractive mode: if there is input on stdin, do a simple REPL.
210 # XXX Note that this purposefully uses STDIN and not Kernel.gets
211 # in order to maintain compatibility with previous behavior where
212 # a user could pass in script2run and then still pipe commands on
214 require "irb/ruby-lex"
215 require "irb/workspace"
216 workspace = IRB::WorkSpace.new(binding())
217 scanner = RubyLex.new
218 scanner.set_input(STDIN)
219 scanner.each_top_level_statement do |statement, linenum|
220 puts(workspace.evaluate(nil, statement, 'stdin', linenum))
222 # XXX We're catching Exception on purpose, because we want to include
223 # unwrapped java exceptions, syntax errors, eval failures, etc.
224 rescue Exception => exception
225 message = exception.to_s
226 # exception unwrapping in shell means we'll have to handle Java exceptions
227 # as a special case in order to format them properly.
228 if exception.kind_of? java.lang.Exception
229 $stderr.puts "java exception"
230 message = exception.get_message
232 # Include the 'ERROR' string to try to make transition easier for scripts that
233 # may have already been relying on grepping output.
234 puts "ERROR #{exception.class}: #{message}"
236 # re-raising the will include a backtrace and exit.