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.

IpLogMeta.java 7.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /*
  2. * Copyright (C) 2010, Google Inc.
  3. * and other copyright owners as documented in the project's IP log.
  4. *
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Distribution License v1.0 which
  7. * accompanies this distribution, is reproduced below, and is
  8. * available at http://www.eclipse.org/org/documents/edl-v10.php
  9. *
  10. * All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or
  13. * without modification, are permitted provided that the following
  14. * conditions are met:
  15. *
  16. * - Redistributions of source code must retain the above copyright
  17. * notice, this list of conditions and the following disclaimer.
  18. *
  19. * - Redistributions in binary form must reproduce the above
  20. * copyright notice, this list of conditions and the following
  21. * disclaimer in the documentation and/or other materials provided
  22. * with the distribution.
  23. *
  24. * - Neither the name of the Eclipse Foundation, Inc. nor the
  25. * names of its contributors may be used to endorse or promote
  26. * products derived from this software without specific prior
  27. * written permission.
  28. *
  29. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  30. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  31. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  32. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  33. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  34. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  35. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  36. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  37. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  38. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  39. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  40. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  41. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  42. */
  43. package org.eclipse.jgit.iplog;
  44. import java.io.File;
  45. import java.io.IOException;
  46. import java.net.URL;
  47. import java.text.MessageFormat;
  48. import java.util.ArrayList;
  49. import java.util.Collection;
  50. import java.util.Collections;
  51. import java.util.Comparator;
  52. import java.util.HashSet;
  53. import java.util.List;
  54. import java.util.Set;
  55. import org.eclipse.jgit.errors.ConfigInvalidException;
  56. import org.eclipse.jgit.lib.Config;
  57. import org.eclipse.jgit.lib.Constants;
  58. import org.eclipse.jgit.lib.ObjectId;
  59. import org.eclipse.jgit.storage.file.FileBasedConfig;
  60. import org.eclipse.jgit.storage.file.LockFile;
  61. import org.eclipse.jgit.util.FS;
  62. import org.eclipse.jgit.util.FileUtils;
  63. /**
  64. * Manages the {@code .eclipse_iplog} file in a project.
  65. */
  66. public class IpLogMeta {
  67. /** Default name of the {@code .eclipse_iplog} file. */
  68. public static final String IPLOG_CONFIG_FILE = ".eclipse_iplog";
  69. private static final String S_PROJECT = "project";
  70. private static final String S_CQ = "CQ";
  71. private static final String S_CONSUMES = "consumes";
  72. private static final String S_REVIEW = "review";
  73. private static final String K_URL = "url";
  74. private static final String K_NAME = "name";
  75. private static final String K_VERSION = "version";
  76. private static final String K_COMMENTS = "comments";
  77. private static final String K_SKIP_COMMIT = "skipCommit";
  78. private static final String K_LICENSE = "license";
  79. private static final String K_DESCRIPTION = "description";
  80. private static final String K_USE = "use";
  81. private static final String K_STATE = "state";
  82. private List<Project> projects = new ArrayList<Project>();
  83. private List<Project> consumedProjects = new ArrayList<Project>();
  84. private Set<CQ> cqs = new HashSet<CQ>();
  85. private String reviewUrl;
  86. List<Project> getProjects() {
  87. return projects;
  88. }
  89. List<Project> getConsumedProjects() {
  90. return consumedProjects;
  91. }
  92. Set<CQ> getCQs() {
  93. return cqs;
  94. }
  95. String getReviewUrl() {
  96. return reviewUrl;
  97. }
  98. void loadFrom(Config cfg) {
  99. projects.clear();
  100. consumedProjects.clear();
  101. cqs.clear();
  102. projects.addAll(parseProjects(cfg, S_PROJECT));
  103. consumedProjects.addAll(parseProjects(cfg, S_CONSUMES));
  104. for (String id : cfg.getSubsections(S_CQ)) {
  105. CQ cq = new CQ(Long.parseLong(id));
  106. cq.setDescription(cfg.getString(S_CQ, id, K_DESCRIPTION));
  107. cq.setLicense(cfg.getString(S_CQ, id, K_LICENSE));
  108. cq.setUse(cfg.getString(S_CQ, id, K_USE));
  109. cq.setState(cfg.getString(S_CQ, id, K_STATE));
  110. cq.setComments(cfg.getString(S_CQ, id, K_COMMENTS));
  111. cqs.add(cq);
  112. }
  113. reviewUrl = cfg.getString(S_REVIEW, null, K_URL);
  114. }
  115. private List<Project> parseProjects(final Config cfg,
  116. final String sectionName) {
  117. final List<Project> dst = new ArrayList<Project>();
  118. for (String id : cfg.getSubsections(sectionName)) {
  119. String name = cfg.getString(sectionName, id, K_NAME);
  120. Project project = new Project(id, name);
  121. project.setVersion(cfg.getString(sectionName, id, K_VERSION));
  122. project.setComments(cfg.getString(sectionName, id, K_COMMENTS));
  123. for (String c : cfg.getStringList(sectionName, id, K_SKIP_COMMIT))
  124. project.addSkipCommit(ObjectId.fromString(c));
  125. for (String license : cfg.getStringList(sectionName, id, K_LICENSE))
  126. project.addLicense(license);
  127. dst.add(project);
  128. }
  129. return dst;
  130. }
  131. /**
  132. * Query the Eclipse Foundation's IPzilla database for CQ records.
  133. * <p>
  134. * Updates the local {@code .eclipse_iplog} configuration file with current
  135. * information by deleting CQs which are no longer relevant, and adding or
  136. * updating any CQs which currently exist in the database.
  137. *
  138. * @param file
  139. * local file to update with current CQ records.
  140. * @param fs
  141. * the file system abstraction which will be necessary to perform
  142. * certain file system operations.
  143. * @param base
  144. * base https:// URL of the IPzilla server.
  145. * @param username
  146. * username to login to IPzilla as. Must be a Bugzilla username
  147. * of someone authorized to query the project's IPzilla records.
  148. * @param password
  149. * password for {@code username}.
  150. * @throws IOException
  151. * IPzilla cannot be queried, or the local file cannot be read
  152. * from or written to.
  153. * @throws ConfigInvalidException
  154. * the local file cannot be read, as it is not a valid
  155. * configuration file format.
  156. */
  157. public void syncCQs(File file, FS fs, URL base, String username,
  158. String password) throws IOException, ConfigInvalidException {
  159. FileUtils.mkdirs(file.getParentFile(), true);
  160. LockFile lf = new LockFile(file, fs);
  161. if (!lf.lock())
  162. throw new IOException(MessageFormat.format(IpLogText.get().cannotLock, file));
  163. try {
  164. FileBasedConfig cfg = new FileBasedConfig(file, fs);
  165. cfg.load();
  166. loadFrom(cfg);
  167. IPZillaQuery ipzilla = new IPZillaQuery(base, username, password);
  168. Set<CQ> current = ipzilla.getCQs(projects);
  169. for (CQ cq : sort(current, CQ.COMPARATOR)) {
  170. String id = Long.toString(cq.getID());
  171. set(cfg, S_CQ, id, K_DESCRIPTION, cq.getDescription());
  172. set(cfg, S_CQ, id, K_LICENSE, cq.getLicense());
  173. set(cfg, S_CQ, id, K_USE, cq.getUse());
  174. set(cfg, S_CQ, id, K_STATE, cq.getState());
  175. set(cfg, S_CQ, id, K_COMMENTS, cq.getComments());
  176. }
  177. for (CQ cq : cqs) {
  178. if (!current.contains(cq))
  179. cfg.unsetSection(S_CQ, Long.toString(cq.getID()));
  180. }
  181. lf.write(Constants.encode(cfg.toText()));
  182. if (!lf.commit())
  183. throw new IOException(MessageFormat.format(IpLogText.get().cannotWrite, file));
  184. } finally {
  185. lf.unlock();
  186. }
  187. }
  188. private static void set(Config cfg, String section, String subsection,
  189. String key, String value) {
  190. if (value == null || "".equals(value))
  191. cfg.unset(section, subsection, key);
  192. else
  193. cfg.setString(section, subsection, key, value);
  194. }
  195. private static <T, Q extends Comparator<T>> Iterable<T> sort(
  196. Collection<T> objs, Q cmp) {
  197. ArrayList<T> sorted = new ArrayList<T>(objs);
  198. Collections.sort(sorted, cmp);
  199. return sorted;
  200. }
  201. }