This project has retired. For details please refer to its Attic page.
AllOptions xref
View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * 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 and
16   * limitations under the License.
17   */
18  package org.apache.giraph.conf;
19  
20  import org.apache.log4j.Logger;
21  
22  import java.io.BufferedWriter;
23  import java.io.FileWriter;
24  import java.io.IOException;
25  import java.lang.reflect.Field;
26  import java.util.ArrayList;
27  import java.util.Arrays;
28  import java.util.Collections;
29  import java.util.List;
30  
31  /**
32   * Tracks all of the Giraph options
33   */
34  public class AllOptions {
35    /**  logger object */
36    private static final Logger LOG = Logger.getLogger(AllOptions.class);
37  
38    /** page name for the HTML page generation */
39    private static final String PAGE_NAME = "Giraph Options";
40  
41    /** Don't construct */
42    private AllOptions() { }
43  
44  
45    /**
46     * String representation of all of the options stored
47     * @param options List of loaded options
48     * @return string
49     */
50    private static String allOptionsString(List<AbstractConfOption> options) {
51      Collections.sort(options);
52      StringBuilder sb = new StringBuilder(options.size() * 30);
53      sb.append("All Options:\n");
54      ConfOptionType lastType = null;
55      for (AbstractConfOption confOption : options) {
56        if (!confOption.getType().equals(lastType)) {
57          sb.append(confOption.getType().toString().toLowerCase()).append(":\n");
58          lastType = confOption.getType();
59        }
60        sb.append(confOption);
61      }
62      return sb.toString();
63    }
64  
65    /**
66     * HTML String representation of all the options stored
67     * @param options List of loaded options
68     * @return String the HTML representation of the registered options
69     */
70    private static String allOptionsHTMLString(List<AbstractConfOption> options) {
71      Collections.sort(options);
72      StringBuilder sb = new StringBuilder(options.size() * 30);
73  
74      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");
112 
113     for (AbstractConfOption confOption : options) {
114       String type = confOption.getType().toString().toLowerCase();
115 
116       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     }
123 
124     sb.append("      </table>\n" +
125               "    </section>\n" +
126               "  </body>\n" +
127               "</document>\n");
128 
129     return sb.toString();
130   }
131 
132   /**
133    * Command line utility to dump all Giraph options
134    *
135    * @param args cmdline args
136    */
137   public static void main(String[] args) throws IllegalAccessException {
138 
139     List<Field> fields = Arrays.asList(GiraphConstants.class.getFields());
140     List<AbstractConfOption> options = new ArrayList<>();
141     for (Field field : fields) {
142       if (AbstractConfOption.class.isAssignableFrom(field.getType())) {
143         AbstractConfOption option = (AbstractConfOption) field.get(null);
144         options.add(option);
145       }
146     }
147 
148     // in case an options was specified, this option is treated as the output
149     // file in which to write the HTML version of the list of available options
150     if (args.length == 1) {
151       String html = allOptionsHTMLString(options);
152 
153       try {
154         FileWriter     fs  = new FileWriter(args[0]);
155         BufferedWriter out = new BufferedWriter(fs);
156 
157         out.write(html);
158         out.close();
159 
160       } catch (IOException e) {
161         LOG.error("Error: " + e.getMessage());
162       }
163     }
164 
165     LOG.info(allOptionsString(options));
166   }
167 }