Home » activemq-parent-5.3.1-source-release » org.apache.activemq.console.command.store.amq » [javadoc | source]

    1   /**
    2    * Licensed to the Apache Software Foundation (ASF) under one or more
    3    * contributor license agreements.  See the NOTICE file distributed with
    4    * this work for additional information regarding copyright ownership.
    5    * The ASF licenses this file to You under the Apache License, Version 2.0
    6    * (the "License"); you may not use this file except in compliance with
    7    * the License.  You may obtain a copy of the License at
    8    *
    9    *      http://www.apache.org/licenses/LICENSE-2.0
   10    *
   11    * Unless required by applicable law or agreed to in writing, software
   12    * distributed under the License is distributed on an "AS IS" BASIS,
   13    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   14    * See the License for the specific language governing permissions and
   15    * limitations under the License.
   16    */
   17   package org.apache.activemq.console.command.store.amq;
   18   
   19   import java.util.ArrayList;
   20   
   21   import org.apache.activemq.util.IntrospectionSupport;
   22   
   23   /**
   24    * Helper utility that can be used to set the properties on any object using
   25    * command line arguments.
   26    * 
   27    * @author <a href="http://hiramchirino.com">Hiram Chirino</a>
   28    */
   29   public final class CommandLineSupport {
   30   
   31       private CommandLineSupport() {
   32       }
   33       
   34       /**
   35        * Sets the properties of an object given the command line args.
   36        * 
   37        * if args contains: --ack-mode=AUTO --url=tcp://localhost:61616 --persistent 
   38        * 
   39        * then it will try to call the following setters on the target object.
   40        * 
   41        * target.setAckMode("AUTO");
   42        * target.setURL(new URI("tcp://localhost:61616") );
   43        * target.setPersistent(true);
   44        * 
   45        * Notice the the proper conversion for the argument is determined by examining the 
   46        * setter arguement type.  
   47        * 
   48        * @param target the object that will have it's properties set
   49        * @param args the commline options
   50        * @return any arguments that are not valid options for the target
   51        */
   52       public static String[] setOptions(Object target, String[] args) {
   53           ArrayList<String> rc = new ArrayList<String>();
   54   
   55           for (int i = 0; i < args.length; i++) {
   56               if (args[i] == null) {
   57                   continue;
   58               }
   59   
   60               if (args[i].startsWith("--")) {
   61   
   62                   // --options without a specified value are considered boolean
   63                   // flags that are enabled.
   64                   String value = "true";
   65                   String name = args[i].substring(2);
   66   
   67                   // if --option=value case
   68                   int p = name.indexOf("=");
   69                   if (p > 0) {
   70                       value = name.substring(p + 1);
   71                       name = name.substring(0, p);
   72                   }
   73   
   74                   // name not set, then it's an unrecognized option
   75                   if (name.length() == 0) {
   76                       rc.add(args[i]);
   77                       continue;
   78                   }
   79   
   80                   String propName = convertOptionToPropertyName(name);
   81                   if (!IntrospectionSupport.setProperty(target, propName, value)) {
   82                       rc.add(args[i]);
   83                       continue;
   84                   }
   85               } else {
   86                   rc.add(args[i]);
   87               }
   88   
   89           }
   90   
   91           String r[] = new String[rc.size()];
   92           rc.toArray(r);
   93           return r;
   94       }
   95   
   96       /**
   97        * converts strings like: test-enabled to testEnabled
   98        * 
   99        * @param name
  100        * @return
  101        */
  102       private static String convertOptionToPropertyName(String name) {
  103           String rc = "";
  104   
  105           // Look for '-' and strip and then convert the subsequent char to
  106           // uppercase
  107           int p = name.indexOf("-");
  108           while (p > 0) {
  109               // strip
  110               rc += name.substring(0, p);
  111               name = name.substring(p + 1);
  112   
  113               // can I convert the next char to upper?
  114               if (name.length() > 0) {
  115                   rc += name.substring(0, 1).toUpperCase();
  116                   name = name.substring(1);
  117               }
  118   
  119               p = name.indexOf("-");
  120           }
  121           return rc + name;
  122       }
  123   }

Home » activemq-parent-5.3.1-source-release » org.apache.activemq.console.command.store.amq » [javadoc | source]