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