KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > cli > framework > CommandFactory


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /*
25  * CommandFactory.java
26  *
27  * Created on June 23, 2003, 4:01 PM
28  */

29
30 package com.sun.enterprise.cli.framework;
31
32
33 import java.util.Vector JavaDoc;
34 import java.util.List JavaDoc;
35 import java.util.HashMap JavaDoc;
36
37 /**
38  * This is a factory class that creates a Command object.
39  * @version $Revision: 1.4 $
40  * @author pa100654
41  */

42 public class CommandFactory
43 {
44     
45     /** Creates a new instance of CommandFactory */
46     public CommandFactory()
47     {
48     }
49
50     
51     /** The static class that creates the command object
52      * @param commandMatched, the ValidCommand object that contains
53      * vital info on the command.
54      * @param options, the options in command line
55      * @param operands,the operands in command line
56      * @throws CommandValidationException if there is an error creating
57      * the command.
58      */

59     public static Command createCommand(ValidCommand commandMatched, HashMap JavaDoc options,
60                                         HashMap JavaDoc envOptions, Vector JavaDoc operands)
61                                         throws CommandValidationException
62     {
63         Command command = null;
64         String JavaDoc commandName = commandMatched.getName();
65         String JavaDoc className = commandMatched.getClassName();
66         if (className == null)
67         {
68         LocalStringsManager lsm =
69         LocalStringsManagerFactory.getFrameworkLocalStringsManager();
70             throw new CommandValidationException(lsm.getString("InvalidCommand",
71                          new Object JavaDoc[] {commandName} ));
72         }
73         try
74         {
75             Class JavaDoc theClass = Class.forName(className);
76             command = (Command)theClass.newInstance();
77             command.setName(commandName);
78             command.setCLOptions(options);
79             command.setEnvOptions(envOptions);
80
81             command.setOperands(determineOperand(operands,
82                                                  commandMatched.getDefaultOperand()));
83             
84             //command.setOperands(operands);
85
command.setUsageText(commandMatched.getUsageText());
86             command.setProperties(commandMatched.getProperties());
87         command.setBooleanOptions(constructBooleanOptionsList(commandMatched));
88         }
89         catch(Exception JavaDoc e)
90         {
91         LocalStringsManager lsm =
92         LocalStringsManagerFactory.getFrameworkLocalStringsManager();
93         throw new CommandValidationException(lsm.getString("UnableToCreateCommand",
94                              new Object JavaDoc[] {commandName}), e);
95             //e.printStackTrace();
96
}
97         return command;
98     }
99
100
101     /**
102      * Create a list of boolean options for this command.
103      * @param ValidCommand contains the list of required and valid options.
104      * @return list of boolean options.
105      */

106     private static List JavaDoc constructBooleanOptionsList(ValidCommand validCommand)
107     {
108     List JavaDoc booleanOptionsList = new Vector JavaDoc();
109     final Vector JavaDoc requiredOptions = validCommand.getRequiredOptions();
110     for (int ii=0; ii<requiredOptions.size(); ii++)
111     {
112         final ValidOption validOption = (ValidOption)requiredOptions.get(ii);
113         if (validOption.getType().equals("boolean"))
114         booleanOptionsList.add(validOption.getName());
115     }
116     final Vector JavaDoc validOptions = validCommand.getValidOptions();
117     for (int ii=0; ii<validOptions.size(); ii++)
118     {
119         final ValidOption validOption = (ValidOption)validOptions.get(ii);
120         if (validOption.getType().equals("boolean"))
121         booleanOptionsList.add(validOption.getName());
122     }
123     return booleanOptionsList;
124     }
125     
126
127         /**
128          * This api will check if the user enters an operand, if not, then
129          * replace it with the defaultoperand specified in the
130          * CLIDescriptor.xml
131          */

132     private static Vector JavaDoc determineOperand(Vector JavaDoc operands, String JavaDoc defaultOperand)
133     {
134         return operands.size()<1 && defaultOperand!=null ?
135                new Vector JavaDoc(java.util.Arrays.asList(
136                new String JavaDoc[]{defaultOperand})):operands;
137     }
138     
139 }
140
Popular Tags