001    /**
002     *
003     * Licensed to the Apache Software Foundation (ASF) under one or more
004     * contributor license agreements.  See the NOTICE file distributed with
005     * this work for additional information regarding copyright ownership.
006     * The ASF licenses this file to You under the Apache License, Version 2.0
007     * (the "License"); you may not use this file except in compliance with
008     * the License.  You may obtain a copy of the License at
009     *
010     * http://www.apache.org/licenses/LICENSE-2.0
011     *
012     * Unless required by applicable law or agreed to in writing, software
013     * distributed under the License is distributed on an "AS IS" BASIS,
014     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015     * See the License for the specific language governing permissions and
016     * limitations under the License.
017     */
018    package org.apache.activemq.store.kahadb.disk.util;
019    
020    import org.apache.activemq.util.IntrospectionSupport;
021    
022    import java.util.ArrayList;
023    
024    /**
025     * Support utility that can be used to set the properties on any object
026     * using command line arguments.
027     * 
028     * @author <a href="http://hiramchirino.com">Hiram Chirino</a>
029     */
030    public class CommandLineSupport {
031            
032            /**
033             * Sets the properties of an object given the command line args.
034             * 
035             * if args contains: --ack-mode=AUTO --url=tcp://localhost:61616 --persistent 
036             * 
037             * then it will try to call the following setters on the target object.
038             * 
039             * target.setAckMode("AUTO");
040             * target.setURL(new URI("tcp://localhost:61616") );
041             * target.setPersistent(true);
042             * 
043             * Notice the the proper conversion for the argument is determined by examining the 
044             * setter argument type.  
045             * 
046             * @param target the object that will have it's properties set
047             * @param args the command line options
048             * @return any arguments that are not valid options for the target
049             */
050            static public String[] setOptions(Object target, String []args) {
051                    ArrayList rc = new ArrayList();
052                    
053                    for (int i = 0; i < args.length; i++) {
054                            if( args[i] == null )
055                                    continue;
056                            
057                            if( args[i].startsWith("--") ) {
058                                    
059                                    // --options without a specified value are considered boolean flags that are enabled.
060                                    String value="true";
061                                    String name = args[i].substring(2);
062                                    
063                                    // if --option=value case
064                                    int p = name.indexOf("=");
065                                    if( p > 0 ) {
066                                            value = name.substring(p+1);
067                                            name = name.substring(0,p);
068                                    }
069                                    
070                                    // name not set, then it's an unrecognized option
071                                    if( name.length()==0 ) {
072                                            rc.add(args[i]);
073                                            continue;
074                                    }
075                                    
076                                    String propName = convertOptionToPropertyName(name);
077                                    if( !IntrospectionSupport.setProperty(target, propName, value) ) {
078                                            rc.add(args[i]);
079                                            continue;
080                                    }
081                            } else {
082                                rc.add(args[i]);
083                            }
084                            
085                    }
086                    
087                    String r[] = new String[rc.size()];
088                    rc.toArray(r);
089                    return r;
090            }
091    
092            /**
093             * converts strings like: test-enabled to testEnabled
094             * @param name
095             * @return
096             */
097            private static String convertOptionToPropertyName(String name) {
098                    String rc="";
099                    
100                    // Look for '-' and strip and then convert the subsequent char to uppercase
101                    int p = name.indexOf("-");
102                    while( p > 0 ) {
103                            // strip
104                            rc += name.substring(0, p);
105                            name = name.substring(p+1);
106                            
107                            // can I convert the next char to upper?
108                            if( name.length() >0 ) {
109                                    rc += name.substring(0,1).toUpperCase();
110                                    name = name.substring(1);
111                            }
112                            
113                            p = name.indexOf("-");
114                    }
115                    return rc+name;
116            }
117    }