You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

StoredUserConfig.java 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. * Copyright 2021 gitblit.com, Ingo Lafrenz
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.gitblit;
  17. import java.io.File;
  18. import java.io.FileWriter;
  19. import java.io.IOException;
  20. import java.io.PrintWriter;
  21. import java.util.List;
  22. import java.util.Map;
  23. import java.util.Objects;
  24. import java.util.SortedMap;
  25. import java.util.TreeMap;
  26. /**
  27. * Simple class with the only purpose to save the realm file (users.conf) in
  28. * a fast efficient manner. The JGit Config classes used previously caused
  29. * a massive CPU hog if the users file got bigger than about 30000 lines.
  30. *
  31. * @author Ingo Lafrenz
  32. *
  33. */
  34. public class StoredUserConfig {
  35. private final File realmFileCopy;
  36. private SortedMap<String, Section> sections = new TreeMap<>();
  37. public StoredUserConfig(File realmFileCopy) {
  38. this.realmFileCopy = realmFileCopy;
  39. }
  40. public void setString(final String section, final String subsection, String name, String value) {
  41. String key = generateKey(section, subsection);
  42. Section s = sections.get(key);
  43. if (s == null) {
  44. s = new Section(section, subsection);
  45. sections.put(key, s);
  46. }
  47. s.addEntry(name, value);
  48. }
  49. public void setBoolean(String section, String subsection, String name, boolean value) {
  50. setString(section, subsection, name, String.valueOf(value));
  51. }
  52. public void setStringList(String section, String subsection, String name, List<String> list) {
  53. for (String value : list) {
  54. setString(section, subsection, name, value);
  55. }
  56. }
  57. public void save() throws IOException {
  58. try (FileWriter fileWriter = new FileWriter(realmFileCopy);
  59. PrintWriter printWriter = new PrintWriter(fileWriter);) {
  60. for (Map.Entry<String,Section> entry : sections.entrySet()) {
  61. writeSection(printWriter, entry.getKey(), entry.getValue());
  62. }
  63. }
  64. }
  65. private static void writeSection(PrintWriter printWriter, String key, Section section) {
  66. if (section.getSubSection() == null) {
  67. printWriter.printf("[%s]\n", section.getName());
  68. }
  69. else {
  70. printWriter.printf("[%s \"%s\"]\n", section.getName(), section.getSubSection());
  71. }
  72. for (Entry entry : section.getEntries().values()) {
  73. writeEntry(printWriter, entry.getKey(), entry.getValue());
  74. }
  75. }
  76. private static void writeEntry(PrintWriter printWriter, String key, String value) {
  77. printWriter.printf("\t%s = %s\n", key, escape(value));
  78. }
  79. private static String escape(String value) {
  80. String fixedValue = '#' == value.charAt(0) ? "\"" + value + "\"" : value;
  81. fixedValue = fixedValue.replace("\\", "\\\\");
  82. return fixedValue;
  83. }
  84. private static String generateKey(String key, String subKey) {
  85. return "k:" + key + "s:" + (subKey == null ? "" : subKey);
  86. }
  87. private static class Section {
  88. private final String name;
  89. private final String subSection;
  90. private final SortedMap<String, Entry> entries = new TreeMap<>();
  91. public Section(String name, String subSection) {
  92. this.name = name;
  93. this.subSection = subSection;
  94. }
  95. public void addEntry(final String key, final String value) {
  96. entries.put(generateKey(key, value), new Entry(key, value));
  97. }
  98. public String getName() {
  99. return name;
  100. }
  101. public String getSubSection() {
  102. return subSection;
  103. }
  104. public SortedMap<String, Entry> getEntries() {
  105. return entries;
  106. }
  107. @Override
  108. public int hashCode() {
  109. return Objects.hash(name, subSection);
  110. }
  111. @Override
  112. public boolean equals(Object obj) {
  113. if (this == obj)
  114. return true;
  115. if (obj == null)
  116. return false;
  117. if (getClass() != obj.getClass())
  118. return false;
  119. Section other = (Section) obj;
  120. return Objects.equals(name, other.name) && Objects.equals(subSection, other.subSection);
  121. }
  122. @Override
  123. public String toString() {
  124. return String.format("Section [name=%s, subSection=%s]", name, subSection);
  125. }
  126. }
  127. private static class Entry {
  128. private final String key;
  129. private final String value;
  130. public Entry(String key, String value) {
  131. this.key = key;
  132. this.value = value;
  133. }
  134. public String getKey() {
  135. return key;
  136. }
  137. public String getValue() {
  138. return value;
  139. }
  140. @Override
  141. public int hashCode() {
  142. return Objects.hash(key, value);
  143. }
  144. @Override
  145. public boolean equals(Object obj) {
  146. if (this == obj)
  147. return true;
  148. if (obj == null)
  149. return false;
  150. if (getClass() != obj.getClass())
  151. return false;
  152. Entry other = (Entry) obj;
  153. return Objects.equals(key, other.key) && Objects.equals(value, other.value);
  154. }
  155. @Override
  156. public String toString() {
  157. return String.format("Entry [key=%s, value=%s]", key, value);
  158. }
  159. }
  160. }