KickJava   Java API By Example, From Geeks To Geeks.

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


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): .
24
25 --------------------------------------------------------------------
26 $Id: OptionContext.java,v 1.1 2004/05/19 15:58:29 rouvoy 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.ContextDescription;
41 import org.objectweb.util.launcher.parser.Repository;
42
43
44 /**
45  * Definition of the --runid context.<BR>
46  * <p>
47  * This context specify the run identifier to use for starting the
48  * application.
49  * </p>
50  *
51  * @author <a HREF="mailto:Romain.Rouvoy@lifl.fr">Romain Rouvoy</a>
52  * @version 0.1
53  */

54 public class OptionContext
55      extends DefaultOptionArgument
56   implements OptionLauncher
57 {
58     /** Short Tag Description */
59     public final static String JavaDoc shortTag = "-ctx" ;
60     /** Long Tag Description */
61     public final static String JavaDoc longTag = "--context" ;
62
63     /** List of contexts to load */
64     protected Vector JavaDoc contexts_ ;
65
66     /**
67      * Default Constructor
68      */

69     public OptionContext(){
70         super(new String JavaDoc[]{shortTag, longTag},
71               "<ContextID>",
72               "The name of a context",
73               "");
74         contexts_=new Vector JavaDoc();
75     }
76
77
78     /**
79      * Loader of context description.
80      *
81      * @author <a HREF="mailto:Romain.Rouvoy@lifl.fr">Romain Rouvoy</a>
82      * @version 0.1
83      */

84     protected class ContextBuilder
85     {
86         /** Description of the context to build */
87         protected String JavaDoc opt ;
88
89         /**
90          * Constructor.
91          * @param arg description of the context to build.
92          */

93         public ContextBuilder(String JavaDoc arg) {
94             this.opt = arg ;
95         }
96         
97         /**
98          * Complete the command java by setting the identifier of
99          * the run to use
100          *
101          * @param cmd - the command to complete
102          */

103         public void complete(Repository repository) {
104             ContextDescription desc = repository.getDescription(opt);
105             CommandFactory.instance().getDefaultClasspath().addAll(desc.getClasses());
106             CommandFactory.instance().getDefaultArguments().addAll(desc.getArguments());
107             CommandFactory.instance().getDefaultProperties().putAll(desc.getProperties());
108         }
109     }
110
111     // ==================================================================
112
//
113
// Public methods for interface org.objectweb.util.cmdline.api.Context
114
//
115
// ==================================================================
116

117     /**
118      * Consumes command line arguments from an iterator.
119      *
120      * @param iterator The command line argument iterator.
121      */

122     public void consume(Iterator iterator) {
123         setArgument(consumeArgument(iterator));
124         contexts_.addElement(new ContextBuilder(getArgument()));
125     }
126     
127     /**
128      * Complete the command java by setting the identifier of
129      * the run to use
130      *
131      * @param cmd - the command to complete
132      */

133     public CommandJava[] complete(Repository repository) {
134         java.util.Iterator JavaDoc contexts = contexts_.iterator();
135         Vector JavaDoc list = new Vector JavaDoc();
136         while (contexts.hasNext())
137             ((ContextBuilder)contexts.next()).complete(repository);
138         return new CommandJava[0];
139     }
140
141     /**
142      * Creates a commandline representation of the context.
143      * @param value value of the context.
144      * @return commandline representation of the context.
145      */

146     public static String JavaDoc create(String JavaDoc opt) {
147         return shortTag+" "+opt+" " ;
148     }
149 }
150
Popular Tags