1/*2 * Licensed to the Apache Software Foundation (ASF) under one3 * or more contributor license agreements. See the NOTICE file4 * distributed with this work for additional information5 * regarding copyright ownership. The ASF licenses this file6 * to you under the Apache License, Version 2.0 (the7 * "License"); you may not use this file except in compliance8 * with the License. You may obtain a copy of the License at9 *10 * http://www.apache.org/licenses/LICENSE-2.011 *12 * Unless required by applicable law or agreed to in writing, software13 * distributed under the License is distributed on an "AS IS" BASIS,14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.15 * See the License for the specific language governing permissions and16 * limitations under the License.17 */18package org.apache.giraph.conf;
1920import org.apache.log4j.Logger;
2122import java.io.BufferedWriter;
23import java.io.FileWriter;
24import java.io.IOException;
25import java.lang.reflect.Field;
26import java.util.ArrayList;
27import java.util.Arrays;
28import java.util.Collections;
29import java.util.List;
3031/**32 * Tracks all of the Giraph options33 */34publicclassAllOptions {
35/** logger object */36privatestaticfinal Logger LOG = Logger.getLogger(AllOptions.class);
3738/** page name for the HTML page generation */39privatestaticfinal String PAGE_NAME = "Giraph Options";
4041/** Don't construct */42privateAllOptions() { }
434445/**46 * String representation of all of the options stored47 * @param options List of loaded options48 * @return string49 */50privatestatic String allOptionsString(List<AbstractConfOption> options) {
51 Collections.sort(options);
52 StringBuilder sb = new StringBuilder(options.size() * 30);
53 sb.append("All Options:\n");
54ConfOptionType lastType = null;
55for (AbstractConfOption confOption : options) {
56if (!confOption.getType().equals(lastType)) {
57 sb.append(confOption.getType().toString().toLowerCase()).append(":\n");
58 lastType = confOption.getType();
59 }
60 sb.append(confOption);
61 }
62return sb.toString();
63 }
6465/**66 * HTML String representation of all the options stored67 * @param options List of loaded options68 * @return String the HTML representation of the registered options69 */70privatestatic String allOptionsHTMLString(List<AbstractConfOption> options) {
71 Collections.sort(options);
72 StringBuilder sb = new StringBuilder(options.size() * 30);
7374 sb.append("<?xml version='1.0' encoding='UTF-8'?>\n" +
75"<!--\n" +
76"Licensed to the Apache Software Foundation (ASF) under one\n" +
77"or more contributor license agreements. See the NOTICE file\n" +
78"distributed with this work for additional information\n" +
79"regarding copyright ownership. The ASF licenses this file\n" +
80"to you under the Apache License, Version 2.0 (the\n" +
81"'License'); you may not use this file except in compliance\n" +
82"with the License. You may obtain a copy of the License at\n" +
83"\n" +
84" http://www.apache.org/licenses/LICENSE-2.0\n" +
85"\n" +
86"Unless required by applicable law or agreed to in writing,\n" +
87"software distributed under the License is distributed on an\n" +
88"'AS IS' BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY\n" +
89"KIND, either express or implied. See the License for the\n" +
90"specific language governing permissions and limitations\n" +
91"under the License.\n" +
92"-->\n" +
93"\n" +
94"<document xmlns='http://maven.apache.org/XDOC/2.0'\n" +
95" xmlns:xsi='http://www.w3.org/2001/" +
96"XMLSchema-instance'\n" +
97" xsi:schemaLocation='" +
98"http://maven.apache.org/XDOC/2.0 " +
99" http://maven.apache.org/xsd/xdoc-2.0.xsd'>\n" +
100" <properties>\n" +
101" <title>" + PAGE_NAME + "</title>\n" +
102" </properties>\n" +
103" <body>\n" +
104" <section name='" + PAGE_NAME + "'>\n" +
105" <table border='0' style='width:110%; max-width:110%'>\n" +
106" <tr>\n" +
107" <th>label</th>\n" +
108" <th>type</th>\n" +
109" <th>default value</th>\n" +
110" <th>description</th>\n" +
111" </tr>\n");
112113for (AbstractConfOption confOption : options) {
114 String type = confOption.getType().toString().toLowerCase();
115116 sb.append(" <tr>\n");
117 sb.append(" <td>" + confOption.getKey() + "</td>\n");
118 sb.append(" <td>" + type + "</td>\n");
119 sb.append(" <td>" + confOption.getDefaultValueStr() + "</td>\n");
120 sb.append(" <td>" + confOption.getDescription() + "</td>\n");
121 sb.append(" </tr>\n");
122 }
123124 sb.append(" </table>\n" +
125" </section>\n" +
126" </body>\n" +
127"</document>\n");
128129return sb.toString();
130 }
131132/**133 * Command line utility to dump all Giraph options134 *135 * @param args cmdline args136 */137publicstaticvoid main(String[] args) throws IllegalAccessException {
138139 List<Field> fields = Arrays.asList(GiraphConstants.class.getFields());
140 List<AbstractConfOption> options = new ArrayList<>();
141for (Field field : fields) {
142if (AbstractConfOption.class.isAssignableFrom(field.getType())) {
143AbstractConfOption option = (AbstractConfOption) field.get(null);
144 options.add(option);
145 }
146 }
147148// in case an options was specified, this option is treated as the output149// file in which to write the HTML version of the list of available options150if (args.length == 1) {
151 String html = allOptionsHTMLString(options);
152153try {
154 FileWriter fs = new FileWriter(args[0]);
155 BufferedWriter out = new BufferedWriter(fs);
156157 out.write(html);
158 out.close();
159160 } catch (IOException e) {
161 LOG.error("Error: " + e.getMessage());
162 }
163 }
164165 LOG.info(allOptionsString(options));
166 }
167 }