KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > CommnadLineSupport


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
19 import java.util.ArrayList JavaDoc;
20
21 import org.apache.activemq.util.IntrospectionSupport;
22
23 /**
24  * Helper utility that can be used to set the properties on any object
25  * using command line arguments.
26  *
27  * @author <a HREF="http://hiramchirino.com">Hiram Chirino</a>
28  */

29 public class CommnadLineSupport {
30     
31     /**
32      * Sets the properties of an object given the command line args.
33      *
34      * if args contains: --ack-mode=AUTO --url=tcp://localhost:61616 --persistent
35      *
36      * then it will try to call the following setters on the target object.
37      *
38      * target.setAckMode("AUTO");
39      * target.setURL(new URI("tcp://localhost:61616") );
40      * target.setPersistent(true);
41      *
42      * Notice the the proper conversion for the argument is determined by examining the
43      * setter arguement type.
44      *
45      * @param target the object that will have it's properties set
46      * @param args the commline options
47      * @return any arguments that are not valid options for the target
48      */

49     static public String JavaDoc[] setOptions(Object JavaDoc target, String JavaDoc []args) {
50         ArrayList JavaDoc rc = new ArrayList JavaDoc();
51         
52         for (int i = 0; i < args.length; i++) {
53             if( args[i] == null )
54                 continue;
55             
56             if( args[i].startsWith("--") ) {
57                 
58                 // --options without a specified value are considered boolean flags that are enabled.
59
String JavaDoc value="true";
60                 String JavaDoc name = args[i].substring(2);
61                 
62                 // if --option=value case
63
int p = name.indexOf("=");
64                 if( p > 0 ) {
65                     value = name.substring(p+1);
66                     name = name.substring(0,p);
67                 }
68                 
69                 // name not set, then it's an unrecognized option
70
if( name.length()==0 ) {
71                     rc.add(args[i]);
72                     continue;
73                 }
74                 
75                 String JavaDoc propName = convertOptionToPropertyName(name);
76                 if( !IntrospectionSupport.setProperty(target, propName, value) ) {
77                     rc.add(args[i]);
78                     continue;
79                 }
80             }
81             
82         }
83         
84         String JavaDoc r[] = new String JavaDoc[rc.size()];
85         rc.toArray(r);
86         return r;
87     }
88
89     /**
90      * converts strings like: test-enabled to testEnabled
91      * @param name
92      * @return
93      */

94     private static String JavaDoc convertOptionToPropertyName(String JavaDoc name) {
95         String JavaDoc rc="";
96         
97         // Look for '-' and strip and then convert the subsequent char to uppercase
98
int p = name.indexOf("-");
99         while( p > 0 ) {
100             // strip
101
rc += name.substring(0, p);
102             name = name.substring(p+1);
103             
104             // can I convert the next char to upper?
105
if( name.length() >0 ) {
106                 rc += name.substring(0,1).toUpperCase();
107                 name = name.substring(1);
108             }
109             
110             p = name.indexOf("-");
111         }
112         return rc+name;
113     }
114 }
115
Popular Tags