1 23 24 29 30 package com.sun.enterprise.cli.framework; 31 32 33 import java.util.Vector ; 34 import java.util.List ; 35 import java.util.HashMap ; 36 37 42 public class CommandFactory 43 { 44 45 46 public CommandFactory() 47 { 48 } 49 50 51 59 public static Command createCommand(ValidCommand commandMatched, HashMap options, 60 HashMap envOptions, Vector operands) 61 throws CommandValidationException 62 { 63 Command command = null; 64 String commandName = commandMatched.getName(); 65 String className = commandMatched.getClassName(); 66 if (className == null) 67 { 68 LocalStringsManager lsm = 69 LocalStringsManagerFactory.getFrameworkLocalStringsManager(); 70 throw new CommandValidationException(lsm.getString("InvalidCommand", 71 new Object [] {commandName} )); 72 } 73 try 74 { 75 Class 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.setUsageText(commandMatched.getUsageText()); 86 command.setProperties(commandMatched.getProperties()); 87 command.setBooleanOptions(constructBooleanOptionsList(commandMatched)); 88 } 89 catch(Exception e) 90 { 91 LocalStringsManager lsm = 92 LocalStringsManagerFactory.getFrameworkLocalStringsManager(); 93 throw new CommandValidationException(lsm.getString("UnableToCreateCommand", 94 new Object [] {commandName}), e); 95 } 97 return command; 98 } 99 100 101 106 private static List constructBooleanOptionsList(ValidCommand validCommand) 107 { 108 List booleanOptionsList = new Vector (); 109 final Vector 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 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 132 private static Vector determineOperand(Vector operands, String defaultOperand) 133 { 134 return operands.size()<1 && defaultOperand!=null ? 135 new Vector (java.util.Arrays.asList( 136 new String []{defaultOperand})):operands; 137 } 138 139 } 140 | Popular Tags |