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.

CommandCatalog.java 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org>
  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.pgm;
  44. import static java.nio.charset.StandardCharsets.UTF_8;
  45. import java.io.BufferedReader;
  46. import java.io.IOException;
  47. import java.io.InputStreamReader;
  48. import java.net.URL;
  49. import java.util.ArrayList;
  50. import java.util.Arrays;
  51. import java.util.Collection;
  52. import java.util.Comparator;
  53. import java.util.Enumeration;
  54. import java.util.HashMap;
  55. import java.util.Map;
  56. import java.util.Vector;
  57. /**
  58. * List of all commands known by jgit's command line tools.
  59. * <p>
  60. * Commands are implementations of {@link org.eclipse.jgit.pgm.TextBuiltin},
  61. * with an optional {@link org.eclipse.jgit.pgm.Command} class annotation to
  62. * insert additional documentation or override the default command name (which
  63. * is guessed from the class name).
  64. * <p>
  65. * Commands may be registered by adding them to a services file in the same JAR
  66. * (or classes directory) as the command implementation. The service file name
  67. * is <code>META-INF/services/org.eclipse.jgit.pgm.TextBuiltin</code> and it
  68. * contains one concrete implementation class name per line.
  69. * <p>
  70. * Command registration is identical to Java 6's services, however the catalog
  71. * uses a lightweight wrapper to delay creating a command instance as much as
  72. * possible. This avoids initializing the AWT or SWT GUI toolkits even if the
  73. * command's constructor might require them.
  74. */
  75. public class CommandCatalog {
  76. private static final CommandCatalog INSTANCE = new CommandCatalog();
  77. /**
  78. * Locate a single command by its user friendly name.
  79. *
  80. * @param name
  81. * name of the command. Typically in dash-lower-case-form, which
  82. * was derived from the DashLowerCaseForm class name.
  83. * @return the command instance; null if no command exists by that name.
  84. */
  85. public static CommandRef get(String name) {
  86. return INSTANCE.commands.get(name);
  87. }
  88. /**
  89. * Get all commands sorted by their name
  90. *
  91. * @return all known commands, sorted by command name.
  92. */
  93. public static CommandRef[] all() {
  94. return toSortedArray(INSTANCE.commands.values());
  95. }
  96. /**
  97. * Get all common commands sorted by their name
  98. *
  99. * @return all common commands, sorted by command name.
  100. */
  101. public static CommandRef[] common() {
  102. final ArrayList<CommandRef> common = new ArrayList<>();
  103. for (CommandRef c : INSTANCE.commands.values())
  104. if (c.isCommon())
  105. common.add(c);
  106. return toSortedArray(common);
  107. }
  108. private static CommandRef[] toSortedArray(Collection<CommandRef> c) {
  109. final CommandRef[] r = c.toArray(new CommandRef[0]);
  110. Arrays.sort(r, new Comparator<CommandRef>() {
  111. @Override
  112. public int compare(CommandRef o1, CommandRef o2) {
  113. return o1.getName().compareTo(o2.getName());
  114. }
  115. });
  116. return r;
  117. }
  118. private final ClassLoader ldr;
  119. private final Map<String, CommandRef> commands;
  120. private CommandCatalog() {
  121. ldr = Thread.currentThread().getContextClassLoader();
  122. commands = new HashMap<>();
  123. final Enumeration<URL> catalogs = catalogs();
  124. while (catalogs.hasMoreElements())
  125. scan(catalogs.nextElement());
  126. }
  127. private Enumeration<URL> catalogs() {
  128. try {
  129. final String pfx = "META-INF/services/"; //$NON-NLS-1$
  130. return ldr.getResources(pfx + TextBuiltin.class.getName());
  131. } catch (IOException err) {
  132. return new Vector<URL>().elements();
  133. }
  134. }
  135. private void scan(URL cUrl) {
  136. try (BufferedReader cIn = new BufferedReader(
  137. new InputStreamReader(cUrl.openStream(), UTF_8))) {
  138. String line;
  139. while ((line = cIn.readLine()) != null) {
  140. if (line.length() > 0 && !line.startsWith("#")) //$NON-NLS-1$
  141. load(line);
  142. }
  143. } catch (IOException e) {
  144. // Ignore errors
  145. }
  146. }
  147. private void load(String cn) {
  148. final Class<? extends TextBuiltin> clazz;
  149. try {
  150. clazz = Class.forName(cn, false, ldr).asSubclass(TextBuiltin.class);
  151. } catch (ClassNotFoundException notBuiltin) {
  152. // Doesn't exist, even though the service entry is present.
  153. //
  154. return;
  155. } catch (ClassCastException notBuiltin) {
  156. // Isn't really a builtin, even though its listed as such.
  157. //
  158. return;
  159. }
  160. final CommandRef cr;
  161. final Command a = clazz.getAnnotation(Command.class);
  162. if (a != null)
  163. cr = new CommandRef(clazz, a);
  164. else
  165. cr = new CommandRef(clazz);
  166. commands.put(cr.getName(), cr);
  167. }
  168. }