KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > lib > cvsclient > commandLine > CommandFactory


1 /*****************************************************************************
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14
15  * The Original Software is the CVS Client Library.
16  * The Initial Developer of the Original Software is Robert Greig.
17  * Portions created by Robert Greig are Copyright (C) 2000.
18  * All Rights Reserved.
19
20  * Contributor(s): Robert Greig.
21  *****************************************************************************/

22 package org.netbeans.lib.cvsclient.commandLine;
23
24 import java.lang.reflect.*;
25 import java.util.HashMap JavaDoc;
26 import java.util.HashSet JavaDoc;
27 import java.util.Map JavaDoc;
28 import java.util.Set JavaDoc;
29
30 import org.netbeans.lib.cvsclient.command.*;
31 import org.netbeans.lib.cvsclient.commandLine.command.CommandProvider;
32
33 /**
34  * A factory for commands. Given a command name, and any arguments passed to
35  * that command on the command line, it will return a configured Command
36  * object, ready for execution.
37  * @author Robert Greig
38  * @see org.netbeans.lib.cvsclient.command.Command
39  */

40 public class CommandFactory {
41     
42     private static final String JavaDoc[] COMMAND_CLASSES = new String JavaDoc[] {
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 JavaDoc commandProvidersByNames;
50     
51     private CommandFactory() {
52         createCommandProviders();
53     }
54     
55     private void createCommandProviders() {
56         commandProvidersByNames = new HashMap JavaDoc();
57         String JavaDoc packageName = CommandFactory.class.getPackage().getName() + ".command.";
58         for (int i = 0; i < COMMAND_CLASSES.length; i++) {
59             Class JavaDoc 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 JavaDoc[] synonyms = provider.getSynonyms();
65                 for (int j = 0; j < synonyms.length; j++) {
66                     commandProvidersByNames.put(synonyms[j], provider);
67                 }
68             } catch (Exception JavaDoc e) {
69                 System.err.println("Creation of command '"+COMMAND_CLASSES[i]+"' failed:");
70                 e.printStackTrace(System.err);
71                 continue;
72             }
73         }
74     }
75     
76     /**
77      * Get the default instance of CommandFactory.
78      */

79     public static synchronized CommandFactory getDefault() {
80         if (instance == null) {
81             instance = new CommandFactory();
82         }
83         return instance;
84     }
85     
86     /**
87      * Create a CVS command.
88      * @param commandName The name of the command to create
89      * @param args The array of arguments
90      * @param startingIndex The index of the first argument of the command in the array
91      * @param workingDir The working directory
92      */

93     public Command createCommand(String JavaDoc commandName, String JavaDoc[] args,
94                                  int startingIndex, GlobalOptions gopt,
95                                  String JavaDoc workingDir) throws IllegalArgumentException JavaDoc {
96         CommandProvider provider = (CommandProvider) commandProvidersByNames.get(commandName);
97         if (provider == null) {
98             throw new IllegalArgumentException JavaDoc("Unknown command: '"+commandName+"'");
99         }
100         return provider.createCommand(args, startingIndex, gopt, workingDir);
101     }
102     
103     /**
104      * Get the provider of a command.
105      * @param name The name of the command to get the provider for.
106      */

107     public CommandProvider getCommandProvider(String JavaDoc name) {
108         return (CommandProvider) commandProvidersByNames.get(name);
109     }
110     
111     /**
112      * Get the array of all command providers.
113      */

114     public CommandProvider[] getCommandProviders() {
115         Set JavaDoc providers = new HashSet JavaDoc(commandProvidersByNames.values());
116         return (CommandProvider[]) providers.toArray(new CommandProvider[0]);
117     }
118     
119     /*
120     public static Command getCommand(String commandName, String[] args,
121                                      int startingIndex, String workingDir)
122             throws IllegalArgumentException {
123         Class helper;
124         try {
125             helper = Class.forName("org.netbeans.lib.cvsclient.commandLine." +
126                                    "command." + commandName);
127         }
128         catch (Exception e) {
129             commandName = Character.toUpperCase(commandName.charAt(0)) + commandName.substring(1);
130             try {
131                 helper = Class.forName("org.netbeans.lib.cvsclient.commandLine." +
132                                        "command." + commandName);
133             }
134             catch (Exception ex) {
135                 System.err.println("Exception is: " + ex);
136                 throw new IllegalArgumentException("Unknown command " +
137                                                    commandName);
138             }
139         }
140
141         // the method invoked can throw an exception
142         try {
143             Method m = helper.getMethod("createCommand", new Class[]{
144                 String[].class,
145                 Integer.class,
146                 String.class});
147             return (Command) m.invoke(null, new Object[] { args,
148                     new Integer(startingIndex), workingDir });
149         }
150         catch (IllegalArgumentException e) {
151             throw e;
152         }
153         catch (InvocationTargetException ite) {
154             Throwable t = ite.getCause();
155             if (t instanceof IllegalArgumentException) {
156                 throw (IllegalArgumentException) t;
157             } else {
158                 IllegalArgumentException iaex = new IllegalArgumentException(t.getMessage());
159                 iaex.initCause(t);
160                 throw iaex;
161             }
162         }
163         catch (Exception e) {
164             IllegalArgumentException iaex = new IllegalArgumentException(e.getMessage());
165             iaex.initCause(e);
166             throw iaex;
167         }
168     }
169      */

170 }
Popular Tags