KickJava   Java API By Example, From Geeks To Geeks.

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


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 /**
32  * This class is the unset command.
33  * It will unset the given environment variable in multimode.
34  */

35 public class UnsetCommand extends S1ASCommand
36 {
37
38     private final static String JavaDoc ENVIRONMENT_PREFIX = "AS_ADMIN_";
39     private final static String JavaDoc ENVIRONMENT_DELIMITER = "=";
40
41     private CommandEnvironment commandEnvironment = CommandEnvironment.getInstance();
42
43     /** Creates new UnsetCommand */
44     public UnsetCommand()
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         for (int ii = 0; ii < operands.size(); ii++)
66         {
67         updateEnvironment((String JavaDoc)operands.elementAt(ii));
68         }
69     }
70
71     
72     /**
73      * this method check for prefix AS_ADMIN
74      * returns the name without the prefix AS_ADMIN
75      */

76     private String JavaDoc checkForPrefix(String JavaDoc name) throws CommandException
77     {
78     String JavaDoc envName = null;
79     //check for prefix AS_ADMIN
80
if (name.regionMatches(true, 0, ENVIRONMENT_PREFIX, 0,
81                                ENVIRONMENT_PREFIX.length()))
82         {
83         //extract AS_ADMIN from sOperandName
84
envName = name.substring(ENVIRONMENT_PREFIX.length());
85         }
86     else
87         {
88         throw new CommandException(getLocalizedString("CouldNotUnsetVariable",
89                               new Object JavaDoc[] {name}));
90     }
91     return envName.toLowerCase();
92     }
93
94     
95     /**
96      * this method updates the variables by removing them from the
97      * command environment.
98      * @param nameStr - the name of the environment to remove
99      * @throws CommandException if environment could not be removed.
100      */

101     private void updateEnvironment(String JavaDoc nameStr)
102     throws CommandException
103     {
104     final String JavaDoc envName = checkForPrefix(nameStr);
105     if (envName.equals("*"))
106     {
107         removeAllEnvironments();
108     }
109     else
110     {
111         if (commandEnvironment.removeEnvironment(envName) == null)
112         throw new CommandException(getLocalizedString("UnableToRemoveEnv",
113                        new Object JavaDoc[] {nameStr}));
114     }
115     }
116     
117
118     /**
119      * this method is called when AS_ADMIN_* is encountered.
120      * The wildcard means to remove all variables set in the environment.
121      */

122     private void removeAllEnvironments()
123     {
124     final java.util.HashMap JavaDoc envMap = new
125         java.util.HashMap JavaDoc(commandEnvironment.getEnvironments());
126     final java.util.Iterator JavaDoc envIter = envMap.keySet().iterator();
127
128     while (envIter.hasNext())
129     {
130         final String JavaDoc envName = (String JavaDoc)envIter.next();
131         if (commandEnvironment.removeEnvironment(envName) == null)
132         CLILogger.getInstance().printWarning(getLocalizedString(
133                              "UnableToRemoveEnv",
134                              new Object JavaDoc[] {envName}));
135     }
136     }
137
138 }
139
Popular Tags