HBASE-17532 Replaced explicit type with diamond operator
[hbase.git] / hbase-server / src / main / java / org / apache / hadoop / hbase / util / ServerCommandLine.java
blob9cc6d5a19a12d75c053863f479c73e2763c7ab17
1 /**
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 package org.apache.hadoop.hbase.util;
21 import java.lang.management.ManagementFactory;
22 import java.lang.management.RuntimeMXBean;
23 import java.util.Arrays;
24 import java.util.HashSet;
25 import java.util.Locale;
26 import java.util.Map.Entry;
27 import java.util.Set;
29 import org.apache.commons.logging.Log;
30 import org.apache.commons.logging.LogFactory;
31 import org.apache.hadoop.hbase.classification.InterfaceAudience;
32 import org.apache.hadoop.conf.Configuration;
33 import org.apache.hadoop.conf.Configured;
34 import org.apache.hadoop.hbase.HBaseConfiguration;
35 import org.apache.hadoop.util.Tool;
36 import org.apache.hadoop.util.ToolRunner;
38 /**
39 * Base class for command lines that start up various HBase daemons.
41 @InterfaceAudience.Private
42 public abstract class ServerCommandLine extends Configured implements Tool {
43 private static final Log LOG = LogFactory.getLog(ServerCommandLine.class);
44 @SuppressWarnings("serial")
45 private static final Set<String> DEFAULT_SKIP_WORDS = new HashSet<String>() {
47 add("secret");
48 add("passwd");
49 add("password");
50 add("credential");
54 /**
55 * Implementing subclasses should return a usage string to print out.
57 protected abstract String getUsage();
59 /**
60 * Print usage information for this command line.
62 * @param message if not null, print this message before the usage info.
64 protected void usage(String message) {
65 if (message != null) {
66 System.err.println(message);
67 System.err.println("");
70 System.err.println(getUsage());
73 /**
74 * Log information about the currently running JVM.
76 public static void logJVMInfo() {
77 // Print out vm stats before starting up.
78 RuntimeMXBean runtime = ManagementFactory.getRuntimeMXBean();
79 if (runtime != null) {
80 LOG.info("vmName=" + runtime.getVmName() + ", vmVendor=" +
81 runtime.getVmVendor() + ", vmVersion=" + runtime.getVmVersion());
82 LOG.info("vmInputArguments=" + runtime.getInputArguments());
86 /**
87 * Logs information about the currently running JVM process including
88 * the environment variables. Logging of env vars can be disabled by
89 * setting {@code "hbase.envvars.logging.disabled"} to {@code "true"}.
90 * <p>If enabled, you can also exclude environment variables containing
91 * certain substrings by setting {@code "hbase.envvars.logging.skipwords"}
92 * to comma separated list of such substrings.
94 public static void logProcessInfo(Configuration conf) {
95 // log environment variables unless asked not to
96 if (conf == null || !conf.getBoolean("hbase.envvars.logging.disabled", false)) {
97 Set<String> skipWords = new HashSet<>(DEFAULT_SKIP_WORDS);
98 if (conf != null) {
99 String[] confSkipWords = conf.getStrings("hbase.envvars.logging.skipwords");
100 if (confSkipWords != null) {
101 skipWords.addAll(Arrays.asList(confSkipWords));
105 nextEnv:
106 for (Entry<String, String> entry : System.getenv().entrySet()) {
107 String key = entry.getKey().toLowerCase(Locale.ROOT);
108 String value = entry.getValue().toLowerCase(Locale.ROOT);
109 // exclude variables which may contain skip words
110 for(String skipWord : skipWords) {
111 if (key.contains(skipWord) || value.contains(skipWord))
112 continue nextEnv;
114 LOG.info("env:"+entry);
117 // and JVM info
118 logJVMInfo();
122 * Parse and run the given command line. This may exit the JVM if
123 * a nonzero exit code is returned from <code>run()</code>.
125 public void doMain(String args[]) {
126 try {
127 int ret = ToolRunner.run(HBaseConfiguration.create(), this, args);
128 if (ret != 0) {
129 System.exit(ret);
131 } catch (Exception e) {
132 LOG.error("Failed to run", e);
133 System.exit(-1);