Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

IpLogMeta.java 6.7KB

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