KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > enterprise > cli > commands > ExportCommand


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 package com.sun.enterprise.cli.commands;
25
26 import java.util.Iterator JavaDoc;
27 import java.util.Vector JavaDoc;
28 import com.sun.enterprise.cli.framework.*;
29
30 /**
31  * This class is the export command.
32  * It will set the given environment variable in multimode.
33  */

34 public class ExportCommand extends S1ASCommand
35 {
36
37     private final static String JavaDoc ENVIRONMENT_PREFIX = "AS_ADMIN_";
38     private final static String JavaDoc ENVIRONMENT_DELIMITER = "=";
39     private final static String JavaDoc PASSWORD_STRING = "password";
40
41     private CommandEnvironment commandEnvironment = CommandEnvironment.getInstance();
42
43     /** Creates new ExportCommand */
44     public ExportCommand()
45     {
46     }
47
48     /**
49      * Validates the Options for correctness
50      * @return boolean returns true if validation is succesful else false
51      */

52     public boolean validateOptions() throws CommandValidationException
53     {
54         return super.validateOptions();
55     }
56     
57     /**
58      * Executes the command
59      * @throws CommandException
60      */

61     public void runCommand() throws CommandException, CommandValidationException
62     {
63         validateOptions();
64         
65         Vector JavaDoc 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 JavaDoc)operands.elementAt(ii));
74         }
75     }
76
77     
78     /**
79      * this method check for prefix AS_ADMIN
80      * retursn the name without the prefix AS_ADMIN
81      */

82     private String JavaDoc checkForPrefix(String JavaDoc name) throws CommandException
83     {
84         String JavaDoc envName = null;
85         //check for prefix AS_ADMIN
86
if (name.regionMatches(true, 0, ENVIRONMENT_PREFIX, 0,
87                                ENVIRONMENT_PREFIX.length()))
88         {
89             //extract AS_ADMIN from sOperandName
90
envName = name.substring(ENVIRONMENT_PREFIX.length());
91         }
92         else
93         {
94             throw new CommandException(getLocalizedString("CouldNotSetVariable",
95                                                           new Object JavaDoc[] {name}));
96         }
97         return envName.toLowerCase();
98     }
99
100     
101     /**
102      * this method prints the environment variable.
103      */

104     private void printEnvValues()
105     {
106         if (commandEnvironment.getNumEnvironments() == 0)
107             CLILogger.getInstance().printDetailMessage(getLocalizedString("NoEnvironment"));
108         
109         Iterator JavaDoc envIterator = commandEnvironment.getEnvironments().keySet().iterator();
110         while (envIterator.hasNext())
111         {
112             final String JavaDoc name = (String JavaDoc) envIterator.next();
113             final String JavaDoc envName = ENVIRONMENT_PREFIX.concat(name.toUpperCase());
114
115             CLILogger.getInstance().printDebugMessage(commandEnvironment.toString());
116             printOneEnvValue(name, envName);
117         }
118     }
119
120     
121     /**
122      * print one environment
123      * @param name used in the environment
124      * @param displayName is the display name with AS_ADMIN_ prefix
125      */

126     private void printOneEnvValue(String JavaDoc name, String JavaDoc displayName)
127     {
128         //Do not display the password value in clear text
129
if (name.endsWith(PASSWORD_STRING))
130         {
131             CLILogger.getInstance().printMessage(displayName + " = ********");
132         }
133         else
134         {
135             CLILogger.getInstance().printMessage(displayName + " = " +
136                                                  (String JavaDoc)commandEnvironment.getEnvironmentValue(name));
137         }
138     }
139     
140
141     /**
142      * this method updates the variable in the CommandEnvironment object.
143      * @param nameStr - the name of the environment
144      */

145     private void updateEnvironment(String JavaDoc nameStr)
146     throws CommandException
147     {
148         final int nameend = nameStr.indexOf(ENVIRONMENT_DELIMITER);
149
150         if (nameend == nameStr.length()-1)
151         {
152             final String JavaDoc envName = checkForPrefix(nameStr.substring(0, nameend));
153             if (commandEnvironment.removeEnvironment(envName) == null)
154                 throw new CommandException(getLocalizedString("UnableToRemoveEnv",
155                                                               new Object JavaDoc[] {nameStr}));
156         }
157         //print the value is the command is "export AS_ADMIN_<name>"
158
else if (nameend == -1)
159         {
160             final String JavaDoc name = checkForPrefix(nameStr);
161             printOneEnvValue(name, nameStr);
162         }
163         else
164         {
165             final String JavaDoc envName = checkForPrefix(nameStr.substring(0, nameend));
166             commandEnvironment.setEnvironment(envName,
167                                               nameStr.substring(nameend+1));
168         }
169     }
170 }
171
Popular Tags