Coverage Report - org.apache.giraph.conf.AllOptions
 
Classes in this File Line Coverage Branch Coverage Complexity
AllOptions
0%
0/45
0%
0/12
0
 
 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  0
   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  0
   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  0
     Collections.sort(options);
 52  0
     StringBuilder sb = new StringBuilder(options.size() * 30);
 53  0
     sb.append("All Options:\n");
 54  0
     ConfOptionType lastType = null;
 55  0
     for (AbstractConfOption confOption : options) {
 56  0
       if (!confOption.getType().equals(lastType)) {
 57  0
         sb.append(confOption.getType().toString().toLowerCase()).append(":\n");
 58  0
         lastType = confOption.getType();
 59  
       }
 60  0
       sb.append(confOption);
 61  0
     }
 62  0
     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  0
     Collections.sort(options);
 72  0
     StringBuilder sb = new StringBuilder(options.size() * 30);
 73  
 
 74  0
     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  0
     for (AbstractConfOption confOption : options) {
 114  0
       String type = confOption.getType().toString().toLowerCase();
 115  
 
 116  0
       sb.append("       <tr>\n");
 117  0
       sb.append("         <td>" + confOption.getKey() + "</td>\n");
 118  0
       sb.append("         <td>" + type  + "</td>\n");
 119  0
       sb.append("         <td>" + confOption.getDefaultValueStr() + "</td>\n");
 120  0
       sb.append("         <td>" + confOption.getDescription() + "</td>\n");
 121  0
       sb.append("       </tr>\n");
 122  0
     }
 123  
 
 124  0
     sb.append("      </table>\n" +
 125  
               "    </section>\n" +
 126  
               "  </body>\n" +
 127  
               "</document>\n");
 128  
 
 129  0
     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  0
     List<Field> fields = Arrays.asList(GiraphConstants.class.getFields());
 140  0
     List<AbstractConfOption> options = new ArrayList<>();
 141  0
     for (Field field : fields) {
 142  0
       if (AbstractConfOption.class.isAssignableFrom(field.getType())) {
 143  0
         AbstractConfOption option = (AbstractConfOption) field.get(null);
 144  0
         options.add(option);
 145  
       }
 146  0
     }
 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  0
     if (args.length == 1) {
 151  0
       String html = allOptionsHTMLString(options);
 152  
 
 153  
       try {
 154  0
         FileWriter     fs  = new FileWriter(args[0]);
 155  0
         BufferedWriter out = new BufferedWriter(fs);
 156  
 
 157  0
         out.write(html);
 158  0
         out.close();
 159  
 
 160  0
       } catch (IOException e) {
 161  0
         LOG.error("Error: " + e.getMessage());
 162  0
       }
 163  
     }
 164  
 
 165  0
     LOG.info(allOptionsString(options));
 166  0
   }
 167  
 }