View Javadoc
1   /*
2    * Copyright (C) 2008, Shawn O. Pearce <spearce@spearce.org> and others
3    *
4    * This program and the accompanying materials are made available under the
5    * terms of the Eclipse Distribution License v. 1.0 which is available at
6    * https://www.eclipse.org/org/documents/edl-v10.php.
7    *
8    * SPDX-License-Identifier: BSD-3-Clause
9    */
10  
11  package org.eclipse.jgit.pgm;
12  
13  import static java.nio.charset.StandardCharsets.UTF_8;
14  
15  import java.io.BufferedReader;
16  import java.io.IOException;
17  import java.io.InputStreamReader;
18  import java.net.URL;
19  import java.util.ArrayList;
20  import java.util.Arrays;
21  import java.util.Collection;
22  import java.util.Enumeration;
23  import java.util.HashMap;
24  import java.util.Map;
25  import java.util.Vector;
26  
27  /**
28   * List of all commands known by jgit's command line tools.
29   * <p>
30   * Commands are implementations of {@link org.eclipse.jgit.pgm.TextBuiltin},
31   * with an optional {@link org.eclipse.jgit.pgm.Command} class annotation to
32   * insert additional documentation or override the default command name (which
33   * is guessed from the class name).
34   * <p>
35   * Commands may be registered by adding them to a services file in the same JAR
36   * (or classes directory) as the command implementation. The service file name
37   * is <code>META-INF/services/org.eclipse.jgit.pgm.TextBuiltin</code> and it
38   * contains one concrete implementation class name per line.
39   * <p>
40   * Command registration is identical to Java 6's services, however the catalog
41   * uses a lightweight wrapper to delay creating a command instance as much as
42   * possible. This avoids initializing the AWT or SWT GUI toolkits even if the
43   * command's constructor might require them.
44   */
45  public class CommandCatalog {
46  	private static final CommandCatalog INSTANCE = new CommandCatalog();
47  
48  	/**
49  	 * Locate a single command by its user friendly name.
50  	 *
51  	 * @param name
52  	 *            name of the command. Typically in dash-lower-case-form, which
53  	 *            was derived from the DashLowerCaseForm class name.
54  	 * @return the command instance; null if no command exists by that name.
55  	 */
56  	public static CommandRef get(String name) {
57  		return INSTANCE.commands.get(name);
58  	}
59  
60  	/**
61  	 * Get all commands sorted by their name
62  	 *
63  	 * @return all known commands, sorted by command name.
64  	 */
65  	public static CommandRef[] all() {
66  		return toSortedArray(INSTANCE.commands.values());
67  	}
68  
69  	/**
70  	 * Get all common commands sorted by their name
71  	 *
72  	 * @return all common commands, sorted by command name.
73  	 */
74  	public static CommandRef[] common() {
75  		final ArrayList<CommandRef> common = new ArrayList<>();
76  		for (CommandRef c : INSTANCE.commands.values())
77  			if (c.isCommon())
78  				common.add(c);
79  		return toSortedArray(common);
80  	}
81  
82  	private static CommandRef[] toSortedArray(Collection<CommandRef> c) {
83  		final CommandRef[] r = c.toArray(new CommandRef[0]);
84  		Arrays.sort(r, (CommandRef o1, CommandRef o2) -> o1.getName()
85  				.compareTo(o2.getName()));
86  		return r;
87  	}
88  
89  	private final ClassLoader ldr;
90  
91  	private final Map<String, CommandRef> commands;
92  
93  	private CommandCatalog() {
94  		ldr = Thread.currentThread().getContextClassLoader();
95  		commands = new HashMap<>();
96  
97  		final Enumeration<URL> catalogs = catalogs();
98  		while (catalogs.hasMoreElements())
99  			scan(catalogs.nextElement());
100 	}
101 
102 	private Enumeration<URL> catalogs() {
103 		try {
104 			final String pfx = "META-INF/services/"; //$NON-NLS-1$
105 			return ldr.getResources(pfx + TextBuiltin.class.getName());
106 		} catch (IOException err) {
107 			return new Vector<URL>().elements();
108 		}
109 	}
110 
111 	private void scan(URL cUrl) {
112 		try (BufferedReader cIn = new BufferedReader(
113 				new InputStreamReader(cUrl.openStream(), UTF_8))) {
114 			String line;
115 			while ((line = cIn.readLine()) != null) {
116 				if (line.length() > 0 && !line.startsWith("#")) //$NON-NLS-1$
117 					load(line);
118 			}
119 		} catch (IOException e) {
120 			// Ignore errors
121 		}
122 	}
123 
124 	private void load(String cn) {
125 		final Class<? extends TextBuiltin> clazz;
126 		try {
127 			clazz = Class.forName(cn, false, ldr).asSubclass(TextBuiltin.class);
128 		} catch (ClassNotFoundException | ClassCastException notBuiltin) {
129 			// Doesn't exist, even though the service entry is present.
130 			// Isn't really a builtin, even though its listed as such.
131 			return;
132 		}
133 
134 		final CommandRef cr;
135 		final Command a = clazz.getAnnotation(Command.class);
136 		if (a != null)
137 			cr = new CommandRef(clazz, a);
138 		else
139 			cr = new CommandRef(clazz);
140 
141 		commands.put(cr.getName(), cr);
142 	}
143 }