View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *  http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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          // Command vars always has a parent, set only makes sence when setting in parent's scope
90          Variables vars = variables.parent();
91  
92          vars.unset(name);
93      }
94  }