1 23 24 package com.sun.enterprise.cli.commands; 25 26 import java.util.Iterator ; 27 import java.util.Vector ; 28 import com.sun.enterprise.cli.framework.*; 29 30 34 public class ExportCommand extends S1ASCommand 35 { 36 37 private final static String ENVIRONMENT_PREFIX = "AS_ADMIN_"; 38 private final static String ENVIRONMENT_DELIMITER = "="; 39 private final static String PASSWORD_STRING = "password"; 40 41 private CommandEnvironment commandEnvironment = CommandEnvironment.getInstance(); 42 43 44 public ExportCommand() 45 { 46 } 47 48 52 public boolean validateOptions() throws CommandValidationException 53 { 54 return super.validateOptions(); 55 } 56 57 61 public void runCommand() throws CommandException, CommandValidationException 62 { 63 validateOptions(); 64 65 Vector operands = getOperands(); 66 if (operands.size() == 0) 67 { 68 printEnvValues(); 69 return; 70 } 71 for (int ii = 0; ii < operands.size(); ii++) 72 { 73 updateEnvironment((String )operands.elementAt(ii)); 74 } 75 } 76 77 78 82 private String checkForPrefix(String name) throws CommandException 83 { 84 String envName = null; 85 if (name.regionMatches(true, 0, ENVIRONMENT_PREFIX, 0, 87 ENVIRONMENT_PREFIX.length())) 88 { 89 envName = name.substring(ENVIRONMENT_PREFIX.length()); 91 } 92 else 93 { 94 throw new CommandException(getLocalizedString("CouldNotSetVariable", 95 new Object [] {name})); 96 } 97 return envName.toLowerCase(); 98 } 99 100 101 104 private void printEnvValues() 105 { 106 if (commandEnvironment.getNumEnvironments() == 0) 107 CLILogger.getInstance().printDetailMessage(getLocalizedString("NoEnvironment")); 108 109 Iterator envIterator = commandEnvironment.getEnvironments().keySet().iterator(); 110 while (envIterator.hasNext()) 111 { 112 final String name = (String ) envIterator.next(); 113 final String envName = ENVIRONMENT_PREFIX.concat(name.toUpperCase()); 114 115 CLILogger.getInstance().printDebugMessage(commandEnvironment.toString()); 116 printOneEnvValue(name, envName); 117 } 118 } 119 120 121 126 private void printOneEnvValue(String name, String displayName) 127 { 128 if (name.endsWith(PASSWORD_STRING)) 130 { 131 CLILogger.getInstance().printMessage(displayName + " = ********"); 132 } 133 else 134 { 135 CLILogger.getInstance().printMessage(displayName + " = " + 136 (String )commandEnvironment.getEnvironmentValue(name)); 137 } 138 } 139 140 141 145 private void updateEnvironment(String nameStr) 146 throws CommandException 147 { 148 final int nameend = nameStr.indexOf(ENVIRONMENT_DELIMITER); 149 150 if (nameend == nameStr.length()-1) 151 { 152 final String envName = checkForPrefix(nameStr.substring(0, nameend)); 153 if (commandEnvironment.removeEnvironment(envName) == null) 154 throw new CommandException(getLocalizedString("UnableToRemoveEnv", 155 new Object [] {nameStr})); 156 } 157 else if (nameend == -1) 159 { 160 final String name = checkForPrefix(nameStr); 161 printOneEnvValue(name, nameStr); 162 } 163 else 164 { 165 final String envName = checkForPrefix(nameStr.substring(0, nameend)); 166 commandEnvironment.setEnvironment(envName, 167 nameStr.substring(nameend+1)); 168 } 169 } 170 } 171 | Popular Tags |