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

Save This Page
Home » activemq-parent-5.3.1-source-release » org.apache.kahadb.util » [javadoc | source]