KickJava   Java API By Example, From Geeks To Geeks.

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


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: OptionRun.java,v 1.2 2004/09/28 15:19:47 contrera Exp $
27 ====================================================================*/

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

55 public class OptionRun
56      extends DefaultOptionArgument
57   implements OptionLauncher
58 {
59     /** Short Tag Description */
60     public final static String JavaDoc shortTag = "-run" ;
61     /** Long Tag Description */
62     public final static String JavaDoc longTag = "--runid" ;
63
64     /** List of runs to build */
65     protected Vector JavaDoc runs_ ;
66
67     /**
68      * Default Constructor
69      */

70     public OptionRun() {
71         super(new String JavaDoc[]{shortTag, longTag},
72               "<RunID>",
73               "The identifier of the run to launch",
74               "default");
75         runs_ = new Vector JavaDoc();
76     }
77
78     /**
79      * Loader of run descriptions.
80      *
81      * @author <a HREF="mailto:Romain.Rouvoy@lifl.fr">Romain Rouvoy</a>
82      * @version 0.1
83      */

84     protected class RunBuilder {
85         /** Description of the run to use */
86         protected String JavaDoc argument_ ;
87         
88         /**
89          * Constructor.
90          * @param arg description of the run to build.
91          */

92         public RunBuilder(String JavaDoc arg) {
93             this.argument_ = arg ;
94         }
95         
96         /**
97          * Complete the Java Command with the run description.
98          * @param cmd the command to complete.
99          * @param desc the description of parameters to update.
100          */

101         protected void completeRun(CommandJava cmd, RunDescription desc) {
102             cmd.setName(desc.getName());
103             TraceSystem.get("launcher").info("Completing java command " + cmd.getName());
104             cmd.setClassname(desc.getMainclass());
105             cmd.setMode(desc.getMode());
106             try {
107                 cmd.getLoader().addURL(desc.getClasses().toStringArray());
108             } catch (java.net.MalformedURLException JavaDoc ex) {
109                 TraceSystem.get("launcher").error("Exception raised: " + ex);
110             }
111             cmd.addArguments(desc.getArguments());
112             cmd.addProperties(desc.getProperties());
113         }
114         
115
116         /**
117          * Complete the command java by setting the identifier of
118          * the run to use
119          *
120          * @param cmd the command to complete
121          */

122         public CommandJava complete(Repository repository) {
123             CommandJava cmd = CommandFactory.instance().create();
124             completeRun(cmd,(RunDescription)repository.getDescription(argument_));
125             return cmd;
126         }
127     }
128     
129     
130     // ==================================================================
131
//
132
// Public methods for interface org.objectweb.util.cmdline.api.Option
133
//
134
// ==================================================================
135

136     /**
137      * Consumes command line arguments from an iterator.
138      *
139      * @param iterator The command line argument iterator.
140      */

141     public void consume(Iterator iterator) {
142         setArgument(consumeArgument(iterator));
143         runs_.addElement(new RunBuilder(getArgument()));
144     }
145     
146     
147     
148     /**
149      * Complete the command java by setting the identifier of
150      * the run to use.
151      *
152      * @param cmd the command to complete.
153      */

154     public CommandJava[] complete(Repository repository) {
155         java.util.Iterator JavaDoc runs = runs_.iterator();
156         Vector JavaDoc list = new Vector JavaDoc();
157         while (runs.hasNext())
158             list.addElement(((RunBuilder)runs.next()).complete(repository));
159         return (CommandJava[])list.toArray(new CommandJava[0]);
160     }
161        
162     /**
163      * Creates a commandline representation of the run.
164      * @param value value of the run.
165      * @return commandline representation of the run.
166      */

167     public static String JavaDoc create(String JavaDoc run) {
168         return shortTag+" "+run+" " ;
169     }
170 }
171
Popular Tags