View Javadoc
1   /*
2    * Copyright (C) 2016, Matthias Sohn <matthias.sohn@sap.com> 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  package org.eclipse.jgit.attributes;
11  
12  import java.io.IOException;
13  import java.io.InputStream;
14  import java.io.OutputStream;
15  import java.util.Map;
16  import java.util.Set;
17  import java.util.concurrent.ConcurrentHashMap;
18  
19  import org.eclipse.jgit.lib.Repository;
20  
21  /**
22   * Registry for built-in filters
23   *
24   * @since 4.6
25   */
26  public class FilterCommandRegistry {
27  	private static Map<String, FilterCommandFactory> filterCommandRegistry = new ConcurrentHashMap<>();
28  
29  	/**
30  	 * Register a {@link org.eclipse.jgit.attributes.FilterCommandFactory}
31  	 * responsible for creating
32  	 * {@link org.eclipse.jgit.attributes.FilterCommand}s for a certain command
33  	 * name. If the factory f1 is registered for the name "jgit://builtin/x"
34  	 * then a call to <code>getCommand("jgit://builtin/x", ...)</code> will call
35  	 * <code>f1(...)</code> to create a new instance of
36  	 * {@link org.eclipse.jgit.attributes.FilterCommand}
37  	 *
38  	 * @param filterCommandName
39  	 *            the command name for which this factory is registered
40  	 * @param factory
41  	 *            the factory responsible for creating
42  	 *            {@link org.eclipse.jgit.attributes.FilterCommand}s for the
43  	 *            specified name
44  	 * @return the previous factory associated with <tt>commandName</tt>, or
45  	 *         <tt>null</tt> if there was no mapping for <tt>commandName</tt>
46  	 */
47  	public static FilterCommandFactory register(String filterCommandName,
48  			FilterCommandFactory factory) {
49  		return filterCommandRegistry.put(filterCommandName, factory);
50  	}
51  
52  	/**
53  	 * Unregister the {@link org.eclipse.jgit.attributes.FilterCommandFactory}
54  	 * registered for the given command name
55  	 *
56  	 * @param filterCommandName
57  	 *            the FilterCommandFactory's filter command name
58  	 * @return the previous factory associated with <tt>filterCommandName</tt>,
59  	 *         or <tt>null</tt> if there was no mapping for <tt>commandName</tt>
60  	 */
61  	public static FilterCommandFactory unregister(String filterCommandName) {
62  		return filterCommandRegistry.remove(filterCommandName);
63  	}
64  
65  	/**
66  	 * Check whether any
67  	 * {@link org.eclipse.jgit.attributes.FilterCommandFactory} is registered
68  	 * for a given command name
69  	 *
70  	 * @param filterCommandName
71  	 *            the name for which the registry should be checked
72  	 * @return <code>true</code> if any factory was registered for the name
73  	 */
74  	public static boolean isRegistered(String filterCommandName) {
75  		return filterCommandRegistry.containsKey(filterCommandName);
76  	}
77  
78  	/**
79  	 * Get registered filter commands
80  	 *
81  	 * @return Set of commandNames for which a
82  	 *         {@link org.eclipse.jgit.attributes.FilterCommandFactory} is
83  	 *         registered
84  	 */
85  	public static Set<String> getRegisteredFilterCommands() {
86  		return filterCommandRegistry.keySet();
87  	}
88  
89  	/**
90  	 * Create a new {@link org.eclipse.jgit.attributes.FilterCommand} for the
91  	 * given name. A factory must be registered for the name in advance.
92  	 *
93  	 * @param filterCommandName
94  	 *            The name for which a new
95  	 *            {@link org.eclipse.jgit.attributes.FilterCommand} should be
96  	 *            created
97  	 * @param db
98  	 *            the repository this command should work on
99  	 * @param in
100 	 *            the {@link java.io.InputStream} this
101 	 *            {@link org.eclipse.jgit.attributes.FilterCommand} should read
102 	 *            from
103 	 * @param out
104 	 *            the {@link java.io.OutputStream} this
105 	 *            {@link org.eclipse.jgit.attributes.FilterCommand} should write
106 	 *            to
107 	 * @return the command if a command could be created or <code>null</code> if
108 	 *         there was no factory registered for that name
109 	 * @throws java.io.IOException
110 	 */
111 	public static FilterCommand createFilterCommand(String filterCommandName,
112 			Repository db, InputStream in, OutputStream out)
113 			throws IOException {
114 		FilterCommandFactory cf = filterCommandRegistry.get(filterCommandName);
115 		return (cf == null) ? null : cf.create(db, in, out);
116 	}
117 
118 }