1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.geronimo.gshell.commands.builtins;
21
22 import java.util.List;
23
24 import org.apache.geronimo.gshell.DefaultVariables;
25 import org.apache.geronimo.gshell.clp.Argument;
26 import org.apache.geronimo.gshell.clp.Option;
27 import org.apache.geronimo.gshell.command.CommandSupport;
28 import org.apache.geronimo.gshell.command.Variables;
29 import org.apache.geronimo.gshell.command.annotation.CommandComponent;
30
31 /**
32 * Unset a variable or property.
33 *
34 * @version $Rev: 576848 $ $Date: 2007-09-18 04:22:35 -0700 (Tue, 18 Sep 2007) $
35 */
36 @CommandComponent(id="unset", description="Unset a variable")
37 public class UnsetCommand
38 extends CommandSupport
39 {
40 enum Mode
41 {
42 VARIABLE,
43 PROPERTY
44 }
45
46 @Option(name="-m", aliases={"--mode"}, description="Unset mode")
47 private Mode mode = Mode.VARIABLE;
48
49 @Argument(required=true, description="Variable name")
50 private List<String> args;
51
52 protected Object doExecute() throws Exception {
53 for (String arg : args) {
54 String namevalue = String.valueOf(arg);
55
56 switch (mode) {
57 case PROPERTY:
58 unsetProperty(namevalue);
59 break;
60
61 case VARIABLE:
62 unsetVariable(namevalue);
63 break;
64 }
65 }
66
67 return SUCCESS;
68 }
69
70 private void ensureIsIdentifier(final String name) {
71 if (!DefaultVariables.isIdentifier(name)) {
72 throw new RuntimeException("Invalid identifer name: " + name);
73 }
74 }
75
76 private void unsetProperty(final String name) {
77 log.info("Unsetting system property: {}", name);
78
79 ensureIsIdentifier(name);
80
81 System.getProperties().remove(name);
82 }
83
84 private void unsetVariable(final String name) {
85 log.info("Unsetting variable: {}", name);
86
87 ensureIsIdentifier(name);
88
89
90 Variables vars = variables.parent();
91
92 vars.unset(name);
93 }
94 }