Config.java 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. package uk.ac.ic.wlgitbridge.application;
  2. import com.google.gson.Gson;
  3. import com.google.gson.JsonElement;
  4. import com.google.gson.JsonObject;
  5. import com.google.gson.JsonParseException;
  6. import uk.ac.ic.wlgitbridge.application.exception.InvalidConfigFileException;
  7. import uk.ac.ic.wlgitbridge.writelatex.api.request.base.JSONSource;
  8. import java.io.FileReader;
  9. import java.io.IOException;
  10. /**
  11. * Created by Winston on 05/12/14.
  12. */
  13. public class Config implements JSONSource {
  14. private int port;
  15. private String rootGitDirectory;
  16. private String username;
  17. private String password;
  18. private String apiBaseURL;
  19. private String postbackURL;
  20. private String serviceName;
  21. public Config(String configFilePath) throws InvalidConfigFileException, IOException {
  22. try {
  23. fromJSON(new Gson().fromJson(new FileReader(configFilePath), JsonElement.class));
  24. } catch (JsonParseException e) {
  25. throw new IOException();
  26. }
  27. }
  28. @Override
  29. public void fromJSON(JsonElement json) {
  30. JsonObject configObject = json.getAsJsonObject();
  31. port = getElement(configObject, "port").getAsInt();
  32. rootGitDirectory = getElement(configObject, "rootGitDirectory").getAsString();
  33. username = getOptionalString(configObject, "username");
  34. password = getOptionalString(configObject, "password");
  35. String apiBaseURL = getElement(configObject, "apiBaseUrl").getAsString();
  36. if (!apiBaseURL.endsWith("/")) {
  37. apiBaseURL += "/";
  38. }
  39. this.apiBaseURL = apiBaseURL;
  40. serviceName = getElement(configObject, "serviceName").getAsString();
  41. postbackURL = getElement(configObject, "postbackUrl").getAsString();
  42. if (!postbackURL.endsWith("/")) {
  43. postbackURL += "/";
  44. }
  45. }
  46. public int getPort() {
  47. return port;
  48. }
  49. public String getRootGitDirectory() {
  50. return rootGitDirectory;
  51. }
  52. public String getUsername() {
  53. return username;
  54. }
  55. public String getPassword() {
  56. return password;
  57. }
  58. public String getAPIBaseURL() {
  59. return apiBaseURL;
  60. }
  61. private JsonElement getElement(JsonObject configObject, String name) {
  62. JsonElement element = configObject.get(name);
  63. if (element == null) {
  64. throw new RuntimeException(new InvalidConfigFileException(name));
  65. }
  66. return element;
  67. }
  68. private String getOptionalString(JsonObject configObject, String name) {
  69. JsonElement element = configObject.get(name);
  70. if (element == null || !element.isJsonPrimitive()) {
  71. return "";
  72. }
  73. return element.getAsString();
  74. }
  75. public String getServiceName() {
  76. return serviceName;
  77. }
  78. public String getPostbackURL() {
  79. return postbackURL;
  80. }
  81. }