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.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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.FileBasedConfig;
  59. import org.eclipse.jgit.lib.LockFile;
  60. import org.eclipse.jgit.lib.ObjectId;
  61. /**
  62. * Manages the {@code .eclipse_iplog} file in a project.
  63. */
  64. public class IpLogMeta {
  65. /** Default name of the {@code .eclipse_iplog} file. */
  66. public static final String IPLOG_CONFIG_FILE = ".eclipse_iplog";
  67. private static final String S_PROJECT = "project";
  68. private static final String S_CQ = "CQ";
  69. private static final String S_CONSUMES = "consumes";
  70. private static final String S_REVIEW = "review";
  71. private static final String K_URL = "url";
  72. private static final String K_NAME = "name";
  73. private static final String K_VERSION = "version";
  74. private static final String K_COMMENTS = "comments";
  75. private static final String K_SKIP_COMMIT = "skipCommit";
  76. private static final String K_LICENSE = "license";
  77. private static final String K_DESCRIPTION = "description";
  78. private static final String K_USE = "use";
  79. private static final String K_STATE = "state";
  80. private List<Project> projects = new ArrayList<Project>();
  81. private List<Project> consumedProjects = new ArrayList<Project>();
  82. private Set<CQ> cqs = new HashSet<CQ>();
  83. private String reviewUrl;
  84. List<Project> getProjects() {
  85. return projects;
  86. }
  87. List<Project> getConsumedProjects() {
  88. return consumedProjects;
  89. }
  90. Set<CQ> getCQs() {
  91. return cqs;
  92. }
  93. String getReviewUrl() {
  94. return reviewUrl;
  95. }
  96. void loadFrom(Config cfg) {
  97. projects.clear();
  98. consumedProjects.clear();
  99. cqs.clear();
  100. projects.addAll(parseProjects(cfg, S_PROJECT));
  101. consumedProjects.addAll(parseProjects(cfg, S_CONSUMES));
  102. for (String id : cfg.getSubsections(S_CQ)) {
  103. CQ cq = new CQ(Long.parseLong(id));
  104. cq.setDescription(cfg.getString(S_CQ, id, K_DESCRIPTION));
  105. cq.setLicense(cfg.getString(S_CQ, id, K_LICENSE));
  106. cq.setUse(cfg.getString(S_CQ, id, K_USE));
  107. cq.setState(cfg.getString(S_CQ, id, K_STATE));
  108. cq.setComments(cfg.getString(S_CQ, id, K_COMMENTS));
  109. cqs.add(cq);
  110. }
  111. reviewUrl = cfg.getString(S_REVIEW, null, K_URL);
  112. }
  113. private List<Project> parseProjects(final Config cfg,
  114. final String sectionName) {
  115. final List<Project> dst = new ArrayList<Project>();
  116. for (String id : cfg.getSubsections(sectionName)) {
  117. String name = cfg.getString(sectionName, id, K_NAME);
  118. Project project = new Project(id, name);
  119. project.setVersion(cfg.getString(sectionName, id, K_VERSION));
  120. project.setComments(cfg.getString(sectionName, id, K_COMMENTS));
  121. for (String c : cfg.getStringList(sectionName, id, K_SKIP_COMMIT))
  122. project.addSkipCommit(ObjectId.fromString(c));
  123. for (String license : cfg.getStringList(sectionName, id, K_LICENSE))
  124. project.addLicense(license);
  125. dst.add(project);
  126. }
  127. return dst;
  128. }
  129. /**
  130. * Query the Eclipse Foundation's IPzilla database for CQ records.
  131. * <p>
  132. * Updates the local {@code .eclipse_iplog} configuration file with current
  133. * information by deleting CQs which are no longer relevant, and adding or
  134. * updating any CQs which currently exist in the database.
  135. *
  136. * @param file
  137. * local file to update with current CQ records.
  138. * @param base
  139. * base https:// URL of the IPzilla server.
  140. * @param username
  141. * username to login to IPzilla as. Must be a Bugzilla username
  142. * of someone authorized to query the project's IPzilla records.
  143. * @param password
  144. * password for {@code username}.
  145. * @throws IOException
  146. * IPzilla cannot be queried, or the local file cannot be read
  147. * from or written to.
  148. * @throws ConfigInvalidException
  149. * the local file cannot be read, as it is not a valid
  150. * configuration file format.
  151. */
  152. public void syncCQs(File file, URL base, String username, String password)
  153. throws IOException, ConfigInvalidException {
  154. if (!file.getParentFile().exists())
  155. file.getParentFile().mkdirs();
  156. LockFile lf = new LockFile(file);
  157. if (!lf.lock())
  158. throw new IOException(MessageFormat.format(IpLogText.get().cannotLock, file));
  159. try {
  160. FileBasedConfig cfg = new FileBasedConfig(file);
  161. cfg.load();
  162. loadFrom(cfg);
  163. IPZillaQuery ipzilla = new IPZillaQuery(base, username, password);
  164. Set<CQ> current = ipzilla.getCQs(projects);
  165. for (CQ cq : sort(current, CQ.COMPARATOR)) {
  166. String id = Long.toString(cq.getID());
  167. set(cfg, S_CQ, id, K_DESCRIPTION, cq.getDescription());
  168. set(cfg, S_CQ, id, K_LICENSE, cq.getLicense());
  169. set(cfg, S_CQ, id, K_USE, cq.getUse());
  170. set(cfg, S_CQ, id, K_STATE, cq.getState());
  171. set(cfg, S_CQ, id, K_COMMENTS, cq.getComments());
  172. }
  173. for (CQ cq : cqs) {
  174. if (!current.contains(cq))
  175. cfg.unsetSection(S_CQ, Long.toString(cq.getID()));
  176. }
  177. lf.write(Constants.encode(cfg.toText()));
  178. if (!lf.commit())
  179. throw new IOException(MessageFormat.format(IpLogText.get().cannotWrite, file));
  180. } finally {
  181. lf.unlock();
  182. }
  183. }
  184. private static void set(Config cfg, String section, String subsection,
  185. String key, String value) {
  186. if (value == null || "".equals(value))
  187. cfg.unset(section, subsection, key);
  188. else
  189. cfg.setString(section, subsection, key, value);
  190. }
  191. private static <T, Q extends Comparator<T>> Iterable<T> sort(
  192. Collection<T> objs, Q cmp) {
  193. ArrayList<T> sorted = new ArrayList<T>(objs);
  194. Collections.sort(sorted, cmp);
  195. return sorted;
  196. }
  197. }