KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > util > launcher > option > OptionLoad


1 /*====================================================================
2
3 ObjectWeb Util Launcher Package.
4 Copyright (C) 2004 INRIA & USTL - LIFL - GOAL
5 Contact: openccm@objectweb.org
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 USA
21
22 Initial developer(s): Romain Rouvoy.
23 Contributor(s): Christophe Contreras.
24
25 --------------------------------------------------------------------
26 $Id: OptionLoad.java,v 1.5 2005/02/03 17:04:45 rouvoy Exp $
27 ====================================================================*/

28
29 package org.objectweb.util.launcher.option;
30
31
32 import java.util.Vector JavaDoc;
33
34 import jregex.Matcher;
35 import jregex.Pattern;
36
37 import org.objectweb.util.cmdline.api.Iterator;
38 import org.objectweb.util.cmdline.lib.DefaultRegExOptionArgument;
39
40 import org.objectweb.util.launcher.CommandJava;
41 import org.objectweb.util.launcher.CommandFactory;
42 import org.objectweb.util.launcher.StringList;
43
44 import org.objectweb.util.launcher.parser.Repository;
45 import org.objectweb.util.launcher.parser.RunDescription;
46 import org.objectweb.util.launcher.parser.ContextDescription;
47 import org.objectweb.util.trace.TraceSystem;
48
49
50 /**
51  * Definition of the --runid option.<BR>
52  * <p>
53  * This option specify the run identifier to use for starting the
54  * application.
55  * </p>
56  *
57  * @author <a HREF="mailto:Romain.Rouvoy@lifl.fr">Romain Rouvoy</a>
58  * @version 0.1
59  */

60 public class OptionLoad
61      extends DefaultRegExOptionArgument
62   implements OptionLauncher
63 {
64     /** Short Tag Description. */
65     public final static String JavaDoc shortTag = "-load" ;
66     /** Long Tag Description. */
67     public final static String JavaDoc longTag = "--loadid" ;
68
69     /** Element separator. */
70     public final static String JavaDoc separator = ":" ;
71     /** List separator. */
72     public final static String JavaDoc list = ",";
73
74     /** List of run to build. */
75     protected Vector JavaDoc runs_ ;
76
77     /**
78      * Default Constructor.
79      */

80     public OptionLoad(){
81         super(new String JavaDoc[]{shortTag, longTag},
82               "<RunID"+separator+
83               "Option1"+list+"..."+list+"OptionX"+separator+
84               "Arg1"+list+"..."+list+"ArgX"+separator+
85               "Prop1"+list+"..."+list+"PropX>",
86               "The identifier of the run to launch",
87               "default"+separator+separator+separator,
88               "([^:]+)"+separator+"([^:]*)"+separator+"([^:]*)"+separator+"(.*)");
89         runs_ = new Vector JavaDoc();
90     }
91
92     /**
93      * Loader of run descriptions.
94      * @author <a HREF="mailto:Romain.Rouvoy@lifl.fr">Romain Rouvoy</a>
95      * @version 0.1
96      */

97     protected class LoadBuilder
98     {
99         /** the argument to configure */
100         protected String JavaDoc argument_ ;
101
102         /**
103          * Constructor.
104          * @param arg the load description.
105          */

106         public LoadBuilder(String JavaDoc arg) {
107             this.argument_ = arg ;
108         }
109         
110         /**
111          * Complete the Java Command with the run description.
112          * @param cmd the command to complete.
113          * @param desc the description of parameters to update.
114          */

115         protected void completeRun(CommandJava cmd, RunDescription desc) {
116             cmd.setName(desc.getName());
117             TraceSystem.get("launcher").info("Completing java command " + cmd.getName());
118             cmd.setClassname(desc.getMainclass());
119             cmd.setMode(desc.getMode());
120             try {
121                 cmd.getLoader().addURL(desc.getClasses().toStringArray());
122             } catch (java.net.MalformedURLException JavaDoc ex) {
123                 TraceSystem.get("launcher").error("Exception raised: " + ex);
124             }
125             cmd.addArguments(desc.getArguments());
126             cmd.addProperties(desc.getProperties());
127         }
128         
129         /**
130          * Complete the Java Command with the context description.
131          * @param cmd the command to complete.
132          * @param desc the description of parameters to update.
133          */

134         protected void completeContext(CommandJava cmd, ContextDescription desc) {
135             try {
136                 cmd.getLoader().addURL(desc.getClasses().toStringArray());
137             } catch (java.net.MalformedURLException JavaDoc ex) {
138                 TraceSystem.get("launcher").error("Exception raised: " + ex);
139             }
140             cmd.addArguments(desc.getArguments());
141             cmd.addProperties(desc.getProperties());
142         }
143         
144         /**
145          * Complete the Java Command with the arguments description.
146          * @param cmd the command to complete.
147          * @param args the arguments to add.
148          */

149         protected void completeArgument(CommandJava cmd, String JavaDoc args) {
150             Pattern p = new Pattern(list);
151             String JavaDoc[] arg = p.tokenizer(args).split();
152             StringList list = new StringList();
153             for (int i=0 ; i<arg.length ; i++){
154                 if (!arg[i].equals("")) {
155                     list.add(arg[i]);
156                 }
157             }
158             cmd.addArguments(list);
159         }
160         
161         /**
162          * Complete the Java Command with the properties description.
163          * @param cmd the command to complete.
164          * @param args the properties to add.
165          */

166         protected void completeProperties(CommandJava cmd, String JavaDoc props) {
167             Pattern pattern = new Pattern(list);
168             String JavaDoc[] prop = pattern.tokenizer(props).split();
169             java.util.Properties JavaDoc list = new java.util.Properties JavaDoc();
170             for (int i=0 ; i<prop.length ; i++){
171                 if (!prop[i].equals("")) {
172                     Pattern pat = new Pattern("=");
173                     String JavaDoc[] p = pattern.tokenizer(prop[i]).split();
174                     if (p.length==2)
175                         list.put(p[0],p[1]);
176                     else if (p.length==1)
177                         list.put(p[0],"");
178                 }
179             }
180             cmd.addProperties(list);
181         }
182         
183         /**
184          * Complete the command java by setting the identifier of
185          * the run to use
186          *
187          * @param cmd - the command to complete
188          */

189         public CommandJava complete(Repository repository){
190             CommandJava cmd = CommandFactory.instance().create();
191             Matcher match = getPattern().matcher(argument_);
192             match.matches();
193
194             // The order of these calls determines the order of priority
195
completeProperties(cmd,match.group(4));
196             Pattern pattern = new Pattern(list);
197             String JavaDoc[] opt = pattern.tokenizer(match.group(2)).split();
198             for (int i=0 ; i<opt.length ; i++){
199                 if (!opt[i].equals("")) {
200                     ContextDescription desc = repository.getDescription(opt[i]);
201                     completeContext(cmd,desc);
202                 }
203             }
204             completeRun(cmd,(RunDescription)repository.getDescription(match.group(1)));
205             completeArgument(cmd,match.group(3));
206             return cmd;
207         }
208     }
209
210
211     // ==================================================================
212
//
213
// Public methods for interface org.objectweb.util.cmdline.api.Option
214
//
215
// ==================================================================
216

217     /**
218      * Consumes command line arguments from an iterator.
219      * @param iterator The command line argument iterator.
220      */

221     public void consume(Iterator iterator) {
222         setArgument(consumeArgument(iterator));
223         runs_.addElement(new LoadBuilder(getArgument()));
224     }
225     
226     /**
227      * Complete the command java by setting the identifier of the run to use.
228      * @param cmd the command to complete.
229      */

230     public CommandJava[] complete(Repository repository) {
231         java.util.Iterator JavaDoc runs = runs_.iterator();
232         Vector JavaDoc list = new Vector JavaDoc();
233         while (runs.hasNext())
234             list.addElement(((LoadBuilder)runs.next()).complete(repository));
235         return (CommandJava[])list.toArray(new CommandJava[0]);
236     }
237
238     /**
239      * Creates a commandline representation of the load.
240      * @param value value of the load.
241      * @return commandline representation of the load.
242      */

243     public static String JavaDoc create(String JavaDoc run, String JavaDoc opt, String JavaDoc arg, String JavaDoc prop) {
244         return shortTag+" "+run+separator+opt+separator+arg+separator+prop+" " ;
245     }
246 }
247
Popular Tags