PageRenderTime 885ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

/propconf/src/main/java/de/uniluebeck/itm/util/propconf/PropConfBuilder.java

https://github.com/itm/itm-utils
Java | 101 lines | 81 code | 20 blank | 0 comment | 4 complexity | bb710a3304a783c48192ef4b8e444b7a MD5 | raw file
  1. package de.uniluebeck.itm.util.propconf;
  2. import com.google.inject.Guice;
  3. import java.io.OutputStream;
  4. import java.io.PrintWriter;
  5. import java.io.StringWriter;
  6. import java.io.Writer;
  7. import java.lang.reflect.Field;
  8. import java.util.*;
  9. import static com.google.common.base.Throwables.propagate;
  10. import static de.uniluebeck.itm.util.propconf.PropConfHelper.getDeclaredAnnotations;
  11. public abstract class PropConfBuilder {
  12. @SuppressWarnings("unused")
  13. public static <T> T buildConfig(final Class<? extends T> configClass, final Properties properties) {
  14. return Guice.createInjector(new PropConfModule(properties, configClass)).getInstance(configClass);
  15. }
  16. @SuppressWarnings("unused")
  17. public static String getDocumentationString(final Class<?>... configClasses) {
  18. final StringWriter stringWriter = new StringWriter();
  19. printDocumentation(stringWriter, configClasses);
  20. return stringWriter.toString();
  21. }
  22. @SuppressWarnings("unused")
  23. public static OutputStream printDocumentation(final OutputStream outputStream, final Class<?>... configClasses) {
  24. printDocumentation(new PrintWriter(outputStream), configClasses);
  25. return outputStream;
  26. }
  27. public static void printDocumentationAndExit(final OutputStream outputStream, final Class<?>... configClasses) {
  28. printDocumentation(outputStream, configClasses);
  29. System.exit(1);
  30. }
  31. public static void printDocumentationAndExit(final Writer writer, final Class<?>... configClasses) {
  32. printDocumentation(writer, configClasses);
  33. System.exit(1);
  34. }
  35. @SuppressWarnings("unused")
  36. public static Writer printDocumentation(final Writer writer, final Class<?>... configClasses) {
  37. try {
  38. final SortedMap<Field, PropConf> sortedMap = new TreeMap<Field, PropConf>(new Comparator<Field>() {
  39. @Override
  40. public int compare(final Field o1, final Field o2) {
  41. try {
  42. return ((String) o1.get(null)).compareTo(((String) o2.get(null)));
  43. } catch (IllegalAccessException e) {
  44. throw propagate(e);
  45. }
  46. }
  47. }
  48. );
  49. sortedMap.putAll(getDeclaredAnnotations(configClasses));
  50. for (Map.Entry<Field, PropConf> entry : sortedMap.entrySet()) {
  51. final String usage = entry.getValue().usage();
  52. final String example = entry.getValue().example();
  53. final String defaultValue = entry.getValue().defaultValue();
  54. final String key = (String) entry.getKey().get(null);
  55. writer.write("# Usage: ");
  56. writer.write(usage);
  57. writer.write("\n");
  58. if (!example.isEmpty()) {
  59. writer.write("# Example: ");
  60. writer.write(example);
  61. writer.write("\n");
  62. }
  63. if (!defaultValue.isEmpty()) {
  64. writer.write("# Default value: ");
  65. writer.write(defaultValue);
  66. writer.write("\n");
  67. }
  68. if (!defaultValue.isEmpty()) {
  69. writer.write("#");
  70. }
  71. writer.write(String.format("%s = %s", key, defaultValue));
  72. writer.write("\n");
  73. writer.write("\n");
  74. writer.flush();
  75. }
  76. } catch (Exception e) {
  77. throw propagate(e);
  78. }
  79. return writer;
  80. }
  81. }