1 22 package org.netbeans.lib.cvsclient.commandLine; 23 24 import java.lang.reflect.*; 25 import java.util.HashMap ; 26 import java.util.HashSet ; 27 import java.util.Map ; 28 import java.util.Set ; 29 30 import org.netbeans.lib.cvsclient.command.*; 31 import org.netbeans.lib.cvsclient.commandLine.command.CommandProvider; 32 33 40 public class CommandFactory { 41 42 private static final String [] COMMAND_CLASSES = new String [] { 43 "Import", "add", "annotate", "checkout", "commit", "diff", "export", 44 "locbundlecheck", "log", "rannotate", "remove", "rlog", "rtag", "status", 45 "tag", "update" }; 46 47 private static CommandFactory instance; 48 49 private Map commandProvidersByNames; 50 51 private CommandFactory() { 52 createCommandProviders(); 53 } 54 55 private void createCommandProviders() { 56 commandProvidersByNames = new HashMap (); 57 String packageName = CommandFactory.class.getPackage().getName() + ".command."; 58 for (int i = 0; i < COMMAND_CLASSES.length; i++) { 59 Class providerClass; 60 try { 61 providerClass = Class.forName(packageName + COMMAND_CLASSES[i]); 62 CommandProvider provider = (CommandProvider) providerClass.newInstance(); 63 commandProvidersByNames.put(provider.getName(), provider); 64 String [] synonyms = provider.getSynonyms(); 65 for (int j = 0; j < synonyms.length; j++) { 66 commandProvidersByNames.put(synonyms[j], provider); 67 } 68 } catch (Exception e) { 69 System.err.println("Creation of command '"+COMMAND_CLASSES[i]+"' failed:"); 70 e.printStackTrace(System.err); 71 continue; 72 } 73 } 74 } 75 76 79 public static synchronized CommandFactory getDefault() { 80 if (instance == null) { 81 instance = new CommandFactory(); 82 } 83 return instance; 84 } 85 86 93 public Command createCommand(String commandName, String [] args, 94 int startingIndex, GlobalOptions gopt, 95 String workingDir) throws IllegalArgumentException { 96 CommandProvider provider = (CommandProvider) commandProvidersByNames.get(commandName); 97 if (provider == null) { 98 throw new IllegalArgumentException ("Unknown command: '"+commandName+"'"); 99 } 100 return provider.createCommand(args, startingIndex, gopt, workingDir); 101 } 102 103 107 public CommandProvider getCommandProvider(String name) { 108 return (CommandProvider) commandProvidersByNames.get(name); 109 } 110 111 114 public CommandProvider[] getCommandProviders() { 115 Set providers = new HashSet (commandProvidersByNames.values()); 116 return (CommandProvider[]) providers.toArray(new CommandProvider[0]); 117 } 118 119 170 } | Popular Tags |