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 6.8KB

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